mm_os 4.2.6 → 4.2.8
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/README.md +1 -1
- package/README_EN.md +2 -2
- package/adapter/adapter.js +105 -52
- package/adapter/custom_persistence.js +32 -31
- package/adapter/mqtt.js +59 -79
- package/adapter/socket.js +188 -58
- package/adapter/web.js +92 -68
- package/adapter/websocket.js +43 -146
- package/com/api/drive.js +732 -708
- package/com/api/index.js +196 -198
- package/com/cmd/config.tpl.json +48 -122
- package/com/cmd/drive.js +176 -1548
- package/com/cmd/index.js +287 -1066
- package/com/cmd/job/config.json +66 -0
- package/com/cmd/job/index.js +241 -0
- package/com/cmd/job/test.js +188 -0
- package/com/cmd/script.tpl.js +12 -33
- package/com/db/drive.js +2206 -2019
- package/com/db/index.js +241 -242
- package/com/event/config.tpl.json +3 -1
- package/com/event/drive.js +3 -1
- package/com/event/index.js +73 -49
- package/com/event/script.tpl.js +1 -1
- package/com/mqtt/config.tpl.json +16 -8
- package/com/mqtt/drive.js +696 -676
- package/com/mqtt/index.js +1012 -822
- package/com/mqtt/index.test.js +503 -0
- package/com/mqtt/mm_mqtt.js +446 -420
- package/com/mqtt/script.tpl.js +3 -7
- package/com/nav/drive.js +7 -7
- package/com/nav/index.js +228 -231
- package/com/param/index.js +19 -17
- package/com/pendant/index.js +467 -441
- package/com/socket/drive.js +679 -616
- package/com/socket/index.js +94 -48
- package/com/sql/drive.js +1283 -1259
- package/com/sql/index.js +152 -150
- package/com/static/drive.js +1 -1
- package/com/static/index.js +232 -226
- package/com/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- package/com/template/drive.js +1 -1
- package/com/template/index.js +141 -138
- package/com.js +87 -6
- package/common/middleware/web_after/index.js +4 -10
- package/common/middleware/web_base/index.js +1 -1
- package/common/middleware/web_cors/index.js +1 -1
- package/common/middleware/web_error/index.js +131 -119
- package/common/middleware/web_error/middleware.json +2 -1
- package/common/middleware/web_logger/index.js +1 -1
- package/common/middleware/web_main/index.js +2 -0
- package/common/middleware/web_static/index.js +2 -2
- package/common/middleware/web_waf/index.js +1 -1
- package/core/app/config.tpl.json +4 -4
- package/core/app/index.js +710 -534
- package/core/middleware/index.js +1 -9
- package/core/mod/config.tpl.json +1 -1
- package/core/model/config.tpl.json +1 -1
- package/core/plugin/config.tpl.json +15 -3
- package/core/plugin/index.js +31 -0
- package/core/pusher/index.js +158 -161
- package/core/scene/loop.js +1306 -1255
- package/core/store/index.js +1794 -1755
- package/core/store/sql.js +1488 -1464
- package/core/zone/bat/index.js +744 -728
- package/core/zone/config.tpl.json +3 -3
- package/core/zone/index.js +655 -616
- package/os.js +0 -4
- package/package.json +16 -16
- package/server.js +583 -427
- package/com/cmd/nlp.js +0 -525
- package/core/app/index copy.js +0 -611
- package/software.js +0 -194
- package/tcp.js +0 -279
package/com/mqtt/mm_mqtt.js
CHANGED
|
@@ -1,420 +1,446 @@
|
|
|
1
|
-
const MQTT = require('mqtt');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @module MQTT客户端
|
|
5
|
-
*/
|
|
6
|
-
class Mqtt {
|
|
7
|
-
static config = {
|
|
8
|
-
hostname: '127.0.0.1',
|
|
9
|
-
port: '',
|
|
10
|
-
protocol: 'ws',
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
username: '
|
|
15
|
-
password: '
|
|
16
|
-
clean: false,
|
|
17
|
-
topic_receive: 'client/message/',
|
|
18
|
-
longtime: 15000,
|
|
19
|
-
// maxInflight: 20,
|
|
20
|
-
// 重连间隔时间,单位为毫秒, 默认6秒
|
|
21
|
-
reco_period: 6000,
|
|
22
|
-
// 连接超时时间,单位为毫秒, 默认30秒, 即尝试连接5次
|
|
23
|
-
connect_timeout: 30000,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
method
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
*
|
|
108
|
-
* @
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
*
|
|
131
|
-
* @
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
delete cg.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
* @
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
*
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
return false;
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
*
|
|
294
|
-
* @param {
|
|
295
|
-
* @returns {
|
|
296
|
-
* @private
|
|
297
|
-
*/
|
|
298
|
-
Mqtt.prototype.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
Mqtt.prototype.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
this.
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
Mqtt.prototype.
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
|
|
1
|
+
const MQTT = require('mqtt');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @module MQTT客户端
|
|
5
|
+
*/
|
|
6
|
+
class Mqtt {
|
|
7
|
+
static config = {
|
|
8
|
+
hostname: '127.0.0.1',
|
|
9
|
+
port: '',
|
|
10
|
+
protocol: 'ws',
|
|
11
|
+
subs_qos: 0,
|
|
12
|
+
publish_qos: 1,
|
|
13
|
+
clientid: 'server',
|
|
14
|
+
username: 'admin',
|
|
15
|
+
password: process.env.MQTT_PASSWORD || '',
|
|
16
|
+
clean: false,
|
|
17
|
+
topic_receive: 'client/message/',
|
|
18
|
+
longtime: 15000,
|
|
19
|
+
// maxInflight: 20,
|
|
20
|
+
// 重连间隔时间,单位为毫秒, 默认6秒
|
|
21
|
+
reco_period: 6000,
|
|
22
|
+
// 连接超时时间,单位为毫秒, 默认30秒, 即尝试连接5次
|
|
23
|
+
connect_timeout: 30000,
|
|
24
|
+
...config,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 构造函数
|
|
28
|
+
* @param {object} config 配置参数
|
|
29
|
+
*/
|
|
30
|
+
constructor(config) {
|
|
31
|
+
this.config = { ...Mqtt.config, ...config };
|
|
32
|
+
// mqtt客户端服务器
|
|
33
|
+
this.client = null;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* message queue
|
|
37
|
+
*/
|
|
38
|
+
this.list_msg = [
|
|
39
|
+
/*
|
|
40
|
+
{
|
|
41
|
+
// information ID
|
|
42
|
+
id: "",
|
|
43
|
+
// request method
|
|
44
|
+
method: "",
|
|
45
|
+
// message parameters
|
|
46
|
+
params: {},
|
|
47
|
+
// Callback
|
|
48
|
+
func: function(res){}
|
|
49
|
+
}
|
|
50
|
+
*/
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 订阅集合 (主题 => 函数列表)
|
|
55
|
+
*/
|
|
56
|
+
this.dict_subscribe = {};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* method collection
|
|
60
|
+
*/
|
|
61
|
+
this.methods = {
|
|
62
|
+
/**
|
|
63
|
+
* 获取所有方法
|
|
64
|
+
* @returns {Array} 方法列表
|
|
65
|
+
*/
|
|
66
|
+
getMethod: function () {
|
|
67
|
+
return Object.keys(_this.methods);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
this.retry_times = 0;
|
|
72
|
+
this.connecting = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 重新连接
|
|
78
|
+
*/
|
|
79
|
+
Mqtt.prototype.reconnect = function () {
|
|
80
|
+
this.retry_times += 1;
|
|
81
|
+
if (this.retryTimes > 5) {
|
|
82
|
+
try {
|
|
83
|
+
this.client.end();
|
|
84
|
+
this._reconnect_timer = setTimeout(() => {
|
|
85
|
+
this.init();
|
|
86
|
+
}, this.config.reconnectPeriod);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
this.$message.error(error.toString());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 初始化
|
|
95
|
+
* @param {object} config 配置参数
|
|
96
|
+
*/
|
|
97
|
+
Mqtt.prototype.init = function (config) {
|
|
98
|
+
this.config = Object.assign(this.config, config);
|
|
99
|
+
this.client = {
|
|
100
|
+
connected: false,
|
|
101
|
+
};
|
|
102
|
+
this.retry_times = 0;
|
|
103
|
+
this.connecting = false;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 运行MQTT
|
|
108
|
+
* @param {object} config 配置参数
|
|
109
|
+
* @returns {Promise} 连接结果
|
|
110
|
+
*/
|
|
111
|
+
Mqtt.prototype.run = function (config) {
|
|
112
|
+
this.connecting = true;
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
var cg = Object.assign(this.config, config);
|
|
116
|
+
this._createClient(cg);
|
|
117
|
+
|
|
118
|
+
if (this.client.on) {
|
|
119
|
+
return this._setupClientEvents();
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
this.connecting = false;
|
|
123
|
+
return this._rejectPromise(error);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return this._rejectPromise();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 创建客户端
|
|
131
|
+
* @param {object} config 配置参数
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
Mqtt.prototype._createClient = function (config) {
|
|
135
|
+
// 创建配置副本以避免修改原始参数
|
|
136
|
+
var cg = { ...config };
|
|
137
|
+
if (cg.host) {
|
|
138
|
+
delete cg.hostname;
|
|
139
|
+
delete cg.port;
|
|
140
|
+
this.client = MQTT.connect(cg.host, cg);
|
|
141
|
+
} else {
|
|
142
|
+
this.client = MQTT.connect(cg);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 设置客户端事件
|
|
148
|
+
* @returns {Promise} 连接结果
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
Mqtt.prototype._setupClientEvents = function () {
|
|
152
|
+
var timeout_ms = this.config.connect_timeout || 30000;
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
var timeout_timer = setTimeout(() => {
|
|
155
|
+
this.connecting = false;
|
|
156
|
+
reject(new Error('MQTT连接超时'));
|
|
157
|
+
}, timeout_ms);
|
|
158
|
+
|
|
159
|
+
this.client.on('connect', (packet, err) => {
|
|
160
|
+
clearTimeout(timeout_timer);
|
|
161
|
+
this.connecting = false;
|
|
162
|
+
if (err) {
|
|
163
|
+
reject(err);
|
|
164
|
+
} else {
|
|
165
|
+
this.client.on('message', async (topic, message) => {
|
|
166
|
+
await this.receive(topic, message.toString());
|
|
167
|
+
});
|
|
168
|
+
resolve(packet);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
this.client.on('reconnect', this.reconnect);
|
|
172
|
+
this.client.on('error', (error) => {
|
|
173
|
+
clearTimeout(timeout_timer);
|
|
174
|
+
console.error('Connection failed', error);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 创建被拒绝的Promise
|
|
181
|
+
* @param {Error} error 错误对象
|
|
182
|
+
* @returns {Promise} 被拒绝的Promise
|
|
183
|
+
* @private
|
|
184
|
+
*/
|
|
185
|
+
Mqtt.prototype._rejectPromise = function (error) {
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
if (error) {
|
|
188
|
+
reject(error);
|
|
189
|
+
} else {
|
|
190
|
+
resolve(null);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 监听事件
|
|
197
|
+
* @param {string} event_name 事件名称
|
|
198
|
+
* @param {Function} func 事件处理函数
|
|
199
|
+
*/
|
|
200
|
+
Mqtt.prototype.on = function (event_name, func) {
|
|
201
|
+
this.client.on(event_name, func);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 接收消息
|
|
206
|
+
* @param {string} topic 订阅板块
|
|
207
|
+
* @param {object} msg 消息主体
|
|
208
|
+
*/
|
|
209
|
+
Mqtt.prototype.message = async function (topic, msg) {
|
|
210
|
+
if (this.dict_subscribe[topic]) {
|
|
211
|
+
var list = this.dict_subscribe[topic];
|
|
212
|
+
try {
|
|
213
|
+
for (var key in list) {
|
|
214
|
+
list[key](msg);
|
|
215
|
+
}
|
|
216
|
+
} catch (err) {
|
|
217
|
+
console.error(err);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 接收消息
|
|
224
|
+
* @param {string} topic 订阅
|
|
225
|
+
* @param {string} body_str 消息主体字符串
|
|
226
|
+
* @returns {object} 解析后的消息主体
|
|
227
|
+
*/
|
|
228
|
+
Mqtt.prototype.receive = async function (topic, body_str) {
|
|
229
|
+
var json = this._parseMessage(body_str);
|
|
230
|
+
|
|
231
|
+
if (json) {
|
|
232
|
+
if (this._handleMessage(json)) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (this._handleMethodMessage(json)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
await this.message(topic, json);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
await this.message(topic, body_str);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 解析消息
|
|
249
|
+
* @param {string} body_str 消息主体字符串
|
|
250
|
+
* @returns {object|null} 解析后的消息
|
|
251
|
+
* @private
|
|
252
|
+
*/
|
|
253
|
+
Mqtt.prototype._parseMessage = function (body_str) {
|
|
254
|
+
if (
|
|
255
|
+
body_str &&
|
|
256
|
+
(body_str.indexOf('[') === 0 || body_str.indexOf('{') === 0)
|
|
257
|
+
) {
|
|
258
|
+
try {
|
|
259
|
+
return JSON.parse(body_str);
|
|
260
|
+
} catch {}
|
|
261
|
+
}
|
|
262
|
+
return null;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 处理消息
|
|
267
|
+
* @param {object} json 消息对象
|
|
268
|
+
* @returns {boolean} 是否处理成功
|
|
269
|
+
* @private
|
|
270
|
+
*/
|
|
271
|
+
Mqtt.prototype._handleMessage = function (json) {
|
|
272
|
+
var id = json.id;
|
|
273
|
+
if (json.result && id) {
|
|
274
|
+
var lt = this.list_msg;
|
|
275
|
+
var len = lt.length;
|
|
276
|
+
for (var i = 0; i < len; i++) {
|
|
277
|
+
var o = lt[i];
|
|
278
|
+
if (id === o.id) {
|
|
279
|
+
o.func(json.result);
|
|
280
|
+
lt.splice(i, 1);
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// 限制消息队列大小, 超过200条时清理最早的一半
|
|
286
|
+
if (this.list_msg.length > 200) {
|
|
287
|
+
this.list_msg = this.list_msg.slice(-100);
|
|
288
|
+
}
|
|
289
|
+
return false;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 处理方法消息
|
|
294
|
+
* @param {object} json 消息对象
|
|
295
|
+
* @returns {boolean} 是否处理成功
|
|
296
|
+
* @private
|
|
297
|
+
*/
|
|
298
|
+
Mqtt.prototype._handleMethodMessage = function (json) {
|
|
299
|
+
if (json.method) {
|
|
300
|
+
var func = this._getMethod(json.method);
|
|
301
|
+
if (func) {
|
|
302
|
+
this._execMethod(func, json);
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* 获取方法
|
|
311
|
+
* @param {string} methodStr 方法字符串
|
|
312
|
+
* @returns {Function|null} 方法函数
|
|
313
|
+
* @private
|
|
314
|
+
*/
|
|
315
|
+
Mqtt.prototype._getMethod = function (methodStr) {
|
|
316
|
+
var methods = this.methods;
|
|
317
|
+
var arr = methodStr.split('.');
|
|
318
|
+
|
|
319
|
+
for (var i = 0; i < arr.length; i++) {
|
|
320
|
+
var m = methods[arr[i]];
|
|
321
|
+
if (m) {
|
|
322
|
+
methods = m;
|
|
323
|
+
} else {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return typeof methods == 'function' ? methods : null;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 执行方法
|
|
333
|
+
* @param {Function} func 方法函数
|
|
334
|
+
* @param {object} json 消息对象
|
|
335
|
+
* @private
|
|
336
|
+
*/
|
|
337
|
+
Mqtt.prototype._execMethod = function (func, json) {
|
|
338
|
+
var params = json.params;
|
|
339
|
+
var from = params.from;
|
|
340
|
+
var id = json.id;
|
|
341
|
+
var ret = func(params);
|
|
342
|
+
|
|
343
|
+
if (ret) {
|
|
344
|
+
var obj = {};
|
|
345
|
+
if (id) {
|
|
346
|
+
obj.id = id;
|
|
347
|
+
}
|
|
348
|
+
obj.result = ret;
|
|
349
|
+
|
|
350
|
+
if (from) {
|
|
351
|
+
var topic_receive = this.config.topic_receive + from;
|
|
352
|
+
this.send(topic_receive, obj);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
Mqtt.prototype.subscribe = function (topic, func, qos = null, retain = false) {
|
|
358
|
+
this.client.subscribe(topic, {
|
|
359
|
+
// 订阅消息方式,0为保留 , 1为确认收到1次
|
|
360
|
+
qos: qos || 0,
|
|
361
|
+
retain,
|
|
362
|
+
});
|
|
363
|
+
if (func) {
|
|
364
|
+
if (!this.dict_subscribe[topic]) {
|
|
365
|
+
this.dict_subscribe[topic] = {};
|
|
366
|
+
}
|
|
367
|
+
var key = this.key_num + 1;
|
|
368
|
+
this.dict_subscribe[topic][key] = func;
|
|
369
|
+
return key;
|
|
370
|
+
}
|
|
371
|
+
return '';
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
Mqtt.prototype.unsubscribe = function (topic, key) {
|
|
375
|
+
this.client.unsubscribe(topic);
|
|
376
|
+
if (this.dict_subscribe[topic]) {
|
|
377
|
+
delete this.dict_subscribe[topic][key];
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
Mqtt.prototype.end = function () {
|
|
382
|
+
if (this._reconnect_timer) {
|
|
383
|
+
clearTimeout(this._reconnect_timer);
|
|
384
|
+
this._reconnect_timer = null;
|
|
385
|
+
}
|
|
386
|
+
if (this.client) {
|
|
387
|
+
if (this.client.removeAllListeners) {
|
|
388
|
+
this.client.removeAllListeners();
|
|
389
|
+
}
|
|
390
|
+
this.client.end(true);
|
|
391
|
+
this.client = null;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
Mqtt.prototype.publish = function (topic, message, qos = null, retain = false) {
|
|
396
|
+
this.client.publish(topic, message, {
|
|
397
|
+
qos: qos || 0,
|
|
398
|
+
retain,
|
|
399
|
+
});
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
Mqtt.prototype.send = function (topic, msg_obj) {
|
|
403
|
+
this.publish(topic, JSON.stringify(msg_obj));
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
Mqtt.prototype.req = function (topic, method, params, func) {
|
|
407
|
+
var data = {
|
|
408
|
+
id: this.client.options.clientId + '_' + Date.parse(new Date()),
|
|
409
|
+
method,
|
|
410
|
+
params,
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
if (func) {
|
|
414
|
+
data.func = func;
|
|
415
|
+
this.list_msg.push(data);
|
|
416
|
+
}
|
|
417
|
+
this.send(topic, data);
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 同步请求 - 可及时取回消息
|
|
422
|
+
* @param {string} topic 订阅板块
|
|
423
|
+
* @param {string} method 请求方法
|
|
424
|
+
* @param {object} params 传递参数
|
|
425
|
+
* @returns {object} 返回响应结果
|
|
426
|
+
*/
|
|
427
|
+
Mqtt.prototype.reqAsync = function (topic, method, params) {
|
|
428
|
+
var _this = this;
|
|
429
|
+
return new Promise((resolve, reject) => {
|
|
430
|
+
var has_msg = false;
|
|
431
|
+
var timer = setTimeout(() => {
|
|
432
|
+
if (!has_msg) {
|
|
433
|
+
reject(new Error('请求超时'));
|
|
434
|
+
}
|
|
435
|
+
}, 3000);
|
|
436
|
+
_this.req(topic, method, params, (res) => {
|
|
437
|
+
if (!has_msg) {
|
|
438
|
+
has_msg = true;
|
|
439
|
+
clearTimeout(timer);
|
|
440
|
+
resolve(res);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
module.exports = Mqtt;
|