@mysetup/mqtt 1.14.3 → 1.14.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mqttClient.d.ts +18 -12
- package/dist/mqttClient.d.ts.map +1 -1
- package/dist/mqttClient.js +42 -65
- package/dist/test/test-mqtt.js +7 -2
- package/package.json +1 -1
package/dist/mqttClient.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import mqtt from "mqtt";
|
|
2
2
|
import type { MqttClient } from "mqtt";
|
|
3
3
|
export type { MqttClient };
|
|
4
|
-
|
|
4
|
+
interface MqttConfigProps {
|
|
5
|
+
protocol: string;
|
|
6
|
+
port: number;
|
|
7
|
+
host: string;
|
|
8
|
+
path: string;
|
|
9
|
+
username?: string | undefined;
|
|
10
|
+
password?: string | undefined;
|
|
11
|
+
}
|
|
5
12
|
interface ConnectionProps {
|
|
6
|
-
|
|
7
|
-
|
|
13
|
+
clientId: string;
|
|
14
|
+
config: MqttConfigProps;
|
|
8
15
|
topicList: string[];
|
|
9
16
|
messageCallback?: (topic: string, message: string) => void;
|
|
10
|
-
|
|
17
|
+
environment?: string | undefined;
|
|
11
18
|
}
|
|
12
|
-
declare const mqttSub: (topicList: string[], messageCallback?: (topic: string, message: string) => void,
|
|
13
|
-
declare const mqttUnSub: (topicList: string[],
|
|
14
|
-
declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean,
|
|
15
|
-
declare const
|
|
16
|
-
declare const
|
|
17
|
-
declare const
|
|
18
|
-
|
|
19
|
-
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, mqttPublish, };
|
|
19
|
+
declare const mqttSub: (topicList: string[], clientId: string, messageCallback?: (topic: string, message: string) => void, environment?: string) => void;
|
|
20
|
+
declare const mqttUnSub: (topicList: string[], clientId: string, environment?: string) => void;
|
|
21
|
+
declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean, environment?: string) => void;
|
|
22
|
+
declare const mqttPublish: (topic: string, message: object, clientId: string, environment?: string) => void;
|
|
23
|
+
declare const mqttCreateConnection: (props: ConnectionProps) => mqtt.MqttClient | null;
|
|
24
|
+
declare const getMqttClient: (id: string) => mqtt.MqttClient | null;
|
|
25
|
+
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttPublish, getMqttClient, };
|
|
20
26
|
//# sourceMappingURL=mqttClient.d.ts.map
|
package/dist/mqttClient.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mqttClient.d.ts","sourceRoot":"","sources":["../mqttClient.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAGvC,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,
|
|
1
|
+
{"version":3,"file":"mqttClient.d.ts","sourceRoot":"","sources":["../mqttClient.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAGvC,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,UAAU,eAAe;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACjC;AAED,UAAU,eAAe;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3D,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAkED,QAAA,MAAM,OAAO,GACT,WAAW,MAAM,EAAE,EACnB,UAAU,MAAM,EAChB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,EAC1D,cAAc,MAAM,SA0BvB,CAAC;AAEF,QAAA,MAAM,SAAS,GACX,WAAW,MAAM,EAAE,EACnB,UAAU,MAAM,EAChB,cAAc,MAAM,SAoBvB,CAAC;AAEF,QAAA,MAAM,cAAc,GAChB,OAAO,eAAe,EACtB,QAAQ,OAAO,EACf,cAAc,MAAM,SAgBvB,CAAC;AAeF,QAAA,MAAM,WAAW,GACb,OAAO,MAAM,EACb,SAAS,MAAM,EACf,UAAU,MAAM,EAChB,cAAc,MAAM,SAqBvB,CAAC;AAsBF,QAAA,MAAM,oBAAoB,GAAI,OAAO,eAAe,2BAEnD,CAAC;AAEF,QAAA,MAAM,aAAa,GAAI,IAAI,MAAM,2BAEhC,CAAC;AAOF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,aAAa,GAChB,CAAC"}
|
package/dist/mqttClient.js
CHANGED
|
@@ -1,101 +1,76 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
6
|
+
exports.getMqttClient = exports.mqttPublish = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
|
|
18
7
|
var mqtt_1 = __importDefault(require("mqtt"));
|
|
19
8
|
var logger_1 = require("@mysetup/logger");
|
|
20
9
|
var cache_1 = require("@mysetup/cache");
|
|
21
|
-
var
|
|
22
|
-
var serverConnectOptions = {
|
|
23
|
-
protocol: "mqtt",
|
|
24
|
-
port: 1883,
|
|
25
|
-
host: MQTT_HOST,
|
|
26
|
-
};
|
|
27
|
-
var webConnectOptions = {
|
|
28
|
-
protocol: "ws",
|
|
29
|
-
port: 8083,
|
|
30
|
-
host: MQTT_HOST,
|
|
31
|
-
};
|
|
32
|
-
var mqttClientId = "";
|
|
10
|
+
var mqttClientCache = {};
|
|
33
11
|
var checkInitConnect = function (props) {
|
|
34
|
-
var _a = props.
|
|
35
|
-
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port);
|
|
12
|
+
var _a = props.config, protocol = _a.protocol, host = _a.host, port = _a.port, path = _a.path, username = _a.username, password = _a.password;
|
|
13
|
+
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port).concat(path);
|
|
36
14
|
var client = mqtt_1.default.connect(connectUrl, {
|
|
37
|
-
clientId: props.
|
|
15
|
+
clientId: props.clientId,
|
|
38
16
|
clean: false, /// for retaining previous message purpose
|
|
39
17
|
rejectUnauthorized: true,
|
|
40
18
|
resubscribe: true,
|
|
41
19
|
keepalive: 300,
|
|
42
|
-
username:
|
|
43
|
-
password:
|
|
20
|
+
username: username,
|
|
21
|
+
password: password,
|
|
44
22
|
});
|
|
45
|
-
mqttClientId = props.value;
|
|
46
23
|
client.setMaxListeners(100);
|
|
47
|
-
if (props.messageCallback) {
|
|
48
|
-
mqttOnMessage(props.messageCallback, client); // 💡 Always attach
|
|
49
|
-
}
|
|
50
24
|
client.on("connect", function () {
|
|
51
|
-
if (
|
|
25
|
+
if (props.environment === "development") {
|
|
52
26
|
logger_1.logger.info("MQTT connection successful");
|
|
53
27
|
}
|
|
54
28
|
(0, cache_1.setCacheData)("mqttClientStatus", "connect");
|
|
55
|
-
mqttSub(props.topicList, props.messageCallback,
|
|
29
|
+
mqttSub(props.topicList, props.clientId, props.messageCallback, props.environment);
|
|
56
30
|
});
|
|
57
31
|
client.on("error", function (err) {
|
|
58
|
-
if (
|
|
32
|
+
if (props.environment === "development") {
|
|
59
33
|
logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
|
|
60
34
|
}
|
|
61
35
|
(0, cache_1.setCacheData)("mqttClientStatus", "error");
|
|
62
|
-
mqttDisconnect(props, true
|
|
36
|
+
mqttDisconnect(props, true);
|
|
63
37
|
});
|
|
64
38
|
client.on("reconnect", function () {
|
|
65
|
-
if (
|
|
39
|
+
if (props.environment === "development") {
|
|
66
40
|
logger_1.logger.info("MQTT Reconnecting");
|
|
67
41
|
}
|
|
68
42
|
(0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
|
|
69
43
|
});
|
|
70
44
|
client.on("close", function () {
|
|
71
|
-
if (
|
|
45
|
+
if (props.environment === "development") {
|
|
72
46
|
logger_1.logger.info("MQTT connection closed");
|
|
73
47
|
}
|
|
74
48
|
(0, cache_1.setCacheData)("mqttClientStatus", "close");
|
|
75
49
|
});
|
|
76
50
|
client.on("offline", function () {
|
|
77
|
-
if (
|
|
51
|
+
if (props.environment === "development") {
|
|
78
52
|
logger_1.logger.info("MQTT connection offline");
|
|
79
53
|
}
|
|
80
54
|
(0, cache_1.setCacheData)("mqttClientStatus", "offline");
|
|
81
55
|
});
|
|
82
56
|
return client;
|
|
83
57
|
};
|
|
84
|
-
var mqttSub = function (topicList, messageCallback,
|
|
58
|
+
var mqttSub = function (topicList, clientId, messageCallback, environment) {
|
|
85
59
|
try {
|
|
86
|
-
|
|
60
|
+
var client_1 = getMqttClient(clientId);
|
|
61
|
+
client_1 === null || client_1 === void 0 ? void 0 : client_1.subscribe(topicList, { qos: 0 }, function (error, granted) {
|
|
87
62
|
if (error) {
|
|
88
63
|
logger_1.logger.error("MQTT Subscribe to topics error: ".concat(error.message));
|
|
89
64
|
}
|
|
90
65
|
var grantedList = [];
|
|
91
66
|
granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
|
|
92
67
|
grantedList.push(element.topic);
|
|
93
|
-
if (
|
|
68
|
+
if (environment === "development") {
|
|
94
69
|
logger_1.logger.info("MQTT Subscribed to topics: ".concat(element.topic));
|
|
95
70
|
}
|
|
96
71
|
});
|
|
97
72
|
(0, cache_1.setCacheData)("subscribedTopic", grantedList);
|
|
98
|
-
messageCallback && mqttOnMessage(messageCallback,
|
|
73
|
+
messageCallback && mqttOnMessage(messageCallback, client_1);
|
|
99
74
|
});
|
|
100
75
|
}
|
|
101
76
|
catch (error) {
|
|
@@ -103,14 +78,15 @@ var mqttSub = function (topicList, messageCallback, client) {
|
|
|
103
78
|
}
|
|
104
79
|
};
|
|
105
80
|
exports.mqttSub = mqttSub;
|
|
106
|
-
var mqttUnSub = function (topicList,
|
|
81
|
+
var mqttUnSub = function (topicList, clientId, environment) {
|
|
107
82
|
try {
|
|
83
|
+
var client = getMqttClient(clientId);
|
|
108
84
|
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
|
|
109
85
|
if (error) {
|
|
110
86
|
logger_1.logger.error("MQTT Unsubscribe to topics error: ".concat(error.message));
|
|
111
87
|
return;
|
|
112
88
|
}
|
|
113
|
-
if (
|
|
89
|
+
if (environment === "development") {
|
|
114
90
|
topicList.forEach(function (topic) {
|
|
115
91
|
logger_1.logger.info("MQTT Unsubscribed to topic: ".concat(topic));
|
|
116
92
|
});
|
|
@@ -122,14 +98,16 @@ var mqttUnSub = function (topicList, client) {
|
|
|
122
98
|
}
|
|
123
99
|
};
|
|
124
100
|
exports.mqttUnSub = mqttUnSub;
|
|
125
|
-
var mqttDisconnect = function (props, reinit,
|
|
101
|
+
var mqttDisconnect = function (props, reinit, environment) {
|
|
126
102
|
try {
|
|
103
|
+
var client = getMqttClient(props.clientId);
|
|
127
104
|
client === null || client === void 0 ? void 0 : client.end(false, function () {
|
|
128
|
-
if (
|
|
105
|
+
if (environment === "development") {
|
|
129
106
|
logger_1.logger.info("MQTT disconnected successfully");
|
|
130
107
|
}
|
|
108
|
+
removeMqttClient(props.clientId);
|
|
131
109
|
if (reinit) {
|
|
132
|
-
cachedConnection(
|
|
110
|
+
cachedConnection(props);
|
|
133
111
|
}
|
|
134
112
|
});
|
|
135
113
|
}
|
|
@@ -148,16 +126,16 @@ var mqttOnMessage = function (messageCallback, client) {
|
|
|
148
126
|
logger_1.logger.error("MQTT On-message error: ".concat(JSON.stringify(error)));
|
|
149
127
|
}
|
|
150
128
|
};
|
|
151
|
-
|
|
152
|
-
var mqttPublish = function (topic, message, client) {
|
|
129
|
+
var mqttPublish = function (topic, message, clientId, environment) {
|
|
153
130
|
try {
|
|
131
|
+
var client = getMqttClient(clientId);
|
|
154
132
|
client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
|
|
155
133
|
if (err) {
|
|
156
|
-
if (
|
|
134
|
+
if (environment === "development") {
|
|
157
135
|
logger_1.logger.error("MQTT Publish error: ".concat(err.message));
|
|
158
136
|
}
|
|
159
137
|
}
|
|
160
|
-
else if (
|
|
138
|
+
else if (environment === "development") {
|
|
161
139
|
logger_1.logger.info("MQTT Payload published");
|
|
162
140
|
}
|
|
163
141
|
});
|
|
@@ -168,23 +146,18 @@ var mqttPublish = function (topic, message, client) {
|
|
|
168
146
|
};
|
|
169
147
|
exports.mqttPublish = mqttPublish;
|
|
170
148
|
var memoizedDb = function () {
|
|
171
|
-
var cache = {};
|
|
172
149
|
return function (props) {
|
|
173
|
-
if (props.
|
|
174
|
-
if (
|
|
150
|
+
if (props.clientId in mqttClientCache) {
|
|
151
|
+
if (props.environment === "development") {
|
|
175
152
|
logger_1.logger.info("MQTT Fetching mqtt cache");
|
|
176
153
|
}
|
|
177
|
-
|
|
178
|
-
if (props.messageCallback) {
|
|
179
|
-
mqttOnMessage(props.messageCallback, cache[props.value]);
|
|
180
|
-
}
|
|
181
|
-
return cache[props.value];
|
|
154
|
+
return mqttClientCache[props.clientId];
|
|
182
155
|
}
|
|
183
|
-
if (
|
|
156
|
+
if (props.environment === "development") {
|
|
184
157
|
logger_1.logger.info("MQTT Creating new mqtt cache");
|
|
185
158
|
}
|
|
186
159
|
var result = checkInitConnect(props);
|
|
187
|
-
|
|
160
|
+
mqttClientCache[props.clientId] = result;
|
|
188
161
|
return result;
|
|
189
162
|
};
|
|
190
163
|
};
|
|
@@ -194,7 +167,11 @@ var mqttCreateConnection = function (props) {
|
|
|
194
167
|
return cachedConnection(props);
|
|
195
168
|
};
|
|
196
169
|
exports.mqttCreateConnection = mqttCreateConnection;
|
|
197
|
-
var getMqttClient = function () {
|
|
198
|
-
return
|
|
170
|
+
var getMqttClient = function (id) {
|
|
171
|
+
return mqttClientCache[id];
|
|
199
172
|
};
|
|
200
173
|
exports.getMqttClient = getMqttClient;
|
|
174
|
+
var removeMqttClient = function (id) {
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
176
|
+
delete mqttClientCache[id];
|
|
177
|
+
};
|
package/dist/test/test-mqtt.js
CHANGED
|
@@ -4,8 +4,13 @@ var logger_1 = require("@mysetup/logger");
|
|
|
4
4
|
var mqttClient_1 = require("../mqttClient");
|
|
5
5
|
var CLIENT_ID = "testClient-".concat(Math.random().toString(16).slice(2, 8));
|
|
6
6
|
(0, mqttClient_1.mqttCreateConnection)({
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
clientId: CLIENT_ID,
|
|
8
|
+
config: {
|
|
9
|
+
host: "localhost",
|
|
10
|
+
port: 1883,
|
|
11
|
+
protocol: "mqtt",
|
|
12
|
+
path: "",
|
|
13
|
+
},
|
|
9
14
|
topicList: ["mj/ems/test"],
|
|
10
15
|
messageCallback: function (topic, message) {
|
|
11
16
|
logger_1.logger.info("Message received \u2192 topic: ".concat(topic, ", message: ").concat(message.toString()));
|