-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.js
170 lines (161 loc) · 6.54 KB
/
subscription.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
module.exports = function(RED) {
var request = require('request')
var express = require('express');
var subscriptionApp = express();
var bodyParser = require('body-parser');
subscriptionApp.use(bodyParser.json());
subscriptionApp.use(bodyParser.urlencoded({ extended: false }))
var ngrokUrl = undefined;
RED.settings.set('ngrokUrl', ngrokUrl);
function getNgrokURL() {
return new Promise((resolve, reject) => {
var timeOutCount = 0;
var loop = () => setTimeout(()=>{
timeOutCount++;
if (RED.settings.get('ngrokUrl') !== undefined) {
resolve(RED.settings.get('ngrokUrl'))
} else {
if (timeOutCount > 50)
reject("ngrok url timeout");
else
loop();
}
}, 100);
loop();
})
}
RED.settings.set('getNgrokURL', getNgrokURL);
RED.nodes.registerType("subscription",function (config) {
RED.nodes.createNode(this,config);
if (!config.url)
return;
this.hasCreate = false;
this.sendSub = () => {
getNgrokURL().then(url=>{
this.log('get ngrok url : '+url)
this.createOption = {
url : config.url,
method : 'POST',
headers : {
'X-M2M-Origin' : 'admin:admin',
'Accept' : 'application/json',
'Content-Type' : 'application/json;ty=23',
},
body : {
"m2m:sub": {
"rn" : this.id,
"nu" : url + '/sub/' + this.id,
"nct" : 2,
}
},
rejectUnauthorized: false,
json : true,
}
this.deleteOption = {
url : config.url + '/' + this.id,
method : 'DELETE',
headers : {
'X-M2M-Origin' : 'admin:admin',
'Accept' : 'application/json',
'Content-Type' : 'application/json;ty=23',
},
body : {},
rejectUnauthorized: false,
json : true,
}
request(this.createOption,(error, response, body)=>{
if (error)
this.error(error)
else if (typeof body == "string") {
// if has created, delete and create
this.error(`[${response.statusCode}] ${body}`)
if (response.statusCode === 409) {
request(this.deleteOption,(error, response, body)=>{
if (error)
this.error(error)
else if (typeof body == "string") {
this.error(body)
} else {
request(this.createOption,(error, response, body)=>{
if (error)
this.error(error)
else if (typeof body == "string") {
this.error(`[${response.statusCode}] ${body}`)
} else {
this.log('create sub : ' + JSON.stringify(body))
this.hasCreate = true;
}
})
}
})
}
} else {
this.log('create sub : ' + JSON.stringify(body))
this.hasCreate = true;
}
})
}).catch(error => this.error(error))
}
this.sendSub();
this.on('input', function(msg) {
if (!this.hasCreate)
this.sendSub();
})
this.on('close', function(done) {
this.log('close sub')
if (this.hasCreate) {
// need to delete sub
request(this.deleteOption,(error, response, body)=>{
if (error)
this.error(error)
else if (typeof body == "string") {
this.error(body)
} else {
this.log('delete sub : ' + JSON.stringify(body))
//this.hasCreate = true;
}
done();
})
} else
done();
});
});
subscriptionApp.use('/:id',(req, res)=>{
var subNode = RED.nodes.getNode(req.params.id);
if (subNode && subNode.type == "subscription") {
// send all resource info
try {
subNode.send(req.body["m2m:sgn"]["m2m:nev"]["m2m:rep"]);
res.write(`success send\nid = ${req.params.id}\n`)
res.write(JSON.stringify(req.body["m2m:sgn"]["m2m:nev"]["m2m:rep"]))
} catch (error) {
//subNode.error(error)
//subNode.send(req.body);
res.write('success send all body\nid = '+req.params.id + '\n');
res.write(JSON.stringify(req.body))
}
} else {
res.write('fail send, error id');
}
res.end();
})
subscriptionApp.use('', (req, res)=>{
RED.settings.get('getNgrokURL')().then(url=>{
res.write(url)
res.end()
}).catch(error=>RED.log.error(error))
})
RED.httpNode.use('/sub',subscriptionApp);
var ngrok = require('ngrok');
request.debug = false;
ngrok.connect(RED.settings.get('uiPort'),(err, url) => {
if (err)
RED.log.error('[ngrok]'+err);
else {
RED.log.info(`[ngrok] http:127.0.0.1:${RED.settings.get('uiPort')} -> ${url}`)
ngrokUrl = url.replace("https","http");
RED.settings.set('ngrokUrl', ngrokUrl);
}
request.debug = true;
})
}