@mysetup/mqtt 1.14.2 → 1.14.4
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 +36 -66
- 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,SAevB,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;AAEF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,aAAa,GAChB,CAAC"}
|
package/dist/mqttClient.js
CHANGED
|
@@ -1,103 +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: NEXT_PUBLIC_MQTT_HOST,
|
|
26
|
-
path: "",
|
|
27
|
-
};
|
|
28
|
-
var webConnectOptions = {
|
|
29
|
-
protocol: "ws",
|
|
30
|
-
port: 1883,
|
|
31
|
-
host: NEXT_PUBLIC_MQTT_HOST,
|
|
32
|
-
path: "/mqtt",
|
|
33
|
-
};
|
|
34
|
-
var mqttClientId = "";
|
|
10
|
+
var mqttClientCache = {};
|
|
35
11
|
var checkInitConnect = function (props) {
|
|
36
|
-
var _a = props.
|
|
12
|
+
var _a = props.config, protocol = _a.protocol, host = _a.host, port = _a.port, path = _a.path, username = _a.username, password = _a.password;
|
|
37
13
|
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port).concat(path);
|
|
38
14
|
var client = mqtt_1.default.connect(connectUrl, {
|
|
39
|
-
clientId: props.
|
|
15
|
+
clientId: props.clientId,
|
|
40
16
|
clean: false, /// for retaining previous message purpose
|
|
41
17
|
rejectUnauthorized: true,
|
|
42
18
|
resubscribe: true,
|
|
43
19
|
keepalive: 300,
|
|
44
|
-
username:
|
|
45
|
-
password:
|
|
20
|
+
username: username,
|
|
21
|
+
password: password,
|
|
46
22
|
});
|
|
47
|
-
mqttClientId = props.value;
|
|
48
23
|
client.setMaxListeners(100);
|
|
49
|
-
if (props.messageCallback) {
|
|
50
|
-
mqttOnMessage(props.messageCallback, client); // 💡 Always attach
|
|
51
|
-
}
|
|
52
24
|
client.on("connect", function () {
|
|
53
|
-
if (
|
|
25
|
+
if (props.environment === "development") {
|
|
54
26
|
logger_1.logger.info("MQTT connection successful");
|
|
55
27
|
}
|
|
56
28
|
(0, cache_1.setCacheData)("mqttClientStatus", "connect");
|
|
57
|
-
mqttSub(props.topicList, props.messageCallback,
|
|
29
|
+
mqttSub(props.topicList, props.clientId, props.messageCallback, props.environment);
|
|
58
30
|
});
|
|
59
31
|
client.on("error", function (err) {
|
|
60
|
-
if (
|
|
32
|
+
if (props.environment === "development") {
|
|
61
33
|
logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
|
|
62
34
|
}
|
|
63
35
|
(0, cache_1.setCacheData)("mqttClientStatus", "error");
|
|
64
|
-
mqttDisconnect(props, true
|
|
36
|
+
mqttDisconnect(props, true);
|
|
65
37
|
});
|
|
66
38
|
client.on("reconnect", function () {
|
|
67
|
-
if (
|
|
39
|
+
if (props.environment === "development") {
|
|
68
40
|
logger_1.logger.info("MQTT Reconnecting");
|
|
69
41
|
}
|
|
70
42
|
(0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
|
|
71
43
|
});
|
|
72
44
|
client.on("close", function () {
|
|
73
|
-
if (
|
|
45
|
+
if (props.environment === "development") {
|
|
74
46
|
logger_1.logger.info("MQTT connection closed");
|
|
75
47
|
}
|
|
76
48
|
(0, cache_1.setCacheData)("mqttClientStatus", "close");
|
|
77
49
|
});
|
|
78
50
|
client.on("offline", function () {
|
|
79
|
-
if (
|
|
51
|
+
if (props.environment === "development") {
|
|
80
52
|
logger_1.logger.info("MQTT connection offline");
|
|
81
53
|
}
|
|
82
54
|
(0, cache_1.setCacheData)("mqttClientStatus", "offline");
|
|
83
55
|
});
|
|
84
56
|
return client;
|
|
85
57
|
};
|
|
86
|
-
var mqttSub = function (topicList, messageCallback,
|
|
58
|
+
var mqttSub = function (topicList, clientId, messageCallback, environment) {
|
|
87
59
|
try {
|
|
88
|
-
|
|
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) {
|
|
89
62
|
if (error) {
|
|
90
63
|
logger_1.logger.error("MQTT Subscribe to topics error: ".concat(error.message));
|
|
91
64
|
}
|
|
92
65
|
var grantedList = [];
|
|
93
66
|
granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
|
|
94
67
|
grantedList.push(element.topic);
|
|
95
|
-
if (
|
|
68
|
+
if (environment === "development") {
|
|
96
69
|
logger_1.logger.info("MQTT Subscribed to topics: ".concat(element.topic));
|
|
97
70
|
}
|
|
98
71
|
});
|
|
99
72
|
(0, cache_1.setCacheData)("subscribedTopic", grantedList);
|
|
100
|
-
messageCallback && mqttOnMessage(messageCallback,
|
|
73
|
+
messageCallback && mqttOnMessage(messageCallback, client_1);
|
|
101
74
|
});
|
|
102
75
|
}
|
|
103
76
|
catch (error) {
|
|
@@ -105,14 +78,15 @@ var mqttSub = function (topicList, messageCallback, client) {
|
|
|
105
78
|
}
|
|
106
79
|
};
|
|
107
80
|
exports.mqttSub = mqttSub;
|
|
108
|
-
var mqttUnSub = function (topicList,
|
|
81
|
+
var mqttUnSub = function (topicList, clientId, environment) {
|
|
109
82
|
try {
|
|
83
|
+
var client = getMqttClient(clientId);
|
|
110
84
|
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
|
|
111
85
|
if (error) {
|
|
112
86
|
logger_1.logger.error("MQTT Unsubscribe to topics error: ".concat(error.message));
|
|
113
87
|
return;
|
|
114
88
|
}
|
|
115
|
-
if (
|
|
89
|
+
if (environment === "development") {
|
|
116
90
|
topicList.forEach(function (topic) {
|
|
117
91
|
logger_1.logger.info("MQTT Unsubscribed to topic: ".concat(topic));
|
|
118
92
|
});
|
|
@@ -124,14 +98,15 @@ var mqttUnSub = function (topicList, client) {
|
|
|
124
98
|
}
|
|
125
99
|
};
|
|
126
100
|
exports.mqttUnSub = mqttUnSub;
|
|
127
|
-
var mqttDisconnect = function (props, reinit,
|
|
101
|
+
var mqttDisconnect = function (props, reinit, environment) {
|
|
128
102
|
try {
|
|
103
|
+
var client = getMqttClient(props.clientId);
|
|
129
104
|
client === null || client === void 0 ? void 0 : client.end(false, function () {
|
|
130
|
-
if (
|
|
105
|
+
if (environment === "development") {
|
|
131
106
|
logger_1.logger.info("MQTT disconnected successfully");
|
|
132
107
|
}
|
|
133
108
|
if (reinit) {
|
|
134
|
-
cachedConnection(
|
|
109
|
+
cachedConnection(props);
|
|
135
110
|
}
|
|
136
111
|
});
|
|
137
112
|
}
|
|
@@ -150,16 +125,16 @@ var mqttOnMessage = function (messageCallback, client) {
|
|
|
150
125
|
logger_1.logger.error("MQTT On-message error: ".concat(JSON.stringify(error)));
|
|
151
126
|
}
|
|
152
127
|
};
|
|
153
|
-
|
|
154
|
-
var mqttPublish = function (topic, message, client) {
|
|
128
|
+
var mqttPublish = function (topic, message, clientId, environment) {
|
|
155
129
|
try {
|
|
130
|
+
var client = getMqttClient(clientId);
|
|
156
131
|
client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
|
|
157
132
|
if (err) {
|
|
158
|
-
if (
|
|
133
|
+
if (environment === "development") {
|
|
159
134
|
logger_1.logger.error("MQTT Publish error: ".concat(err.message));
|
|
160
135
|
}
|
|
161
136
|
}
|
|
162
|
-
else if (
|
|
137
|
+
else if (environment === "development") {
|
|
163
138
|
logger_1.logger.info("MQTT Payload published");
|
|
164
139
|
}
|
|
165
140
|
});
|
|
@@ -170,23 +145,18 @@ var mqttPublish = function (topic, message, client) {
|
|
|
170
145
|
};
|
|
171
146
|
exports.mqttPublish = mqttPublish;
|
|
172
147
|
var memoizedDb = function () {
|
|
173
|
-
var cache = {};
|
|
174
148
|
return function (props) {
|
|
175
|
-
if (props.
|
|
176
|
-
if (
|
|
149
|
+
if (props.clientId in mqttClientCache) {
|
|
150
|
+
if (props.environment === "development") {
|
|
177
151
|
logger_1.logger.info("MQTT Fetching mqtt cache");
|
|
178
152
|
}
|
|
179
|
-
|
|
180
|
-
if (props.messageCallback) {
|
|
181
|
-
mqttOnMessage(props.messageCallback, cache[props.value]);
|
|
182
|
-
}
|
|
183
|
-
return cache[props.value];
|
|
153
|
+
return mqttClientCache[props.clientId];
|
|
184
154
|
}
|
|
185
|
-
if (
|
|
155
|
+
if (props.environment === "development") {
|
|
186
156
|
logger_1.logger.info("MQTT Creating new mqtt cache");
|
|
187
157
|
}
|
|
188
158
|
var result = checkInitConnect(props);
|
|
189
|
-
|
|
159
|
+
mqttClientCache[props.clientId] = result;
|
|
190
160
|
return result;
|
|
191
161
|
};
|
|
192
162
|
};
|
|
@@ -196,7 +166,7 @@ var mqttCreateConnection = function (props) {
|
|
|
196
166
|
return cachedConnection(props);
|
|
197
167
|
};
|
|
198
168
|
exports.mqttCreateConnection = mqttCreateConnection;
|
|
199
|
-
var getMqttClient = function () {
|
|
200
|
-
return
|
|
169
|
+
var getMqttClient = function (id) {
|
|
170
|
+
return mqttClientCache[id];
|
|
201
171
|
};
|
|
202
172
|
exports.getMqttClient = getMqttClient;
|
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()));
|