mm_os 4.2.4 → 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/api/script.tpl.js +24 -24
- 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 -46
- package/com/event/script.tpl.js +22 -22
- 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 -651
- 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/static/script.tpl.js +25 -25
- package/com/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- package/com/task/script.tpl.js +37 -34
- 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 +5 -11
- 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/socket/index.js
CHANGED
|
@@ -56,7 +56,7 @@ class Socket extends Manager {
|
|
|
56
56
|
* 是否懒加载
|
|
57
57
|
* @type {boolean}
|
|
58
58
|
*/
|
|
59
|
-
lazy_load:
|
|
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
|
-
|
|
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
|
|
122
|
-
for (var
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (path ===
|
|
127
|
-
if (mod) {
|
|
128
|
-
mod.
|
|
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}
|
|
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 (
|
|
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(
|
|
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}
|
|
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 (
|
|
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(
|
|
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}
|
|
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 (
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
242
|
-
|
|
243
|
-
},
|
|
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
|
|
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
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
}
|