-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhysteresis.js
177 lines (164 loc) · 9.45 KB
/
hysteresis.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
module.exports = function(RED) {
function HysteresisNode(config) {
RED.nodes.createNode(this, config);
this.ThresholdType = config.ThresholdType || 'fixed';
this.ThresholdRising = config.ThresholdRising;
this.ThresholdFalling = config.ThresholdFalling;
this.TopicThreshold = config.TopicThreshold;
this.TopicCurrent = config.TopicCurrent;
this.ThresholdDeltaRising = config.ThresholdDeltaRising;
this.ThresholdDeltaFalling = config.ThresholdDeltaFalling;
this.DynRaiseError = config.DynRaiseError;
this.InitialMessage = config.InitialMessage;
this.OutRisingType = config.OutRisingType;
this.OutRisingValue = config.OutRisingValue;
this.OutFallingType = config.OutFallingType;
this.OutFallingValue = config.OutFallingValue;
this.OutTopicType = config.OutTopicType;
this.OutTopicValue = config.OutTopicValue || '';
if (this.OutRisingType !== 'pay') {
if ((this.OutRisingType === 'num') && (!Number.isNaN(this.OutRisingValue))) {
this.OutRisingValue = Number.parseFloat(this.OutRisingValue);
} else if (this.OutRisingType === 'bool' && (this.OutRisingValue === 'true' || this.OutRisingValue === 'false')) {
(this.OutRisingValue === 'true' ? this.OutRisingValue = true : this.OutRisingValue = false);
} else if (this.OutRisingValue === 'null') {
this.OutRisingType = 'null';
this.OutRisingValue = null;
} else {
this.OutRisingValue = String(this.OutRisingValue);
}
}
if (this.OutFallingType !== 'pay') {
if ((this.OutFallingType === 'num') && (!Number.isNaN(this.OutFallingValue))) {
this.OutFallingValue = Number.parseFloat(this.OutFallingValue);
} else if (this.OutFallingType === 'bool' && (this.OutFallingValue === 'true' || this.OutFallingValue === 'false')) {
(this.OutFallingValue === 'true' ? this.OutFallingValue = true : this.OutFallingValue = false);
} else if (this.OutFallingValue === 'null') {
this.OutFallingType = 'null';
this.OutFallingValue = null;
} else {
this.OutFallingValue = String(this.OutFallingValue);
}
}
// Define a node context
var nodeContext = this.context();
// eslint-disable-next-line prefer-const
let node = this;
let TriggerValueRising = nodeContext.get('TriggerValueRising') || '';
let TriggerValueFalling = nodeContext.get('TriggerValueFalling') || '';
if (this.ThresholdType === 'fixed') {
TriggerValueRising = Number.parseFloat(this.ThresholdRising);
TriggerValueFalling = Number.parseFloat(this.ThresholdFalling);
}
// get from context or clear direction flag
node.direction = nodeContext.get('Direction') || '';
// Set initial status
if (!TriggerValueRising) {
node.status({fill:'red', shape:'ring', text: 'Thresholds missing'});
} else {
node.status({fill:'yellow', shape:'ring', text: TriggerValueFalling + '/--/' + TriggerValueRising});
}
this.on('input', function(msg, send, done) {
// For maximum backwards compatibility, check that send exists.
// If this node is installed in Node-RED 0.x, it will need to
// fallback to using `node.send`
send = send || function() {
node.send.apply(node, arguments);
};
// Check for proper topic when using dynamic threshold
if (this.ThresholdType === 'dynamic' && msg.topic === this.TopicThreshold && !Number.isNaN(msg.payload)) {
TriggerValueRising = Number.parseFloat(msg.payload) + Number.parseFloat(this.ThresholdDeltaRising);
TriggerValueFalling = Number.parseFloat(msg.payload) - Number.parseFloat(this.ThresholdDeltaFalling);
nodeContext.set('TriggerValueRising', TriggerValueRising);
nodeContext.set('TriggerValueFalling', TriggerValueFalling);
node.status({fill:'yellow', shape:'ring', text: TriggerValueFalling + '/--/' + TriggerValueRising});
}
// Raise error when receiving a 'TopicCurrent' payload but no dynamic threshold set
if (Object.prototype.hasOwnProperty.call(msg, 'payload') && this.ThresholdType === 'dynamic' &&
!TriggerValueRising && msg.topic === this.TopicCurrent && this.DynRaiseError) {
const err = new Error('Thresholds missing');
if (err) {
if (done) {
// Node-RED 1.0 compatible
done(err);
} else {
// Node-RED 0.x compatible
node.error(err, msg);
}
}
}
// original msg object
const msgNew = RED.util.cloneMessage(msg);
// set topic
if (this.OutTopicType === 'str') {
msgNew.topic = this.OutTopicValue;
}
if ((Object.prototype.hasOwnProperty.call(msg, 'payload') && this.ThresholdType === 'fixed' && !Number.isNaN(msg.payload)) ||
(Object.prototype.hasOwnProperty.call(msg, 'payload') && this.ThresholdType === 'dynamic' &&
msg.topic === this.TopicCurrent && TriggerValueRising && !Number.isNaN(msg.payload))) {
const CurrentValue = Number.parseFloat(msg.payload);
// Cover the case where no initial values are known
if (this.InitialMessage && node.direction === '' && !Number.isNaN(CurrentValue)) {
if (CurrentValue >= TriggerValueRising) {
msgNew.payload = (this.OutRisingType === 'pay' ? msgNew.payload : this.OutRisingValue);
msgNew.hystdirection = 'initial high';
send(msgNew);
node.direction = 'high';
nodeContext.set('Direction', node.direction);
node.status({fill:'green', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (initial high band)'});
} else if ((CurrentValue > TriggerValueFalling) && (CurrentValue < TriggerValueRising)) {
node.status({fill:'green', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (initial dead band)'});
} else if (CurrentValue <= TriggerValueFalling) {
msgNew.payload = (this.OutFallingType === 'pay' ? msgNew.payload : this.OutFallingValue);
msgNew.hystdirection = 'initial low';
send(msgNew);
node.direction = 'low';
nodeContext.set('Direction', node.direction);
node.status({fill:'blue', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (initial low band)'});
}
// Last value known. Work as hysteresis
} else if (!Number.isNaN(CurrentValue) && node.LastValue) {
// rising
if (CurrentValue > node.LastValue && CurrentValue >= TriggerValueRising && node.direction !== 'high') {
msgNew.payload = (this.OutRisingType === 'pay' ? msgNew.payload : this.OutRisingValue);
msgNew.hystdirection = 'high';
send(msgNew);
node.direction = 'high';
nodeContext.set('Direction', node.direction);
node.status({fill:'green', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (high band)'});
// falling
} else if (CurrentValue < node.LastValue && CurrentValue <= TriggerValueFalling && node.direction !== 'low') {
msgNew.payload = (this.OutFallingType === 'pay' ? msgNew.payload : this.OutFallingValue);
msgNew.hystdirection = 'low';
send(msgNew);
node.direction = 'low';
nodeContext.set('Direction', node.direction);
node.status({fill:'blue', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (low band)'});
} else if (CurrentValue > node.LastValue && CurrentValue >= TriggerValueRising && node.direction === 'high') {
node.status({fill:'green', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (high band rising)'});
} else if (CurrentValue < node.LastValue && CurrentValue >= TriggerValueRising && node.direction === 'high') {
node.status({fill:'green', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (high band falling)'});
} else if (CurrentValue > node.LastValue && CurrentValue > TriggerValueFalling && CurrentValue < TriggerValueRising && node.direction === 'high') {
node.status({fill:'green', shape:'ring', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (high dead band rising)'});
} else if (CurrentValue < node.LastValue && CurrentValue > TriggerValueFalling && CurrentValue < TriggerValueRising && node.direction === 'high') {
node.status({fill:'green', shape:'ring', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (high dead band falling)'});
} else if (CurrentValue > node.LastValue && CurrentValue > TriggerValueFalling && CurrentValue < TriggerValueRising && node.direction === 'low') {
node.status({fill:'blue', shape:'ring', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (low dead band rising)'});
} else if (CurrentValue < node.LastValue && CurrentValue > TriggerValueFalling && CurrentValue < TriggerValueRising && node.direction === 'low') {
node.status({fill:'blue', shape:'ring', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (low dead band falling)'});
} else if (CurrentValue > node.LastValue && CurrentValue <= TriggerValueFalling && node.direction === 'low') {
node.status({fill:'blue', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueRising + ' (low band rising)'});
} else if (CurrentValue < node.LastValue && CurrentValue <= TriggerValueFalling && node.direction === 'low') {
node.status({fill:'blue', shape:'dot', text: TriggerValueFalling + '/' + CurrentValue + '/' + TriggerValueFalling + ' (low band falling)'});
}
}
node.LastValue = CurrentValue;
nodeContext.set('LastValue', node.LastValue);
if (done) {
done();
}
}
});
}
RED.nodes.registerType('hysteresis', HysteresisNode);
};