This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
/
dist-wire.js
363 lines (310 loc) · 11.3 KB
/
dist-wire.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
357
358
359
360
361
362
363
/**
* Copyright 2014 Sense Tecnic Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var Broker = require('./broker');
var dnrInterface = require('dnr-interface');
var {stringify} = require('flatted/cjs');
var ctxConstant = dnrInterface.Context;
var DnrSyncRes = dnrInterface.DnrSyncRes;
module.exports = function(RED) {
"use strict";
function DnrNode(n){
RED.nodes.createNode(this,n);
var node = this;
node.gateway = RED.nodes.getNode(n.gateway);
if (!node.gateway){
throw "No dnr gateway configured for this flow";
}
// node.input: "sourceId_port"
node.input = n.input;
node.linkType = n.linkType || 'NN';
node.state = ctxConstant.NORMAL;
node.on('input', function(msg){
node.gateway.dispatch(node, msg)
});
node.on('close', function(){
node.gateway.broker.unsubscribe(node.id)
});
node.gateway.register(node);
}
DnrNode.prototype.stateUpdate = function(state) {
this.state = state;
switch (state) {
case ctxConstant.COPY_FETCH_FORWARD:
this.status({fill:"blue",shape:"dot",text:"CFF _\\_ " + this.subscribeTopic});
break;
case ctxConstant.FETCH_FORWARD:
this.status({fill:"blue",shape:"dot",text:"FF \\_ " + this.subscribeTopic});
break;
case ctxConstant.RECEIVE_REDIRECT_COPY:
this.status({fill:"yellow",shape:"dot",text:"RRC _/_ " + this.publishTopic});
break;
case ctxConstant.RECEIVE_REDIRECT:
this.status({fill:"yellow",shape:"dot",text:"RR _/ " + this.publishTopic});
break;
case ctxConstant.NORMAL:
this.status({});
break;
case ctxConstant.DROP:
this.status({fill:"grey",shape:"dot",text:"DRP"});
break;
}
if (state !== ctxConstant.FETCH_FORWARD && state !== ctxConstant.COPY_FETCH_FORWARD){
this.gateway.broker.unsubscribe(this.id)
}
};
DnrNode.prototype.resubscribe = function(topic) {
if (!topic){
return
}
this.subscribeTopic = topic;
const deviceId = this.gateway.daemon.getLocalNR().deviceId
this.gateway.broker.subscribe(this.id, this.subscribeTopic, function(dnrMsg){
// got message from external devices
// FETCH_FORWARD, COPY_FETCH_FORWARD
const {msg, msgContext} = dnrMsg
const msgId = msgContext.msgId
// reconstruct msgContext to msg if any
const storedMsg = this.gateway.msgContextStore[msgId]
if (storedMsg){
storedMsg.payload = msg.payload
}
this.send(storedMsg || msg)
delete this.gateway.msgContextStore[msgId]
}.bind(this))
};
// one per flow
function DnrGatewayNode(n) {
RED.nodes.createNode(this,n);
this.config = n.config;
this.broker = new Broker(this.config);
this.flow = this.config.flow;
this.nodeIndexes = {};
this.dnrNodesMap = {}; // key: link, value: the dnr node on it
this.msgContextStore = {};
this.daemon = RED.nodes.getNode(n.config.daemon);
this.daemon.flowGateway[this.flow.id] = this.id;
this.context = this.daemon.getContext();
this.flowCoordinator = this.daemon.getOperatorUrl();// TODO: should get this from flow meta-data
this.dnrLinksToRequest = {};
this.nodesToContribute = {};
for (let i in this.flow.nodes){
let node = this.flow.nodes[i];
this.nodeIndexes[node.id] = i
}
// dnr sync response received from daemon
this.on('input', function(msg){
let response = new DnrSyncRes().fromObj(msg);
this.processSyncRes(response)
}.bind(this))
}
DnrGatewayNode.prototype.serialize = function (msg) {
try {
JSON.stringify(msg)
} catch (e) {
// circular, not serializable, likely that system pointers are presented
this.msgContextStore[msg._msgid] = msg
}
return stringify({
msg: msg,
msgContext: {
device: this.daemon.getLocalNR().deviceId,
msgId: msg._msgid
}
})
}
// to be triggered by daemon node
DnrGatewayNode.prototype.heartbeat = function() {
// TODO: should we??? device not registered
if (!this.daemon.getLocalNR().deviceId){
return
}
// update the state of each dnr node according to device context
for (let k in this.dnrNodesMap){
// aNode ------ dnrNode ----- cNode
let dnrNode = this.dnrNodesMap[k];
let cNode = this.flow.nodes[this.nodeIndexes[dnrNode.wires[0][0]]];
var aNode = this.flow.nodes[this.nodeIndexes[dnrNode.input.split('_')[0]]];
// need to decide how this dnr node should behave
var state = this.context.reason(aNode, cNode, dnrNode.linkType);
if (dnrNode.state === state){
continue
}
/*
TODO: consider when doing dnr-ization, skip adding dnr node in Normal state
1. NORMAL state:
aNode ------dnr-----> cNode
2. DROP state:
aNode ------dnr cNode
3. FETCH_FORWARD state:
external
\
\
aNode \______\ cNode
/
4. RECEIVE_REDIRECT state:
_ external
/|
/
aNode ______/ aNode
5.COPY_FETCH_FORWARD state:
external
\
\
aNode ______\______\ cNode
6.RECEIVE_REDIRECT_COPY state:
_ external
/|
/
aNode ______/_______\ aNode
*/
switch (state){
case ctxConstant.COPY_FETCH_FORWARD:
case ctxConstant.FETCH_FORWARD:
this.nodesToContribute[cNode.id] = 1;
delete this.nodesToContribute[aNode.id];
break;
case ctxConstant.RECEIVE_REDIRECT_COPY:
case ctxConstant.RECEIVE_REDIRECT:
this.nodesToContribute[aNode.id] = 1;
delete this.nodesToContribute[cNode.id];
break;
default:
delete this.nodesToContribute[aNode.id];
delete this.nodesToContribute[cNode.id];
dnrNode.stateUpdate(state);
continue
}
// update the pub/sub topics according to the state
// there are several cases where daemons don't need to ask
// for where they should send/fetch data to/from
//
// if the link type is NN, the topic for pub/sub is
// always <srcId>_<srcPort>_<destId>
if (dnrNode.linkType === 'NN'){
if (state === ctxConstant.FETCH_FORWARD){
dnrNode.resubscribe(k)
} else if (state === ctxConstant.RECEIVE_REDIRECT){
dnrNode.publishTopic = k
}
}
// if the link type is 1N and the dnrNode status is RECEIVE_REDIRECT,
// the topic for publishing is always
// "from_<myself>_<srcId>_<srcPort>_<destId>"
// similarly, if the link type is N1 and status is FETCH_FORWARD,
// the topic for subscribing is always
// "to_<myself>_<srcId>_<srcPort>_<destId>"
//
// in short: if inactive node is on the 1 side, it needs to ask coordinator
// if inactive node is on the N side, it uses itself
if (dnrNode.linkType === 'N1' && (state === ctxConstant.FETCH_FORWARD || state === ctxConstant.COPY_FETCH_FORWARD)){
dnrNode.resubscribe('to_' + this.daemon.getLocalNR().deviceId + '_' + k)
}
if (dnrNode.linkType === '1N' && (state === ctxConstant.RECEIVE_REDIRECT || state === ctxConstant.RECEIVE_REDIRECT_COPY)){
dnrNode.publishTopic = 'from_' + this.daemon.getLocalNR().deviceId + '_' + k
}
if ((dnrNode.linkType === '1N' && state === ctxConstant.FETCH_FORWARD) ||
(dnrNode.linkType === 'N1' && state === ctxConstant.RECEIVE_REDIRECT) ||
(dnrNode.linkType === '11')
){
this.dnrLinksToRequest[dnrNode.input + '_' + cNode.id + '-' + state] = 1
}
dnrNode.stateUpdate(state)
}// end for
// update with coordinator/requesting topics for dnrlinks
if (Object.keys(this.dnrLinksToRequest).length === 0 &&
Object.keys(this.nodesToContribute).length === 0){
delete this.daemon.dnrSyncReqs[this.flow.id];
return
}
this.daemon.dnrSyncReqs[this.flow.id] = new dnrInterface.DnrSyncReq(
this.daemon.getLocalNR().deviceId, this.flow.id,
Object.keys(this.dnrLinksToRequest),
Object.keys(this.nodesToContribute)
)
};
/* @param response:
{
dnrLinks: {
<link> : <topic>
},
brokers: []
}
*/
DnrGatewayNode.prototype.processSyncRes = function(response) {
let dnrLinks = response.dnrLinks;
let brokers = response.brokers;
if (brokers.length > 0){
this.broker.updateEndpoint(brokers[0])// TODO, we have a list of brokers
} else {
let path = this.daemon.getOperatorUrl() +
(this.daemon.getOperatorUrl().slice(-1) == "/"?"":"/") +
"mqttws";
this.broker.updateEndpoint(path)
}
for (let link in dnrLinks){
// link: <src node Id>_<outport>_<dest node Id>
// get the DNR node for this link
let dnrNode = this.dnrNodesMap[link];
if (!dnrNode){
log.warn('receive a dnrlink response that does not exist - ' + link);
continue
}
let commTopic = dnrLinks[link];
// update its comm topics
if (dnrNode.state === ctxConstant.FETCH_FORWARD || dnrNode.state === ctxConstant.COPY_FETCH_FORWARD){
dnrNode.resubscribe(commTopic)
} else if (dnrNode.state === ctxConstant.RECEIVE_REDIRECT || dnrNode.state === ctxConstant.RECEIVE_REDIRECT_COPY){
dnrNode.publishTopic = commTopic
}
dnrNode.stateUpdate(dnrNode.state);
// remove the pending dnrLink To Request
delete this.dnrLinksToRequest[link]
}
};
DnrGatewayNode.prototype.register = function(dnrNode) {
let key = dnrNode.input + '_' + dnrNode.wires[0][0];
this.dnrNodesMap[key] = dnrNode
};
DnrGatewayNode.prototype.dispatch = function(dnrNode, msg) {
if (dnrNode.state === ctxConstant.DROP){
return
}
if (dnrNode.state === ctxConstant.FETCH_FORWARD){
console.log('BUG ALERT: dispatching in a FETCH_FORWARD state!!!')
return
}
if (
dnrNode.state === ctxConstant.NORMAL ||
dnrNode.state === ctxConstant.COPY_FETCH_FORWARD) {
dnrNode.send(msg)
return
}
if (dnrNode.state === ctxConstant.RECEIVE_REDIRECT_COPY) {
dnrNode.send(msg)
}
if (dnrNode.publishTopic) {
var serializedMsg = this.serialize(msg)
this.broker.publish(dnrNode, dnrNode.publishTopic, serializedMsg)
}
};
function DnrPlaceholderNode(n){
RED.nodes.createNode(this,n);
this.replaceFor = n.replaceFor
}
RED.nodes.registerType("dnr-gateway", DnrGatewayNode, {});
RED.nodes.registerType("dnr-placeholder", DnrPlaceholderNode);
RED.nodes.registerType("dnr", DnrNode);
};