@mysetup/mqtt 1.14.3 → 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.
@@ -1,20 +1,26 @@
1
1
  import mqtt from "mqtt";
2
2
  import type { MqttClient } from "mqtt";
3
3
  export type { MqttClient };
4
- type MQttOptionProps = "web" | "server";
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
- value: string;
7
- option: MQttOptionProps;
13
+ clientId: string;
14
+ config: MqttConfigProps;
8
15
  topicList: string[];
9
16
  messageCallback?: (topic: string, message: string) => void;
10
- path?: string;
17
+ environment?: string | undefined;
11
18
  }
12
- declare const mqttSub: (topicList: string[], messageCallback?: (topic: string, message: string) => void, client?: MqttClient) => void;
13
- declare const mqttUnSub: (topicList: string[], client?: MqttClient) => void;
14
- declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean, client?: MqttClient) => void;
15
- declare const mqttOnMessage: (messageCallback: (topic: string, message: string) => void, client?: MqttClient) => void;
16
- declare const mqttPublish: (topic: string, message: object, client?: MqttClient) => void;
17
- declare const mqttCreateConnection: (props: ConnectionProps) => mqtt.MqttClient;
18
- declare const getMqttClient: () => string;
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
@@ -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,KAAK,eAAe,GAAG,KAAK,GAAG,QAAQ,CAAC;AAIxC,UAAU,eAAe;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,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,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AA8ED,QAAA,MAAM,OAAO,GACT,WAAW,MAAM,EAAE,EACnB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,EAC1D,SAAS,UAAU,SAwBtB,CAAC;AAEF,QAAA,MAAM,SAAS,GAAI,WAAW,MAAM,EAAE,EAAE,SAAS,UAAU,SAkB1D,CAAC;AAEF,QAAA,MAAM,cAAc,GAChB,OAAO,eAAe,EACtB,QAAQ,OAAO,EACf,SAAS,UAAU,SAiBtB,CAAC;AAEF,QAAA,MAAM,aAAa,GACf,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,EACzD,SAAS,UAAU,SAStB,CAAC;AAEF,QAAA,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,SAAS,MAAM,EAAE,SAAS,UAAU,SAmBvE,CAAC;AA2BF,QAAA,MAAM,oBAAoB,GAAI,OAAO,eAAe,oBAEnD,CAAC;AAEF,QAAA,MAAM,aAAa,cAElB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,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"}
@@ -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.mqttPublish = exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
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 _a = process.env, ENVIRONMENT = _a.ENVIRONMENT, MQTT_HOST = _a.MQTT_HOST, MQTT_USERNAME = _a.MQTT_USERNAME, MQTT_PASSWORD = _a.MQTT_PASSWORD;
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.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port;
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.value,
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: MQTT_USERNAME,
43
- password: MQTT_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 (ENVIRONMENT === "development") {
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, client);
29
+ mqttSub(props.topicList, props.clientId, props.messageCallback, props.environment);
56
30
  });
57
31
  client.on("error", function (err) {
58
- if (ENVIRONMENT === "development") {
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, client);
36
+ mqttDisconnect(props, true);
63
37
  });
64
38
  client.on("reconnect", function () {
65
- if (ENVIRONMENT === "development") {
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 (ENVIRONMENT === "development") {
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 (ENVIRONMENT === "development") {
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, client) {
58
+ var mqttSub = function (topicList, clientId, messageCallback, environment) {
85
59
  try {
86
- client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, function (error, granted) {
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 (ENVIRONMENT === "development") {
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, client);
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, client) {
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 (ENVIRONMENT === "development") {
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,15 @@ var mqttUnSub = function (topicList, client) {
122
98
  }
123
99
  };
124
100
  exports.mqttUnSub = mqttUnSub;
125
- var mqttDisconnect = function (props, reinit, client) {
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 (ENVIRONMENT === "development") {
105
+ if (environment === "development") {
129
106
  logger_1.logger.info("MQTT disconnected successfully");
130
107
  }
131
108
  if (reinit) {
132
- cachedConnection(__assign(__assign({}, props), { value: "client".concat(Math.random().toString(36).substring(7)) }));
109
+ cachedConnection(props);
133
110
  }
134
111
  });
135
112
  }
@@ -148,16 +125,16 @@ var mqttOnMessage = function (messageCallback, client) {
148
125
  logger_1.logger.error("MQTT On-message error: ".concat(JSON.stringify(error)));
149
126
  }
150
127
  };
151
- exports.mqttOnMessage = mqttOnMessage;
152
- var mqttPublish = function (topic, message, client) {
128
+ var mqttPublish = function (topic, message, clientId, environment) {
153
129
  try {
130
+ var client = getMqttClient(clientId);
154
131
  client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
155
132
  if (err) {
156
- if (ENVIRONMENT === "development") {
133
+ if (environment === "development") {
157
134
  logger_1.logger.error("MQTT Publish error: ".concat(err.message));
158
135
  }
159
136
  }
160
- else if (ENVIRONMENT === "development") {
137
+ else if (environment === "development") {
161
138
  logger_1.logger.info("MQTT Payload published");
162
139
  }
163
140
  });
@@ -168,23 +145,18 @@ var mqttPublish = function (topic, message, client) {
168
145
  };
169
146
  exports.mqttPublish = mqttPublish;
170
147
  var memoizedDb = function () {
171
- var cache = {};
172
148
  return function (props) {
173
- if (props.value in cache) {
174
- if (ENVIRONMENT === "development") {
149
+ if (props.clientId in mqttClientCache) {
150
+ if (props.environment === "development") {
175
151
  logger_1.logger.info("MQTT Fetching mqtt cache");
176
152
  }
177
- //to invoke the message callback if it exists,when use cached value
178
- if (props.messageCallback) {
179
- mqttOnMessage(props.messageCallback, cache[props.value]);
180
- }
181
- return cache[props.value];
153
+ return mqttClientCache[props.clientId];
182
154
  }
183
- if (ENVIRONMENT === "development") {
155
+ if (props.environment === "development") {
184
156
  logger_1.logger.info("MQTT Creating new mqtt cache");
185
157
  }
186
158
  var result = checkInitConnect(props);
187
- cache[props.value] = result;
159
+ mqttClientCache[props.clientId] = result;
188
160
  return result;
189
161
  };
190
162
  };
@@ -194,7 +166,7 @@ var mqttCreateConnection = function (props) {
194
166
  return cachedConnection(props);
195
167
  };
196
168
  exports.mqttCreateConnection = mqttCreateConnection;
197
- var getMqttClient = function () {
198
- return mqttClientId;
169
+ var getMqttClient = function (id) {
170
+ return mqttClientCache[id];
199
171
  };
200
172
  exports.getMqttClient = getMqttClient;
@@ -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
- value: CLIENT_ID,
8
- option: "server",
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()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysetup/mqtt",
3
- "version": "1.14.3",
3
+ "version": "1.14.4",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [