-
Notifications
You must be signed in to change notification settings - Fork 5
/
state.js
129 lines (116 loc) · 4.98 KB
/
state.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
module.exports = function(RED) {
function StateOutputNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.gateway = RED.nodes.getNode(config.gateway);
node.group = config.group;
node.node = config.node;
node.name = config.name;
node.delay = config.delay;
this.status({fill: "grey", shape: "dot", text: "not connected"});
node.statusReceived = function(data)
{
node.status(data)
}
node.on('input', function(msg, send, done) {
var control=[];
control[0] = { node: Number(node.node),
group: Number(node.group)
};
if(msg.topic === "control" )
{
if( msg.hasOwnProperty('payload'))
{
if(typeof msg.payload === 'number') {
if (isGroupValid(msg.payload)) {
control[0] = {group: msg.payload,
node: 0};
}
}
else if (Array.isArray(msg.payload)){
if (typeof msg.payload[0] === 'number'){
if (isGroupValid(msg.payload[0])) {
control[0] = {group: msg.payload[0],
node : 0};
if (isNodeValid(msg.payload[1])) {
control[0].node = msg.payload[1];
}
}
}
else if(typeof msg.payload[0] === 'object') {
for (var i=0; i < msg.payload.length; i++) {
if (msg.payload[i].hasOwnProperty('group')) {
if (isGroupValid(msg.payload[i].group)) {
control[i] = {group: msg.payload[i].group,
node: 0};
if (msg.payload[i].hasOwnProperty('node')) {
if (isNodeValid(msg.payload[i].node)) {
control[i].node = msg.payload[i].node
}
}
}
node.log("msg:"+msg.payload[i].group+":"+msg.payload[i].node);
node.log("control:"+control[i].group+":"+control[i].node);
}
}
}
}
else if(typeof msg.payload === 'object') {
if (msg.payload.hasOwnProperty('group')){
if (isGroupValid(msg.payload.group)) {
control[0] = {group: msg.payload.group,
node : 0 };
if (msg.payload.hasOwnProperty('node')) {
if (isNodeValid(msg.payload.node)) {
control[0].node = msg.payload.node;
}
}
}
}
}
else {
node.log("nieprawidłowa wartość msg.payload");
}
}
}
sendMessage(control, node, msg, done);
});
node.gateway.eventEmitter.on('statusChanged', node.statusReceived)
this.on('close', function() {
node.gateway.eventEmitter.removeListener('statusChanged', node.statusReceived)
});
function isNodeValid(nodeNr)
{
var isValid = (nodeNr >= 0 && nodeNr < 255);
if(!isValid)
node.error('Invalid node: '+ nodeNr);
return isValid;
}
function isGroupValid(groupNr)
{
var isValid = (groupNr > 0 && groupNr < 255);
if(!isValid)
node.error('Invalid group: '+ groupNr);
return isValid;
}
function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sendMessage(control, node, msg, done){
for (var i=0; i<control.length; i++){
if (control[i].node === 0 ) {
var hapcanMsg = Buffer.from([0xAA, 0x10,0x80, 0xF0,0xF0, 0xFF,0xFF, 0x00,control[i].group, 0xFF,0xFF,0xFF,0xFF,0xFF,0xA5]);
}
else {
var hapcanMsg = Buffer.from([0xAA, 0x10,0x90, 0xF0,0xF0, 0xFF,0xFF, control[i].node,control[i].group, 0xFF,0xFF,0xFF,0xFF,0xFF,0xA5]);
}
msg.payload = hapcanMsg;
msg.topic = 'control';
await sleep(node.delay);
node.gateway.send(msg);
}
done()
}
}
RED.nodes.registerType("state-output",StateOutputNode);
}