-
Notifications
You must be signed in to change notification settings - Fork 1
/
zllswitch.js
131 lines (106 loc) · 4.86 KB
/
zllswitch.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
/**
* NodeRED Hue Bridge
* Copyright (C) 2018 Michael Jacobsen.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
module.exports = function(RED) {
"use strict";
/******************************************************************************************************************
*
*
*/
function ZGPSwitchNode(config) {
RED.nodes.createNode(this, config);
this.client = config.client;
this.clientConn = RED.nodes.getNode(this.client);
this.timer = null;
if (!this.clientConn) {
this.error(RED._("zllswitch.errors.missing-config"));
this.status({fill:"red", shape:"dot", text:"Missing config"});
return;
} else if (typeof this.clientConn.register !== 'function') {
this.error(RED._("zllswitch.errors.missing-bridge"));
this.status({fill:"red", shape:"dot", text:"Missing bridge"});
return;
}
this.switchid = this.clientConn.register(this, 'zll', config.name, 'ZGPSwitch');
if (this.switchid === false) {
this.error(RED._("zllswitch.errors.create"));
this.status({fill:"red", shape:"dot", text:RED._("zllswitch.errors.create")});
return;
}
var node = this;
this.status({fill:"green", shape:"dot", text:"Ready"});
/******************************************************************************************************************
* respond to inputs from NodeRED
*
*/
this.on('input', function (msg) {
RED.log.debug("ZGPSwitchNode(input): msg = " + JSON.stringify(msg));
RED.log.debug("ZGPSwitchNode(input): typeof payload = " + typeof msg.payload);
var buttonid = 0;
if (typeof msg.payload === 'number') {
switch (msg.payload) {
case 1:
buttonid = 34;
node.status({fill:"green", shape:"dot", text:"Button 1"});
setTimeout(function () { node.status({}) }, 5000);
break;
case 2:
buttonid = 16;
node.status({fill:"green", shape:"dot", text:"Button 2"});
setTimeout(function () { node.status({}) }, 5000);
break;
case 3:
buttonid = 17;
node.status({fill:"green", shape:"dot", text:"Button 3"});
setTimeout(function () { node.status({}) }, 5000);
break;
case 4:
buttonid = 18;
node.status({fill:"green", shape:"dot", text:"Button 4"});
setTimeout(function () { node.status({}) }, 5000);
break;
default:
return;
}
}
var obj = node.clientConn.bridge.dsGetSensor(node.switchid);
RED.log.debug("ZGPSwitchNode(input): obj = " + JSON.stringify(obj));
obj.state.buttonevent = buttonid;
RED.log.debug("ZGPSwitchNode(input): obj = " + JSON.stringify(obj));
node.clientConn.bridge.dsUpdateSensorState(node.switchid, obj.state);
setTimeout(function(node) {
RED.log.debug("ZGPSwitchNode(timer):");
var obj = node.clientConn.bridge.dsGetSensor(node.switchid);
RED.log.debug("ZGPSwitchNode(timer): obj = " + JSON.stringify(obj));
obj.state.buttonevent = 0;
RED.log.debug("ZGPSwitchNode(timer): obj = " + JSON.stringify(obj));
node.clientConn.bridge.dsUpdateSensorState(node.switchid, obj.state);
}, 1000, node);
});
this.on('close', function(removed, done) {
if (removed) {
// this node has been deleted
node.clientConn.remove(node, 'zll');
} else {
// this node is being restarted
node.clientConn.deregister(node, 'zll');
}
done();
});
}
RED.nodes.registerType("huebridge-zgpswitch", ZGPSwitchNode);
}