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.
Files changed (77) 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/api/script.tpl.js +24 -24
  12. package/com/cmd/config.tpl.json +48 -122
  13. package/com/cmd/drive.js +176 -1548
  14. package/com/cmd/index.js +287 -1066
  15. package/com/cmd/job/config.json +66 -0
  16. package/com/cmd/job/index.js +241 -0
  17. package/com/cmd/job/test.js +188 -0
  18. package/com/cmd/script.tpl.js +12 -33
  19. package/com/db/drive.js +2206 -2019
  20. package/com/db/index.js +241 -242
  21. package/com/event/config.tpl.json +3 -1
  22. package/com/event/drive.js +3 -1
  23. package/com/event/index.js +73 -46
  24. package/com/event/script.tpl.js +22 -22
  25. package/com/mqtt/config.tpl.json +16 -8
  26. package/com/mqtt/drive.js +696 -676
  27. package/com/mqtt/index.js +1012 -822
  28. package/com/mqtt/index.test.js +503 -0
  29. package/com/mqtt/mm_mqtt.js +446 -420
  30. package/com/mqtt/script.tpl.js +3 -7
  31. package/com/nav/drive.js +7 -7
  32. package/com/nav/index.js +228 -231
  33. package/com/param/index.js +19 -17
  34. package/com/pendant/index.js +467 -441
  35. package/com/socket/drive.js +679 -651
  36. package/com/socket/index.js +94 -48
  37. package/com/sql/drive.js +1283 -1259
  38. package/com/sql/index.js +152 -150
  39. package/com/static/drive.js +1 -1
  40. package/com/static/index.js +232 -226
  41. package/com/static/script.tpl.js +25 -25
  42. package/com/task/drive.js +428 -405
  43. package/com/task/index.js +154 -148
  44. package/com/task/script.tpl.js +37 -34
  45. package/com/template/drive.js +1 -1
  46. package/com/template/index.js +141 -138
  47. package/com.js +87 -6
  48. package/common/middleware/web_after/index.js +5 -11
  49. package/common/middleware/web_base/index.js +1 -1
  50. package/common/middleware/web_cors/index.js +1 -1
  51. package/common/middleware/web_error/index.js +131 -119
  52. package/common/middleware/web_error/middleware.json +2 -1
  53. package/common/middleware/web_logger/index.js +1 -1
  54. package/common/middleware/web_main/index.js +2 -0
  55. package/common/middleware/web_static/index.js +2 -2
  56. package/common/middleware/web_waf/index.js +1 -1
  57. package/core/app/config.tpl.json +4 -4
  58. package/core/app/index.js +710 -534
  59. package/core/middleware/index.js +1 -9
  60. package/core/mod/config.tpl.json +1 -1
  61. package/core/model/config.tpl.json +1 -1
  62. package/core/plugin/config.tpl.json +15 -3
  63. package/core/plugin/index.js +31 -0
  64. package/core/pusher/index.js +158 -161
  65. package/core/scene/loop.js +1306 -1255
  66. package/core/store/index.js +1794 -1755
  67. package/core/store/sql.js +1488 -1464
  68. package/core/zone/bat/index.js +744 -728
  69. package/core/zone/config.tpl.json +3 -3
  70. package/core/zone/index.js +655 -616
  71. package/os.js +0 -4
  72. package/package.json +16 -16
  73. package/server.js +583 -427
  74. package/com/cmd/nlp.js +0 -525
  75. package/core/app/index copy.js +0 -611
  76. package/software.js +0 -194
  77. package/tcp.js +0 -279
