forked from anthonywebb/node-cbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·105 lines (93 loc) · 3.21 KB
/
server.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
var _ = require('underscore');
CONFIG = require('./config');
////////////////////////
// LOAD THE CONNECTORS
////////////////////////
if(CONFIG.cgate){
console.log('Initializing the cgate connector');
CBUS = require('./cgate').init();
}
//////////////////////////
// GLOBAL HELPER METHODS
//////////////////////////
COMMON = require('./common');
// TODO: should check and see if this file exists first, and if not, create it
DB = require('./db.json');
////////////////////////
// WEBSERVER SETUP
////////////////////////
var express = require('express');
var app = express();
var server = require('http').Server(app);
IO = require('socket.io')(server);
app.use('/',express.static(__dirname + '/public'));
server.listen(CONFIG.webserver.port,CONFIG.webserver.host);
console.log('Console on: http://'+CONFIG.webserver.host+':'+CONFIG.webserver.port+'/console.html');
// HTTP ROUTES
app.get('/cgate', function (req, res) {
if (req.query.cmd) {
var command = req.query.cmd.trim() + '\r';
console.log('remoteCommand : ' + command);
CBUS.write(command);
res.json({ status: 'ok', executed: req.query.cmd});
}
else {
res.json({ status: 'error', message: 'you must specify a command'},400);
}
});
app.get('/cmd', function (req, res) {
console.log(req.query);
var commandArray = [{type:'lighting',group:req.query.device,level:parseInt(req.query.level),delay:parseInt(req.query.delay),timeout:parseInt(req.query.timeout)}];
COMMON.doCommands(commandArray);
res.json(JSON.stringify({ status: 'ok'}));
});
app.get('/locations', function (req, res) {
// loop over the keys and build the response (an array of objects)
var resp = _.uniq(_.pluck(COMMON.deviceObjToArray(DB.devices), 'location'));
resp = _.sortBy(resp, function(str){ return str; });
res.json(resp);
});
app.get('/scenes', function (req, res) {
res.json(DB.scenes);
});
app.get('/devices', function (req, res) {
res.json(COMMON.deviceObjToArray(DB.devices));
});
/////////////////////////////
// SCHEDULED TASKS
/////////////////////////////
var cronjobs = {};
var cronjob = require('cron').CronJob;
// load up everything currently in the DB
_.each(DB.tasks,function(task){
// only create cron jobs for tasks that are enabled
if (task.enabled) {
addCronJob(task.id,task.cronstring,task.expression,task.commands,CONFIG.location.timezone);
}
})
function addCronJob(id,cronstring,expression,commands,timezone){
try {
console.log('Adding cronjob task: '+id);
cronjobs[id] = new cronjob(cronstring, function(){
console.log('Starting Cronjob Task: '+id);
// some of these tasks may have a conditional expression, some can just run
if(expression){
if(eval(expression)) {
COMMON.doCommands(commands);
}
}
else {
COMMON.doCommands(commands);
}
}, function(){console.log('Cronjob stopped: '+id)}, true, timezone);
} catch(ex) {
console.log("cronstring pattern not valid for "+id+": "+cronstring);
}
}
function deleteCronJob(id) {
console.log('Killing cronjob '+id);
if(cronjobs[id]){
cronjobs[id].stop();
}
delete cronjobs[id];
}