-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
289 lines (241 loc) · 6.76 KB
/
app.js
File metadata and controls
289 lines (241 loc) · 6.76 KB
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
/**
* TODO
* - send previously discovered nodes to client
* - send device health status (i.e. whether online) to client
*/
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, io = require('socket.io');
var config = require('./config');
var Coordinator = require("./lib/coordinator").Coordinator;
var app = express();
app.locals.basepath = config.basePath; // set basepath variable
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
var server = http.createServer(app);
var sio = io.listen(server);
sio.set('log level', 2);
server.listen(app.get('port'), function(){
console.log('Zigbee manager server listening on port ' + app.get('port'));
});
// create new XBee HA coordinator
var coordinator = new Coordinator(config);
// handle ZB coordinator initialisation
coordinator.on("init", function() {
console.log("--- received init from coordinator");
coordinator.getStoredNodes(function(err, nodes) {
for (var i=0; i<nodes.length; i++) {
var node = nodes[i];
//console.log("got stored node: " + stringify(node));
sio.sockets.emit('node', node);
}
});
coordinator.getStoredDevices(function(err, devices) {
for (var i=0; i<devices.length; i++) {
var device = devices[i];
//console.log("got stored device: " + stringify(device));
sio.sockets.emit('device', device);
}
});
});
// handle node discovery
coordinator.on("node", function(node) {
sio.sockets.emit('node', node.toDesc());
sio.sockets.emit('lifecycle', { address64: node.remote64.hex, state: "alive" });
});
// handle devices/application-object discovery
coordinator.on("device", function(device) {
if (device) {
if (device.node) {
device = device.toSpec();
}
sio.sockets.emit('device', device);
}
else {
console.log("why no device/application object??");
}
});
coordinator.zbee.on("lifecycle", function(address64, state) {
sio.sockets.emit('lifecycle', {
address64 : address64,
state : state,
});
});
/**
* cluster message
*/
//coordinator.on("explicit", function(message) {
// sio.sockets.emit("explicit", message);
//});
/**
* Report on cluster attributes
*/
coordinator.on("attributeReport", function(message) {
sio.sockets.emit("attributeReport", message);
});
// handle a client socket connection
sio.sockets.on('connection', function (socket) {
console.log('A client socket connected!');
// send nodes to client
coordinator.getStoredNodes(function(err, nodes) {
for (var i=0; i<nodes.length; i++) {
var node = nodes[i];
socket.emit('node', node);
}
});
// send devices to client
coordinator.getStoredDevices(function(err, devices) {
for (var i=0; i<devices.length; i++) {
var device = devices[i];
socket.emit('device', device);
}
});
// listen for commands from the client
socket.on('command', function (cmd, data) {
console.log('received command: ', cmd, ', data: ', data);
switch (cmd) {
case "discover":
// Discover Zigbee nodes and Zigbee devices on the network.
// Not yet implemented properly
coordinator.discover();
break;
case "discoverNodeEndpoints":
// make a Zigbee devices notify itself
coordinator.discoverNodeEndpoints(data.address64);
break;
case "identify":
// make a Zigbee devices notify itself
coordinator.identifyDevice(data.address64, data.endpoint);
break;
case "configReporting":
// configure reporting of attributes to the local node
coordinator.configReporting(data.address64, data);
break;
case "addBinding":
// add binding between clusters on different ZigBee devices.
coordinator.addBinding(data);
break;
case "discoverAttributes":
// Find attributes on a particular cluster
coordinator.discoverAttributes(data.address64, data.endpoint, data.clusterId, data.start, data.max);
break;
case "configure":
// Configure the XBee for HA
coordinator.configure();
break;
case "save":
// Write XBee settings to flash
coordinator.save();
break;
case "reset":
// Reset XBee settings to factory default
coordinator.reset();
break;
case "join":
// Allow new nodes to join the network.
coordinator.allowJoin();
break;
case "leave":
// Make the Zbee leave the network
coordinator.leave();
break;
case "association":
// Check the network association state on the local Node.
coordinator.checkAssociation();
break;
case "test":
coordinator.test();
break;
case "queryAddresses":
coordinator.queryAddresses();
break;
}
});
// node setup
socket.on('node', function (nodeAddress, cmd, data) {
console.log('received node command: ' + nodeAddress + " : " + cmd + ' : ', data);
switch (cmd) {
// configure reporting for attribute (node,endpoint,cluster,attributeId)
case "report":
coordinator.configReporting();
// lookup node
// get cluster
var node = coordinator.getNode(nodeAddress);
if (node) {
console.log("got node, now getting cluster")
node.devices[endpoint];
node.getDevice(endpoint);
}
break;
case "unreport":
break;
case "bind":
var binding = data;
// var binding = {
// sourceAddress: byte[8]
// sourceEndpoint: int(1)
// clusterId: int(2)
// type: BindingType
// destAddress: byte[8]
// destEndpoint: int(1)
// };
coordinator.addBinding(binding);
//node.zdo.requestBind(binding);
break;
case "unbind":
break;
}
});
// node|endpoint|cluster : method,
socket.on('facet', function (nodeAddress, facet, method, args) {
console.log('received facet message: ', nodeAddress + ': ' + data);
switch (cmd) {
case "value":
break;
}
});
// listen for AT command from client
socket.on('at', function (command) {
console.log('I received a an AT command: ', command);
coordinator.at(command);
});
// handle client disconnection
socket.on('disconnect', function () {
sio.sockets.emit('client socket disconnected');
});
});
var stringify = function(o) {
var cache = [];
var s = JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null;
return s;
}