-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbdata.js
85 lines (72 loc) · 1.8 KB
/
dbdata.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
var g = require("./globals");
var os = require("os");
var fs = require('fs');
var Q = require("q");
function db() {
var self = this;
function makeSave() {
var obj = {};
obj.devices = self.devices;
obj.events = self.events;
return JSON.stringify(obj,null,4);
}
this.saveall = function() {
var str = makeSave();
return Q.nfcall(fs.writeFile,"devices_events.json",str,{"encoding":"ascii"});
}
this.savesync = function() {
var str = makeSave();
fs.writeFileSync("devices_events.json",str, {"encoding":"ascii"});
}
// loadDevices gets called first
this.loadDevicesEvents = function() {
return Q.nfcall(fs.readFile, "devices_events.json","ascii").
then(function (data) {
var obj = null;
try {
obj = JSON.parse(data);
self.devices = obj.devices;
self.events = obj.events;
for (var i in self.devices) {
var d = self.devices[i];
d.state = "none";
if (!d.id) d.id = d.name;
}
}
catch(e) {
console.log('exception loading data: ' + e);
process.exit(1);
}
process.on('exit', function(code) {
self.savesync();
console.log('About to exit with code:', code);
});
/*
process.on('SIGINT', function() {
console.log('Got SIGINT. Press Control-D to exit.');
process.exit(0);
});
*/
return self;
});
};
g.ti103initialize = function() {
return "192.168.0.110:2001";
};
// 9600
// ti103init: '/dev/ttyUSB0,{ "baudrate": 9600 }',
// acrf2init: "/dev/nul",
g.acrfinitialize = function() {
if (os.type().toLowerCase() == 'linux') {
console.log("acrfdev = " + g.argv.acrfdev);
return g.argv.acrfdev + ',{"baudrate":4800}';
}
else {
return 'COM7,{ "baudrate": 4800 }';
}
}
// these are numbered to run after the "base interface" initialization above
g.rcsx10binitialize1 = "ti103,K";
g.keypadinitialize1 = "ti103,H";
}
module.exports = new db();