mm_os 4.3.7 → 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 (232) 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 -84
  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 -704
  46. package/com/mqtt/index.js +0 -1052
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/script.tpl.js +0 -719
  49. package/com/nav/README.md +0 -2
  50. package/com/nav/com.json +0 -5
  51. package/com/nav/config.tpl.json +0 -84
  52. package/com/nav/drive.js +0 -702
  53. package/com/nav/index.js +0 -228
  54. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  55. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  56. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  57. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  58. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  59. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  60. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  61. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  62. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  63. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  64. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  65. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  67. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  68. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  69. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  70. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  71. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  72. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  74. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  75. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  76. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  77. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  78. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  79. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  81. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  82. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  83. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  84. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  85. package/com/nav/viewmodel.js +0 -446
  86. package/com/param/README.md +0 -2
  87. package/com/param/com.json +0 -5
  88. package/com/param/config.tpl.json +0 -136
  89. package/com/param/drive.js +0 -502
  90. package/com/param/index.js +0 -158
  91. package/com/param/script.tpl.js +0 -12
  92. package/com/pendant/README.md +0 -3
  93. package/com/pendant/com.json +0 -5
  94. package/com/pendant/config.tpl.json +0 -32
  95. package/com/pendant/drive.js +0 -204
  96. package/com/pendant/index.js +0 -467
  97. package/com/pendant/pendant.html +0 -16
  98. package/com/pendant/script.tpl.js +0 -18
  99. package/com/socket/README.md +0 -47
  100. package/com/socket/com.json +0 -5
  101. package/com/socket/config.tpl.json +0 -12
  102. package/com/socket/drive.js +0 -679
  103. package/com/socket/index.js +0 -402
  104. package/com/socket/script.tpl.js +0 -41
  105. package/com/sql/README.md +0 -3
  106. package/com/sql/com.json +0 -5
  107. package/com/sql/config.tpl.json +0 -74
  108. package/com/sql/drive.js +0 -1328
  109. package/com/sql/index.js +0 -152
  110. package/com/sql/script.tpl.js +0 -47
  111. package/com/static/README.md +0 -2
  112. package/com/static/com.json +0 -5
  113. package/com/static/config.tpl.json +0 -24
  114. package/com/static/drive.js +0 -194
  115. package/com/static/index.js +0 -237
  116. package/com/static/script.tpl.js +0 -28
  117. package/com/task/README.md +0 -3
  118. package/com/task/com.json +0 -5
  119. package/com/task/config.tpl.json +0 -24
  120. package/com/task/drive.js +0 -428
  121. package/com/task/index.js +0 -159
  122. package/com/task/script.tpl.js +0 -40
  123. package/com/template/com.json +0 -5
  124. package/com/template/config.tpl.json +0 -16
  125. package/com/template/drive.js +0 -81
  126. package/com/template/index.js +0 -141
  127. package/com.js +0 -237
  128. package/common/README.md +0 -2
  129. package/common/middleware/web_after/index.js +0 -26
  130. package/common/middleware/web_after/middleware.json +0 -9
  131. package/common/middleware/web_base/index.js +0 -113
  132. package/common/middleware/web_base/middleware.json +0 -19
  133. package/common/middleware/web_before/index.js +0 -33
  134. package/common/middleware/web_before/middleware.json +0 -9
  135. package/common/middleware/web_cors/index.js +0 -87
  136. package/common/middleware/web_cors/middleware.json +0 -24
  137. package/common/middleware/web_error/index.js +0 -131
  138. package/common/middleware/web_error/middleware.json +0 -19
  139. package/common/middleware/web_ip/index.js +0 -15
  140. package/common/middleware/web_ip/middleware.json +0 -14
  141. package/common/middleware/web_logger/index.js +0 -156
  142. package/common/middleware/web_logger/middleware.json +0 -14
  143. package/common/middleware/web_main/index.js +0 -26
  144. package/common/middleware/web_main/middleware.json +0 -9
  145. package/common/middleware/web_static/index.js +0 -73
  146. package/common/middleware/web_static/middleware.json +0 -54
  147. package/common/middleware/web_waf/index.js +0 -395
  148. package/common/middleware/web_waf/middleware.json +0 -13
  149. package/core/app/config.tpl.json +0 -67
  150. package/core/app/index.js +0 -711
  151. package/core/app/script.tpl.js +0 -52
  152. package/core/channel/index.js +0 -899
  153. package/core/channel/matcher.js +0 -585
  154. package/core/com/config.tpl.json +0 -16
  155. package/core/com/index.js +0 -77
  156. package/core/com/script.tpl.js +0 -5
  157. package/core/component/component.js +0 -42
  158. package/core/component/config.tpl.json +0 -63
  159. package/core/component/index.js +0 -273
  160. package/core/component/script.tpl.js +0 -19
  161. package/core/controller/config.tpl.json +0 -14
  162. package/core/controller/index.js +0 -371
  163. package/core/controller/script.tpl.js +0 -27
  164. package/core/factory/config.tpl.json +0 -14
  165. package/core/factory/entity.js +0 -275
  166. package/core/factory/index.js +0 -241
  167. package/core/factory/script.tpl.js +0 -16
  168. package/core/game/bat/index.js +0 -137
  169. package/core/game/bat/world.js +0 -622
  170. package/core/game/config.tpl.json +0 -16
  171. package/core/game/entity_admin.js +0 -230
  172. package/core/game/index.js +0 -186
  173. package/core/handler/config.tpl.json +0 -22
  174. package/core/handler/index.js +0 -181
  175. package/core/handler/script.tpl.js +0 -23
  176. package/core/logic/config.tpl.json +0 -14
  177. package/core/logic/index.js +0 -59
  178. package/core/logic/script.tpl.js +0 -19
  179. package/core/middleware/config.tpl.json +0 -16
  180. package/core/middleware/index.js +0 -117
  181. package/core/middleware/script.tpl.js +0 -37
  182. package/core/mod/config.tpl.json +0 -22
  183. package/core/mod/index.js +0 -136
  184. package/core/mod/script.tpl.js +0 -34
  185. package/core/model/config.tpl.json +0 -219
  186. package/core/model/index.js +0 -272
  187. package/core/model/model.js +0 -27
  188. package/core/model/script.tpl.js +0 -20
  189. package/core/notifier/config.tpl.json +0 -14
  190. package/core/notifier/index.js +0 -77
  191. package/core/notifier/script.tpl.js +0 -20
  192. package/core/plugin/config.tpl.json +0 -36
  193. package/core/plugin/index.js +0 -267
  194. package/core/plugin/script.tpl.js +0 -51
  195. package/core/pusher/config.tpl.json +0 -14
  196. package/core/pusher/index.js +0 -158
  197. package/core/pusher/script.tpl.js +0 -20
  198. package/core/room/bat/index.js +0 -170
  199. package/core/room/bat/room.js +0 -524
  200. package/core/room/config.tpl.json +0 -20
  201. package/core/room/index.js +0 -249
  202. package/core/room/room.js +0 -61
  203. package/core/scene/config.tpl.json +0 -14
  204. package/core/scene/index.js +0 -467
  205. package/core/scene/loop.js +0 -1306
  206. package/core/scene/map.js +0 -28
  207. package/core/scene/script.tpl.js +0 -22
  208. package/core/sender/config.tpl.json +0 -14
  209. package/core/sender/index.js +0 -79
  210. package/core/sender/script.tpl.js +0 -20
  211. package/core/service/config.tpl.json +0 -14
  212. package/core/service/index.js +0 -100
  213. package/core/service/script.tpl.js +0 -25
  214. package/core/store/config.tpl.json +0 -26
  215. package/core/store/index.js +0 -1794
  216. package/core/store/script.tpl.js +0 -22
  217. package/core/system/config.tpl.json +0 -18
  218. package/core/system/index.js +0 -312
  219. package/core/system/script.tpl.js +0 -77
  220. package/core/view/config.tpl.json +0 -14
  221. package/core/view/index.js +0 -91
  222. package/core/view/script.tpl.js +0 -20
  223. package/core/zone/bat/index.js +0 -744
  224. package/core/zone/config.tpl.json +0 -54
  225. package/core/zone/index.js +0 -655
  226. package/core/zone/script.tpl.js +0 -10
  227. package/core/zone/zone_bat.js +0 -136
  228. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  229. package/os.js +0 -53
  230. package/server.js +0 -602
  231. package/ulits/id_gen.js +0 -134
  232. package/ulits/word.js +0 -206
