-
Notifications
You must be signed in to change notification settings - Fork 12
/
maxcube.js
executable file
·94 lines (75 loc) · 2.78 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
var MaxCube = require('maxcube');
module.exports = function(RED) {
function MaxcubeNodeIn(config) {
var node = this;
RED.nodes.createNode(this, config);
this.serverConfig = RED.nodes.getNode(config.server);
if (!this.serverConfig) {
return;
}
node.maxCube = this.serverConfig.maxCube;
node.maxCube.on('closed', function () {
node.status({fill:"red",shape:"ring",text:"disconnected"});
});
node.maxCube.on('connected', function () {
node.status({fill:"green",shape:"dot",text:"connected"});
});
node.on('input', function(msg) {
node.maxCube.setTemperature(msg.payload.rf_address, msg.payload.degrees, msg.payload.mode, msg.payload.untilDate).then(function (success) {
if (success) {
node.log('Temperature set (' + [msg.payload.rf_address, msg.payload.degrees, msg.payload.mode, msg.payload.untilDate].filter(function (val) {return val;}).join(', ') + ')');
} else {
node.log('Error setting temperature');
}
}).catch(function(e) {
node.warn(e);
});
});
}
RED.nodes.registerType("maxcube in", MaxcubeNodeIn);
function MaxcubeNodeOut(config) {
var node = this;
RED.nodes.createNode(this, config);
this.serverConfig = RED.nodes.getNode(config.server);
if (!this.serverConfig) {
return;
}
node.maxCube = this.serverConfig.maxCube;
node.maxCube.on('closed', function () {
node.status({fill:"red",shape:"ring",text:"disconnected"});
});
node.maxCube.on('connected', function () {
node.status({fill:"green",shape:"dot",text:"connected"});
});
node.on('input', function(msg) {
node.log(JSON.stringify(node.maxCube.getCommStatus()));
node.maxCube.getDeviceStatus().then(function (payload) {
// send devices statuses as separate messages
node.send([payload.map(function function_name(deviceStatus) {
// add device name, room name, to status object
var deviceInfo = node.maxCube.getDeviceInfo(deviceStatus.rf_address);
if(deviceInfo!==undefined){
deviceStatus.device_name = deviceInfo.device_name;
deviceStatus.room_name = deviceInfo.room_name;
}
return { rf_address: deviceStatus.rf_address, payload: deviceStatus };
})]);
});
});
}
RED.nodes.registerType("maxcube out", MaxcubeNodeOut);
function MaxcubeServerNode(config) {
var node = this;
RED.nodes.createNode(this, config);
this.host = config.host;
this.port = config.port;
if (!node.host || !node.port) {
return;
}
node.maxCube = new MaxCube(node.host, node.port);
node.on("close", function() {
node.maxCube.close();
});
}
RED.nodes.registerType("maxcube-server", MaxcubeServerNode);
}