forked from maxill1/node-red-contrib-maxcube2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxcube.js
executable file
·307 lines (267 loc) · 9.62 KB
/
maxcube.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
var MaxCube = require('maxcube2');
module.exports = function(RED) {
function sendCommStatus(node, success, data, error){
var maxCube = node.serverConfig.maxCube;
var duty_cycle = maxCube.getCommStatus();
node.log(JSON.stringify(duty_cycle));
var msg = {};
msg.success = success;
msg.data = data;
msg.error = error;
msg.comm_status = duty_cycle;
node.send({payload: msg});
}
//missing configurations
function initNode(node, config){
//create node
RED.nodes.createNode(node, config);
//check and propagate configurations
node.serverConfig = RED.nodes.getNode(config.server);
node.singleMessage = config.singleMessage;
if (!node.serverConfig) {
return false;
}
//handle status icons
node.serverConfig.on('closed', function () {
node.status({fill:"red",shape:"ring",text:"disconnected"});
});
node.serverConfig.on('connected', function () {
node.status({fill:"green",shape:"dot",text:"connected"});
});
node.serverConfig.on('error', function (err) {
node.log(err);
node.status({fill:"red",shape:"dot",text:"Error: "+err});
});
return true;
}
function checkInputDisabled(node){
var serverConfig = node.serverConfig;
//temporary disabled by settings
if(serverConfig.disabled){
node.status({fill:"yellow",shape:"dot",text:"disabled"});
node.warn("maxcube "+serverConfig.host+" disabled");
//close existing
if(serverConfig.maxCube){
node.warn("closing exising connection: "+serverConfig.host);
serverConfig.maxCube.close();
}
return true;
}
if(!serverConfig.maxCube){
node.warn("maxCube item is not ready");
node.status({fill:"red",shape:"ring",text:"error"});
}
return false;
}
function validateMsg(msg){
//maxcube.js won't accept mode if lowercase
if(msg.payload.mode){
msg.payload.mode = msg.payload.mode.toUpperCase();
}
}
function MaxcubeNodeIn(config) {
var node = this;
if(!initNode(node, config)){
return;
}
node.on('input', function(msg) {
if(checkInputDisabled(node)){
return;
}
validateMsg(msg);
var maxCube = node.serverConfig.maxCube;
var setTemp = function(rf_address, degrees, mode, untilDate){
// Give the cube 30 seconds to anwer
maxCube.setTemperature(rf_address, degrees, mode, untilDate, 30000).then(function (success) {
var data = [rf_address, degrees, mode, untilDate].filter(function (val) {return val;}).join(', ');
if (success) {
node.log('Temperature set (' + data+ ')');
} else {
node.log('Error setting temperature (' + data+ ')');
}
node.status({fill:"green",shape:"dot",text: "last msg: "+JSON.stringify(data)});
sendCommStatus(node, success, data);
}).catch(function(e) {
node.warn(e);
sendCommStatus(node, false, data, e);
});
};
var devices = [];
//specific device
if(msg.payload.rf_address){
setTemp(msg.payload.rf_address, msg.payload.degrees, msg.payload.mode, msg.payload.untilDate);
}else{
//all devices: query getDeviceStatus, then update all!
// Give the cube 30 seconds to answer
maxCube.getDeviceStatus( undefined, 30000 ).then(function (devices) {
for (var i = 0; i < devices.length; i++) {
var deviceStatus = devices[i];
//ignoring eco buttons/window switch/etc
// cube 0
// radiator thermostat 1
// radiator thermostat plus 2
// wall thermostat 3
// shutter contact 4
// eco button 5
// unknown 6
var deviceInfo = maxCube.getDeviceInfo(deviceStatus.rf_address);
if(deviceInfo.device_type == '1' || deviceInfo.device_type == '2' || deviceInfo.device_type == '3'){
setTemp(deviceStatus.rf_address, msg.payload.degrees, msg.payload.mode, msg.payload.untilDate);
}else{
node.log("Ignoring device "+deviceStatus.rf_address + "(device_type "+deviceInfo.device_type+")");
}
}
});
}
});
}
RED.nodes.registerType("maxcube in", MaxcubeNodeIn);
function MaxcubeNodeOut(config) {
var node = this;
if(!initNode(node, config)){
return;
}
node.on('input', function(msg) {
if(checkInputDisabled(node)){
return;
}
var additionalData = function(deviceStatus, maxCube){
var deviceInfo = maxCube.getDeviceInfo(deviceStatus.rf_address);
if(deviceInfo){
var whitelist = ['device_type', 'device_name', 'room_name', 'room_id'];
for (var i = 0; i < whitelist.length; i++) {
var key = whitelist[i];
if(deviceInfo[key]){
deviceStatus[key] = deviceInfo[key];
}
}
}
};
var maxCube = node.serverConfig.maxCube;
var duty_cycle = maxCube.getCommStatus();
node.log(JSON.stringify(duty_cycle));
// Give the cube 30 seconds to answer
maxCube.getDeviceStatus( undefined, 30000).then(function (devices) {
if(node.singleMessage){
// send devices statuses as single message
var msg = {};
for (var i = 0; i < devices.length; i++) {
var deviceStatus = devices[i];
additionalData(deviceStatus, maxCube);
msg[deviceStatus.rf_address] = deviceStatus;
}
msg.comm_status = duty_cycle;
node.send({payload: msg});
}else{
// send devices statuses as separate messages
node.send([devices.map(function function_name(deviceStatus) {
// add device name, room name, to status object
additionalData(deviceStatus, maxCube);
deviceStatus.comm_status = duty_cycle;
return { rf_address: deviceStatus.rf_address, payload: deviceStatus };
})]);
}
node.status({fill:"green",shape:"dot",text: "last call: "+new Date().toTimeString()});
});
});
}
RED.nodes.registerType("maxcube out", MaxcubeNodeOut);
function MaxcubeDeviceConfigNodeOut(config) {
var node = this;
if(!initNode(node, config)){
return;
}
node.on('input', function(msg) {
if(checkInputDisabled(node)){
return;
}
var maxCube = node.serverConfig.maxCube;
var duty_cycle = maxCube.getCommStatus();
node.log(JSON.stringify(duty_cycle));
// Give the cube 30 seconds to answer
maxCube.getDeviceStatus( undefined, 30000 ).then(function (devices) {
if(node.singleMessage){
// send devices statuses as single message
var msg = {};
for (var i = 0; i < devices.length; i++) {
var deviceStatus = devices[i];
var conf = maxCube.getDeviceConfiguration(deviceStatus.rf_address);
msg[deviceStatus.rf_address] = conf;
}
msg.comm_status = duty_cycle;
node.send({payload: msg});
}else{
// send devices statuses as separate messages
node.send([devices.map(function function_name(deviceStatus) {
var conf = maxCube.getDeviceConfiguration(deviceStatus.rf_address);
conf.comm_status = duty_cycle;
return { rf_address: deviceStatus.rf_address, payload: conf };
})]);
}
node.status({fill:"green",shape:"dot",text: "last call: "+new Date().toTimeString()});
});
});
}
RED.nodes.registerType("maxcube device config", MaxcubeDeviceConfigNodeOut);
function MaxcubeServerNode(config) {
var node = this;
RED.nodes.createNode(this, config);
node.log(config.singleMessage);
this.host = config.host;
this.port = config.port;
this.disabled = config.disabled;
node.maxcubeConnect = function(){
if(node.maxCube){
node.maxCube.removeAllListeners('closed');
node.maxCube.close();
node.maxCube = undefined;
}
if (node.disabled || !node.host || !node.port) {
node.log("Maxcube disabled");
return;
}
//connect/new instance
node.log("Preparing new maxcube connection");
node.maxCube = new MaxCube(node.host, node.port);
//common events
if(node.maxCube){
node.log("Preparing new Maxcube events callback");
node.maxCube.on('closed', function () {
node.emit('closed');
connected = false;
if(node.maxCube != null) {
node.log("Maxcube connection closed unexpectedly... will try to reconnect in one second.");
setTimeout( node.maxcubeConnect, 1000 );
}
else
node.log("Maxcube connection closed...");
});
node.maxCube.on('error', function (e) {
node.emit('error', e);
node.log("Error connecting to the cube.");
node.log(JSON.stringify(e));
connected = false;
//force node to init connection if not available
node.log("Maxcube was disconnected... will try to reconnect in one second.");
setTimeout( node.maxcubeConnect, 1000 );
});
node.maxCube.on('connected', function () {
node.emit('connected');
node.log("Maxcube connected");
connected = true;
});
}
};
node.on("close", function() {
if(node.maxCube){
var maxCube = node.maxCube;
node.maxCube = null;
maxCube.close();
}
});
//first connection
var connected = false;
node.maxcubeConnect();
}
RED.nodes.registerType("maxcube-server", MaxcubeServerNode);
}