mm_os 4.2.6 → 4.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +1 -1
  2. package/README_EN.md +2 -2
  3. package/adapter/adapter.js +105 -52
  4. package/adapter/custom_persistence.js +32 -31
  5. package/adapter/mqtt.js +59 -79
  6. package/adapter/socket.js +188 -58
  7. package/adapter/web.js +92 -68
  8. package/adapter/websocket.js +43 -146
  9. package/com/api/drive.js +732 -708
  10. package/com/api/index.js +196 -198
  11. package/com/cmd/config.tpl.json +48 -122
  12. package/com/cmd/drive.js +176 -1548
  13. package/com/cmd/index.js +287 -1066
  14. package/com/cmd/job/config.json +66 -0
  15. package/com/cmd/job/index.js +241 -0
  16. package/com/cmd/job/test.js +188 -0
  17. package/com/cmd/script.tpl.js +12 -33
  18. package/com/db/drive.js +2206 -2019
  19. package/com/db/index.js +241 -242
  20. package/com/event/config.tpl.json +3 -1
  21. package/com/event/drive.js +3 -1
  22. package/com/event/index.js +73 -49
  23. package/com/event/script.tpl.js +1 -1
  24. package/com/mqtt/config.tpl.json +16 -8
  25. package/com/mqtt/drive.js +696 -676
  26. package/com/mqtt/index.js +1012 -822
  27. package/com/mqtt/index.test.js +503 -0
  28. package/com/mqtt/mm_mqtt.js +446 -420
  29. package/com/mqtt/script.tpl.js +3 -7
  30. package/com/nav/drive.js +7 -7
  31. package/com/nav/index.js +228 -231
  32. package/com/param/index.js +19 -17
  33. package/com/pendant/index.js +467 -441
  34. package/com/socket/drive.js +679 -616
  35. package/com/socket/index.js +94 -48
  36. package/com/sql/drive.js +1283 -1259
  37. package/com/sql/index.js +152 -150
  38. package/com/static/drive.js +1 -1
  39. package/com/static/index.js +232 -226
  40. package/com/task/drive.js +428 -405
  41. package/com/task/index.js +154 -148
  42. package/com/template/drive.js +1 -1
  43. package/com/template/index.js +141 -138
  44. package/com.js +87 -6
  45. package/common/middleware/web_after/index.js +4 -10
  46. package/common/middleware/web_base/index.js +1 -1
  47. package/common/middleware/web_cors/index.js +1 -1
  48. package/common/middleware/web_error/index.js +131 -119
  49. package/common/middleware/web_error/middleware.json +2 -1
  50. package/common/middleware/web_logger/index.js +1 -1
  51. package/common/middleware/web_main/index.js +2 -0
  52. package/common/middleware/web_static/index.js +2 -2
  53. package/common/middleware/web_waf/index.js +1 -1
  54. package/core/app/config.tpl.json +4 -4
  55. package/core/app/index.js +710 -534
  56. package/core/middleware/index.js +1 -9
  57. package/core/mod/config.tpl.json +1 -1
  58. package/core/model/config.tpl.json +1 -1
  59. package/core/plugin/config.tpl.json +15 -3
  60. package/core/plugin/index.js +31 -0
  61. package/core/pusher/index.js +158 -161
  62. package/core/scene/loop.js +1306 -1255
  63. package/core/store/index.js +1794 -1755
  64. package/core/store/sql.js +1488 -1464
  65. package/core/zone/bat/index.js +744 -728
  66. package/core/zone/config.tpl.json +3 -3
  67. package/core/zone/index.js +655 -616
  68. package/os.js +0 -4
  69. package/package.json +16 -16
  70. package/server.js +583 -427
  71. package/com/cmd/nlp.js +0 -525
  72. package/core/app/index copy.js +0 -611
  73. package/software.js +0 -194
  74. package/tcp.js +0 -279
@@ -56,7 +56,7 @@ class Socket extends Manager {
56
56
  * 是否懒加载
57
57
  * @type {boolean}
58
58
  */
59
- lazy_load: true,
59
+ lazy_load: false,
60
60
  /**
61
61
  * 模式
62
62
  * 1.生产模式,改变文件不会重新加载
@@ -66,7 +66,7 @@ class Socket extends Manager {
66
66
  * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
67
67
  * @type {number}
68
68
  */
69
- mode: 3
69
+ mode: process.env.NODE_ENV === 'production' ? 1 : 3,
70
70
  };
71
71
 
72
72
  /**
@@ -77,7 +77,12 @@ class Socket extends Manager {
77
77
  constructor(config, parent) {
78
78
  super({ ...Socket.config, ...config }, parent);
79
79
  this.dict = {};
80
-
80
+
81
+ /**
82
+ * 消息ID索引 (id => message对象), 用于O(1)查找
83
+ */
84
+ this.dict_msg = {};
85
+
81
86
  /**
82
87
  * 消息队列
83
88
  */
