-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.js
More file actions
56 lines (53 loc) · 1.45 KB
/
device.js
File metadata and controls
56 lines (53 loc) · 1.45 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
var redis = require('redis');
var Device = function(publish_db,device_db) {
// parse port and host of device db
var store = redis.createClient(device_db.substring(device_db.indexOf(':')+1),device_db.substring(0,device_db.indexOf(':')));
var connected_devices = 0;
return {
connect: function(cb) {
// save device info to db
store.incr('connected_devices',function (err, reply) {
if ( err ) {
console.log("error: trying to update connected device counter");
cb(err);
}
else {
console.log("connected device# "+reply+" connected!");
connected_devices = did = reply;
var key = 'device:'+did;
store.set(key,publish_db,redis.print);
cb(null,did);
}
});
}
, disconnect: function(did) {
// store in redis hash
var key = 'device:'+did;
store.decr('connected_devices');
store.set(key,'-1',redis.print);
}
, devices: function() {
return connected_devices;
}
, quit: function() {
store.quit();
}
};
};
exports.Device = Device;
/*
var Dev = Device(1,process.env.DEVICE_PORT,process.env.DEVICE_HOST);
Dev.connect(function(err,did) {
if (err) return;
Dev.connect(function(err,did1) {
if (err) return;
Dev.connect(function(err,did2) {
if (err) return;
Dev.disconnect(did2);
Dev.disconnect(did1);
Dev.disconnect(did);
Dev.quit();
});
});
});
*/