-
Notifications
You must be signed in to change notification settings - Fork 5
/
11-eibd.js
223 lines (208 loc) · 6.66 KB
/
11-eibd.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
/*
KNX/eibd nodes for IBM's Node-Red
https://github.com/ekarak/node-red-contrib-eibd
(c) 2014, Elias Karakoulakis <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function timestamp() {
return new Date().
toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '')
}
function log(msg, args) {
console.log(timestamp() + ': ' + msg, args);
}
module.exports = function(RED) {
log("loading eibd/KNX for node-red");
var eibd = require('eibd');
/**
* ====== EIBD-CONTROLLER ================
* Holds configuration for eibd host+port,
* initializes new eibd connections
* =======================================
*/
function EibdControllerNode(config) {
log("new EibdControllerNode, config: %j", config);
RED.nodes.createNode(this, config);
this.host = config.host;
this.port = config.port;
var node = this;
/**
* Initialize an eibd socket, calling the handler function
* when successfully connected, passing it the eibd connection
*/
this.initializeEibdSocket = function (handler) {
log('connecting to eibd server at %s:%d', config.host, config.port);
var eibdconn = new eibd.Connection();
eibdconn.socketRemote({ host: config.host, port: config.port }, function(err) {
if (err) {
log('eibd.socketRemote error: %s', err.code);
//setTimeout(this, 10000);
} else {
log('EIBD: successfully connected to %s:%d', config.host, config.port);
if (handler && (typeof handler === 'function')) {
handler(eibdconn);
}
}
});
};
}
RED.nodes.registerType("eibd-controller", EibdControllerNode);
/**
* ====== EIBD-OUT =======================
* Sends outgoing KNX telegrams from
* messages received via node-red flows
* =======================================
*/
function EibdOut(config) {
log('new EIBD-OUT, config: %j', config);
RED.nodes.createNode(this, config);
this.name = config.name;
this.ctrl = RED.nodes.getNode(config.controller);
var node = this;
//
this.on("input", function(msg) {
//log('eibdout.onInput, msg=%j', msg);
if (!(msg && msg.hasOwnProperty('payload'))) {
log('eibdout.onInput: illegal msg.payload!');
return;
}
var payload;
switch (typeof(msg.payload)) {
case 'object': payload = msg.payload; break;
case 'string': payload = JSON.parse(msg.payload); break;
default:
log('eibdout.onInput: illegal msg.payload type: '+typeof(msg.payload));
return;
}
var apci;
switch(true) {
case /read/.test(msg.topic):
apci = 0x00; break;
case /respon/.test(msg.topic):
apci = 0x40; break;
default: apci = 0x80;
}
this.groupAddrSend(payload.dstgad, payload.value, payload.dpt, apci, function(err) {
if (err) {
log('groupAddrSend error: %j', err);
}
});
});
this.on("close", function() {
log('eibdOut.close');
});
/**
* send a group write telegram to a group address (see bin/groupswrite and bin/groupwrite)
* Initializes new eibd connection per request - FIXME
* dstgad: dest group address '1/2/34'
* dpt: DataPointType eg. '1' for boolean
* value: the value to write
* callback:
*
* Usage:
* groupAddrSend({ host: 'localhost', port: 6720}, '1/2/34', 1, 1, function(err) {
* if(err) console.error(err);
* });
*/
this.groupAddrSend = function(dstgad, value, dpt, apci, callback) {
if (typeof apci === 'undefined') apci = 0x80;
//log('groupAddrSend dstgad:%s, value:%s, dpt:%s', dstgad, value, dpt);
// init a new connection from the effectively singleton EibdController
this.ctrl.initializeEibdSocket(function(conn) {
conn.openTGroup(eibd.str2addr(dstgad), 0, function (err) {
if(err && (typeof callback === 'function')) {
log('error calling openTGroup!: %j', err);
callback(err);
} else {
var data = node.formatAPDU(value, dpt, apci);
//log("sendAPDU: %j", JSON.stringify(data));
conn.sendAPDU(data, callback);
}
});
});
}
// format a KNX APDU (telegram) for sending
// dpt: TODO handle more than booleans and 4-bit
// apci: 0x00=read, 0x40=response, 0x80=write
this.formatAPDU = function(value, dpt, apci) {
var data;
//log("formatAPDU value=%j dpt=%j", value, dpt);
// most common case
if ((dpt === 1) || (dpt === '1' ) || (dpt === 'DPT1')) {
data = new Array(2);
data[0] = 0;
data[1] = apci | value;
} else {
data = new Array(3);
data[0] = 0;
data[1] = apci;
data[2] = (0xff & value);
// TODO: what about strings?
}
return(data);
};
}
//
RED.nodes.registerType("eibd-out", EibdOut);
/**
* ====== EIBD-IN ========================
* Handles incoming KNX events, injecting
* json into node-red flows
* =======================================
*/
function EibdIn(config) {
log('new EIBD-IN, config: %j', config);
RED.nodes.createNode(this, config);
this.name = config.name;
this.inconn = null;
var node = this;
var eibdController = RED.nodes.getNode(config.controller);
/* ===== Node-Red events ===== */
this.on("input", function(msg) {
if (msg != null) {
};
});
this.on("close", function() {
log('eibdIn.close');
if (this.inconn) {
this.inconn.end();
}
});
// this.on("error", function(msg) {});
/* ===== eibd events ===== */
// initialize incoming KNX event socket (openGroupSocket)
// there's only one connection for eibd-in:
eibdController.initializeEibdSocket(function(conn) {
log('Initialized eibd socket');
this.inconn = conn;
this.inconn.openGroupSocket(0, function(parser) {
parser.on('write', function(src, dest, dpt, val){
//log('Write from '+src+' to '+dest+': '+val);
node.send({ topic: 'knx: write', payload: {'srcphy': src, 'dstgad': dest, 'dpt': dpt, 'value': val }});
});
//
parser.on('response', function(src, dest, val) {
//log('Response from %s to %s: %s', src, dest, val);
node.send({ topic: 'knx: response', payload: {'srcphy': src, 'dstgad': dest, 'value': val }});
});
//
parser.on('read', function(src, dest) {
//log('Read from %s to %s', src, dest);
node.send({ topic: 'knx: read', payload: {'srcphy': src, 'dstgad': dest}});
});
});
});
}
//
RED.nodes.registerType("eibd-in", EibdIn);
}