@@ -116,17 +121,23 @@ Socket.prototype.Drive = Drive;
116
121
  * @param {Function} next 跳过当前, 然后继续执行函数
117
122
  */
118
123
  Socket.prototype.run = async function (ctx, next) {
119
- await next();
124
+ if (typeof next === 'function') {
125
+ await next();
126
+ }
127
+ if (!ctx || !ctx.path) {
128
+ return;
129
+ }
120
130
  let path = ctx.path.toLocaleLowerCase();
121
- var infos = this.getInfos();
122
- for (var i = 0, info; info = infos[i++];) {
123
- // console.log("监听webscoket路径是否正确", path === info.path);
124
- if (info.state === 1) {
125
- let mod = this.getMod(info.name);
126
- if (path === info.path) {
127
- if (mod) {
128
- mod.add(ctx);
131
+ var mods = this.getMods();
132
+ for (var name in mods) {
133
+ var mod = mods[name];
134
+ var mod_path = mod.config && mod.config.path;
135
+ if (mod.config && mod.config.state === 1 && mod_path) {
136
+ if (path === mod_path) {
137
+ if (!mod.isLoaded()) {
138
+ await mod.do('load');
129
139
  }
140
+ mod.add(ctx);
130
141
  break;
131
142
  }
132
143
  }
@@ -135,19 +146,19 @@ Socket.prototype.run = async function (ctx, next) {
135
146
 
136
147
  /**
137
148
  * 发送消息到指定客户端
138
- * @param {string} client_id 客户端ID
149
+ * @param {string} clientid 客户端ID
139
150
  * @param {string} method 方法名称
140
151
  * @param {object} params 请求参数
141
152
  * @returns {boolean} 发送是否成功
142
153
  */
143
- Socket.prototype.send = function (client_id, method, params) {
154
+ Socket.prototype.send = function (clientid, method, params) {
144
155
  var mods = this.getMods();
145
156
  for (var k in mods) {
146
157
  var o = mods[k];
147
158
  if (o.config.state === 1 && o.send) {
148
159
  // 设置管理器引用,用于消息回复处理
149
160
  o.manager = this;
150
- return o.send(client_id, method, params);
161
+ return o.send(clientid, method, params);
151
162
  }
152
163
  }
153
164
  return false;
@@ -172,21 +183,21 @@ Socket.prototype.sendAll = function (method, params) {
172
183
 
173
184
  /**
174
185
  * 发送请求到指定客户端
175
- * @param {string} client_id 客户端ID
186
+ * @param {string} clientid 客户端ID
176
187
  * @param {string} method 方法名称
177
188
  * @param {object} params 请求参数
178
189
  * @param {Function} func 回调函数
179
190
  * @param {number} timeout 超时时间(毫秒)
180
191
  * @returns {boolean} 发送是否成功
181
192
  */
182
- Socket.prototype.req = function (client_id, method, params, func, timeout = 0) {
193
+ Socket.prototype.req = function (clientid, method, params, func, timeout = 0) {
183
194
  var mods = this.getMods();
184
195
  for (var k in mods) {
185
196
  var o = mods[k];
186
197
  if (o.config.state === 1 && o.req) {
187
198
  // 设置管理器引用,用于消息回复处理
188
199
  o.manager = this;
189
- return o.req(client_id, method, params, func, timeout);
200
+ return o.req(clientid, method, params, func, timeout);
190
201
  }
191
202
  }
192
203
  return false;
@@ -213,18 +224,35 @@ Socket.prototype.reqAll = function (method, params, func, timeout = 0) {
213
224
 
214
225
  /**
215
226
  * 同步请求到指定客户端 - 可及时取回消息
216
- * @param {string} client_id 客户端ID
227
+ * @param {string} clientid 客户端ID
217
228
  * @param {string} method 请求方法
218
229
  * @param {object} params 传递参数
219
230
  * @param {number} timeout 超时时间(毫秒)
220
231
  * @returns {Promise} 返回响应结果的Promise
221
232
  */
222
- Socket.prototype.reqAsync = function (client_id, method, params, timeout = 0) {
233
+ Socket.prototype.reqAsync = function (clientid, method, params, timeout = 0) {
223
234
  var _this = this;
235
+ var timeout_ms = timeout || 3000;
224
236
  return new Promise((resolve, reject) => {
225
- _this.req(client_id, method, params, (res) => {
226
- resolve(res);
227
- }, timeout);
237
+ var has_res = false;
238
+ var timer = setTimeout(() => {
239
+ if (!has_res) {
240
+ reject(new Error('Socket请求超时'));
241
+ }
242
+ }, timeout_ms);
243
+ _this.req(
244
+ clientid,
245
+ method,
246
+ params,
247
+ (res) => {
248
+ if (!has_res) {
249
+ has_res = true;
250
+ clearTimeout(timer);
251
+ resolve(res);
252
+ }
253
+ },
254
+ timeout,
255
+ );
228
256
  });
229
257
  };
230
258
 
@@ -237,10 +265,20 @@ Socket.prototype.reqAsync = function (client_id, method, params, timeout = 0) {
237
265
  */
238
266
  Socket.prototype.reqAsyncAll = function (method, params, timeout = 0) {
239
267
  var _this = this;
268
+ var timeout_ms = timeout || 3000;
240
269
  return new Promise((resolve, reject) => {
241
- _this.reqAll(method, params, (res) => {
242
- resolve(res);
243
- }, timeout);
270
+ var timer = setTimeout(() => {
271
+ reject(new Error('Socket广播请求超时'));
272
+ }, timeout_ms);
273
+ _this.reqAll(
274
+ method,
275
+ params,
276
+ (res) => {
277
+ clearTimeout(timer);
278
+ resolve(res);
279
+ },
280
+ timeout,
281
+ );
244
282
  });
245
283
  };
246
284
 
@@ -250,7 +288,12 @@ Socket.prototype.reqAsyncAll = function (method, params, timeout = 0) {
250
288
  * @private
251
289
  */
252
290
  Socket.prototype._genId = function () {
253
- return 'socket_' + Date.parse(new Date()) + '_' + Math.random().toString(36).substr(2, 9);
291
+ return (
292
+ 'socket_' +
293
+ Date.parse(new Date()) +
294
+ '_' +
295
+ Math.random().toString(36).substr(2, 9)
296
+ );
254
297
  };
255
298
 
256
299
  /**
@@ -259,12 +302,19 @@ Socket.prototype._genId = function () {
259
302
  * @private
260
303
  */
261
304
  Socket.prototype._delMsgById = function (msg_id) {
305
+ var o = this.dict_msg[msg_id];
306
+ if (!o) {
307
+ return;
308
+ }
309
+ // 清除定时器
310
+ if (o.timer) {
311
+ clearTimeout(o.timer);
312
+ }
313
+ delete this.dict_msg[msg_id];
314
+
315
+ // 从数组中移除 (保持list_msg与dict_msg同步)
262
316
  for (var i = 0; i < this.list_msg.length; i++) {
263
317
  if (this.list_msg[i].id === msg_id) {
264
- // 清除定时器
265
- if (this.list_msg[i].timer) {
266
- clearTimeout(this.list_msg[i].timer);
267
- }
268
318
  this.list_msg.splice(i, 1);
269
319
  break;
270
320
  }
@@ -280,21 +330,18 @@ Socket.prototype._handleResponse = function (msg) {
280
330
  if (!msg || !msg.id) {
281
331
  return;
282
332
  }
283
-
284
- for (var i = 0; i < this.list_msg.length; i++) {
285
- let o = this.list_msg[i];
286
- if (o.id === msg.id && o.func) {
287
- try {
288
- // 执行回调函数
289
- o.func(msg);
290
- } catch (error) {
291
- console.error('Socket回调函数执行错误:', error);
292
- }
293
-
294
- // 从列表中移除已处理的消息
295
- this._delMsgById(o.id);
296
- break;
333
+
334
+ var o = this.dict_msg[msg.id];
335
+ if (o && o.func) {
336
+ try {
337
+ // 执行回调函数
338
+ o.func(msg);
339
+ } catch (error) {
340
+ console.error('Socket回调函数执行错误:', error);
297
341
  }
342
+
343
+ // 从列表中移除已处理的消息
344
+ this._delMsgById(o.id);
298
345
  }
299
346
  };
300
347
 
@@ -308,7 +355,7 @@ Socket.prototype.updateConfigAll = async function (path) {
308
355
  var list_scope = $.dir.getAll(p, 'socket');
309
356
 
310
357
  // 遍历目录路径
311
- for (var i = 0, f; f = list_scope[i++];) {
358
+ for (var i = 0, f; (f = list_scope[i++]);) {
312
359
  var list_file = $.file.getAll(f, '*' + this.type + '.json');
313
360
  await this.loadList(list_file);
314
361
  }
@@ -316,7 +363,6 @@ Socket.prototype.updateConfigAll = async function (path) {
316
363
 
317
364
  exports.Socket = Socket;
318
365
 
319
-
320
366
  /**
321
367
  * Socket组件池
322
368
  */
@@ -336,7 +382,7 @@ function socketAdmin(scope, title) {
336
382
  if (!obj) {
337
383
  $.pool.socket[sc] = new Socket({
338
384
  name: sc,
339
- title: title
385
+ title: title,
340
386
  });
341
387
  obj = $.pool.socket[sc];
342
388
  }