-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
74 lines (60 loc) · 1.81 KB
/
server.ts
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
'use strict';
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var fs = require('fs');
import { PiClock, LightState } from './pi-clock';
var shell = require('shelljs');
let piClock = new PiClock();
piClock.start();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('app'));
app.use('/bower_components', express.static('bower_components'));
let getConfig = function(){
return JSON.parse(fs.readFileSync('./config/config.json'));
};
app.get('/config', function(req, res) {
return res.send(getConfig());
});
app.get('/configreset', function(req, res){
// fs.createReadStream('./config/default.json').pipe(fs.createWriteStream('./config/config.json'));
fs.writeFileSync('./config/config.json', fs.readFileSync('./config/default.json'));
piClock.start();
return res.send(getConfig());
});
app.post('/config', function(req, res) {
let config = req.body;
fs.writeFileSync('./config/config.json', JSON.stringify(config, null, 4));
piClock.start();
return res.send('Success!');
});
app.get('/downloadLog', function(req, res) {
return res.send(fs.readFileSync('./out-0.log'));
});
app.get('/lightState', function(req, res) {
let color = '#505050';
switch(piClock.lightState) {
case LightState.wake:
color = '#00FF00';
break;
case LightState.sleep:
color = '#FF0000';
break;
}
return res.send({"background-color": `${color}`});
})
app.get('/shutdown', function(req, res) {
piClock.dispose();
shell.exec('/usr/bin/sudo /sbin/poweroff');
});
app.listen(3000, function() {
console.log('pi-clock SERVER started');
});
process.on('SIGINT', function() {
piClock.dispose();
process.exit();
})
export {PiClock} from './pi-clock';