-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnoti.js
148 lines (128 loc) · 6.05 KB
/
noti.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
/**
* 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 Il Yeup, Ahn in KETI on 2016-09-21.
*/
//var tas_sec = require('./thyme_tas_sec');
//var tas_mission = require('./thyme_tas_mission');
var _this = this;
exports.parse_sgn = function (rqi, pc, callback) {
if(pc.sgn) {
var nmtype = pc['sgn'] != null ? 'short' : 'long';
var sgnObj = {};
var cinObj = {};
sgnObj = pc['sgn'] != null ? pc['sgn'] : pc['singleNotification'];
if (nmtype === 'long') {
console.log('oneM2M spec. define only short name for resource')
}
else { // 'short'
if (sgnObj.sur) {
if(sgnObj.sur.charAt(0) != '/') {
sgnObj.sur = '/' + sgnObj.sur;
}
var path_arr = sgnObj.sur.split('/');
}
if (sgnObj.nev) {
if (sgnObj.nev.rep) {
if (sgnObj.nev.rep['m2m:cin']) {
sgnObj.nev.rep.cin = sgnObj.nev.rep['m2m:cin'];
delete sgnObj.nev.rep['m2m:cin'];
}
if (sgnObj.nev.rep.cin) {
cinObj = sgnObj.nev.rep.cin;
}
else {
console.log('[mqtt_noti_action] m2m:cin is none');
cinObj = null;
}
}
else {
console.log('[mqtt_noti_action] rep tag of m2m:sgn.nev is none. m2m:notification format mismatch with oneM2M spec.');
cinObj = null;
}
}
else if (sgnObj.sud) {
console.log('[mqtt_noti_action] received notification of verification');
cinObj = {};
cinObj.sud = sgnObj.sud;
}
else if (sgnObj.vrq) {
console.log('[mqtt_noti_action] received notification of verification');
cinObj = {};
cinObj.vrq = sgnObj.vrq;
}
else {
console.log('[mqtt_noti_action] nev tag of m2m:sgn is none. m2m:notification format mismatch with oneM2M spec.');
cinObj = null;
}
}
}
else {
console.log('[mqtt_noti_action] m2m:sgn tag is none. m2m:notification format mismatch with oneM2M spec.');
console.log(pc);
}
callback(path_arr, cinObj, rqi);
};
exports.response_mqtt = function (rsp_topic, rsc, to, fr, rqi, inpc, bodytype) {
var rsp_message = {};
rsp_message['m2m:rsp'] = {};
rsp_message['m2m:rsp'].rsc = rsc;
rsp_message['m2m:rsp'].to = to;
rsp_message['m2m:rsp'].fr = fr;
rsp_message['m2m:rsp'].rqi = rqi;
rsp_message['m2m:rsp'].pc = inpc;
if(bodytype === 'xml') {
}
else if (bodytype === 'cbor') {
}
else { // 'json'
mqtt_client.publish(rsp_topic, JSON.stringify(rsp_message['m2m:rsp']));
}
};
exports.mqtt_noti_action = function(topic_arr, jsonObj) {
if (jsonObj != null) {
var bodytype = conf.ae.bodytype;
if(topic_arr[5] != null) {
bodytype = topic_arr[5];
}
var op = (jsonObj['m2m:rqp']['op'] == null) ? '' : jsonObj['m2m:rqp']['op'];
var to = (jsonObj['m2m:rqp']['to'] == null) ? '' : jsonObj['m2m:rqp']['to'];
var fr = (jsonObj['m2m:rqp']['fr'] == null) ? '' : jsonObj['m2m:rqp']['fr'];
var rqi = (jsonObj['m2m:rqp']['rqi'] == null) ? '' : jsonObj['m2m:rqp']['rqi'];
var pc = {};
pc = (jsonObj['m2m:rqp']['pc'] == null) ? {} : jsonObj['m2m:rqp']['pc'];
if(pc['m2m:sgn']) {
pc.sgn = {};
pc.sgn = pc['m2m:sgn'];
delete pc['m2m:sgn'];
}
_this.parse_sgn(rqi, pc, function (path_arr, cinObj, rqi) {
if(cinObj) {
var resp_topic = '/oneM2M/resp/' + topic_arr[3] + '/' + topic_arr[4] + '/' + topic_arr[5];
_this.response_mqtt(resp_topic, 2001, '', conf.ae.id, rqi, '', topic_arr[5]);
if(cinObj.sud || cinObj.vrq) {
}
else {
console.log('mqtt ' + bodytype + ' notification <----');
console.log('mqtt response - 2001 ---->');
if(getType(cinObj.con) == 'string') {
muv_mqtt_client.publish(path_arr.join('/').replace('/' + path_arr[path_arr.length - 1], ''), cinObj.con);
}
else {
muv_mqtt_client.publish(path_arr.join('/').replace('/' + path_arr[path_arr.length - 1], ''), JSON.stringify(cinObj.con));
}
}
}
});
}
else {
console.log('[mqtt_noti_action] message is not noti');
}
};