@mysetup/mqtt 1.2.0 → 1.4.0
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/index.js +1 -17
- package/dist/mqttClient.js +60 -76
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./mqttClient"), exports);
|
|
1
|
+
export * from "./mqttClient";
|
package/dist/mqttClient.js
CHANGED
|
@@ -1,35 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
|
|
15
|
-
var mqtt_1 = require("mqtt");
|
|
16
|
-
var logger_1 = require("@mysetup/logger");
|
|
17
|
-
var cache_1 = require("@mysetup/cache");
|
|
18
|
-
var serverConnectOptions = {
|
|
1
|
+
import { connect } from "mqtt";
|
|
2
|
+
import { logger } from "@mysetup/logger";
|
|
3
|
+
import { setCacheData } from "@mysetup/cache";
|
|
4
|
+
const serverConnectOptions = {
|
|
19
5
|
protocol: "mqtt",
|
|
20
6
|
port: 1883,
|
|
21
7
|
host: "test.mosquitto.org",
|
|
22
8
|
};
|
|
23
|
-
|
|
9
|
+
const webConnectOptions = {
|
|
24
10
|
protocol: "wss",
|
|
25
11
|
port: 8081,
|
|
26
12
|
host: "test.mosquitto.org",
|
|
27
13
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
14
|
+
let mqttClientId = "";
|
|
15
|
+
const checkInitConnect = (props) => {
|
|
16
|
+
const { protocol, host, port } = props.option === "web" ? webConnectOptions : serverConnectOptions;
|
|
17
|
+
const connectUrl = `${protocol}://${host}:${port}`;
|
|
18
|
+
const client = connect(connectUrl, {
|
|
33
19
|
clientId: props.value,
|
|
34
20
|
clean: true,
|
|
35
21
|
rejectUnauthorized: false,
|
|
@@ -38,112 +24,110 @@ var checkInitConnect = function (props) {
|
|
|
38
24
|
});
|
|
39
25
|
mqttClientId = props.value;
|
|
40
26
|
client.setMaxListeners(100);
|
|
41
|
-
client.on("connect",
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
client.on("connect", () => {
|
|
28
|
+
logger.info("MQTT connection successful");
|
|
29
|
+
setCacheData("mqttClientStatus", "connect");
|
|
44
30
|
mqttSub(props.topicList, props.messageCallback, client);
|
|
45
31
|
});
|
|
46
|
-
client.on("error",
|
|
47
|
-
|
|
48
|
-
|
|
32
|
+
client.on("error", (err) => {
|
|
33
|
+
logger.error(`MQTT Connection error: ${JSON.stringify(err)}`);
|
|
34
|
+
setCacheData("mqttClientStatus", "error");
|
|
49
35
|
mqttDisconnect(props, true, client);
|
|
50
36
|
});
|
|
51
|
-
client.on("reconnect",
|
|
52
|
-
|
|
53
|
-
|
|
37
|
+
client.on("reconnect", () => {
|
|
38
|
+
setCacheData("mqttClientStatus", "reconnect");
|
|
39
|
+
logger.info("MQTT Reconnecting");
|
|
54
40
|
});
|
|
55
|
-
client.on("close",
|
|
56
|
-
|
|
57
|
-
|
|
41
|
+
client.on("close", () => {
|
|
42
|
+
setCacheData("mqttClientStatus", "close");
|
|
43
|
+
logger.info("MQTT connection closed");
|
|
58
44
|
});
|
|
59
|
-
client.on("offline",
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
client.on("offline", () => {
|
|
46
|
+
setCacheData("mqttClientStatus", "offline");
|
|
47
|
+
logger.info("MQTT connection offline");
|
|
62
48
|
});
|
|
63
49
|
return client;
|
|
64
50
|
};
|
|
65
|
-
|
|
51
|
+
const mqttSub = (topicList, messageCallback, client) => {
|
|
66
52
|
try {
|
|
67
|
-
client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 },
|
|
53
|
+
client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, (error, granted) => {
|
|
68
54
|
if (error) {
|
|
69
|
-
|
|
55
|
+
logger.error(`Subscribe to topics error: ${error.message}`);
|
|
70
56
|
}
|
|
71
|
-
|
|
72
|
-
granted === null || granted === void 0 ? void 0 : granted.forEach(
|
|
57
|
+
const grantedList = [];
|
|
58
|
+
granted === null || granted === void 0 ? void 0 : granted.forEach((element) => {
|
|
73
59
|
grantedList.push(element.topic);
|
|
74
|
-
|
|
60
|
+
logger.info(`Subscribed to topics: ${element.topic}`);
|
|
75
61
|
});
|
|
76
|
-
|
|
62
|
+
setCacheData("subscribedTopic", grantedList);
|
|
77
63
|
messageCallback && mqttOnMessage(messageCallback, client);
|
|
78
64
|
});
|
|
79
65
|
}
|
|
80
66
|
catch (error) {
|
|
81
|
-
|
|
67
|
+
logger.error(`Subscribed error ${JSON.stringify(error)}`);
|
|
82
68
|
}
|
|
83
69
|
};
|
|
84
|
-
|
|
85
|
-
var mqttUnSub = function (topicList, client) {
|
|
70
|
+
const mqttUnSub = (topicList, client) => {
|
|
86
71
|
try {
|
|
87
|
-
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList,
|
|
72
|
+
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, (error) => {
|
|
88
73
|
if (error) {
|
|
89
|
-
|
|
74
|
+
logger.error(`Unsubscribe to topics error: ${error.message}`);
|
|
90
75
|
return;
|
|
91
76
|
}
|
|
92
|
-
topicList.forEach(
|
|
93
|
-
|
|
77
|
+
topicList.forEach((topic) => {
|
|
78
|
+
logger.info(`unsubscribed to topic: ${topic}`);
|
|
94
79
|
});
|
|
95
80
|
});
|
|
96
81
|
}
|
|
97
82
|
catch (error) {
|
|
98
|
-
|
|
83
|
+
logger.error(`unsubscribed error: ${JSON.stringify(error)}`);
|
|
99
84
|
}
|
|
100
85
|
};
|
|
101
|
-
|
|
102
|
-
var mqttDisconnect = function (props, reinit, client) {
|
|
86
|
+
const mqttDisconnect = (props, reinit, client) => {
|
|
103
87
|
try {
|
|
104
|
-
client === null || client === void 0 ? void 0 : client.end(false,
|
|
105
|
-
|
|
88
|
+
client === null || client === void 0 ? void 0 : client.end(false, () => {
|
|
89
|
+
logger.info("MQTT disconnected successfully");
|
|
106
90
|
if (reinit) {
|
|
107
|
-
cachedConnection(
|
|
91
|
+
cachedConnection({
|
|
92
|
+
...props,
|
|
93
|
+
value: `client${Math.random().toString(36).substring(7)}`,
|
|
94
|
+
});
|
|
108
95
|
}
|
|
109
96
|
});
|
|
110
97
|
}
|
|
111
98
|
catch (error) {
|
|
112
|
-
|
|
99
|
+
logger.error(`disconnect error: ${JSON.stringify(error)}`);
|
|
113
100
|
}
|
|
114
101
|
};
|
|
115
|
-
|
|
116
|
-
var mqttOnMessage = function (messageCallback, client) {
|
|
102
|
+
const mqttOnMessage = (messageCallback, client) => {
|
|
117
103
|
try {
|
|
118
|
-
client === null || client === void 0 ? void 0 : client.on("message",
|
|
104
|
+
client === null || client === void 0 ? void 0 : client.on("message", (topic, message) => {
|
|
119
105
|
messageCallback(topic, message.toString());
|
|
120
106
|
});
|
|
121
107
|
}
|
|
122
108
|
catch (error) {
|
|
123
|
-
|
|
109
|
+
logger.error(`MQTT onmessage error: ${JSON.stringify(error)}`);
|
|
124
110
|
}
|
|
125
111
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return function (props) {
|
|
112
|
+
const memoizedDb = () => {
|
|
113
|
+
const cache = {};
|
|
114
|
+
return (props) => {
|
|
130
115
|
if (props.value in cache) {
|
|
131
|
-
|
|
116
|
+
logger.info("fetching mqtt cache");
|
|
132
117
|
return cache[props.value];
|
|
133
118
|
}
|
|
134
|
-
|
|
135
|
-
|
|
119
|
+
logger.info("Creating new mqtt cache");
|
|
120
|
+
const result = checkInitConnect(props);
|
|
136
121
|
cache[props.value] = result;
|
|
137
122
|
return result;
|
|
138
123
|
};
|
|
139
124
|
};
|
|
140
125
|
// returned function from memoizedDb
|
|
141
|
-
|
|
142
|
-
|
|
126
|
+
const cachedConnection = memoizedDb();
|
|
127
|
+
const mqttCreateConnection = (props) => {
|
|
143
128
|
return cachedConnection(props);
|
|
144
129
|
};
|
|
145
|
-
|
|
146
|
-
var getMqttClient = function () {
|
|
130
|
+
const getMqttClient = () => {
|
|
147
131
|
return mqttClientId;
|
|
148
132
|
};
|
|
149
|
-
|
|
133
|
+
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mysetup/mqtt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"./package.json": "./package.json"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
+
"build": "rm -rf ./dist && tsc",
|
|
18
19
|
"lint": "eslint .",
|
|
19
20
|
"typecheck": "tsc --noEmit",
|
|
20
|
-
"build": "rm -rf ./dist && tsc",
|
|
21
21
|
"format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
|
|
22
22
|
"checks": "pnpm typecheck && pnpm lint",
|
|
23
23
|
"clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""
|