package/com/mqtt/drive.js DELETED
@@ -1,704 +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
- await this._updateDeviceInfo(clientid, online, ip);
311
- };
312
-
313
- /**
314
- * 更新数据库设备在线信息
315
- * @param {string} clientid 客户端ID
316
- * @param {boolean} online 是否在线 0为没在线 1为在线
317
- * @param {string} ip 设备IP
318
- * @private
319
- */
320
- Drive.prototype._updateDeviceInfo = async function (clientid, online, ip) {
321
- try {
322
- var { device } = (await $.eventer.emitFirst('mqtt:check_device', clientid)) || {};
323
- if (device && online && !device.online) {
324
- device.online = online;
325
- if (ip) {
326
- device.ip = ip;
327
- }
328
- }
329
- } catch (error) {
330
- this.log("error", "更新设备在线失败", error);
331
- }
332
- };
333
-
334
- /**
335
- * 推送消息
336
- * @param {string} method 方法
337
- * @param {string} clientid 客户端ID
338
- * @param {object} params 信息
339
- * @param {number} longtime 超时回馈
340
- * @returns {object} 返回消息主体
341
- */
342
- Drive.prototype.push = function (method, clientid, params, longtime = 0) {
343
- var cg = this.config;
344
- var msg = {};
345
- msg[cg.msgid] = this.getMsgId(method);
346
- msg[cg.method] = method;
347
- msg[cg.params] = params;
348
- return this.pushS(clientid, msg, longtime);
349
- };
350
-
351
- /**
352
- * 删除消息
353
- * @param {string} clientid 设备ID
354
- * @param {string} key 消息键
355
- */
356
- Drive.prototype.delMsg = function (clientid, key) {
357
- if (this.clients[clientid]) {
358
- var dict = this.clients[clientid];
359
- delete dict[key];
360
- // 清除关联的定时器
361
- if (this._push_timers && this._push_timers[key]) {
362
- clearTimeout(this._push_timers[key]);
363
- delete this._push_timers[key];
364
- }
365
- // 当该客户端没有待处理消息时,清理空对象
366
- if (Object.keys(dict).length === 0) {
367
- delete this.clients[clientid];
368
- }
369
- }
370
- };
371
-
372
- /**
373
- * 处理事件
374
- * @param {string} clientid 客户端ID
375
- * @param {string} key 键
376
- * @param {object} json 信息
377
- */
378
- Drive.prototype.runEvent = function (clientid, key, json) {
379
- var method = key.left("_");
380
- if (this[method + "_event"]) {
381
- try {
382
- this[method + "_event"](clientid, json);
383
- } catch (err) {
384
- this.log("error", "MQTT事件执行失败!", key, err);
385
- }
386
- }
387
- };
388
-
389
- /**
390
- * 获取客户端ID
391
- * @param {string} push_topic 推送过来的主题
392
- * @param {object} body 消息主体
393
- * @param {object} msg 消息源
394
- * @param {string} topic 订阅的主题
395
- * @returns {string} 返回客户端ID
396
- */
397
- Drive.prototype.getClientId = function (push_topic, body, msg, topic) {
398
- var cg = this.config;
399
- var clientid = body[cg.clientid] || msg[cg.clientid];
400
- if (!clientid) {
401
- var str = topic.left("#");
402
- clientid = push_topic.right(str).left("/", true);
403
- }
404
- return clientid;
405
- };
406
-
407
- /**
408
- * 数据格式转换-转入
409
- * @param {string} push_topic 推送过来的主题
410
- * @param {object} msg 消息源
411
- * @param {string} topic 订阅的主题
412
- * @param {number} index 索引
413
- * @returns {object} 返回消息主体
414
- */
415
- Drive.prototype.toIn = function (push_topic, msg, topic, index) {
416
- var cg = this.config;
417
- var method = msg[cg.method];
418
- var json = {
419
- clientid: "",
420
- id: msg[cg.msgid] || this.getMsgId(method, "client"),
421
- method,
422
- };
423
- if (msg[cg.params]) {
424
- json.params = msg[cg.params];
425
- }
426
- if (msg[cg.result]) {
427
- json.result = msg[cg.result];
428
- }
429
- json.clientid = this.getClientId(
430
- push_topic,
431
- json.params || json.result || {},
432
- msg,
433
- topic,
434
- );
435
- return json;
436
- };
437
-
438
- /**
439
- * 数据格式转换-转出
440
- * @param {string} push_topic 推送过来的主题
441
- * @param {object} msg 消息主体
442
- * @param {string} topic 订阅的主题
443
- * @param {number} index 索引
444
- * @returns {object} 返回消息主体
445
- */
446
- Drive.prototype.toOut = function (push_topic, msg, topic, index) {
447
- return msg;
448
- };
449
-
450
- /**
451
- * 获取回复主题
452
- * @param {string} topic_tpl 主题模板
453
- * @param {string} clientid 客户端ID
454
- * @param {object} params 消息
455
- * @returns {string} 返回主题
456
- */
457
- Drive.prototype.getTopic = function (topic_tpl, clientid, params) {
458
- return (topic_tpl || "")
459
- .replace("${clientid}", clientid)
460
- .replace("${method}", params[this.config.method]);
461
- };
462
-
463
- /**
464
- * 处理函数, 用于处理订阅内容
465
- * @param {string} push_topic 推送过来的主题
466
- * @param {object} msg 消息正文
467
- * @param {string} topic 订阅的主题
468
- * @param {number} index 索引
469
- * @returns {object} 返回执行结果
470
- */
471
- Drive.prototype.handle = async function (push_topic, msg, topic, index) {
472
- var json = this.toIn(push_topic, msg, topic, index);
473
-
474
- if (await this._handleMessage(json)) {
475
- return;
476
- }
477
-
478
- if (json.method) {
479
- await this._handleMethodMessage(json, msg, topic, index);
480
- }
481
-
482
- // 即时推送设备上报数据到监控大屏
483
- // 通过事件总线通知订阅者(如 iot/socket/device 模块)
484
- if (json && json.clientid && json.params) {
485
- try {
486
- await $.eventer.emit('iot:device_data', json.clientid, json.params);
487
- } catch (error) {
488
- this.log("error", "即时推送设备数据失败", error);
489
- }
490
- }
491
-
492
- return;
493
- };
494
-
495
- /**
496
- * 执行响应
497
- * @param {string} clientid 客户端ID
498
- * @param {string} key 键
499
- * @param {object} json 信息
500
- * @returns {object} 返回执行结果
501
- */
502
- Drive.prototype.main = async function (clientid, key, json) {
503
- var o;
504
- if (this.clients[clientid]) {
505
- o = this.clients[clientid][key];
506
- }
507
- if (o) {
508
- delete this.clients[clientid][key];
509
- try {
510
- await o.func(json);
511
- } catch (err) {
512
- this.log("error", "MQTT执行失败!", key, err);
513
- }
514
- } else {
515
- await this.runEvent(clientid, key, json);
516
- }
517
- return o;
518
- };
519
-
520
- /**
521
- * 处理消息
522
- * @param {object} json 消息对象
523
- * @returns {boolean} 是否处理成功
524
- * @private
525
- */
526
- Drive.prototype._handleMessage = async function (json) {
527
- var id = json.id;
528
- if (json.result && id) {
529
- var key = this.getKey(json.method, json.clientid, id);
530
- var o = await this.run(json.clientid, key, json.result);
531
- if (o) {
532
- return true;
533
- }
534
- }
535
- return false;
536
- };
537
-
538
- /**
539
- * 处理方法消息
540
- * @param {object} json 消息对象
541
- * @param {object} msg 原始消息
542
- * @param {string} topic 订阅主题
543
- * @param {number} index 索引
544
- * @private
545
- */
546
- Drive.prototype._handleMethodMessage = async function (
547
- json,
548
- msg,
549
- topic,
550
- index,
551
- ) {
552
- var func = this._getMethod(json.method);
553
- if (func) {
554
- var ret = await this._execMethod(func, json, msg);
555
- if (ret) {
556
- await this._sendMethodRes(ret, json, topic, index);
557
- }
558
- }
559
- };
560
-
561
- /**
562
- * 获取方法
563
- * @param {string} method_name 方法字符串
564
- * @returns {Function|null} 方法函数
565
- * @private
566
- */
567
- Drive.prototype._getMethod = function (method_name) {
568
- var methods = this.methods;
569
- var arr = method_name.split(".");
570
-
571
- for (var i = 0; i < arr.length; i++) {
572
- var key = arr[i];
573
- var m = methods[key];
574
- if (m) {
575
- methods = m;
576
- } else {
577
- return null;
578
- }
579
- }
580
-
581
- return typeof methods == "function" ? methods : null;
582
- };
583
-
584
- /**
585
- * 执行方法
586
- * @param {Function} func 方法函数
587
- * @param {object} json 消息对象
588
- * @param {object} msg 原始消息
589
- * @returns {*} 方法执行结果
590
- * @private
591
- */
592
- Drive.prototype._execMethod = async function (func, json, msg) {
593
- var id = json.id;
594
- var params = json.params;
595
- var ret;
596
-
597
- try {
598
- ret = func(json.clientid, params, msg, id);
599
- if (util.types.isPromise(ret)) {
600
- ret = await ret;
601
- }
602
- } catch (err) {
603
- this.log("error", "事件回调失败!", err);
604
- }
605
-
606
- return ret;
607
- };
608
-
609
- /**
610
- * 发送方法响应
611
- * @param {*} ret 方法执行结果
612
- * @param {object} json 消息对象
613
- * @param {string} topic 订阅主题
614
- * @param {number} index 索引
615
- * @private
616
- */
617
- Drive.prototype._sendMethodRes = async function (ret, json, topic, index) {
618
- var arr_tc = this.config.topic_receive;
619
- if (arr_tc.length > index) {
620
- var obj = {};
621
- var cg = this.config;
622
- var id = json.id;
623
-
624
- if (id) {
625
- obj[cg.msgid] = id;
626
- }
627
-
628
- try {
629
- obj[cg.method] = this.getReplyMethod(json.method);
630
- obj[cg.result] = ret;
631
- var topic_receive = this.getTopic(
632
- arr_tc[index],
633
- json.clientid,
634
- json.params,
635
- );
636
- var reply_msg = this.toOut(topic_receive, obj, topic, index);
637
- await this.send(topic_receive, reply_msg);
638
- } catch (err) {
639
- this.log("error", "MQTT事件响应失败!", err);
640
- }
641
- }
642
- };
643
-
644
- // /**
645
- // * 主函数, 用于接收并处理订阅内容
646
- // * @param {string} push_topic 推送过来的主题
647
- // * @param {object} msg 消息正文
648
- // * @param {string} topic 订阅的主题
649
- // * @param {number} index 索引
650
- // * @returns {object} 返回执行结果
651
- // */
652
- // Drive.prototype.main = async function (push_topic, msg, topic, index) {
653
- // return this.handle(push_topic, msg, topic, index);
654
- // };
655
-
656
- // /**
657
- // * 接收订阅消息
658
- // * @param {object} ret 执行结果
659
- // * @param {object} push_topic 主题
660
- // * @param {object} msg 消息正文
661
- // * @param {string} topic 订阅的主题
662
- // * @param {number} index 索引
663
- // */
664
- // Drive.prototype.mainAfter = async function (ret, push_topic, msg, topic, index) {
665
- // await this.message(push_topic, msg, topic, index);
666
- // };
667
-
668
- /**
669
- * 获取插件
670
- * @param {string} app 应用名称
671
- * @param {string} name 插件名称
672
- * @returns {object} 返回获取到的插件
673
- */
674
- Drive.prototype.plugin = function (app, name) {
675
- var l = $.slash;
676
- var app_name = app || this.config_file.between("app" + l, l);
677
- var plugin_name = name || this.config_file.between("plugin" + l, l);
678
- var plus;
679
- var plugins = $.pool.plugin[app_name];
680
- if (plugins) {
681
- plus = plugins.get(plugin_name);
682
- }
683
- return plus;
684
- };
685
-
686
- /**
687
- * 获取模型
688
- * @param {string} type 模型类型
689
- * @returns {object} 返回获取到的模型
690
- */
691
- Drive.prototype.getModel = function (type) {
692
- let model = { ...this.config };
693
- let dir = this.getDir();
694
- let l = $.slash;
695
- let app_name = dir.between("app" + l, l);
696
- let plugin_name = dir.between("plugin" + l, l);
697
- let name = dir.basename();
698
- model.app = app_name;
699
- model.plugin = plugin_name;
700
- model.name = model.name || name;
701
- return model;
702
- };
703
-
704
- module.exports = Drive;