-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowcontrol.js
More file actions
162 lines (124 loc) · 4.66 KB
/
flowcontrol.js
File metadata and controls
162 lines (124 loc) · 4.66 KB
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
// Change Version
// npm publish --access public
// git add .
// git commit -m fix
// git push
// https://github.com/calkoe/node-red-contrib-flowcontrol
// git add . && git commit && git push && npm publish --access public
const match = function (filter, topic) {
const filterArray = filter.split('/')
const length = filterArray.length;
const topicArray = topic.split('/')
for (var i = 0; i < length; ++i) {
var left = filterArray[i]
var right = topicArray[i]
if (left === '#') return topicArray.length >= length - 1
if (left !== '+' && left !== right) return false
}
return length === topicArray.length
};
const checkTopic = function(topics,topic){
for(var t of topics)
if(match(t, topic)) return true; else continue;
return false
}
const store = function (global,topic,payload){
var g = global.get(topic)||{};
for(var o in payload) g[o] = [payload[o],new Date];
g["_lastSeen"] = new Date;
global.set(topic,g);
}
module.exports = function(RED) {
var nodes = {};
//NODE IN
function flowcontrolIn(config){
RED.nodes.createNode(this,config);
var node = this;
node.counter = 0;
node.on('input', function(msg){
///////EQUAL
//Copy Message
msg = JSON.parse(JSON.stringify(msg));
//Make Object
if(!msg.payload || typeof msg.payload !== 'object') msg.payload = {value:msg.payload};
//Deny Context
if(config.context)
if(msg.payload.Context === config.context || (msg.hap||{}).context === config.context) return;
//Deny Blacklist Topic
if(config.blTopic)
if(checkTopic(config.blTopic.split(","),msg.topic)) return;
//Deny Blacklist Obj
if(config.blObj)
for(var o of config.blObj.split(","))
if(o in msg.payload) delete msg.payload[o];
//Set Context
if(config.context){
msg.payload.Context = config.context;
}
///////
//Set Version
if(config.version){
if(!msg.version){msg.version = [];}
var copy = JSON.parse(JSON.stringify(msg));
delete copy.version;
copy.time = new Date;
msg.version.push(copy);
}
//Add to Storage and Send
if(config.topic||msg.topic){
for(var t of (config.topic||msg.topic).split(",")){
store(this.context().global,t,msg.payload);
//Find Target Topics
for (id in nodes) {
if(t && nodes[id].config.topic && checkTopic(nodes[id].config.topic.split(","),t)) flowcontrolOutHandler(msg,nodes[id]);
}
//Status
node.counter++;
node.status({fill:"blue",shape:"dot",text:node.counter});
}
}else{
node.status({fill:"red",shape:"dot",text:"No topic defined!"});
}
});
}
RED.nodes.registerType("flowcontrolIn",flowcontrolIn);
//NODE OUT
function flowcontrolOut(config) {
RED.nodes.createNode(this,config);
this.counter = 0;
this.config = config;
nodes[this.id] = this;
}
function flowcontrolOutHandler(msg,node){
var config = node.config;
///////EQUAL
//Copy Message
msg = JSON.parse(JSON.stringify(msg));
//Make Object
if(!msg.payload || typeof msg.payload !== 'object') msg.payload = {value:msg.payload};
//Deny Context
if(config.context)
if(msg.payload.Context === config.context || (msg.hap||{}).context === config.context) return;
//Deny Blacklist Topic
if(config.blTopic)
if(checkTopic(config.blTopic.split(","),msg.topic)) return;
//Deny Blacklist Obj
if(config.blObj)
for(var o of config.blObj.split(","))
if(o in msg.payload) delete msg.payload[o];
//Set Context
if(config.context){
msg.payload.Context = config.context;
}
///////
//Deny Retained
if(config.retained)
if(msg.version && msg.version[0].retain) return;
//Status
node.counter++;
node.status({fill:"blue",shape:"dot",text:node.counter});
//Send
node.send(msg);
};
RED.nodes.registerType("flowcontrolOut",flowcontrolOut);
}