-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·142 lines (116 loc) · 3.1 KB
/
app.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
#!/usr/bin/env node
// TODO: save enabled state to disk so state is preserved over reboots
// TODO: get log time to be local, not UTC
var winston = require('winston');
var express = require('express');
var app = express();
var powerOn = false;
var enabled = true;
var pollingInterval = 1000; // ms
var port = 3000;
var pythonBinary = '/usr/bin/python';
var powerOnScript = '/home/pi/feeder/power-on.py';
var powerOffScript = '/home/pi/feeder/power-off.py';
winston.add(require('winston-daily-rotate-file'), {
filename: __dirname + '/feeder-log',
datePattern: '.yyyy-MM-dd',
timestamp: function() {
return new Date().toLocaleTimeString();
},
prepend: false,
json: false,
count: 7
});
app.get('/feed', function (req, res) {
res.send('Hello World!');
});
app.get('/feed/timestamp', function (req, res) {
winston.info('received timestamp request');
var now = new Date(Date.now()).toLocaleString();
res.send(now);
});
app.get('/feed/status', function (req, res) {
winston.info('received feed status request');
if (powerOn) {
res.send('1');
}
else {
res.send('0');
}
});
app.get('/feed/enabled', function (req, res) {
winston.info('received feed enabled request');
if (enabled) {
res.send('1');
}
else {
res.send('0');
}
});
app.get('/feed/enabled/on', function (req, res) {
winston.info('turning enabled on');
res.send('enabled');
enabled = true;
});
app.get('/feed/enabled/off', function (req, res) {
winston.info('turning enabled off');
res.send('disabled');
enabled = false;
power(false);
});
app.get('/feed/timer/:seconds', function (req, res) {
var seconds = req.params.seconds;
if (enabled) {
res.send('executing for ' + req.params.seconds + ' seconds');
winston.info('executing for ' + req.params.seconds + ' seconds');
var currentTime = timestamp();
var stopTime = currentTime + seconds * 1000;
setStopTime(stopTime);
}
else {
res.send('request denied -- currently disabled');
}
});
app.listen(port, function () {
winston.info('listening on port ' + port);
winston.info('initializing stopTime');
setStopTime(timestamp());
});
setInterval(function() {
checkStopTime();
}, pollingInterval);
var timestamp = function() {
return new Date().getTime();
}
var setStopTime = function(stamp) {
stopTime = stamp;
winston.info("set stop time: " + stamp);
}
var checkStopTime = function() {
var currentTime = timestamp();
winston.info('(current: ' + currentTime + ', stop: ' + stopTime + ', enabled: ' + enabled + ', power: ' + powerOn + ')');
if ((stopTime >= currentTime) && (enabled) && (!powerOn)) {
power(true);
}
if ((stopTime < currentTime) && (enabled) && (powerOn)) {
power(false);
}
}
var power = function(on) {
var script = '';
if (on) {
powerOn = true;
winston.info('sending ON signal');
script = powerOnScript;
}
else {
powerOn = false;
winston.info('sending OFF signal');
script = powerOffScript;
}
if (script !== '') {
var cmd = pythonBinary + " " + script;
var child = require('child_process').exec(cmd);
child.stdout.pipe(process.stdout);
}
}