@mysetup/mqtt 1.14.2 → 1.14.3
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.js +20 -22
- package/package.json +1 -1
package/dist/mqttClient.js
CHANGED
|
@@ -18,31 +18,29 @@ exports.mqttPublish = exports.getMqttClient = exports.mqttOnMessage = exports.mq
|
|
|
18
18
|
var mqtt_1 = __importDefault(require("mqtt"));
|
|
19
19
|
var logger_1 = require("@mysetup/logger");
|
|
20
20
|
var cache_1 = require("@mysetup/cache");
|
|
21
|
-
var _a = process.env,
|
|
21
|
+
var _a = process.env, ENVIRONMENT = _a.ENVIRONMENT, MQTT_HOST = _a.MQTT_HOST, MQTT_USERNAME = _a.MQTT_USERNAME, MQTT_PASSWORD = _a.MQTT_PASSWORD;
|
|
22
22
|
var serverConnectOptions = {
|
|
23
23
|
protocol: "mqtt",
|
|
24
24
|
port: 1883,
|
|
25
|
-
host:
|
|
26
|
-
path: "",
|
|
25
|
+
host: MQTT_HOST,
|
|
27
26
|
};
|
|
28
27
|
var webConnectOptions = {
|
|
29
28
|
protocol: "ws",
|
|
30
|
-
port:
|
|
31
|
-
host:
|
|
32
|
-
path: "/mqtt",
|
|
29
|
+
port: 8083,
|
|
30
|
+
host: MQTT_HOST,
|
|
33
31
|
};
|
|
34
32
|
var mqttClientId = "";
|
|
35
33
|
var checkInitConnect = function (props) {
|
|
36
|
-
var _a = props.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port
|
|
37
|
-
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port)
|
|
34
|
+
var _a = props.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port;
|
|
35
|
+
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port);
|
|
38
36
|
var client = mqtt_1.default.connect(connectUrl, {
|
|
39
37
|
clientId: props.value,
|
|
40
38
|
clean: false, /// for retaining previous message purpose
|
|
41
39
|
rejectUnauthorized: true,
|
|
42
40
|
resubscribe: true,
|
|
43
41
|
keepalive: 300,
|
|
44
|
-
username:
|
|
45
|
-
password:
|
|
42
|
+
username: MQTT_USERNAME,
|
|
43
|
+
password: MQTT_PASSWORD,
|
|
46
44
|
});
|
|
47
45
|
mqttClientId = props.value;
|
|
48
46
|
client.setMaxListeners(100);
|
|
@@ -50,33 +48,33 @@ var checkInitConnect = function (props) {
|
|
|
50
48
|
mqttOnMessage(props.messageCallback, client); // 💡 Always attach
|
|
51
49
|
}
|
|
52
50
|
client.on("connect", function () {
|
|
53
|
-
if (
|
|
51
|
+
if (ENVIRONMENT === "development") {
|
|
54
52
|
logger_1.logger.info("MQTT connection successful");
|
|
55
53
|
}
|
|
56
54
|
(0, cache_1.setCacheData)("mqttClientStatus", "connect");
|
|
57
55
|
mqttSub(props.topicList, props.messageCallback, client);
|
|
58
56
|
});
|
|
59
57
|
client.on("error", function (err) {
|
|
60
|
-
if (
|
|
58
|
+
if (ENVIRONMENT === "development") {
|
|
61
59
|
logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
|
|
62
60
|
}
|
|
63
61
|
(0, cache_1.setCacheData)("mqttClientStatus", "error");
|
|
64
62
|
mqttDisconnect(props, true, client);
|
|
65
63
|
});
|
|
66
64
|
client.on("reconnect", function () {
|
|
67
|
-
if (
|
|
65
|
+
if (ENVIRONMENT === "development") {
|
|
68
66
|
logger_1.logger.info("MQTT Reconnecting");
|
|
69
67
|
}
|
|
70
68
|
(0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
|
|
71
69
|
});
|
|
72
70
|
client.on("close", function () {
|
|
73
|
-
if (
|
|
71
|
+
if (ENVIRONMENT === "development") {
|
|
74
72
|
logger_1.logger.info("MQTT connection closed");
|
|
75
73
|
}
|
|
76
74
|
(0, cache_1.setCacheData)("mqttClientStatus", "close");
|
|
77
75
|
});
|
|
78
76
|
client.on("offline", function () {
|
|
79
|
-
if (
|
|
77
|
+
if (ENVIRONMENT === "development") {
|
|
80
78
|
logger_1.logger.info("MQTT connection offline");
|
|
81
79
|
}
|
|
82
80
|
(0, cache_1.setCacheData)("mqttClientStatus", "offline");
|
|
@@ -92,7 +90,7 @@ var mqttSub = function (topicList, messageCallback, client) {
|
|
|
92
90
|
var grantedList = [];
|
|
93
91
|
granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
|
|
94
92
|
grantedList.push(element.topic);
|
|
95
|
-
if (
|
|
93
|
+
if (ENVIRONMENT === "development") {
|
|
96
94
|
logger_1.logger.info("MQTT Subscribed to topics: ".concat(element.topic));
|
|
97
95
|
}
|
|
98
96
|
});
|
|
@@ -112,7 +110,7 @@ var mqttUnSub = function (topicList, client) {
|
|
|
112
110
|
logger_1.logger.error("MQTT Unsubscribe to topics error: ".concat(error.message));
|
|
113
111
|
return;
|
|
114
112
|
}
|
|
115
|
-
if (
|
|
113
|
+
if (ENVIRONMENT === "development") {
|
|
116
114
|
topicList.forEach(function (topic) {
|
|
117
115
|
logger_1.logger.info("MQTT Unsubscribed to topic: ".concat(topic));
|
|
118
116
|
});
|
|
@@ -127,7 +125,7 @@ exports.mqttUnSub = mqttUnSub;
|
|
|
127
125
|
var mqttDisconnect = function (props, reinit, client) {
|
|
128
126
|
try {
|
|
129
127
|
client === null || client === void 0 ? void 0 : client.end(false, function () {
|
|
130
|
-
if (
|
|
128
|
+
if (ENVIRONMENT === "development") {
|
|
131
129
|
logger_1.logger.info("MQTT disconnected successfully");
|
|
132
130
|
}
|
|
133
131
|
if (reinit) {
|
|
@@ -155,11 +153,11 @@ var mqttPublish = function (topic, message, client) {
|
|
|
155
153
|
try {
|
|
156
154
|
client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
|
|
157
155
|
if (err) {
|
|
158
|
-
if (
|
|
156
|
+
if (ENVIRONMENT === "development") {
|
|
159
157
|
logger_1.logger.error("MQTT Publish error: ".concat(err.message));
|
|
160
158
|
}
|
|
161
159
|
}
|
|
162
|
-
else if (
|
|
160
|
+
else if (ENVIRONMENT === "development") {
|
|
163
161
|
logger_1.logger.info("MQTT Payload published");
|
|
164
162
|
}
|
|
165
163
|
});
|
|
@@ -173,7 +171,7 @@ var memoizedDb = function () {
|
|
|
173
171
|
var cache = {};
|
|
174
172
|
return function (props) {
|
|
175
173
|
if (props.value in cache) {
|
|
176
|
-
if (
|
|
174
|
+
if (ENVIRONMENT === "development") {
|
|
177
175
|
logger_1.logger.info("MQTT Fetching mqtt cache");
|
|
178
176
|
}
|
|
179
177
|
//to invoke the message callback if it exists,when use cached value
|
|
@@ -182,7 +180,7 @@ var memoizedDb = function () {
|
|
|
182
180
|
}
|
|
183
181
|
return cache[props.value];
|
|
184
182
|
}
|
|
185
|
-
if (
|
|
183
|
+
if (ENVIRONMENT === "development") {
|
|
186
184
|
logger_1.logger.info("MQTT Creating new mqtt cache");
|
|
187
185
|
}
|
|
188
186
|
var result = checkInitConnect(props);
|