-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcustom.js
165 lines (138 loc) · 6.33 KB
/
custom.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
module.exports = function(RED) {
function CustomMessageOutputNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.gateway = RED.nodes.getNode(config.gateway);
node.compGroup = Number(config.group);
node.compNode = Number(config.node);
node.name = config.name;
node.frameType = config.frameType
node.dataNames = [
{ byte: 5, name: config.d0name },
{ byte: 6, name: config.d1name },
{ byte: 7, name: config.d2name },
{ byte: 8, name: config.d3name },
{ byte: 9, name: config.d4name },
{ byte: 10, name: config.d5name },
{ byte: 11, name: config.d6name },
{ byte: 12, name: config.d7name }
]
node.namedDataBytes = node.dataNames.filter(function(current){
return current.name !== ''
})
node.d0value = Number(config.d0value)
node.d1value = Number(config.d1value)
node.d2value = Number(config.d2value)
node.d3value = Number(config.d3value)
node.d4value = Number(config.d4value)
node.d5value = Number(config.d5value)
node.d6value = Number(config.d6value)
node.d7value = Number(config.d7value)
this.status({fill: "grey", shape: "dot", text: "not connected"});
node.statusReceived = function(data)
{
node.status(data)
}
node.on('input', function(msg, send, done) {
var hapcanMsg = Buffer.from([0xAA, 0xFF,0xFF, node.compNode,node.compGroup, 0xF0,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xA5]);
hapcanMsg[1] = node.frameType >>> 4
hapcanMsg[2] = (node.frameType & 0x0F) << 4
hapcanMsg[5] = node.d0value
hapcanMsg[6] = node.d1value
hapcanMsg[7] = node.d2value
hapcanMsg[8] = node.d3value
hapcanMsg[9] = node.d4value
hapcanMsg[10] = node.d5value
hapcanMsg[11] = node.d6value
hapcanMsg[12] = node.d7value
let messageModified = false
if(msg.topic === "control" )
{
if(typeof msg.payload === 'object')
{
for(let i = 0; i < node.namedDataBytes.length; i++){
let namedDataByte = node.namedDataBytes[i]
if (msg.payload[namedDataByte.name] !== undefined ){
hapcanMsg[namedDataByte.byte] = Math.min(255, Math.max(0, Number(msg.payload[namedDataByte.name]) ) )
messageModified = true
}
}
}
// do not send hapcan frame if control message doesn't modiffy the frame
if(!messageModified){
done()
return
}
}
msg.payload = hapcanMsg;
msg.topic = 'control';
node.gateway.send(msg);
done()
});
node.gateway.eventEmitter.on('statusChanged', node.statusReceived)
this.on('close', function() {
node.gateway.eventEmitter.removeListener('statusChanged', node.statusReceived)
});
}
RED.nodes.registerType("custom-output",CustomMessageOutputNode);
function CustomMessageInputNode(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.frameTypeFilter = config.frameTypeFilter;
node.d0name = config.d0name;
node.d1name = config.d1name;
node.d2name = config.d2name;
node.d3name = config.d3name;
node.d4name = config.d4name;
node.d5name = config.d5name;
node.d6name = config.d6name;
node.d7name = config.d7name;
node.hapcanId = ("00" + node.node).slice (-3) + ("00" + node.group).slice (-3) + '_';
this.status({fill: "grey", shape: "dot", text: "not connected"});
node.statusReceived = function(data)
{
node.status(data)
}
node.messageReceived = function(data)
{
var hapcanMessage = data.payload;
if( (Number(node.node)!== 0 && hapcanMessage.node != node.node) || (Number(node.group)!== 0 && hapcanMessage.group != node.group) )
return;
if(node.frameTypeFilter !== 0){
if(hapcanMessage.frameType !== node.frameTypeFilter)
return;
}
if( node.d0name !== '')
hapcanMessage[node.d0name] = hapcanMessage.frame[5];
if( node.d1name !== '')
hapcanMessage[node.d1name] = hapcanMessage.frame[6];
if( node.d2name !== '')
hapcanMessage[node.d2name] = hapcanMessage.frame[7];
if( node.d3name !== '')
hapcanMessage[node.d3name] = hapcanMessage.frame[8];
if( node.d4name !== '')
hapcanMessage[node.d4name] = hapcanMessage.frame[9];
if( node.d5name !== '')
hapcanMessage[node.d5name] = hapcanMessage.frame[10];
if( node.d6name !== '')
hapcanMessage[node.d6name] = hapcanMessage.frame[11];
if( node.d7name !== '')
hapcanMessage[node.d7name] = hapcanMessage.frame[12];
let {deviceName, channelName} = node.gateway.getDeviceInfo(hapcanMessage.node, hapcanMessage.group, null, 1) // channel is unknown
//hapcanMessage.channelName = channelName
hapcanMessage.deviceName = deviceName
node.send({topic: 'Hapcan message', payload: hapcanMessage});
};
node.gateway.eventEmitter.on('messageReceived', node.messageReceived)
node.gateway.eventEmitter.on('statusChanged', node.statusReceived)
this.on('close', function() {
node.gateway.eventEmitter.removeListener('messageReceived', node.messageReceived)
node.gateway.eventEmitter.removeListener('statusChanged', node.statusReceived)
});
}
RED.nodes.registerType("custom-input",CustomMessageInputNode);
}