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.
- 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/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 -49
- package/com/event/script.tpl.js +1 -1
- 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 -616
- 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/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- 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 +4 -10
- 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/adapter/websocket.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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;
|