mm_os 4.3.6 → 5.0.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.
Files changed (234) hide show
  1. package/README.md +101 -504
  2. package/index.js +26 -19
  3. package/lib/loader.js +787 -0
  4. package/lib/os.js +369 -0
  5. package/package.json +55 -80
  6. package/LICENSE +0 -21
  7. package/README_EN.md +0 -505
  8. package/adapter/adapter.js +0 -484
  9. package/adapter/custom_persistence.js +0 -661
  10. package/adapter/mqtt.js +0 -254
  11. package/adapter/socket.js +0 -244
  12. package/adapter/web.js +0 -92
  13. package/adapter/websocket.js +0 -43
  14. package/ai.js +0 -130
  15. package/com/api/README.md +0 -3
  16. package/com/api/com.json +0 -5
  17. package/com/api/config.tpl.json +0 -41
  18. package/com/api/drive.js +0 -732
  19. package/com/api/index.js +0 -201
  20. package/com/api/oauth.js +0 -200
  21. package/com/api/script.tpl.js +0 -32
  22. package/com/cmd/README.md +0 -11
  23. package/com/cmd/com.json +0 -5
  24. package/com/cmd/config.tpl.json +0 -48
  25. package/com/cmd/drive.js +0 -176
  26. package/com/cmd/index.js +0 -287
  27. package/com/cmd/job/config.json +0 -66
  28. package/com/cmd/job/index.js +0 -241
  29. package/com/cmd/job/test.js +0 -188
  30. package/com/cmd/msg.json +0 -48
  31. package/com/cmd/script.tpl.js +0 -12
  32. package/com/db/README.md +0 -3
  33. package/com/db/com.json +0 -5
  34. package/com/db/drive.js +0 -2206
  35. package/com/db/index.js +0 -241
  36. package/com/event/README.md +0 -4
  37. package/com/event/com.json +0 -5
  38. package/com/event/config.tpl.json +0 -20
  39. package/com/event/drive.js +0 -61
  40. package/com/event/index.js +0 -438
  41. package/com/event/script.tpl.js +0 -23
  42. package/com/mqtt/README.md +0 -47
  43. package/com/mqtt/com.json +0 -5
  44. package/com/mqtt/config.tpl.json +0 -42
  45. package/com/mqtt/drive.js +0 -696
  46. package/com/mqtt/index.js +0 -1017
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/mm_mqtt.js +0 -446
  49. package/com/mqtt/script.tpl.js +0 -719
  50. package/com/nav/README.md +0 -2
  51. package/com/nav/com.json +0 -5
  52. package/com/nav/config.tpl.json +0 -84
  53. package/com/nav/drive.js +0 -702
  54. package/com/nav/index.js +0 -228
  55. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  56. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  57. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  58. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  59. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  60. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  61. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  62. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  63. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  64. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  65. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  67. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  68. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  69. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  70. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  71. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  72. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  74. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  75. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  76. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  77. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  78. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  79. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  81. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  82. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  83. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  84. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  85. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  86. package/com/nav/viewmodel.js +0 -446
  87. package/com/param/README.md +0 -2
  88. package/com/param/com.json +0 -5
  89. package/com/param/config.tpl.json +0 -136
  90. package/com/param/drive.js +0 -502
  91. package/com/param/index.js +0 -158
  92. package/com/param/script.tpl.js +0 -12
  93. package/com/pendant/README.md +0 -3
  94. package/com/pendant/com.json +0 -5
  95. package/com/pendant/config.tpl.json +0 -32
  96. package/com/pendant/drive.js +0 -204
  97. package/com/pendant/index.js +0 -467
  98. package/com/pendant/pendant.html +0 -16
  99. package/com/pendant/script.tpl.js +0 -18
  100. package/com/socket/README.md +0 -47
  101. package/com/socket/com.json +0 -5
  102. package/com/socket/config.tpl.json +0 -12
  103. package/com/socket/drive.js +0 -679
  104. package/com/socket/index.js +0 -402
  105. package/com/socket/script.tpl.js +0 -41
  106. package/com/sql/README.md +0 -3
  107. package/com/sql/com.json +0 -5
  108. package/com/sql/config.tpl.json +0 -74
  109. package/com/sql/drive.js +0 -1283
  110. package/com/sql/index.js +0 -152
  111. package/com/sql/script.tpl.js +0 -47
  112. package/com/static/README.md +0 -2
  113. package/com/static/com.json +0 -5
  114. package/com/static/config.tpl.json +0 -24
  115. package/com/static/drive.js +0 -194
  116. package/com/static/index.js +0 -237
  117. package/com/static/script.tpl.js +0 -28
  118. package/com/task/README.md +0 -3
  119. package/com/task/com.json +0 -5
  120. package/com/task/config.tpl.json +0 -24
  121. package/com/task/drive.js +0 -428
  122. package/com/task/index.js +0 -159
  123. package/com/task/script.tpl.js +0 -40
  124. package/com/template/com.json +0 -5
  125. package/com/template/config.tpl.json +0 -16
  126. package/com/template/drive.js +0 -81
  127. package/com/template/index.js +0 -141
  128. package/com.js +0 -237
  129. package/common/README.md +0 -2
  130. package/common/middleware/web_after/index.js +0 -26
  131. package/common/middleware/web_after/middleware.json +0 -9
  132. package/common/middleware/web_base/index.js +0 -113
  133. package/common/middleware/web_base/middleware.json +0 -19
  134. package/common/middleware/web_before/index.js +0 -33
  135. package/common/middleware/web_before/middleware.json +0 -9
  136. package/common/middleware/web_cors/index.js +0 -87
  137. package/common/middleware/web_cors/middleware.json +0 -24
  138. package/common/middleware/web_error/index.js +0 -131
  139. package/common/middleware/web_error/middleware.json +0 -19
  140. package/common/middleware/web_ip/index.js +0 -15
  141. package/common/middleware/web_ip/middleware.json +0 -14
  142. package/common/middleware/web_logger/index.js +0 -156
  143. package/common/middleware/web_logger/middleware.json +0 -14
  144. package/common/middleware/web_main/index.js +0 -26
  145. package/common/middleware/web_main/middleware.json +0 -9
  146. package/common/middleware/web_static/index.js +0 -73
  147. package/common/middleware/web_static/middleware.json +0 -54
  148. package/common/middleware/web_waf/index.js +0 -385
  149. package/common/middleware/web_waf/middleware.json +0 -13
  150. package/core/app/config.tpl.json +0 -67
  151. package/core/app/index.js +0 -710
  152. package/core/app/script.tpl.js +0 -52
  153. package/core/channel/index.js +0 -899
  154. package/core/channel/matcher.js +0 -585
  155. package/core/com/config.tpl.json +0 -16
  156. package/core/com/index.js +0 -77
  157. package/core/com/script.tpl.js +0 -5
  158. package/core/component/component.js +0 -42
  159. package/core/component/config.tpl.json +0 -63
  160. package/core/component/index.js +0 -273
  161. package/core/component/script.tpl.js +0 -19
  162. package/core/controller/config.tpl.json +0 -14
  163. package/core/controller/index.js +0 -371
  164. package/core/controller/script.tpl.js +0 -27
  165. package/core/factory/config.tpl.json +0 -14
  166. package/core/factory/entity.js +0 -275
  167. package/core/factory/index.js +0 -241
  168. package/core/factory/script.tpl.js +0 -16
  169. package/core/game/bat/index.js +0 -137
  170. package/core/game/bat/world.js +0 -622
  171. package/core/game/config.tpl.json +0 -16
  172. package/core/game/entity_admin.js +0 -230
  173. package/core/game/index.js +0 -186
  174. package/core/handler/config.tpl.json +0 -22
  175. package/core/handler/index.js +0 -181
  176. package/core/handler/script.tpl.js +0 -23
  177. package/core/logic/config.tpl.json +0 -14
  178. package/core/logic/index.js +0 -59
  179. package/core/logic/script.tpl.js +0 -19
  180. package/core/middleware/config.tpl.json +0 -16
  181. package/core/middleware/index.js +0 -117
  182. package/core/middleware/script.tpl.js +0 -37
  183. package/core/mod/config.tpl.json +0 -22
  184. package/core/mod/index.js +0 -136
  185. package/core/mod/script.tpl.js +0 -34
  186. package/core/model/config.tpl.json +0 -219
  187. package/core/model/index.js +0 -272
  188. package/core/model/model.js +0 -27
  189. package/core/model/script.tpl.js +0 -20
  190. package/core/notifier/config.tpl.json +0 -14
  191. package/core/notifier/index.js +0 -77
  192. package/core/notifier/script.tpl.js +0 -20
  193. package/core/plugin/config.tpl.json +0 -36
  194. package/core/plugin/index.js +0 -297
  195. package/core/plugin/script.tpl.js +0 -51
  196. package/core/pusher/config.tpl.json +0 -14
  197. package/core/pusher/index.js +0 -158
  198. package/core/pusher/script.tpl.js +0 -20
  199. package/core/room/bat/index.js +0 -170
  200. package/core/room/bat/room.js +0 -524
  201. package/core/room/config.tpl.json +0 -20
  202. package/core/room/index.js +0 -249
  203. package/core/room/room.js +0 -61
  204. package/core/scene/config.tpl.json +0 -14
  205. package/core/scene/index.js +0 -467
  206. package/core/scene/loop.js +0 -1306
  207. package/core/scene/map.js +0 -28
  208. package/core/scene/script.tpl.js +0 -22
  209. package/core/sender/config.tpl.json +0 -14
  210. package/core/sender/index.js +0 -79
  211. package/core/sender/script.tpl.js +0 -20
  212. package/core/service/config.tpl.json +0 -14
  213. package/core/service/index.js +0 -100
  214. package/core/service/script.tpl.js +0 -25
  215. package/core/store/config.tpl.json +0 -26
  216. package/core/store/index.js +0 -1794
  217. package/core/store/script.tpl.js +0 -22
  218. package/core/store/sql.js +0 -1488
  219. package/core/system/config.tpl.json +0 -18
  220. package/core/system/index.js +0 -312
  221. package/core/system/script.tpl.js +0 -77
  222. package/core/view/config.tpl.json +0 -14
  223. package/core/view/index.js +0 -91
  224. package/core/view/script.tpl.js +0 -20
  225. package/core/zone/bat/index.js +0 -744
  226. package/core/zone/config.tpl.json +0 -54
  227. package/core/zone/index.js +0 -655
  228. package/core/zone/script.tpl.js +0 -10
  229. package/core/zone/zone_bat.js +0 -136
  230. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  231. package/os.js +0 -53
  232. package/server.js +0 -595
  233. package/ulits/id_gen.js +0 -134
  234. package/ulits/word.js +0 -206
package/com/mqtt/drive.js DELETED
@@ -1,696 +0,0 @@
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;