@mysetup/mqtt 1.14.1 → 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.d.ts +3 -1
- package/dist/mqttClient.d.ts.map +1 -1
- package/dist/mqttClient.js +55 -29
- package/dist/test/test-mqtt.js +1 -1
- package/package.json +4 -1
package/dist/mqttClient.d.ts
CHANGED
|
@@ -7,12 +7,14 @@ interface ConnectionProps {
|
|
|
7
7
|
option: MQttOptionProps;
|
|
8
8
|
topicList: string[];
|
|
9
9
|
messageCallback?: (topic: string, message: string) => void;
|
|
10
|
+
path?: string;
|
|
10
11
|
}
|
|
11
12
|
declare const mqttSub: (topicList: string[], messageCallback?: (topic: string, message: string) => void, client?: MqttClient) => void;
|
|
12
13
|
declare const mqttUnSub: (topicList: string[], client?: MqttClient) => void;
|
|
13
14
|
declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean, client?: MqttClient) => void;
|
|
14
15
|
declare const mqttOnMessage: (messageCallback: (topic: string, message: string) => void, client?: MqttClient) => void;
|
|
16
|
+
declare const mqttPublish: (topic: string, message: object, client?: MqttClient) => void;
|
|
15
17
|
declare const mqttCreateConnection: (props: ConnectionProps) => mqtt.MqttClient;
|
|
16
18
|
declare const getMqttClient: () => string;
|
|
17
|
-
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
|
|
19
|
+
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, mqttPublish, };
|
|
18
20
|
//# 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;AAG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AAC;AAEF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,aAAa,EACb,WAAW,GACd,CAAC"}
|
package/dist/mqttClient.js
CHANGED
|
@@ -14,19 +14,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
|
|
17
|
+
exports.mqttPublish = exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
|
|
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, ENVIRONMENT = _a.ENVIRONMENT, MQTT_HOST = _a.MQTT_HOST, MQTT_USERNAME = _a.MQTT_USERNAME, MQTT_PASSWORD = _a.MQTT_PASSWORD;
|
|
21
22
|
var serverConnectOptions = {
|
|
22
|
-
protocol: "
|
|
23
|
+
protocol: "mqtt",
|
|
23
24
|
port: 1883,
|
|
24
|
-
host:
|
|
25
|
+
host: MQTT_HOST,
|
|
25
26
|
};
|
|
26
27
|
var webConnectOptions = {
|
|
27
|
-
protocol: "
|
|
28
|
-
port:
|
|
29
|
-
host:
|
|
28
|
+
protocol: "ws",
|
|
29
|
+
port: 8083,
|
|
30
|
+
host: MQTT_HOST,
|
|
30
31
|
};
|
|
31
32
|
var mqttClientId = "";
|
|
32
33
|
var checkInitConnect = function (props) {
|
|
@@ -34,43 +35,46 @@ var checkInitConnect = function (props) {
|
|
|
34
35
|
var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port);
|
|
35
36
|
var client = mqtt_1.default.connect(connectUrl, {
|
|
36
37
|
clientId: props.value,
|
|
37
|
-
clean:
|
|
38
|
+
clean: false, /// for retaining previous message purpose
|
|
38
39
|
rejectUnauthorized: true,
|
|
39
40
|
resubscribe: true,
|
|
40
41
|
keepalive: 300,
|
|
41
|
-
username:
|
|
42
|
-
password:
|
|
42
|
+
username: MQTT_USERNAME,
|
|
43
|
+
password: MQTT_PASSWORD,
|
|
43
44
|
});
|
|
44
45
|
mqttClientId = props.value;
|
|
45
46
|
client.setMaxListeners(100);
|
|
47
|
+
if (props.messageCallback) {
|
|
48
|
+
mqttOnMessage(props.messageCallback, client); // 💡 Always attach
|
|
49
|
+
}
|
|
46
50
|
client.on("connect", function () {
|
|
47
|
-
if (
|
|
51
|
+
if (ENVIRONMENT === "development") {
|
|
48
52
|
logger_1.logger.info("MQTT connection successful");
|
|
49
53
|
}
|
|
50
54
|
(0, cache_1.setCacheData)("mqttClientStatus", "connect");
|
|
51
55
|
mqttSub(props.topicList, props.messageCallback, client);
|
|
52
56
|
});
|
|
53
57
|
client.on("error", function (err) {
|
|
54
|
-
if (
|
|
58
|
+
if (ENVIRONMENT === "development") {
|
|
55
59
|
logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
|
|
56
60
|
}
|
|
57
61
|
(0, cache_1.setCacheData)("mqttClientStatus", "error");
|
|
58
62
|
mqttDisconnect(props, true, client);
|
|
59
63
|
});
|
|
60
64
|
client.on("reconnect", function () {
|
|
61
|
-
if (
|
|
65
|
+
if (ENVIRONMENT === "development") {
|
|
62
66
|
logger_1.logger.info("MQTT Reconnecting");
|
|
63
67
|
}
|
|
64
68
|
(0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
|
|
65
69
|
});
|
|
66
70
|
client.on("close", function () {
|
|
67
|
-
if (
|
|
71
|
+
if (ENVIRONMENT === "development") {
|
|
68
72
|
logger_1.logger.info("MQTT connection closed");
|
|
69
73
|
}
|
|
70
74
|
(0, cache_1.setCacheData)("mqttClientStatus", "close");
|
|
71
75
|
});
|
|
72
76
|
client.on("offline", function () {
|
|
73
|
-
if (
|
|
77
|
+
if (ENVIRONMENT === "development") {
|
|
74
78
|
logger_1.logger.info("MQTT connection offline");
|
|
75
79
|
}
|
|
76
80
|
(0, cache_1.setCacheData)("mqttClientStatus", "offline");
|
|
@@ -81,13 +85,13 @@ var mqttSub = function (topicList, messageCallback, client) {
|
|
|
81
85
|
try {
|
|
82
86
|
client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, function (error, granted) {
|
|
83
87
|
if (error) {
|
|
84
|
-
logger_1.logger.error("Subscribe to topics error: ".concat(error.message));
|
|
88
|
+
logger_1.logger.error("MQTT Subscribe to topics error: ".concat(error.message));
|
|
85
89
|
}
|
|
86
90
|
var grantedList = [];
|
|
87
91
|
granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
|
|
88
92
|
grantedList.push(element.topic);
|
|
89
|
-
if (
|
|
90
|
-
logger_1.logger.info("Subscribed to topics: ".concat(element.topic));
|
|
93
|
+
if (ENVIRONMENT === "development") {
|
|
94
|
+
logger_1.logger.info("MQTT Subscribed to topics: ".concat(element.topic));
|
|
91
95
|
}
|
|
92
96
|
});
|
|
93
97
|
(0, cache_1.setCacheData)("subscribedTopic", grantedList);
|
|
@@ -95,7 +99,7 @@ var mqttSub = function (topicList, messageCallback, client) {
|
|
|
95
99
|
});
|
|
96
100
|
}
|
|
97
101
|
catch (error) {
|
|
98
|
-
logger_1.logger.error("Subscribed error ".concat(JSON.stringify(error)));
|
|
102
|
+
logger_1.logger.error("MQTT Subscribed error ".concat(JSON.stringify(error)));
|
|
99
103
|
}
|
|
100
104
|
};
|
|
101
105
|
exports.mqttSub = mqttSub;
|
|
@@ -103,25 +107,25 @@ var mqttUnSub = function (topicList, client) {
|
|
|
103
107
|
try {
|
|
104
108
|
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
|
|
105
109
|
if (error) {
|
|
106
|
-
logger_1.logger.error("Unsubscribe to topics error: ".concat(error.message));
|
|
110
|
+
logger_1.logger.error("MQTT Unsubscribe to topics error: ".concat(error.message));
|
|
107
111
|
return;
|
|
108
112
|
}
|
|
109
|
-
if (
|
|
113
|
+
if (ENVIRONMENT === "development") {
|
|
110
114
|
topicList.forEach(function (topic) {
|
|
111
|
-
logger_1.logger.info("
|
|
115
|
+
logger_1.logger.info("MQTT Unsubscribed to topic: ".concat(topic));
|
|
112
116
|
});
|
|
113
117
|
}
|
|
114
118
|
});
|
|
115
119
|
}
|
|
116
120
|
catch (error) {
|
|
117
|
-
logger_1.logger.error("
|
|
121
|
+
logger_1.logger.error("MQTT Unsubscribed error: ".concat(JSON.stringify(error)));
|
|
118
122
|
}
|
|
119
123
|
};
|
|
120
124
|
exports.mqttUnSub = mqttUnSub;
|
|
121
125
|
var mqttDisconnect = function (props, reinit, client) {
|
|
122
126
|
try {
|
|
123
127
|
client === null || client === void 0 ? void 0 : client.end(false, function () {
|
|
124
|
-
if (
|
|
128
|
+
if (ENVIRONMENT === "development") {
|
|
125
129
|
logger_1.logger.info("MQTT disconnected successfully");
|
|
126
130
|
}
|
|
127
131
|
if (reinit) {
|
|
@@ -130,7 +134,7 @@ var mqttDisconnect = function (props, reinit, client) {
|
|
|
130
134
|
});
|
|
131
135
|
}
|
|
132
136
|
catch (error) {
|
|
133
|
-
logger_1.logger.error("
|
|
137
|
+
logger_1.logger.error("MQTT Disconnect error: ".concat(JSON.stringify(error)));
|
|
134
138
|
}
|
|
135
139
|
};
|
|
136
140
|
exports.mqttDisconnect = mqttDisconnect;
|
|
@@ -141,21 +145,43 @@ var mqttOnMessage = function (messageCallback, client) {
|
|
|
141
145
|
});
|
|
142
146
|
}
|
|
143
147
|
catch (error) {
|
|
144
|
-
logger_1.logger.error("MQTT
|
|
148
|
+
logger_1.logger.error("MQTT On-message error: ".concat(JSON.stringify(error)));
|
|
145
149
|
}
|
|
146
150
|
};
|
|
147
151
|
exports.mqttOnMessage = mqttOnMessage;
|
|
152
|
+
var mqttPublish = function (topic, message, client) {
|
|
153
|
+
try {
|
|
154
|
+
client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
|
|
155
|
+
if (err) {
|
|
156
|
+
if (ENVIRONMENT === "development") {
|
|
157
|
+
logger_1.logger.error("MQTT Publish error: ".concat(err.message));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else if (ENVIRONMENT === "development") {
|
|
161
|
+
logger_1.logger.info("MQTT Payload published");
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
logger_1.logger.error("MQTT Publish exception: ".concat(JSON.stringify(error)));
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
exports.mqttPublish = mqttPublish;
|
|
148
170
|
var memoizedDb = function () {
|
|
149
171
|
var cache = {};
|
|
150
172
|
return function (props) {
|
|
151
173
|
if (props.value in cache) {
|
|
152
|
-
if (
|
|
153
|
-
logger_1.logger.info("
|
|
174
|
+
if (ENVIRONMENT === "development") {
|
|
175
|
+
logger_1.logger.info("MQTT Fetching mqtt cache");
|
|
176
|
+
}
|
|
177
|
+
//to invoke the message callback if it exists,when use cached value
|
|
178
|
+
if (props.messageCallback) {
|
|
179
|
+
mqttOnMessage(props.messageCallback, cache[props.value]);
|
|
154
180
|
}
|
|
155
181
|
return cache[props.value];
|
|
156
182
|
}
|
|
157
|
-
if (
|
|
158
|
-
logger_1.logger.info("Creating new mqtt cache");
|
|
183
|
+
if (ENVIRONMENT === "development") {
|
|
184
|
+
logger_1.logger.info("MQTT Creating new mqtt cache");
|
|
159
185
|
}
|
|
160
186
|
var result = checkInitConnect(props);
|
|
161
187
|
cache[props.value] = result;
|
package/dist/test/test-mqtt.js
CHANGED
|
@@ -6,7 +6,7 @@ var CLIENT_ID = "testClient-".concat(Math.random().toString(16).slice(2, 8));
|
|
|
6
6
|
(0, mqttClient_1.mqttCreateConnection)({
|
|
7
7
|
value: CLIENT_ID,
|
|
8
8
|
option: "server",
|
|
9
|
-
topicList: ["mj/ems/
|
|
9
|
+
topicList: ["mj/ems/test"],
|
|
10
10
|
messageCallback: function (topic, message) {
|
|
11
11
|
logger_1.logger.info("Message received \u2192 topic: ".concat(topic, ", message: ").concat(message.toString()));
|
|
12
12
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mysetup/mqtt",
|
|
3
|
-
"version": "1.14.
|
|
3
|
+
"version": "1.14.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"./package.json": "./package.json"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
+
"dev": "nodemon",
|
|
18
19
|
"lint": "eslint .",
|
|
19
20
|
"typecheck": "tsc --noEmit",
|
|
20
21
|
"build": "rm -rf ./dist && tsc",
|
|
@@ -33,6 +34,8 @@
|
|
|
33
34
|
"@mysetup/tsconfig": "latest",
|
|
34
35
|
"@types/node": "^22.16.4",
|
|
35
36
|
"eslint": "8.57.0",
|
|
37
|
+
"nodemon": "3.1.7",
|
|
38
|
+
"ts-node": "^10.9.2",
|
|
36
39
|
"typescript": "^5.7.3"
|
|
37
40
|
},
|
|
38
41
|
"prettier": "@mysetup/prettier-config",
|