-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
308 lines (248 loc) · 9.18 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var net = require('net');
var util = require('util');
var Stream = require('stream');
var spawn = require('child_process').spawn;
var _ = require('underscore');
var log4js = require('log4js');
log4js.configure({
appenders: [
{
type: "console"
},
{
"type": "dateFile",
"filename": (process.platform=='linux'?'/var/log/':'')+'ninja-zigbee.log',
"pattern": "-yyyy-MM-dd",
"alwaysIncludePattern": false
}
],
replaceConsole: false
});
var Table = require('cli-table');
var DebugDevice = require('./devices/DebugDevice');
var PresenceDriver = require('./lib/PresenceDriver');
var ZigBeeClient = require('./lib/ZigbeeClient');
// Adds extra logging, and attempts to communicate with any unknown devices.
var DEBUG_MODE = false;
// Uses The Faker to create devices that emit random things
var FAKE_DEVICES = false;
function ZigbeeDriver(opts,app) {
var self = this;
this._app = app;
this._opts = opts;
this.log = log4js.getLogger('ZB');
this._presence = new PresenceDriver(this._opts, this._app);
// Hack to give the Server time to start
// TODO: parse response from server's stdout
app.once('client::up', this.begin.bind(this));
}
module.exports = ZigbeeDriver;
util.inherits(ZigbeeDriver,Stream);
/**
* Creates necessary classes/connections and the pipes between the ZigbeeClient and this driver
*/
ZigbeeDriver.prototype.begin = function() {
if (FAKE_DEVICES) {
require('./fake/Faker')(this);
}
var self = this;
if (this.socket) {
// We already have a connection
return;
}
// Create a new client
var client = this._client = new ZigBeeClient();
var devices = [];
var showTable = false;
client.on('ready', function() {
setTimeout(function() {
showDevicesTable();
showTable = true;
}, 2000);
// Create a new connection to the SRPC server: port 0x2be3 for ZLL
// TODO: incorporate this into the ZigbeeClient
this.socket = net.connect(11235,/* '10.37.129.3',*/ function() {
this.log.info('Connected to TI ZLL Server');
setTimeout(function() {
client.discoverDevices();
/*setTimeout(function() {
client.permitJoin(180);
}, 5000);*/
}, 1000);
setInterval(function() {
//client.discoverDevices();
}, 1000);
}.bind(this));
// Listen for errors on this connection
this.socket.on('error',function(err) {
this.log.error(err);
}.bind(this));
// Node warns after 11 listeners have been attached
// Increase the max listeners as we bind ~3 events per n devices
this.socket.setMaxListeners(999);
var coordinator;
// Setup the bi-directional pipe between the client and socket.
// Additionally setup the pipe between any new devices and the socket
// once they are discovered.
client
.pipe(this.socket)
.pipe(client)
.on('device',function(address, zigbeeDevice, headers, isNew) {
self._presence.emit('deviceSeen', address, zigbeeDevice, headers);
if (isNew) {
if (!zigbeeDevice) {
self.log.warn("Found an unknown device. Please post this to the forums -> Profile:" + hex(headers.profileId) + ' Device:' + hex(headers.deviceId) + ' Address:' + address);
if (DEBUG_MODE) {
zigbeeDevice = {
name: '[Unknown Device]',
profile: headers.profileId,
id: headers.deviceId
};
} else {
return;
}
}
self.log.info('Device found', zigbeeDevice.name + ' ' + zigbeeDevice.profile + ':' + zigbeeDevice.id + ' (' + address + ')');
// Forward all relevant messages from zigbee to our new devices
var newDevices = createNinjaDevices(address, headers, zigbeeDevice, self.socket);
if (!newDevices.length) {
self.log.warn("Found a '" + zigbeeDevice.name + "' device that we weren't able to expose over NinjaBlocks. Please post this to the forums -> Profile:" + hex(headers.profileId) + ' Device:' + hex(headers.deviceId) + ' Address:' + address);
if (DEBUG_MODE) {
newDevices = [new DebugDevice(address, headers, zigbeeDevice, self.socket, 'DebugDevice')];
}
}
_.each(newDevices, function(device) {
self.emit('register', device);
devices.push(device);
device.coordinator = coordinator;
//setTimeout(function() {
//device.sendZCL();
//}, 2000);
/*setTimeout(function() {
/// device.setName("HelloNinja");
//device.discoverAttributes();
var todo = [];
for (var i = 0; i < 50; i++) {
todo.push((function(x) {
return function() {
device.readAttribute(0x0500, x, function(reader) {
console.log("GOT ATTRIBUTE RESPONSE" + x, 'type', reader.vars.dataType.toString(16), reader.vars);
});
setTimeout(next, 500);
}
})(i));
}
function next() {
if (todo.length) {
(todo.pop())();
}
}
next();
}, 2000);*/
//device.discoverAttributes();
//device.identify(10);
client.on(address, function(incomingAddress, zigbeeDevice, reader) {
device.emit('message', incomingAddress, reader);
});
});
if (showTable) {
showDevicesTable();
}
}
})
.on('coordinator', function(address, headers) {
self.log.info('Found coordinator at ', address, headers);
coordinator = headers;
})
.on('deviceSeen', function(address, zigbeeDevice, headers) {
self._presence.emit('deviceSeen', address, zigbeeDevice, headers);
});
function showDevicesTable() {
if (devices.length === 0) {
return;
}
var table = new Table({
head: ['IEEE', 'Address', 'Profile ID', 'Device ID', 'Driver'],
colWidths: [20, 16,16,16,16]
});
_.each(devices, function(device) {
table.push([device._headers.ieeeHex, device.address, hex(device.zigbeeDevice.profile)||'', hex(device.zigbeeDevice.id)||'', device.driver||'']);
});
self.log.info('-- ZigBee Devices --\n' + table.toString());
}
}.bind(this));
};
ZigbeeDriver.prototype.config = function(rpc,cb) {
var self = this;
if (!rpc) {
return cb(null,{"contents":[
{ "type": "input_field_select", "field_name": "pairingTime", "label": "Pairing Time", "options": [{ "name": "1 minute", "value": "60", "selected": true}, { "name": "2 minutes", "value": "120"}, { "name": "3 minutes", "value": "180"}, { "name": "4 minutes", "value": "240"}], "required": false },
{ "type": "submit", "name": "Start Pairing", "rpc_method": "startPairing" }
]});
}
switch (rpc.method) {
case 'startPairing':
var pairingTime = parseInt(rpc.params.pairingTime, 10);
this._client.permitJoin(pairingTime);
cb(null, {
"contents": [
{ "type":"paragraph", "text":"Pairing has been enabled for " + pairingTime + " seconds."},
{ "type":"close", "text":"Close"}
]
});
setTimeout(function() {
self.emit('announcement', {
"contents": [
{ "type":"paragraph", "text":"Zigbee : No longer pairing."},
{ "type":"close", "text":"Close"}
]
});
}, pairingTime * 1000);
break;
default:
log('Unknown rpc method', rpc.method, rpc);
}
};
// TODO: move this out of here! into device drivers?
var mappings = {
"Light" : ["0x0104:0x0102", "0x0104:0x0105", "0xc05e:0x0000",
"0xc05e:0x0100","0xc05e:0x0200","0xc05e:0x0210"],
"Relay" : ["0x0104:0x0009"],
"Power" : ["0x0104:0x0009"],
"Humidity" : ["0x0104:0x0302", "0x0104:0x03fe"],
"Temperature" : ["0x0104:0x0302", "0x0104:0x03fe"],
"LightSensor" : ["0x0104:0x0106"],
"OnOffSwitch" : ["0x0104:0x0103", "0x0104:0x0000", "0x0104:0x0001", "0x0104:0x0107"],
"IASZone" : ["0x0104:0x0402"],
"BasicMonitoredDevice" : ["0x0104:0x0008"]
//,"OccupancySensor" : ["0x0104:0x0107"]
};
var drivers = {};
_.each(mappings, function(deviceIds, driverName) {
drivers[driverName] = require('./devices/' + driverName);
});
function createNinjaDevices(address, headers, zigbeeDevice, socket) {
var id = hex(headers.profileId) + ':' + hex(headers.deviceId);
var devices = [];
_.each(mappings, function(deviceIds, driverName) {
if (deviceIds.indexOf(id) > -1) {
var device = new drivers[driverName](address, headers, zigbeeDevice, socket, driverName);
device.driver = driverName;
device.address = address;
device.headers = headers;
device.zigbeeDevice = zigbeeDevice;
devices.push(device);
}
});
return devices;
}
function hex(v) {
v = '000' + v.toString(16);
return '0x' + v.substring(v.length-4);
}
/*
var BufferMaker = require('buffermaker');
var msg = new BufferMaker();
msg.UInt16LE(512);
console.log("XXX : " + msg.make().toJSON());
*/