@@ -1,146 +1,43 @@
1
- const Koa = require('koa');
2
- const websocket = require('koa-websocket');
3
- const { Adapter } = require('./adapter.js');
4
-
5
- /**
6
- * WebSocket适配器
7
- */
8
- class WebSocket extends Adapter {
9
- static config = {
10
- host: '0.0.0.0',
11
- port: 8300
12
- };
13
- /**
14
- * 构造函数
15
- * @param {object} config 配置对象
16
- * @param {object} parent 父对象
17
- */
18
- constructor(config, parent) {
19
- super({ ...WebSocket.config, ...config }, parent);
20
- }
21
- }
22
-
23
- /**
24
- * 初始化WebSocket适配器
25
- * @param {object} web 应用实例
26
- * @private
27
- */
28
- WebSocket.prototype._init = function (web) {
29
- // 如果提供了应用实例
30
- if (web) {
31
- // 检查是否已经是websocket实例
32
- if (web.ws) {
33
- // 已经是websocket实例,直接使用
34
- this._web = web;
35
- this.external_app = true;
36
- } else {
37
- // 不是websocket实例,包装它
38
- this._web = websocket(web);
39
- this.external_app = true;
40
- }
41
- } else {
42
- // 没有提供应用实例,创建新的
43
- this._web = websocket(new Koa());
44
- this.external_app = false;
45
- }
46
- this.broker = this._web.ws;
47
- // 简化实现:不设置中间件,在连接时处理
48
- };
49
-
50
- /**
51
- * 发送消息
52
- * @private
53
- * @param {string} client_id 客户端ID
54
- * @param {string} message 消息内容
55
- * @returns {boolean} 发送是否成功
56
- */
57
- WebSocket.prototype._send = function (client_id, message) {
58
- let ws = this.clients.get(client_id);
59
- if (ws && ws.readyState === 1) { // WebSocket.OPEN
60
- ws.send(message);
61
- this.log('debug', `服务器向 ${client_id} 发送消息: ${message}`);
62
- return true;
63
- } else {
64
- this.log('warn', `客户端 ${client_id} 不存在或已关闭`);
65
- return false;
66
- }
67
- };
68
-
69
- /**
70
- * 启动WebSocket服务器
71
- * @returns {object|undefined} 服务器实例或undefined
72
- */
73
- WebSocket.prototype.start = function () {
74
- let {
75
- host,
76
- port
77
- } = this.config;
78
- // 如果没有提供外部app实例,启动自己的服务器
79
- if (!this.external_app) {
80
- return this._web.listen(port, host, () => {
81
- this.log('info', `启动成功!监听地址:ws://127.0.0.1:${port}`);
82
- });
83
- } else {
84
- // 如果提供了外部app实例,只需要记录日志
85
- this.log('info', `启动成功!访问地址:ws://127.0.0.1:${port}`);
86
- }
87
-
88
- // 监听服务器错误(仅当是自己创建的服务器时)
89
- if (this._tcp_server && !this.external_app) {
90
- this._tcp_server.on('error', (error) => {
91
- this.log('error', '服务器启动失败:', error);
92
- });
93
- }
94
- };
95
-
96
- /**
97
- * 停止Adapter服务器
98
- * @private
99
- */
100
- WebSocket.prototype._stop = function () {
101
- // 关闭所有客户端连接
102
- for (let [, ws] of this.clients) {
103
- ws.close();
104
- }
105
- };
106
-
107
- /**
108
- * 处理消息接收
109
- * @private
110
- * @param {string} client_id 客户端ID
111
- * @param {object} ws WebSocket实例
112
- * @param {string} message 消息内容
113
- */
114
- WebSocket.prototype._onMessage = function (client_id, ws, message) {
115
- this.log('debug', '收到消息', { client_id: client_id });
116
- };
117
-
118
- /**
119
- * 处理连接关闭
120
- * @private
121
- * @param {string} client_id 客户端ID
122
- */
123
- WebSocket.prototype._onClose = function (client_id) {
124
- this.clients.delete(client_id);
125
- this.log('info', `客户端断开连接: ${client_id}`);
126
- };
127
-
128
- /**
129
- * 处理错误事件
130
- * @private
131
- * @param {string} client_id 客户端ID
132
- * @param {Error} error 错误对象
133
- */
134
- WebSocket.prototype._onError = function (client_id, error) {
135
- this.log('error', `客户端 ${client_id} 发生错误:`, error);
136
- };
137
-
138
- /**
139
- * 使用中间件
140
- * @param {Function} middleware 中间件函数
141
- */
142
- WebSocket.prototype.use = function (middleware) {
143
- this.broker.use(middleware);
144
- };
145
-
146
- exports.WebSocket = WebSocket;
1
+ const Koa = require('koa');
2
+ const websocket = require('koa-websocket');
3
+ const { Adapter } = require('./adapter.js');
4
+
5
+ /**
6
+ * WebSocket适配器
7
+ */
8
+ class WebSocket extends Adapter {
9
+ static config = { host: '0.0.0.0', port: 8300 };
10
+ constructor(config, parent) {
11
+ super({ ...WebSocket.config, ...config }, parent);
12
+ }
13
+ }
14
+
15
+ /**
16
+ * 初始化
17
+ * @param {object} web Web适配器实例
18
+ */
19
+ WebSocket.prototype.init = function (web) {
20
+ this._web_adapter = web;
21
+
22
+ // 用 koa-websocket 包装真正的 Koa 应用
23
+ if (web && web.web) {
24
+ this._web = websocket(web.web);
25
+ } else if (web) {
26
+ this._web = websocket(web);
27
+ } else {
28
+ this._web = websocket(new Koa());
29
+ }
30
+
31
+ this.broker = this._web.ws;
32
+ return this;
33
+ };
34
+
35
+ /**
36
+ * 注册中间件给 koa-websocket
37
+ * @param {Function} middleware 中间件函数
38
+ */
39
+ WebSocket.prototype.use = function (middleware) {
40
+ this.broker.use(middleware);
41
+ };
42
+
43
+ exports.WebSocket = WebSocket;