mm_os 2.8.8 → 2.8.9
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/core/base/index.js +28 -28
- package/core/base/mqtt/index.js +314 -314
- package/core/base/mqtt/lib.js +39 -39
- package/core/base/web/index.js +109 -109
- package/core/com/mqtt/index.js +2 -6
- package/demo/index.js +28 -0
- package/middleware/cors/index.js +84 -84
- package/middleware/cors/middleware.json +6 -6
- package/middleware/log/index.js +20 -20
- package/middleware/log/middleware.json +8 -8
- package/middleware/mqtt_base/index.js +10 -10
- package/middleware/mqtt_base/middleware.json +7 -7
- package/middleware/waf/index.js +64 -64
- package/middleware/waf/middleware.json +8 -8
- package/middleware/waf_ip/index.js +115 -115
- package/middleware/waf_ip/middleware.json +9 -9
- package/middleware/web_base/index.js +65 -65
- package/middleware/web_base/middleware.json +8 -8
- package/middleware/web_event/index.js +412 -412
- package/middleware/web_event/middleware.json +9 -9
- package/middleware/web_proxy/index.js +23 -23
- package/middleware/web_proxy/middleware.json +8 -8
- package/middleware/web_router/index.js +33 -33
- package/middleware/web_router/middleware.json +9 -9
- package/middleware/web_socket/index.js +21 -21
- package/middleware/web_socket/middleware.json +8 -8
- package/middleware/web_static/index.js +25 -25
- package/middleware/web_static/middleware.json +8 -8
- package/package.json +3 -3
- package/static/file/image/user/42/2025_01_02_10_58_15.png +0 -0
package/core/base/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
var WEB = require("./web");
|
|
2
|
-
var MQTT = require("./mqtt");
|
|
3
|
-
|
|
4
|
-
module.exports = function(cg) {
|
|
5
|
-
var middleware = new $.Middleware();
|
|
6
|
-
middleware.init();
|
|
7
|
-
var mqtt, web;
|
|
8
|
-
if (cg.web && cg.web.state) {
|
|
9
|
-
web = new WEB(cg.web);
|
|
10
|
-
web.list = middleware.list.filter((o) => {
|
|
11
|
-
return !o.mode || o.mode == "web"
|
|
12
|
-
});
|
|
13
|
-
web.init();
|
|
14
|
-
}
|
|
15
|
-
if (cg.mqtt && cg.mqtt.state) {
|
|
16
|
-
mqtt = new MQTT(Object.assign(cg.mqtt, {
|
|
17
|
-
redis: cg.redis,
|
|
18
|
-
mongodb: cg.mongodb
|
|
19
|
-
}));
|
|
20
|
-
mqtt.list = middleware.list.filter((o) => {
|
|
21
|
-
return o.mode == "mqtt"
|
|
22
|
-
});
|
|
23
|
-
mqtt.init();
|
|
24
|
-
}
|
|
25
|
-
return {
|
|
26
|
-
mqtt,
|
|
27
|
-
web
|
|
28
|
-
}
|
|
1
|
+
var WEB = require("./web");
|
|
2
|
+
var MQTT = require("./mqtt");
|
|
3
|
+
|
|
4
|
+
module.exports = function(cg) {
|
|
5
|
+
var middleware = new $.Middleware();
|
|
6
|
+
middleware.init();
|
|
7
|
+
var mqtt, web;
|
|
8
|
+
if (cg.web && cg.web.state) {
|
|
9
|
+
web = new WEB(cg.web);
|
|
10
|
+
web.list = middleware.list.filter((o) => {
|
|
11
|
+
return !o.mode || o.mode == "web"
|
|
12
|
+
});
|
|
13
|
+
web.init();
|
|
14
|
+
}
|
|
15
|
+
if (cg.mqtt && cg.mqtt.state) {
|
|
16
|
+
mqtt = new MQTT(Object.assign(cg.mqtt, {
|
|
17
|
+
redis: cg.redis,
|
|
18
|
+
mongodb: cg.mongodb
|
|
19
|
+
}));
|
|
20
|
+
mqtt.list = middleware.list.filter((o) => {
|
|
21
|
+
return o.mode == "mqtt"
|
|
22
|
+
});
|
|
23
|
+
mqtt.init();
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
mqtt,
|
|
27
|
+
web
|
|
28
|
+
}
|
|
29
29
|
}
|
package/core/base/mqtt/index.js
CHANGED
|
@@ -1,315 +1,315 @@
|
|
|
1
|
-
require('./lib.js');
|
|
2
|
-
const mosca = require('mosca');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* mqtt 物联网通讯类
|
|
6
|
-
*/
|
|
7
|
-
class MQTT {
|
|
8
|
-
/**
|
|
9
|
-
* 构造函数
|
|
10
|
-
* @param {Object} config 配置参数
|
|
11
|
-
*/
|
|
12
|
-
constructor(config) {
|
|
13
|
-
/**
|
|
14
|
-
* 配置参数
|
|
15
|
-
*/
|
|
16
|
-
this.config = Object.assign({
|
|
17
|
-
"state": true,
|
|
18
|
-
// mqtt访问端口号
|
|
19
|
-
"socket_port": 1883,
|
|
20
|
-
// 服务端
|
|
21
|
-
"http_host": "localhost",
|
|
22
|
-
// websocket 访问端口
|
|
23
|
-
"http_port": 8083,
|
|
24
|
-
// 缓存方式
|
|
25
|
-
"cache": "mongodb", // "mongodb",
|
|
26
|
-
redis: {
|
|
27
|
-
host: "localhost",
|
|
28
|
-
port: 6379,
|
|
29
|
-
password: "asd159357",
|
|
30
|
-
db: 12
|
|
31
|
-
},
|
|
32
|
-
mongodb: {
|
|
33
|
-
user: "",
|
|
34
|
-
password: "",
|
|
35
|
-
host: "localhost",
|
|
36
|
-
port: 27017,
|
|
37
|
-
database: "mqtt"
|
|
38
|
-
}
|
|
39
|
-
}, config);
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* MQTT服务器
|
|
43
|
-
*/
|
|
44
|
-
this.server = null;
|
|
45
|
-
|
|
46
|
-
this.list = [];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 客户端连接成功
|
|
52
|
-
* @param {Object} client 客户端信息
|
|
53
|
-
*/
|
|
54
|
-
MQTT.prototype.connected = async function(client) {
|
|
55
|
-
//监听连接
|
|
56
|
-
console.info('client connected', client.id);
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 初始化
|
|
62
|
-
* @param {Object} config
|
|
63
|
-
*/
|
|
64
|
-
MQTT.prototype.init = function(config) {
|
|
65
|
-
if (config) {
|
|
66
|
-
this.config = Object.assign(this.config, config);
|
|
67
|
-
}
|
|
68
|
-
var cg = Object.assign({}, this.config);
|
|
69
|
-
var {
|
|
70
|
-
redis,
|
|
71
|
-
mongodb
|
|
72
|
-
} = cg;
|
|
73
|
-
var conf = {
|
|
74
|
-
port: cg.socket_port,
|
|
75
|
-
http: {
|
|
76
|
-
host: cg.http_host || "localhost",
|
|
77
|
-
port: cg.http_port,
|
|
78
|
-
bundle: true,
|
|
79
|
-
static: './'
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (cg.cache === 'redis') {
|
|
83
|
-
if (cg.redis) {
|
|
84
|
-
conf.backend = {
|
|
85
|
-
type: "redis",
|
|
86
|
-
redis: require('redis'),
|
|
87
|
-
host: redis.host || "localhost",
|
|
88
|
-
port: redis.port || 6379,
|
|
89
|
-
password: redis.password,
|
|
90
|
-
db: redis.db || 12,
|
|
91
|
-
return_buffers: true
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
conf.backend = {
|
|
95
|
-
type: "redis",
|
|
96
|
-
redis: require('redis'),
|
|
97
|
-
host: "localhost",
|
|
98
|
-
password: "asd159357",
|
|
99
|
-
port: 6379,
|
|
100
|
-
db: 12,
|
|
101
|
-
return_buffers: true
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
// 数据持久化参数设置
|
|
105
|
-
conf.persistence = Object.assign({
|
|
106
|
-
factory: mosca.persistence.Redis,
|
|
107
|
-
ttl: {
|
|
108
|
-
// TTL for subscriptions is 23 hour
|
|
109
|
-
subscriptions: 23 * 60 * 60 * 1000,
|
|
110
|
-
// TTL for packets is 23 hour
|
|
111
|
-
packets: 23 * 60 * 60 * 1000,
|
|
112
|
-
}
|
|
113
|
-
}, conf.backend);
|
|
114
|
-
} else if (cg.cache === 'mongodb' || cg.cache === 'mongo') {
|
|
115
|
-
var url = `mongodb://${mongodb.host}:${mongodb.port}/${mongodb.database}`;
|
|
116
|
-
if (mongodb.user) {
|
|
117
|
-
url =
|
|
118
|
-
`mongodb://${mongodb.user}:${mongodb.password}@${mongodb.host}:${mongodb.port}/${mongodb.database}`
|
|
119
|
-
}
|
|
120
|
-
conf.backend = {
|
|
121
|
-
// 增加了此项
|
|
122
|
-
type: 'mongo',
|
|
123
|
-
url,
|
|
124
|
-
pubsubCollection: 'ascoltatori',
|
|
125
|
-
mongo: {}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 数据持久化参数设置
|
|
129
|
-
conf.persistence = Object.assign({
|
|
130
|
-
factory: mosca.persistence.Mongo,
|
|
131
|
-
ttl: {
|
|
132
|
-
// TTL for subscriptions is 23 hour
|
|
133
|
-
subscriptions: 23 * 60 * 60 * 1000,
|
|
134
|
-
// TTL for packets is 23 hour
|
|
135
|
-
packets: 23 * 60 * 60 * 1000,
|
|
136
|
-
}
|
|
137
|
-
}, conf.backend);
|
|
138
|
-
}
|
|
139
|
-
this.server = new mosca.Server(conf);
|
|
140
|
-
return this;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* 引用
|
|
145
|
-
* @param {Function} 函数
|
|
146
|
-
*/
|
|
147
|
-
MQTT.prototype.use = function(func) {
|
|
148
|
-
// this.server.use(func);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 运行主程序
|
|
153
|
-
* @param {String} state 状态
|
|
154
|
-
*/
|
|
155
|
-
MQTT.prototype.main = function(state) {
|
|
156
|
-
var cg = this.config;
|
|
157
|
-
sr = this.server;
|
|
158
|
-
|
|
159
|
-
var _this = this;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 对服务器端口进行配置,在此端口进行监听
|
|
163
|
-
* @param {Object} client 客户端信息
|
|
164
|
-
*/
|
|
165
|
-
sr.on('clientConnected', async function(client) {
|
|
166
|
-
return await _this.connected(client);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* 监听MQTT主题消息
|
|
171
|
-
* @param {Object} packet 订阅消息
|
|
172
|
-
* @param {Object} client 客户端信息
|
|
173
|
-
*/
|
|
174
|
-
sr.on('published', function(packet, client) {
|
|
175
|
-
_this.published(packet, client);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* 监听MQTT主题消息
|
|
180
|
-
* @param {String} topic 订阅主题
|
|
181
|
-
* @param {Object} client 客户端信息
|
|
182
|
-
*/
|
|
183
|
-
sr.on('subscribed', function(topic, client) {
|
|
184
|
-
_this.subscribed(topic, client);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* 身份验证
|
|
189
|
-
*/
|
|
190
|
-
sr.authenticate = (client, username, password, callback) => {
|
|
191
|
-
$.log.info("收到身份验证", username, password);
|
|
192
|
-
this.auth(client, username, password, callback);
|
|
193
|
-
return true;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* 验证发布,决定客户端可以发布哪些主题
|
|
198
|
-
*/
|
|
199
|
-
sr.authorizePublish = (client, topic, payload, callback) => {
|
|
200
|
-
this.authPublish(client, topic, payload, callback);
|
|
201
|
-
return true;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 验证订阅,决定客户端可以订阅哪些主题
|
|
207
|
-
*/
|
|
208
|
-
sr.authorizeSubscribe = (client, topic, callback) => {
|
|
209
|
-
this.authSubscribe(client, topic, callback);
|
|
210
|
-
return true;
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 当服务开启时
|
|
216
|
-
*/
|
|
217
|
-
sr.on('ready', function() {
|
|
218
|
-
// 当服务开启时
|
|
219
|
-
console.info(`MQTT访问 mqtt://127.0.0.1:${cg.socket_port} || ws://127.0.0.1:${cg.http_port}`);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* 收到订阅时
|
|
225
|
-
* @param {String} topic 订阅主题
|
|
226
|
-
* @param {Object} client 客户端信息
|
|
227
|
-
*/
|
|
228
|
-
MQTT.prototype.subscribed = function(topic, client) {
|
|
229
|
-
// console.log("订阅", topic, client.id);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* 收到推送消息时
|
|
234
|
-
* @param {Object} packet 订阅的消息
|
|
235
|
-
* @param {Object} client 客户端信息
|
|
236
|
-
*/
|
|
237
|
-
MQTT.prototype.published = function(packet, client) {
|
|
238
|
-
// console.log("发布", packet.topic, packet.payload.toString());
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* 身份验证
|
|
243
|
-
* @param {Object} client 客户端
|
|
244
|
-
* @param {String} username 用户名
|
|
245
|
-
* @param {String} password 密码
|
|
246
|
-
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
247
|
-
*/
|
|
248
|
-
MQTT.prototype.auth = function(client, username, password, callback) {
|
|
249
|
-
// console.log("连接授权", client.id, username, password ? password.toString() : '');
|
|
250
|
-
var bl = true;
|
|
251
|
-
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
252
|
-
callback(null, bl);
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* 验证发布,决定客户端可以发布哪些主题
|
|
257
|
-
* @param {Object} client 客户端
|
|
258
|
-
* @param {String} topic 用户名
|
|
259
|
-
* @param {Object} payload 参数
|
|
260
|
-
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
261
|
-
*/
|
|
262
|
-
MQTT.prototype.authPublish = function(client, topic, payload, callback) {
|
|
263
|
-
var bl = true;
|
|
264
|
-
// if(topic == "temperature"){
|
|
265
|
-
// bl = false;
|
|
266
|
-
// }
|
|
267
|
-
// console.log('收到推送', client.id, topic, payload.toString());
|
|
268
|
-
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
269
|
-
callback(null, bl);
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 验证订阅,决定客户端可以订阅哪些主题
|
|
274
|
-
* @param {Object} client 客户端
|
|
275
|
-
* @param {String} topic 用户名
|
|
276
|
-
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
277
|
-
*/
|
|
278
|
-
MQTT.prototype.authSubscribe = function(client, topic, callback) {
|
|
279
|
-
var bl = true;
|
|
280
|
-
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
281
|
-
callback(null, bl);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* 运行主程序前
|
|
286
|
-
* @param {String} state 状态
|
|
287
|
-
*/
|
|
288
|
-
MQTT.prototype.before = async function(state) {
|
|
289
|
-
var list = this.list;
|
|
290
|
-
for (var i = 0; i < list.length; i++) {
|
|
291
|
-
var o = list[i];
|
|
292
|
-
o.func = require(o.func_file);
|
|
293
|
-
if (o.func) {
|
|
294
|
-
o.func(this.server, this.config);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* 运行主程序后
|
|
301
|
-
* @param {String} state 状态
|
|
302
|
-
*/
|
|
303
|
-
MQTT.prototype.after = async function(state) {};
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* 运行
|
|
307
|
-
* @param {String} state 状态
|
|
308
|
-
*/
|
|
309
|
-
MQTT.prototype.run = async function(state = 'start') {
|
|
310
|
-
await this.before(state);
|
|
311
|
-
await this.main(state);
|
|
312
|
-
await this.after(state);
|
|
313
|
-
};
|
|
314
|
-
|
|
1
|
+
require('./lib.js');
|
|
2
|
+
const mosca = require('mosca');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mqtt 物联网通讯类
|
|
6
|
+
*/
|
|
7
|
+
class MQTT {
|
|
8
|
+
/**
|
|
9
|
+
* 构造函数
|
|
10
|
+
* @param {Object} config 配置参数
|
|
11
|
+
*/
|
|
12
|
+
constructor(config) {
|
|
13
|
+
/**
|
|
14
|
+
* 配置参数
|
|
15
|
+
*/
|
|
16
|
+
this.config = Object.assign({
|
|
17
|
+
"state": true,
|
|
18
|
+
// mqtt访问端口号
|
|
19
|
+
"socket_port": 1883,
|
|
20
|
+
// 服务端
|
|
21
|
+
"http_host": "localhost",
|
|
22
|
+
// websocket 访问端口
|
|
23
|
+
"http_port": 8083,
|
|
24
|
+
// 缓存方式
|
|
25
|
+
"cache": "mongodb", // "mongodb",
|
|
26
|
+
redis: {
|
|
27
|
+
host: "localhost",
|
|
28
|
+
port: 6379,
|
|
29
|
+
password: "asd159357",
|
|
30
|
+
db: 12
|
|
31
|
+
},
|
|
32
|
+
mongodb: {
|
|
33
|
+
user: "",
|
|
34
|
+
password: "",
|
|
35
|
+
host: "localhost",
|
|
36
|
+
port: 27017,
|
|
37
|
+
database: "mqtt"
|
|
38
|
+
}
|
|
39
|
+
}, config);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* MQTT服务器
|
|
43
|
+
*/
|
|
44
|
+
this.server = null;
|
|
45
|
+
|
|
46
|
+
this.list = [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 客户端连接成功
|
|
52
|
+
* @param {Object} client 客户端信息
|
|
53
|
+
*/
|
|
54
|
+
MQTT.prototype.connected = async function(client) {
|
|
55
|
+
//监听连接
|
|
56
|
+
console.info('client connected', client.id);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 初始化
|
|
62
|
+
* @param {Object} config
|
|
63
|
+
*/
|
|
64
|
+
MQTT.prototype.init = function(config) {
|
|
65
|
+
if (config) {
|
|
66
|
+
this.config = Object.assign(this.config, config);
|
|
67
|
+
}
|
|
68
|
+
var cg = Object.assign({}, this.config);
|
|
69
|
+
var {
|
|
70
|
+
redis,
|
|
71
|
+
mongodb
|
|
72
|
+
} = cg;
|
|
73
|
+
var conf = {
|
|
74
|
+
port: cg.socket_port,
|
|
75
|
+
http: {
|
|
76
|
+
host: cg.http_host || "localhost",
|
|
77
|
+
port: cg.http_port,
|
|
78
|
+
bundle: true,
|
|
79
|
+
static: './'
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (cg.cache === 'redis') {
|
|
83
|
+
if (cg.redis) {
|
|
84
|
+
conf.backend = {
|
|
85
|
+
type: "redis",
|
|
86
|
+
redis: require('redis'),
|
|
87
|
+
host: redis.host || "localhost",
|
|
88
|
+
port: redis.port || 6379,
|
|
89
|
+
password: redis.password,
|
|
90
|
+
db: redis.db || 12,
|
|
91
|
+
return_buffers: true
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
conf.backend = {
|
|
95
|
+
type: "redis",
|
|
96
|
+
redis: require('redis'),
|
|
97
|
+
host: "localhost",
|
|
98
|
+
password: "asd159357",
|
|
99
|
+
port: 6379,
|
|
100
|
+
db: 12,
|
|
101
|
+
return_buffers: true
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// 数据持久化参数设置
|
|
105
|
+
conf.persistence = Object.assign({
|
|
106
|
+
factory: mosca.persistence.Redis,
|
|
107
|
+
ttl: {
|
|
108
|
+
// TTL for subscriptions is 23 hour
|
|
109
|
+
subscriptions: 23 * 60 * 60 * 1000,
|
|
110
|
+
// TTL for packets is 23 hour
|
|
111
|
+
packets: 23 * 60 * 60 * 1000,
|
|
112
|
+
}
|
|
113
|
+
}, conf.backend);
|
|
114
|
+
} else if (cg.cache === 'mongodb' || cg.cache === 'mongo') {
|
|
115
|
+
var url = `mongodb://${mongodb.host}:${mongodb.port}/${mongodb.database}`;
|
|
116
|
+
if (mongodb.user) {
|
|
117
|
+
url =
|
|
118
|
+
`mongodb://${mongodb.user}:${mongodb.password}@${mongodb.host}:${mongodb.port}/${mongodb.database}`
|
|
119
|
+
}
|
|
120
|
+
conf.backend = {
|
|
121
|
+
// 增加了此项
|
|
122
|
+
type: 'mongo',
|
|
123
|
+
url,
|
|
124
|
+
pubsubCollection: 'ascoltatori',
|
|
125
|
+
mongo: {}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 数据持久化参数设置
|
|
129
|
+
conf.persistence = Object.assign({
|
|
130
|
+
factory: mosca.persistence.Mongo,
|
|
131
|
+
ttl: {
|
|
132
|
+
// TTL for subscriptions is 23 hour
|
|
133
|
+
subscriptions: 23 * 60 * 60 * 1000,
|
|
134
|
+
// TTL for packets is 23 hour
|
|
135
|
+
packets: 23 * 60 * 60 * 1000,
|
|
136
|
+
}
|
|
137
|
+
}, conf.backend);
|
|
138
|
+
}
|
|
139
|
+
this.server = new mosca.Server(conf);
|
|
140
|
+
return this;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 引用
|
|
145
|
+
* @param {Function} 函数
|
|
146
|
+
*/
|
|
147
|
+
MQTT.prototype.use = function(func) {
|
|
148
|
+
// this.server.use(func);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 运行主程序
|
|
153
|
+
* @param {String} state 状态
|
|
154
|
+
*/
|
|
155
|
+
MQTT.prototype.main = function(state) {
|
|
156
|
+
var cg = this.config;
|
|
157
|
+
sr = this.server;
|
|
158
|
+
|
|
159
|
+
var _this = this;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 对服务器端口进行配置,在此端口进行监听
|
|
163
|
+
* @param {Object} client 客户端信息
|
|
164
|
+
*/
|
|
165
|
+
sr.on('clientConnected', async function(client) {
|
|
166
|
+
return await _this.connected(client);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 监听MQTT主题消息
|
|
171
|
+
* @param {Object} packet 订阅消息
|
|
172
|
+
* @param {Object} client 客户端信息
|
|
173
|
+
*/
|
|
174
|
+
sr.on('published', function(packet, client) {
|
|
175
|
+
_this.published(packet, client);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 监听MQTT主题消息
|
|
180
|
+
* @param {String} topic 订阅主题
|
|
181
|
+
* @param {Object} client 客户端信息
|
|
182
|
+
*/
|
|
183
|
+
sr.on('subscribed', function(topic, client) {
|
|
184
|
+
_this.subscribed(topic, client);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 身份验证
|
|
189
|
+
*/
|
|
190
|
+
sr.authenticate = (client, username, password, callback) => {
|
|
191
|
+
$.log.info("收到身份验证", username, password);
|
|
192
|
+
this.auth(client, username, password, callback);
|
|
193
|
+
return true;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 验证发布,决定客户端可以发布哪些主题
|
|
198
|
+
*/
|
|
199
|
+
sr.authorizePublish = (client, topic, payload, callback) => {
|
|
200
|
+
this.authPublish(client, topic, payload, callback);
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 验证订阅,决定客户端可以订阅哪些主题
|
|
207
|
+
*/
|
|
208
|
+
sr.authorizeSubscribe = (client, topic, callback) => {
|
|
209
|
+
this.authSubscribe(client, topic, callback);
|
|
210
|
+
return true;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 当服务开启时
|
|
216
|
+
*/
|
|
217
|
+
sr.on('ready', function() {
|
|
218
|
+
// 当服务开启时
|
|
219
|
+
console.info(`MQTT访问 mqtt://127.0.0.1:${cg.socket_port} || ws://127.0.0.1:${cg.http_port}`);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 收到订阅时
|
|
225
|
+
* @param {String} topic 订阅主题
|
|
226
|
+
* @param {Object} client 客户端信息
|
|
227
|
+
*/
|
|
228
|
+
MQTT.prototype.subscribed = function(topic, client) {
|
|
229
|
+
// console.log("订阅", topic, client.id);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 收到推送消息时
|
|
234
|
+
* @param {Object} packet 订阅的消息
|
|
235
|
+
* @param {Object} client 客户端信息
|
|
236
|
+
*/
|
|
237
|
+
MQTT.prototype.published = function(packet, client) {
|
|
238
|
+
// console.log("发布", packet.topic, packet.payload.toString());
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 身份验证
|
|
243
|
+
* @param {Object} client 客户端
|
|
244
|
+
* @param {String} username 用户名
|
|
245
|
+
* @param {String} password 密码
|
|
246
|
+
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
247
|
+
*/
|
|
248
|
+
MQTT.prototype.auth = function(client, username, password, callback) {
|
|
249
|
+
// console.log("连接授权", client.id, username, password ? password.toString() : '');
|
|
250
|
+
var bl = true;
|
|
251
|
+
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
252
|
+
callback(null, bl);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 验证发布,决定客户端可以发布哪些主题
|
|
257
|
+
* @param {Object} client 客户端
|
|
258
|
+
* @param {String} topic 用户名
|
|
259
|
+
* @param {Object} payload 参数
|
|
260
|
+
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
261
|
+
*/
|
|
262
|
+
MQTT.prototype.authPublish = function(client, topic, payload, callback) {
|
|
263
|
+
var bl = true;
|
|
264
|
+
// if(topic == "temperature"){
|
|
265
|
+
// bl = false;
|
|
266
|
+
// }
|
|
267
|
+
// console.log('收到推送', client.id, topic, payload.toString());
|
|
268
|
+
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
269
|
+
callback(null, bl);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 验证订阅,决定客户端可以订阅哪些主题
|
|
274
|
+
* @param {Object} client 客户端
|
|
275
|
+
* @param {String} topic 用户名
|
|
276
|
+
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
|
|
277
|
+
*/
|
|
278
|
+
MQTT.prototype.authSubscribe = function(client, topic, callback) {
|
|
279
|
+
var bl = true;
|
|
280
|
+
// 回调第二个参数为true表示验证通过, 为false表示验证失败
|
|
281
|
+
callback(null, bl);
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 运行主程序前
|
|
286
|
+
* @param {String} state 状态
|
|
287
|
+
*/
|
|
288
|
+
MQTT.prototype.before = async function(state) {
|
|
289
|
+
var list = this.list;
|
|
290
|
+
for (var i = 0; i < list.length; i++) {
|
|
291
|
+
var o = list[i];
|
|
292
|
+
o.func = require(o.func_file);
|
|
293
|
+
if (o.func) {
|
|
294
|
+
o.func(this.server, this.config);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 运行主程序后
|
|
301
|
+
* @param {String} state 状态
|
|
302
|
+
*/
|
|
303
|
+
MQTT.prototype.after = async function(state) {};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 运行
|
|
307
|
+
* @param {String} state 状态
|
|
308
|
+
*/
|
|
309
|
+
MQTT.prototype.run = async function(state = 'start') {
|
|
310
|
+
await this.before(state);
|
|
311
|
+
await this.main(state);
|
|
312
|
+
await this.after(state);
|
|
313
|
+
};
|
|
314
|
+
|
|
315
315
|
module.exports = MQTT;
|