-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththermostat.js
322 lines (305 loc) · 14.3 KB
/
thermostat.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
const {logEndpoint, EndpointServer} = require( "@matter/main")
const { hasProperty, isNumber, willUpdate } = require('./utils');
const {battery} = require('./battery')
module.exports = function(RED) {
function MatterThermostat(config) {
RED.nodes.createNode(this,config);
var node = this;
node.bridge = RED.nodes.getNode(config.bridge);
node.name = config.name
node.mode = config.mode
node.heat = (config.mode == 'heat' || config.mode == 'heatcool')
node.cool = (config.mode == 'cool' || config.mode == 'heatcool')
node.ctx = this.context().global;
node.values = node.ctx.get(node.id+"-values") || null
node.temperature = node.ctx.get(node.id+"-temperature") || null
this.log(`Loading Device node ${node.id}`)
node.status({fill:"red",shape:"ring",text:"not running"});
node.pending = false
node.passthrough = /^true$/i.test(config.passthrough)
node.bat = config.bat;
node.topic = config.topic || false
node.identifying = false
node.identifyEvt = function() {
node.identifying = !node.identifying
if (node.identifying){
node.status({fill:"blue",shape:"dot",text:"identify"});
} else {
node.status({fill:"green",shape:"dot",text:"ready"});
}
};
node.modeEvt = function(value, oldValue, context) {
let eventSource = {}
if (hasProperty(context, 'offline')) {
eventSource.local = true
} else {
eventSource.local = false
eventSource.srcAddress = context.exchange.channel.channel.peerAddress
eventSource.srcPort = context.exchange.channel.channel.peerPort
eventSource.fabric = node.bridge.matterServer.state.commissioning.fabrics[context.fabric]
}
node.values ? node.values.systemMode=value : node.values={systemMode:value}
let modes = {0 : 'off', 3 : 'cool', 4 : 'heat'}
let temp = node.device.state.thermostat.localTemperature
data = {'mode' : modes[value], temperature : temp}
if (value == 4){
data.setPoint = node.device.state.thermostat.occupiedHeatingSetpoint
} else if (value == 3){
data.setPoint = node.device.state.thermostat.occupiedCoolingSetpoint
}
if ((node.pending && node.passthrough)) {
var msg = node.pendingmsg
msg.eventSource = eventSource
msg.payload = data
node.send(msg);
} else if (!node.pending){
var msg = {payload : {}};
if (node.topic) {msg.topic = node.topic}
msg.eventSource = eventSource
msg.payload=data
node.send(msg);
}
node.pending = false
};
node.coolSetpointEvt = function(value, oldValue, context) {
let eventSource = {}
if (hasProperty(context, 'offline')) {
eventSource.local = true
} else {
eventSource.local = false
eventSource.srcAddress = context.exchange.channel.channel.peerAddress
eventSource.srcPort = context.exchange.channel.channel.peerPort
eventSource.fabric = node.bridge.matterServer.state.commissioning.fabrics[context.fabric]
}
let temp = node.device.state.thermostat.localTemperature
node.values ? node.values.occupiedCoolingSetpoint=value : node.values={occupiedCoolingSetpoint:value}
data = {mode : 'cool', setPoint : value, temperature: temp}
if ((node.pending && node.passthrough)) {
var msg = node.pendingmsg
msg.eventSource = eventSource
msg.payload = data
node.send(msg);
} else if (!node.pending){
var msg = {payload : {}};
if (node.topic) {msg.topic = node.topic}
msg.eventSource = eventSource
msg.payload=data
node.send(msg);
}
node.pending = false
};
node.heatSetpointEvt = function(value, oldValue, context) {
let eventSource = {}
if (hasProperty(context, 'offline')) {
eventSource.local = true
} else {
eventSource.local = false
eventSource.srcAddress = context.exchange.channel.channel.peerAddress
eventSource.srcPort = context.exchange.channel.channel.peerPort
eventSource.fabric = node.bridge.matterServer.state.commissioning.fabrics[context.fabric]
}
node.values ? node.values.occupiedHeatingSetpoint=value : node.values={occupiedHeatingSetpoint:value}
let temp = node.device.state.thermostat.localTemperature
data = {mode : 'heat', setPoint : value, temperature: temp}
if ((node.pending && node.passthrough)) {
var msg = node.pendingmsg
msg.eventSource = eventSource
msg.payload = data
node.send(msg);
} else if (!node.pending){
var msg = {payload : {}};
if (node.topic) {msg.topic = node.topic}
msg.eventSource = eventSource
msg.payload=data
node.send(msg);
}
node.pending = false
};
node.tempEvt = function(value, oldValue, context) {
let eventSource = {}
if (hasProperty(context, 'offline')) {
eventSource.local = true
} else {
eventSource.local = false
eventSource.srcAddress = context.exchange.channel.channel.peerAddress
eventSource.srcPort = context.exchange.channel.channel.peerPort
eventSource.fabric = node.bridge.matterServer.state.commissioning.fabrics[context.fabric]
}
let modes = {0 : 'off', 3 : 'cool', 4 : 'heat'}
let mode = node.device.state.thermostat.systemMode
data = {'mode' : modes[mode], 'temperature' : value}
if (mode == 4){
data.setPoint = node.device.state.thermostat.occupiedHeatingSetpoint
} else if (mode == 3){
data.setPoint = node.device.state.thermostat.occupiedCoolingSetpoint
}
if ((node.pending && node.passthrough)) {
msg.eventSource = eventSource
var msg = node.pendingmsg
msg.payload = data
node.send(msg);
}
node.pending = false
};
this.on('input', function(msg) {
switch (msg.topic) {
case 'state':
if (hasProperty(msg, 'payload')) {
node.device.set(msg.payload)
}
if (config.wires.length != 0){
msg.payload = node.device.state
node.send(msg)
} else{
node.error((node.device.state));
}
break;
case 'battery':
if (node.bat){
battery(node, msg)
}
break
default:
if (!hasProperty(msg.payload, 'mode') || !hasProperty(msg.payload, 'setPoint') || !hasProperty(msg.payload, 'temperature')){
node.error('Invalid input')
break;
}
var newData = {thermostat : {}}
sysMode(msg, newData)
.then((systemMode) => setPoint(msg, newData, systemMode))
.then(() => temperature(msg, newData))
.then(() => {
//If values are changed then set them & wait for callback otherwise send msg on
if (willUpdate.call(node.device, newData)) {
this.debug('WILL UPDATE')
node.pending = true
node.pendingmsg = msg
node.device.set(newData).catch((err) => {node.debug(err); node.error('Invalid Input')})
} else {
this.debug('WONT UPDATE')
if (node.passthrough){
node.send(msg);
}
}
})
break
}
});
this.on('serverReady', function() {
var node = this
node.device.events.identify.startIdentifying.on(node.identifyEvt)
node.device.events.identify.stopIdentifying.on(node.identifyEvt)
node.device.events.thermostat.systemMode$Changed.on(node.modeEvt)
node.device.events.thermostat.localTemperature$Changed.on(node.tempEvt)
if (node.heat){
node.device.events.thermostat.occupiedHeatingSetpoint$Changed.on(node.heatSetpointEvt)
}
if (node.cool){
node.device.events.thermostat.occupiedCoolingSetpoint$Changed.on(node.coolSetpointEvt)
}
node.status({fill:"green",shape:"dot",text:"ready"});
})
this.on('close', async function(removed, done) {
let node = this
let rtype = removed ? 'Device was removed/disabled' : 'Device was restarted'
node.log(`Closing device: ${this.id}, ${rtype}`)
//Remove Matter.js Events
await node.device.events.identify.startIdentifying.off(node.identifyEvt)
await node.device.events.identify.stopIdentifying.off(node.identifyEvt)
await node.device.events.thermostat.systemMode$Changed.off(node.modeEvt)
await node.device.events.thermostat.localTemperature$Changed.off(node.tempEvt)
if (node.heat){
await node.device.events.thermostat.occupiedHeatingSetpoint$Changed.off(node.heatSetpointEvt)
}
if (node.cool){
await node.device.events.thermostat.occupiedCoolingSetpoint$Changed.off(node.coolSetpointEvt)
}
//Remove Node-RED Custom Events
node.removeAllListeners('serverReady')
//Remove from Bridge Node Registered
let index = node.bridge.registered.indexOf(node);
if (index > -1) {
node.bridge.registered.splice(index, 1);
}
if (removed){
await node.device.close()
}
done();
});
function sysMode(msg, newData){
return new Promise((resolve) => {
let modes = {0 : 'off', 3 : 'cool', 4 : 'heat'}
let systemMode
if (hasProperty(msg.payload, 'mode')){
switch (msg.payload.mode) {
case 'heat':
systemMode = 4
break;
case 'cool':
systemMode = 3
break;
case 'off':
systemMode = 0
break
default:
systemMode = node.device.state.thermostat.systemMode
break;
}
newData.thermostat.systemMode = systemMode
msg.payload.mode = modes[systemMode]
} else {
systemMode = node.device.state.thermostat.systemMode
newData.thermostat.systemMode = systemMode
msg.payload.mode = modes[systemMode]
}
resolve(systemMode)
})
}
function setPoint(msg, newData, systemMode){
return new Promise((resolve) => {
if (hasProperty(msg.payload, 'setPoint') && isNumber(msg.payload.setPoint)) {
if (msg.payload.setPoint < 50) {msg.payload.setPoint = msg.payload.setPoint*100 }
if (systemMode == 4){
newData.thermostat.occupiedHeatingSetpoint = msg.payload.setPoint
} else if (systemMode == 3){
newData.thermostat.occupiedCoolingSetpoint = msg.payload.setPoint
}
} else {
if (systemMode == 4){
newData.thermostat.occupiedHeatingSetpoint = node.device.state.thermostat.occupiedHeatingSetpoint
msg.payload.setPoint = node.device.state.thermostat.occupiedHeatingSetpoint
} else if (systemMode == 3){
msg.payload.setPoint = node.device.state.thermostat.occupiedCoolingSetpoint
newData.thermostat.occupiedCoolingSetpoint = node.device.state.thermostat.occupiedCoolingSetpoint
}
}
resolve()
})
}
function temperature(msg, newData){
return new Promise((resolve) => {
if (hasProperty(msg.payload, 'temperature') && isNumber(msg.payload.temperature)) {
if (msg.payload.temperature < 50) {msg.payload.temperature = msg.payload.temperature*100}
newData.thermostat.localTemperature = msg.payload.temperature
node.ctx.set(node.id+"-temperature", msg.payload.temperature)
node.temperature = msg.payload.temperature
} else {
msg.payload.temperature = node.device.state.thermostat.localTemperature
newData.thermostat.localTemperature = node.device.state.thermostat.localTemperature
}
resolve()
})
}
//Wait till server is started
function waitforserver(node) {
if (!node.bridge.serverReady) {
setTimeout(waitforserver, 100, node)
} else {
node.log('Registering Child......')
node.bridge.emit('registerChild', node)
}
}
waitforserver(node)
}
RED.nodes.registerType("matterthermostat",MatterThermostat)
}