-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_app.js
356 lines (313 loc) · 12.2 KB
/
mqtt_app.js
1
2
3
4
5
6
7
8
9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Copyright (c) 2018, OCEAN
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Created by ryeubi on 2015-11-20.
*/
var fs = require('fs');
var mqtt = require('mqtt');
var util = require('util');
var xml2js = require('xml2js');
var url = require('url');
var cbor = require('cbor');
global.req_topic = '/oneM2M/req/'+conf.ae.id+conf.cse.id+'/'+conf.ae.bodytype;
var reg_resp_topic = '/oneM2M/reg_resp/'+conf.ae.id+'/+/#';
var resp_topic = '/oneM2M/resp/'+conf.ae.id+'/+/#';
var noti_topic = '/oneM2M/req/+/'+conf.ae.id+'/#';
global.sh_adn = require('./mqtt_adn');
var noti = require('./noti');
var tas = require('./thyme_tas');
if (conf.usesecure === 'disable') {
var connectOptions = {
host: conf.cse.host,
port: conf.cse.mqttport,
// username: 'keti',
// password: 'keti123',
protocol: "mqtt",
keepalive: 10,
// clientId: serverUID,
protocolId: "MQTT",
protocolVersion: 4,
clean: true,
reconnectPeriod: 2000,
connectTimeout: 2000,
rejectUnauthorized: false
};
}
else {
connectOptions = {
host: brokerip,
port: conf.cse.mqttport,
protocol: "mqtts",
keepalive: 10,
// clientId: serverUID,
protocolId: "MQTT",
protocolVersion: 4,
clean: true,
reconnectPeriod: 2000,
connectTimeout: 2000,
key: fs.readFileSync("./server-key.pem"),
cert: fs.readFileSync("./server-crt.pem"),
rejectUnauthorized: false
};
}
mqtt_client = mqtt.connect(connectOptions);
mqtt_client.on('connect', function () {
mqtt_client.subscribe(reg_resp_topic);
mqtt_client.subscribe(resp_topic);
mqtt_client.subscribe(noti_topic);
console.log('subscribe reg_resp_topic as ' + reg_resp_topic);
console.log('subscribe resp_topic as ' + resp_topic);
console.log('subscribe noti_topic as ' + noti_topic);
sh_state = 'crtae';
});
mqtt_client.on('message', mqtt_message_handler);
function mqtt_callback(jsonObj) {
for (var i = 0; i < resp_mqtt_ri_arr.length; i++) {
if (resp_mqtt_ri_arr[i] == jsonObj['m2m:rsp'].rqi) {
var socket = socket_q[resp_mqtt_ri_arr[i]];
var to = resp_mqtt_path_arr[resp_mqtt_ri_arr[i]];
console.log(to);
callback_q[resp_mqtt_ri_arr[i]](jsonObj['m2m:rsp'].rsc, jsonObj['m2m:rsp'].pc, to, socket);
delete callback_q[resp_mqtt_ri_arr[i]];
delete resp_mqtt_path_arr[resp_mqtt_ri_arr[i]];
resp_mqtt_ri_arr.splice(i, 1);
break;
}
}
}
function mqtt_message_handler(topic, message) {
var topic_arr = topic.split("/");
var bodytype = conf.ae.bodytype;
if(topic_arr[5] != null) {
bodytype = (topic_arr[5] === 'xml') ? topic_arr[5] : ((topic_arr[5] === 'json') ? topic_arr[5] : ((topic_arr[5] === 'cbor') ? topic_arr[5] : 'json'));
}
console.log(message.toString());
if(topic_arr[1] == 'oneM2M' && (topic_arr[2] == 'resp' || topic_arr[2] == 'reg_resp') && topic_arr[3].replace(':', '/') == conf.ae.id) {
if(bodytype === 'xml') {
var parser = new xml2js.Parser({explicitArray: false});
parser.parseString(message.toString(), function (err, jsonObj) {
if (err) {
console.log('[mqtt-resp xml2js parser error]');
}
else {
if (jsonObj['m2m:rsp'] != null) {
mqtt_callback(jsonObj);
}
else {
NOPRINT==='true'?NOPRINT='true':console.log('[pxymqtt-resp] message is not resp');
noti.response_mqtt(topic_arr[4], 4000, '', conf.ae.id, rqi, '<h1>fail to parsing mqtt message</h1>');
}
}
});
}
else if(bodytype === 'cbor') {
var encoded = message.toString();
cbor.decodeFirst(encoded, function(err, jsonObj) {
if (err) {
console.log('[mqtt-resp cbor parser error]');
}
else {
if (jsonObj['m2m:rsp'] == null) {
jsonObj['m2m:rsp'] = jsonObj;
}
mqtt_callback(jsonObj);
}
});
}
else { // 'json'
var jsonObj = JSON.parse(message.toString());
if (jsonObj['m2m:rsp'] == null) {
jsonObj['m2m:rsp'] = jsonObj;
}
mqtt_callback(jsonObj);
}
}
else if(topic_arr[1] == 'oneM2M' && topic_arr[2] == 'req' && topic_arr[4] == conf.ae.id) {
if(bodytype == 'xml') {
parser = new xml2js.Parser({explicitArray: false});
parser.parseString(message.toString(), function (err, jsonObj) {
if (err) {
console.log('[mqtt noti xml2js parser error]');
}
else {
if(jsonObj['m2m:rqp'].op == '5' || jsonObj['m2m:rqp'].op == 5) {
noti.mqtt_noti_action(topic_arr, jsonObj);
}
}
});
}
else if(bodytype === 'cbor') {
encoded = message.toString();
cbor.decodeFirst(encoded, function(err, jsonObj) {
if (err) {
console.log('[mqtt noti cbor parser error]');
}
else {
noti.mqtt_noti_action(topic_arr, jsonObj);
}
});
}
else { // json
jsonObj = JSON.parse(message.toString());
if (jsonObj['m2m:rqp'] == null) {
jsonObj['m2m:rqp'] = jsonObj;
}
noti.mqtt_noti_action(topic_arr, jsonObj);
}
}
else {
console.log('topic is not supported');
}
}
sh_state = 'connect';
var return_count = 0;
var request_count = 0;
function ae_response_action(status, result) {
var aeid = result['m2m:ae']['aei'];
console.log('x-m2m-rsc : ' + status + ' - ' + aeid + ' <----');
mqtt_client.unsubscribe(reg_resp_topic);
mqtt_client.unsubscribe(resp_topic);
mqtt_client.unsubscribe(noti_topic);
conf.ae.id = aeid;
//fs.writeFileSync('aei.json', JSON.stringify(conf.ae.id, null, 4), 'utf8');
reg_resp_topic = '/oneM2M/reg_resp/'+conf.ae.id+'/+/#';
req_topic = '/oneM2M/req/'+conf.ae.id+'/+/'+conf.ae.bodytype;
resp_topic = '/oneM2M/resp/'+conf.ae.id+'/+/#';
noti_topic = '/oneM2M/req/+/'+conf.ae.id+'/#';
mqtt_client.subscribe(reg_resp_topic);
mqtt_client.subscribe(resp_topic);
mqtt_client.subscribe(noti_topic);
}
function create_cnt_all(count, callback) {
sh_adn.crtct(count, function (rsc) {
console.log(count + ' - ' + conf.cnt[count].name + ' - x-m2m-rsc : ' + rsc + ' <----');
if(rsc == 5106 || rsc == 2001 || rsc == 4105) {
count++;
if(conf.cnt.length > count) {
create_cnt_all(count, function (rsc, count) {
callback(rsc, count);
});
}
else {
callback(rsc, count);
}
}
else {
callback('9999', count);
}
});
}
function delete_sub_all(count, callback) {
sh_adn.delsub(count, function (rsc) {
console.log(count + ' - ' + conf.sub[count].name + ' - x-m2m-rsc : ' + rsc + ' <----');
if(rsc == 5106 || rsc == 2002 || rsc == 2000 || rsc == 4105 || rsc == 4004) {
count++;
if(conf.sub.length > count) {
delete_sub_all(count, function (rsc, count) {
callback(rsc, count);
});
}
else {
callback(rsc, count);
}
}
else {
callback('9999', count);
}
});
}
function create_sub_all(count, callback) {
sh_adn.crtsub(count, function (rsc) {
console.log(count + ' - ' + conf.sub[count].name + ' - x-m2m-rsc : ' + rsc + ' <----');
if(rsc == 5106 || rsc == 2001 || rsc == 4105) {
count++;
if(conf.sub.length > count) {
create_sub_all(count, function (rsc, count) {
callback(rsc, count);
})
}
else {
callback(rsc, count);
}
}
else {
callback('9999', count);
}
});
}
function mqtt_watchdog() {
if(sh_state == 'connect') {
}
else if(sh_state == 'crtae') {
console.log('[sh_state] : ' + sh_state);
sh_adn.crtae(function(status, res_body) {
if(status == 2001) {
ae_response_action(status, res_body);
sh_state = 'crtct';
}
else if(status == 5106 || status == 4105) {
console.log('x-m2m-rsc : ' + status + ' <----');
sh_state = 'rtvae'
}
});
}
else if(sh_state == 'rtvae') {
console.log('[sh_state] : ' + sh_state);
sh_adn.rtvae(function(status, res_body) {
if (status == 2000) {
var aeid = res_body['m2m:ae']['aei'];
console.log('x-m2m-rsc : ' + status + ' - ' + aeid + ' <----');
if(conf.ae.id != aeid && conf.ae.id != ('/'+aeid)) {
console.log('AE-ID created is ' + aeid + ' not equal to device AE-ID is ' + conf.ae.id);
}
else {
sh_state = 'crtct';
}
}
else {
console.log('x-m2m-rsc : ' + status + ' <----');
}
});
}
else if(sh_state == 'crtct') {
console.log('[sh_state] : ' + sh_state);
request_count = 0;
return_count = 0;
create_cnt_all(0, function(status, count) {
if(conf.cnt.length <= count) {
sh_state = 'delsub';
}
});
}
else if(sh_state == 'delsub') {
console.log('[sh_state] : ' + sh_state);
request_count = 0;
return_count = 0;
delete_sub_all(0, function(status, count) {
if(conf.sub.length <= count) {
sh_state = 'crtsub';
}
});
}
else if(sh_state == 'crtsub') {
console.log('[sh_state] : ' + sh_state);
request_count = 0;
return_count = 0;
create_sub_all(0, function(status, count) {
if(conf.sub.length <= count) {
sh_state = 'crtci';
tas.ready();
}
});
}
else if(sh_state == 'crtci') {
}
}
wdt.set_wdt(require('shortid').generate(), 2, mqtt_watchdog);