forked from drmibell/node-red-contrib-queue-gate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q-gate.js
214 lines (214 loc) · 8.85 KB
/
q-gate.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
/**
* Copyright 2018-2020 M. I. Bell
*
* 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.
*
* Modified by Simon Walters 31 Jul 2019 to select toggle behavior: open/closed or open/queueing
* Modified by Colin Law 06 Apr 2020 to implement peek, drop, and status commands
**/
module.exports = function(RED) {
function QueueGateNode(config) {
RED.nodes.createNode(this,config);
const openStatus = {fill:'green',shape:'dot',text:'open'};
const closedStatus = {fill:'red',shape:'ring',text:'closed'};
var queueStatus = {fill:'yellow'};
var maxQueueLength; // debug: is this needed?
// Copy configuration items
this.controlTopic = config.controlTopic.toLowerCase();
this.openCmd = config.openCmd.toLowerCase();
this.closeCmd = config.closeCmd.toLowerCase();
this.toggleCmd = config.toggleCmd.toLowerCase();
this.qToggle = config.qToggle
this.queueCmd = config.queueCmd.toLowerCase();
this.triggerCmd = config.triggerCmd.toLowerCase();
this.flushCmd = config.flushCmd.toLowerCase();
this.resetCmd = config.resetCmd.toLowerCase();
this.peekCmd = (config.peekCmd || "peek").toLowerCase();
this.dropCmd = (config.dropCmd || "drop").toLowerCase();
this.statusCmd = (config.statusCmd || "status").toLowerCase();
this.defaultCmd = config.defaultCmd.toLowerCase();
this.defaultState = config.defaultState.toLowerCase();
this.maxQueueLength = config.maxQueueLength;
this.keepNewest = config.keepNewest;
this.persist = config.persist;
this.storeName = config.storeName;
// Save "this" object
var node = this;
var context = node.context();
var persist = node.persist;
var storeName = node.storeName
var state = context.get('state',storeName);
var queue = context.get('queue',storeName);
if (!persist || typeof state === 'undefined') {
state = node.defaultState;
queue = [];
}
// Gate status & max queue size
context.set('state',state,storeName);
context.set('queue',queue,storeName);
if (node.maxQueueLength <= 0) {
node.maxQueueLength = Infinity;
}
// Initialize status display
switch (state) {
case 'open':
node.status(openStatus);
break;
case 'closed':
node.status(closedStatus);
break;
case 'queueing':
queueStatus.text = 'queuing: ' + queue.length;
queueStatus.shape = (queue.length < node.maxQueueLength) ? 'ring':'dot';
node.status(queueStatus);
break;
default:
node.error('Invalid state');
}
// Process inputs
node.on('input', function(msg) {
var state = context.get('state',storeName) || node.defaultState;
var queue = context.get('queue',storeName) || [];
if (typeof msg.topic === 'string' &&
msg.topic.toLowerCase() === node.controlTopic) {
// Change state
if (typeof msg.payload === 'undefined' || msg.payload === null) {
msg.payload = '';
}
switch (msg.payload.toString().toLowerCase()) {
case node.openCmd:
// flush then open
node.send([queue]);
queue =[];
state = 'open';
break;
case node.closeCmd:
// reset then close
queue = [];
state = 'closed';
break;
case node.queueCmd:
state = 'queueing';
break;
case node.toggleCmd:
if (!node.qToggle) { // default toggle
switch (state) {
case 'open':
state = 'closed';
break;
case 'closed':
state = 'open';
break;
}
} else {
switch (state) { // optional toggle
case 'open':
state = 'queueing';
break;
case 'queueing':
node.send([queue]);
queue =[];
state = 'open';
break;
}
}
break;
case node.triggerCmd:
if (state === 'queueing') {
// Dequeue
if (queue.length > 0) {
node.send(queue.shift());
}
}
break;
case node.peekCmd:
if (state === 'queueing') {
// Send oldest but leave on queue
if (queue.length > 0) {
node.send(RED.util.cloneMessage(queue[0]));
}
}
break;
case node.dropCmd:
if (state === 'queueing') {
// Remove oldest from queue but don't send anything
if (queue.length > 0) {
queue.shift();
}
}
break;
case node.statusCmd:
// just show status, so do nothing here
break;
case node.flushCmd:
node.send([queue]);
case node.resetCmd:
queue = [];
break;
case node.defaultCmd:
// reset then default
queue = [];
state = node.defaultState;
break;
default:
node.warn('Invalid command ignored');
break;
}
// Save state
context.set('state',state,storeName);
context.set('queue',queue,storeName);
// Show status
switch (state) {
case 'open':
node.status(openStatus);
break;
case 'closed':
node.status(closedStatus);
break;
case 'queueing':
queueStatus.text = 'queuing: ' + queue.length;
queueStatus.shape = (queue.length < node.maxQueueLength) ? 'ring':'dot';
node.status(queueStatus);
}
node.send(null);
} else {
// Process message
switch (state) {
case 'open':
node.send(msg);
break;
case 'closed':
node.send(null);
break;
case 'queueing':
// Enqueue
if (queue.length < node.maxQueueLength) {
queue.push(msg);
} else {
if (node.keepNewest) {
queue.push(msg);
queue.shift();
}
}
queueStatus.text = 'queuing: ' + queue.length;
queueStatus.shape = (queue.length < node.maxQueueLength) ? 'ring':'dot';
node.status(queueStatus);
break;
default:
node.error('Invalid state');
}
}
})
}
RED.nodes.registerType("q-gate",QueueGateNode);
}