mm_os 4.2.6 → 4.2.7
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/drive.js
CHANGED
|
@@ -1,676 +1,696 @@
|
|
|
1
|
-
const os = require(
|
|
2
|
-
const util = require(
|
|
3
|
-
const Item = require(
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* mqtt驱动类
|
|
7
|
-
* @augments {Item}
|
|
8
|
-
* @class
|
|
9
|
-
*/
|
|
10
|
-
class Drive extends Item {
|
|
11
|
-
static config = {
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
// "topic": "$SYS/mm/service/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
* @param {string}
|
|
178
|
-
* @
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
* @param {string}
|
|
189
|
-
* @returns {string}
|
|
190
|
-
*/
|
|
191
|
-
Drive.prototype.
|
|
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
|
-
* @param {string}
|
|
219
|
-
* @param {object} msg
|
|
220
|
-
* @param {number}
|
|
221
|
-
* @param {
|
|
222
|
-
*/
|
|
223
|
-
Drive.prototype.
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
229
|
-
* @param {
|
|
230
|
-
* @param {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (
|
|
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
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
if (
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
*
|
|
317
|
-
* @param {string} clientid 客户端ID
|
|
318
|
-
* @param {boolean} online 是否在线 0为没在线 1为在线
|
|
319
|
-
* @param {string} ip 设备IP
|
|
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
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
*
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
*
|
|
414
|
-
* @param {
|
|
415
|
-
* @
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
*
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
*
|
|
470
|
-
* @param {
|
|
471
|
-
* @
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
var
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
*
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
|
|
1
|
+
const os = require("os");
|
|
2
|
+
const util = require("util");
|
|
3
|
+
const Item = require("mm_machine").Drive;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* mqtt驱动类
|
|
7
|
+
* @augments {Item}
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
class Drive extends Item {
|
|
11
|
+
static config = {
|
|
12
|
+
// 名称
|
|
13
|
+
name: "",
|
|
14
|
+
// mqtt 服务标题
|
|
15
|
+
title: "",
|
|
16
|
+
// mqtt 服务介绍
|
|
17
|
+
description: "",
|
|
18
|
+
// 状态 0未启用,1启用
|
|
19
|
+
state: 1,
|
|
20
|
+
// 订阅的主题
|
|
21
|
+
topic: [],
|
|
22
|
+
// 主题回复
|
|
23
|
+
topic_receive: [],
|
|
24
|
+
// 给客户端推送的主题
|
|
25
|
+
topic_push: "face/${clientid}/cmd/${method}",
|
|
26
|
+
// 接收方式
|
|
27
|
+
qos: 1,
|
|
28
|
+
// 推送方式
|
|
29
|
+
qos_push: 1,
|
|
30
|
+
// 保持接收
|
|
31
|
+
retain: true,
|
|
32
|
+
// 超时反馈
|
|
33
|
+
longtime: 10000,
|
|
34
|
+
// 消息ID
|
|
35
|
+
clientid: "clientid",
|
|
36
|
+
// 消息ID
|
|
37
|
+
msgid: "msgid",
|
|
38
|
+
// 请求方法
|
|
39
|
+
method: "method",
|
|
40
|
+
// 参数
|
|
41
|
+
params: "params",
|
|
42
|
+
// 结果
|
|
43
|
+
result: "result",
|
|
44
|
+
// 回复方法
|
|
45
|
+
method_receive: "${method}_ask",
|
|
46
|
+
// 主程序
|
|
47
|
+
main: "./index.js",
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* 构造函数
|
|
51
|
+
* @param {object} config 配置参数
|
|
52
|
+
* @param {object} parent 父对象
|
|
53
|
+
* @class
|
|
54
|
+
*/
|
|
55
|
+
constructor(config, parent) {
|
|
56
|
+
super({ ...Drive.config, ...config }, parent);
|
|
57
|
+
|
|
58
|
+
// 开放给前端调用的函数
|
|
59
|
+
this.methods = { ...$.methods };
|
|
60
|
+
|
|
61
|
+
// 客户端列表
|
|
62
|
+
this.clients = {};
|
|
63
|
+
// 设备在线状态缓存
|
|
64
|
+
this.drives = {};
|
|
65
|
+
this.mode = 3;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 配置示例
|
|
71
|
+
* @returns {object} 返回配置示例
|
|
72
|
+
*/
|
|
73
|
+
Drive.prototype.model = function () {
|
|
74
|
+
// 单层匹配订阅,用于订阅所有客户端要接收的数据 格式:$SYS/<代理服务器>/service/业务/+
|
|
75
|
+
// "topic": "$SYS/mm/service/user/+"
|
|
76
|
+
// 通配方式订阅,用于订阅仅该设备或该客户端要订阅的数据 格式:$SYS/<代理服务器>/service/appid/#
|
|
77
|
+
// "topic": "$SYS/mm/service/idd6877e1561/#"
|
|
78
|
+
// 完全匹配订阅,用于特定的业务订阅,业务压力最小,建议尽可能使用完全匹配
|
|
79
|
+
// "topic": "$SYS/mm/service/state"
|
|
80
|
+
return {
|
|
81
|
+
// 名称, 由中英文和下“_”组成, 用于修改或卸载 例如: demo
|
|
82
|
+
name: "demo_test",
|
|
83
|
+
// mqtt 服务标题
|
|
84
|
+
title: "示例MQTT",
|
|
85
|
+
// mqtt 服务介绍
|
|
86
|
+
description: "",
|
|
87
|
+
// 状态 0未启用,1启用
|
|
88
|
+
state: 1,
|
|
89
|
+
// 订阅的主题 {产品类型}/{设备ID}/{数据类型}/{子类型}
|
|
90
|
+
// 接收设备状态、事件、命令回复、设备请求
|
|
91
|
+
topic: [
|
|
92
|
+
"face/+/status",
|
|
93
|
+
"face/+/event/recognition",
|
|
94
|
+
"face/+/cmd/+/response",
|
|
95
|
+
"face/+/request/+",
|
|
96
|
+
],
|
|
97
|
+
// 回复订阅主题
|
|
98
|
+
topic_receive: ["face/${clientid}/response/${method}"],
|
|
99
|
+
// 给客户端推送的主题
|
|
100
|
+
topic_push: "face/${clientid}/cmd/${method}",
|
|
101
|
+
// 接收方式
|
|
102
|
+
qos: 1,
|
|
103
|
+
// 推送方式
|
|
104
|
+
qos_push: 1,
|
|
105
|
+
// 保持接收
|
|
106
|
+
retain: true,
|
|
107
|
+
// 超时反馈
|
|
108
|
+
longtime: 10000,
|
|
109
|
+
// 消息ID
|
|
110
|
+
clientid: "clientid",
|
|
111
|
+
// 消息ID
|
|
112
|
+
msgid: "msgid",
|
|
113
|
+
// 请求方法
|
|
114
|
+
method: "method",
|
|
115
|
+
// 参数
|
|
116
|
+
params: "params",
|
|
117
|
+
// 结果
|
|
118
|
+
result: "result",
|
|
119
|
+
// 回复方法
|
|
120
|
+
method_receive: "${method}_ask",
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 获取IP
|
|
126
|
+
* @returns {string} 返回IP地址
|
|
127
|
+
*/
|
|
128
|
+
Drive.prototype.getIp = function () {
|
|
129
|
+
let interfaces = os.networkInterfaces();
|
|
130
|
+
for (let iface of Object.values(interfaces)) {
|
|
131
|
+
for (let config of iface) {
|
|
132
|
+
if (config.family === "IPv4" && !config.internal) {
|
|
133
|
+
return config.address;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return "127.0.0.1"; // 如果没有找到,返回本地回环地址
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 新建消息ID
|
|
142
|
+
* @param {string} method 方法
|
|
143
|
+
* @param {string} terminal 终端
|
|
144
|
+
* @returns {string} 返回消息ID
|
|
145
|
+
*/
|
|
146
|
+
Drive.prototype.getMsgId = function (method, terminal = "server") {
|
|
147
|
+
var time = new Date().getTime();
|
|
148
|
+
var ip = this.getIp();
|
|
149
|
+
// var msgid = "ID:localhost-" + clientid + "-" + time;
|
|
150
|
+
var msgid = `${terminal}:${ip}-${time}-${method}`;
|
|
151
|
+
return msgid;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 回复的方法格式
|
|
156
|
+
* @param {string} method 方法
|
|
157
|
+
* @returns {string} 返回回复方法
|
|
158
|
+
*/
|
|
159
|
+
Drive.prototype.getReplyMethod = function (method) {
|
|
160
|
+
var str = this.config.method_receive;
|
|
161
|
+
return str.replace("${method}", method);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 获取回复KEY
|
|
166
|
+
* @param {string} method 方法
|
|
167
|
+
* @param {string} clientid 客户端ID
|
|
168
|
+
* @param {string} msgid 消息ID
|
|
169
|
+
* @returns {string} 返回KEY
|
|
170
|
+
*/
|
|
171
|
+
Drive.prototype.getReceiveKey = function (method, clientid, msgid) {
|
|
172
|
+
return this.getReplyMethod(method) + "_" + clientid + "_" + msgid;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 获取KEY
|
|
177
|
+
* @param {string} method 方法
|
|
178
|
+
* @param {string} clientid 客户端ID
|
|
179
|
+
* @param {string} msgid 消息ID
|
|
180
|
+
* @returns {string} 返回KEY
|
|
181
|
+
*/
|
|
182
|
+
Drive.prototype.getKey = function (method, clientid, msgid) {
|
|
183
|
+
return method + "_" + clientid + "_" + msgid;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 补全路径
|
|
188
|
+
* @param {string} path 路径
|
|
189
|
+
* @returns {string} 返回完整的路径
|
|
190
|
+
*/
|
|
191
|
+
Drive.prototype.fullUrl = function (path) {
|
|
192
|
+
var src = path || "";
|
|
193
|
+
if (src.indexOf("http") !== 0) {
|
|
194
|
+
var host = this.config.host || $.host;
|
|
195
|
+
if (host.indexOf("http") !== 0) {
|
|
196
|
+
host = "http://" + host;
|
|
197
|
+
}
|
|
198
|
+
if (src.indexOf("~/") === 0) {
|
|
199
|
+
src = src.replace("~/", host);
|
|
200
|
+
} else if (src.indexOf("/") === 0) {
|
|
201
|
+
src = src.replace("/", host);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return src;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 推送消息
|
|
209
|
+
* @param {string} topic 推送主题
|
|
210
|
+
* @param {object} msg 推送内容
|
|
211
|
+
* @param {number} qos 接收方式
|
|
212
|
+
* @param {boolean} retain 是否保持推送
|
|
213
|
+
*/
|
|
214
|
+
Drive.prototype.send = function (topic, msg, qos = 0, retain = false) {};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 收到消息
|
|
218
|
+
* @param {string} push_topic 推送过来的主题
|
|
219
|
+
* @param {object} msg 原消息
|
|
220
|
+
* @param {number} index 索引
|
|
221
|
+
* @param {string} topic 订阅的主题
|
|
222
|
+
*/
|
|
223
|
+
Drive.prototype.message = async function (push_topic, msg, index, topic) {};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 推送消息到设备
|
|
227
|
+
* @param {string} clientid 客户端ID
|
|
228
|
+
* @param {object} msg 消息主题
|
|
229
|
+
* @param {Function} func 回调函数
|
|
230
|
+
* @param {number} longtime 超时时间
|
|
231
|
+
*/
|
|
232
|
+
Drive.prototype.pushSync = function (clientid, msg, func, longtime = 0) {
|
|
233
|
+
var { msgid, method, topic_push, qos_push } = this.config;
|
|
234
|
+
var id = msg[msgid];
|
|
235
|
+
if (!id) {
|
|
236
|
+
msg[msgid] = this.getMsgId(msg[method], "server");
|
|
237
|
+
id = msg[msgid];
|
|
238
|
+
}
|
|
239
|
+
var key = this.getReceiveKey(msg[method], clientid, id);
|
|
240
|
+
if (func) {
|
|
241
|
+
if (!this.clients[clientid]) {
|
|
242
|
+
this.clients[clientid] = {};
|
|
243
|
+
}
|
|
244
|
+
this.clients[clientid][key] = {
|
|
245
|
+
clientid,
|
|
246
|
+
id,
|
|
247
|
+
func,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
var topic = this.getTopic(topic_push, clientid, msg);
|
|
252
|
+
this.send(topic, msg, qos_push);
|
|
253
|
+
if (func) {
|
|
254
|
+
var _this = this;
|
|
255
|
+
var timer_key = this.getReceiveKey(msg[method], clientid, id);
|
|
256
|
+
this._push_timers = this._push_timers || {};
|
|
257
|
+
this._push_timers[timer_key] = setTimeout(
|
|
258
|
+
async () => {
|
|
259
|
+
if (_this.clients[clientid] && _this.clients[clientid][key]) {
|
|
260
|
+
try {
|
|
261
|
+
await _this.clients[clientid][key].func(null);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
_this.log("error", "MQTT回调错误", key, error);
|
|
264
|
+
}
|
|
265
|
+
_this.delMsg(clientid, key);
|
|
266
|
+
}
|
|
267
|
+
delete _this._push_timers[timer_key];
|
|
268
|
+
},
|
|
269
|
+
longtime || this.config.longtime || 60000,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 推送消息
|
|
276
|
+
* @param {string} clientid 客户端ID
|
|
277
|
+
* @param {object} msg 信息
|
|
278
|
+
* @param {number} longtime 超时回馈
|
|
279
|
+
* @returns {object} 返回消息主体
|
|
280
|
+
*/
|
|
281
|
+
Drive.prototype.pushS = function (clientid, msg, longtime) {
|
|
282
|
+
var _this = this;
|
|
283
|
+
return new Promise((resolve, reject) => {
|
|
284
|
+
_this.pushSync(
|
|
285
|
+
clientid,
|
|
286
|
+
msg,
|
|
287
|
+
(res) => {
|
|
288
|
+
resolve(res);
|
|
289
|
+
},
|
|
290
|
+
longtime,
|
|
291
|
+
);
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 更新设备在线
|
|
297
|
+
* @param {string} clientid 客户端ID
|
|
298
|
+
* @param {boolean} online 是否在线 0为没在线 1为在线
|
|
299
|
+
* @param {string} ip 设备IP
|
|
300
|
+
*/
|
|
301
|
+
Drive.prototype.updateOnline = async function (clientid, online = 1, ip = "") {
|
|
302
|
+
if (!this.drives[clientid]) {
|
|
303
|
+
this.drives[clientid] = {};
|
|
304
|
+
}
|
|
305
|
+
var time_last = new Date().getTime(); // 跟新时间戳
|
|
306
|
+
this.drives[clientid].online = online;
|
|
307
|
+
if (online) {
|
|
308
|
+
this.drives[clientid].time_last = time_last;
|
|
309
|
+
}
|
|
310
|
+
if ($.server && $.server.checkDevice) {
|
|
311
|
+
await this._updateDeviceInfo(clientid, online, ip);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 更新数据库设备在线信息
|
|
317
|
+
* @param {string} clientid 客户端ID
|
|
318
|
+
* @param {boolean} online 是否在线 0为没在线 1为在线
|
|
319
|
+
* @param {string} ip 设备IP
|
|
320
|
+
* @private
|
|
321
|
+
*/
|
|
322
|
+
Drive.prototype._updateDeviceInfo = async function (clientid, online, ip) {
|
|
323
|
+
try {
|
|
324
|
+
var { device } = await $.server.checkDevice(clientid);
|
|
325
|
+
if (device && online && !device.online) {
|
|
326
|
+
device.online = online;
|
|
327
|
+
if (ip) {
|
|
328
|
+
device.ip = ip;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
} catch (error) {
|
|
332
|
+
this.log("error", "更新设备在线失败", error);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 推送消息
|
|
338
|
+
* @param {string} method 方法
|
|
339
|
+
* @param {string} clientid 客户端ID
|
|
340
|
+
* @param {object} params 信息
|
|
341
|
+
* @param {number} longtime 超时回馈
|
|
342
|
+
* @returns {object} 返回消息主体
|
|
343
|
+
*/
|
|
344
|
+
Drive.prototype.push = function (method, clientid, params, longtime = 0) {
|
|
345
|
+
var cg = this.config;
|
|
346
|
+
var msg = {};
|
|
347
|
+
msg[cg.msgid] = this.getMsgId(method);
|
|
348
|
+
msg[cg.method] = method;
|
|
349
|
+
msg[cg.params] = params;
|
|
350
|
+
return this.pushS(clientid, msg, longtime);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* 删除消息
|
|
355
|
+
* @param {string} clientid 设备ID
|
|
356
|
+
* @param {string} key 消息键
|
|
357
|
+
*/
|
|
358
|
+
Drive.prototype.delMsg = function (clientid, key) {
|
|
359
|
+
if (this.clients[clientid]) {
|
|
360
|
+
var dict = this.clients[clientid];
|
|
361
|
+
delete dict[key];
|
|
362
|
+
// 清除关联的定时器
|
|
363
|
+
if (this._push_timers && this._push_timers[key]) {
|
|
364
|
+
clearTimeout(this._push_timers[key]);
|
|
365
|
+
delete this._push_timers[key];
|
|
366
|
+
}
|
|
367
|
+
// 当该客户端没有待处理消息时,清理空对象
|
|
368
|
+
if (Object.keys(dict).length === 0) {
|
|
369
|
+
delete this.clients[clientid];
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* 处理事件
|
|
376
|
+
* @param {string} clientid 客户端ID
|
|
377
|
+
* @param {string} key 键
|
|
378
|
+
* @param {object} json 信息
|
|
379
|
+
*/
|
|
380
|
+
Drive.prototype.runEvent = function (clientid, key, json) {
|
|
381
|
+
var method = key.left("_");
|
|
382
|
+
if (this[method + "_event"]) {
|
|
383
|
+
try {
|
|
384
|
+
this[method + "_event"](clientid, json);
|
|
385
|
+
} catch (err) {
|
|
386
|
+
this.log("error", "MQTT事件执行失败!", key, err);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 获取客户端ID
|
|
393
|
+
* @param {string} push_topic 推送过来的主题
|
|
394
|
+
* @param {object} body 消息主体
|
|
395
|
+
* @param {object} msg 消息源
|
|
396
|
+
* @param {string} topic 订阅的主题
|
|
397
|
+
* @returns {string} 返回客户端ID
|
|
398
|
+
*/
|
|
399
|
+
Drive.prototype.getClientId = function (push_topic, body, msg, topic) {
|
|
400
|
+
var cg = this.config;
|
|
401
|
+
var clientid = body[cg.clientid] || msg[cg.clientid];
|
|
402
|
+
if (!clientid) {
|
|
403
|
+
var str = topic.left("#");
|
|
404
|
+
clientid = push_topic.right(str).left("/", true);
|
|
405
|
+
}
|
|
406
|
+
return clientid;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 数据格式转换-转入
|
|
411
|
+
* @param {string} push_topic 推送过来的主题
|
|
412
|
+
* @param {object} msg 消息源
|
|
413
|
+
* @param {string} topic 订阅的主题
|
|
414
|
+
* @param {number} index 索引
|
|
415
|
+
* @returns {object} 返回消息主体
|
|
416
|
+
*/
|
|
417
|
+
Drive.prototype.toIn = function (push_topic, msg, topic, index) {
|
|
418
|
+
var cg = this.config;
|
|
419
|
+
var method = msg[cg.method];
|
|
420
|
+
var json = {
|
|
421
|
+
clientid: "",
|
|
422
|
+
id: msg[cg.msgid] || this.getMsgId(method, "client"),
|
|
423
|
+
method,
|
|
424
|
+
};
|
|
425
|
+
if (msg[cg.params]) {
|
|
426
|
+
json.params = msg[cg.params];
|
|
427
|
+
}
|
|
428
|
+
if (msg[cg.result]) {
|
|
429
|
+
json.result = msg[cg.result];
|
|
430
|
+
}
|
|
431
|
+
json.clientid = this.getClientId(
|
|
432
|
+
push_topic,
|
|
433
|
+
json.params || json.result || {},
|
|
434
|
+
msg,
|
|
435
|
+
topic,
|
|
436
|
+
);
|
|
437
|
+
return json;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 数据格式转换-转出
|
|
442
|
+
* @param {string} push_topic 推送过来的主题
|
|
443
|
+
* @param {object} msg 消息主体
|
|
444
|
+
* @param {string} topic 订阅的主题
|
|
445
|
+
* @param {number} index 索引
|
|
446
|
+
* @returns {object} 返回消息主体
|
|
447
|
+
*/
|
|
448
|
+
Drive.prototype.toOut = function (push_topic, msg, topic, index) {
|
|
449
|
+
return msg;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* 获取回复主题
|
|
454
|
+
* @param {string} topic_tpl 主题模板
|
|
455
|
+
* @param {string} clientid 客户端ID
|
|
456
|
+
* @param {object} params 消息
|
|
457
|
+
* @returns {string} 返回主题
|
|
458
|
+
*/
|
|
459
|
+
Drive.prototype.getTopic = function (topic_tpl, clientid, params) {
|
|
460
|
+
return (topic_tpl || "")
|
|
461
|
+
.replace("${clientid}", clientid)
|
|
462
|
+
.replace("${method}", params[this.config.method]);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* 处理函数, 用于处理订阅内容
|
|
467
|
+
* @param {string} push_topic 推送过来的主题
|
|
468
|
+
* @param {object} msg 消息正文
|
|
469
|
+
* @param {string} topic 订阅的主题
|
|
470
|
+
* @param {number} index 索引
|
|
471
|
+
* @returns {object} 返回执行结果
|
|
472
|
+
*/
|
|
473
|
+
Drive.prototype.handle = async function (push_topic, msg, topic, index) {
|
|
474
|
+
var json = this.toIn(push_topic, msg, topic, index);
|
|
475
|
+
|
|
476
|
+
if (await this._handleMessage(json)) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (json.method) {
|
|
481
|
+
await this._handleMethodMessage(json, msg, topic, index);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return;
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* 执行响应
|
|
489
|
+
* @param {string} clientid 客户端ID
|
|
490
|
+
* @param {string} key 键
|
|
491
|
+
* @param {object} json 信息
|
|
492
|
+
* @returns {object} 返回执行结果
|
|
493
|
+
*/
|
|
494
|
+
Drive.prototype.main = async function (clientid, key, json) {
|
|
495
|
+
var o;
|
|
496
|
+
if (this.clients[clientid]) {
|
|
497
|
+
o = this.clients[clientid][key];
|
|
498
|
+
}
|
|
499
|
+
if (o) {
|
|
500
|
+
delete this.clients[clientid][key];
|
|
501
|
+
try {
|
|
502
|
+
await o.func(json);
|
|
503
|
+
} catch (err) {
|
|
504
|
+
this.log("error", "MQTT执行失败!", key, err);
|
|
505
|
+
}
|
|
506
|
+
} else {
|
|
507
|
+
await this.runEvent(clientid, key, json);
|
|
508
|
+
}
|
|
509
|
+
return o;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* 处理消息
|
|
514
|
+
* @param {object} json 消息对象
|
|
515
|
+
* @returns {boolean} 是否处理成功
|
|
516
|
+
* @private
|
|
517
|
+
*/
|
|
518
|
+
Drive.prototype._handleMessage = async function (json) {
|
|
519
|
+
var id = json.id;
|
|
520
|
+
if (json.result && id) {
|
|
521
|
+
var key = this.getKey(json.method, json.clientid, id);
|
|
522
|
+
var o = await this.run(json.clientid, key, json.result);
|
|
523
|
+
if (o) {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return false;
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* 处理方法消息
|
|
532
|
+
* @param {object} json 消息对象
|
|
533
|
+
* @param {object} msg 原始消息
|
|
534
|
+
* @param {string} topic 订阅主题
|
|
535
|
+
* @param {number} index 索引
|
|
536
|
+
* @private
|
|
537
|
+
*/
|
|
538
|
+
Drive.prototype._handleMethodMessage = async function (
|
|
539
|
+
json,
|
|
540
|
+
msg,
|
|
541
|
+
topic,
|
|
542
|
+
index,
|
|
543
|
+
) {
|
|
544
|
+
var func = this._getMethod(json.method);
|
|
545
|
+
if (func) {
|
|
546
|
+
var ret = await this._execMethod(func, json, msg);
|
|
547
|
+
if (ret) {
|
|
548
|
+
await this._sendMethodRes(ret, json, topic, index);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* 获取方法
|
|
555
|
+
* @param {string} method_name 方法字符串
|
|
556
|
+
* @returns {Function|null} 方法函数
|
|
557
|
+
* @private
|
|
558
|
+
*/
|
|
559
|
+
Drive.prototype._getMethod = function (method_name) {
|
|
560
|
+
var methods = this.methods;
|
|
561
|
+
var arr = method_name.split(".");
|
|
562
|
+
|
|
563
|
+
for (var i = 0; i < arr.length; i++) {
|
|
564
|
+
var key = arr[i];
|
|
565
|
+
var m = methods[key];
|
|
566
|
+
if (m) {
|
|
567
|
+
methods = m;
|
|
568
|
+
} else {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return typeof methods == "function" ? methods : null;
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 执行方法
|
|
578
|
+
* @param {Function} func 方法函数
|
|
579
|
+
* @param {object} json 消息对象
|
|
580
|
+
* @param {object} msg 原始消息
|
|
581
|
+
* @returns {*} 方法执行结果
|
|
582
|
+
* @private
|
|
583
|
+
*/
|
|
584
|
+
Drive.prototype._execMethod = async function (func, json, msg) {
|
|
585
|
+
var id = json.id;
|
|
586
|
+
var params = json.params;
|
|
587
|
+
var ret;
|
|
588
|
+
|
|
589
|
+
try {
|
|
590
|
+
ret = func(json.clientid, params, msg, id);
|
|
591
|
+
if (util.types.isPromise(ret)) {
|
|
592
|
+
ret = await ret;
|
|
593
|
+
}
|
|
594
|
+
} catch (err) {
|
|
595
|
+
this.log("error", "事件回调失败!", err);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
return ret;
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* 发送方法响应
|
|
603
|
+
* @param {*} ret 方法执行结果
|
|
604
|
+
* @param {object} json 消息对象
|
|
605
|
+
* @param {string} topic 订阅主题
|
|
606
|
+
* @param {number} index 索引
|
|
607
|
+
* @private
|
|
608
|
+
*/
|
|
609
|
+
Drive.prototype._sendMethodRes = async function (ret, json, topic, index) {
|
|
610
|
+
var arr_tc = this.config.topic_receive;
|
|
611
|
+
if (arr_tc.length > index) {
|
|
612
|
+
var obj = {};
|
|
613
|
+
var cg = this.config;
|
|
614
|
+
var id = json.id;
|
|
615
|
+
|
|
616
|
+
if (id) {
|
|
617
|
+
obj[cg.msgid] = id;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
try {
|
|
621
|
+
obj[cg.method] = this.getReplyMethod(json.method);
|
|
622
|
+
obj[cg.result] = ret;
|
|
623
|
+
var topic_receive = this.getTopic(
|
|
624
|
+
arr_tc[index],
|
|
625
|
+
json.clientid,
|
|
626
|
+
json.params,
|
|
627
|
+
);
|
|
628
|
+
var reply_msg = this.toOut(topic_receive, obj, topic, index);
|
|
629
|
+
await this.send(topic_receive, reply_msg);
|
|
630
|
+
} catch (err) {
|
|
631
|
+
this.log("error", "MQTT事件响应失败!", err);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
// /**
|
|
637
|
+
// * 主函数, 用于接收并处理订阅内容
|
|
638
|
+
// * @param {string} push_topic 推送过来的主题
|
|
639
|
+
// * @param {object} msg 消息正文
|
|
640
|
+
// * @param {string} topic 订阅的主题
|
|
641
|
+
// * @param {number} index 索引
|
|
642
|
+
// * @returns {object} 返回执行结果
|
|
643
|
+
// */
|
|
644
|
+
// Drive.prototype.main = async function (push_topic, msg, topic, index) {
|
|
645
|
+
// return this.handle(push_topic, msg, topic, index);
|
|
646
|
+
// };
|
|
647
|
+
|
|
648
|
+
// /**
|
|
649
|
+
// * 接收订阅消息
|
|
650
|
+
// * @param {object} ret 执行结果
|
|
651
|
+
// * @param {object} push_topic 主题
|
|
652
|
+
// * @param {object} msg 消息正文
|
|
653
|
+
// * @param {string} topic 订阅的主题
|
|
654
|
+
// * @param {number} index 索引
|
|
655
|
+
// */
|
|
656
|
+
// Drive.prototype.mainAfter = async function (ret, push_topic, msg, topic, index) {
|
|
657
|
+
// await this.message(push_topic, msg, topic, index);
|
|
658
|
+
// };
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* 获取插件
|
|
662
|
+
* @param {string} app 应用名称
|
|
663
|
+
* @param {string} name 插件名称
|
|
664
|
+
* @returns {object} 返回获取到的插件
|
|
665
|
+
*/
|
|
666
|
+
Drive.prototype.plugin = function (app, name) {
|
|
667
|
+
var l = $.slash;
|
|
668
|
+
var app_name = app || this.config_file.between("app" + l, l);
|
|
669
|
+
var plugin_name = name || this.config_file.between("plugin" + l, l);
|
|
670
|
+
var plus;
|
|
671
|
+
var plugins = $.pool.plugin[app_name];
|
|
672
|
+
if (plugins) {
|
|
673
|
+
plus = plugins.get(plugin_name);
|
|
674
|
+
}
|
|
675
|
+
return plus;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* 获取模型
|
|
680
|
+
* @param {string} type 模型类型
|
|
681
|
+
* @returns {object} 返回获取到的模型
|
|
682
|
+
*/
|
|
683
|
+
Drive.prototype.getModel = function (type) {
|
|
684
|
+
let model = { ...this.config };
|
|
685
|
+
let dir = this.getDir();
|
|
686
|
+
let l = $.slash;
|
|
687
|
+
let app_name = dir.between("app" + l, l);
|
|
688
|
+
let plugin_name = dir.between("plugin" + l, l);
|
|
689
|
+
let name = dir.basename();
|
|
690
|
+
model.app = app_name;
|
|
691
|
+
model.plugin = plugin_name;
|
|
692
|
+
model.name = model.name || name;
|
|
693
|
+
return model;
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
module.exports = Drive;
|