-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (92 loc) · 2.49 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
var express = require('express')
, logger = require('morgan')
, app = express()
, homepage = require('jade').compileFile(__dirname + '/source/templates/homepage.jade')
, addcomputer = require('jade').compileFile(__dirname + '/source/templates/addcomputer.jade')
, fs = require('fs')
, wol = require('node-wol')
, file = "/static/computers.json"
, computers = ''
app.use(logger('dev'))
app.use(express.static(__dirname + '/static'))
function getComputers() {
var filepath = __dirname + file;
console.log('computers file: ' + filepath);
if (fs.existsSync(filepath)) {
var data = fs.readFileSync(filepath, 'utf8');
if(data) {
computers = JSON.parse(data);
}
}
return computers;
}
app.get('/', function (req, res, next) {
try {
var html = homepage({ computers: getComputers() })
res.send(html)
}
catch (ex) {
next(ex)
}
})
app.get('/add', function (req, res, next) {
try
{
var html = addcomputer()
res.send(html)
}
catch(ex) {
next(ex)
}
})
app.get('/addcomputer', function (req, res, next) {
try
{
var filepath = __dirname + file;
if (fs.existsSync(filepath)) {
console.log('Hostname: ' + req.query.hostname +
' \n macaddress: ' + req.query.macaddress +
' \n group:' + req.query.group);
// computers[req.params];
// fs.writeFileSync(filepath, computers, 'utf8');
var html = homepage({ computers: getComputers() })
res.send(html)
}
}
catch(ex) {
next(ex)
}
})
app.get('/wake', function (req, res, next) {
try
{
console.log('Waking up: ' + req.query.id)
wol.wake( req.query.id, function(error) {
if(error) {
console.log('Error waking up: ' + req.query.id + ' - ' + error);
return;
}
});
var html = homepage({ computers: computers })
res.send(html)
}
catch(ex) {
next(ex)
}
})
app.get('/wakeall', function (req, res, next) {
for (var comp of getComputers()) {
console.log('Waking up: ' + comp.macaddress)
wol.wake(comp.macaddress, function(error) {
if(error) {
console.log('Error waking up: ' + comp.macaddress + ' - ' + error);
return;
}
});
var html = homepage({ computers: computers })
res.send(html)
}
})
app.listen(process.env.PORT || 3002, function () {
console.log('Listening on http://localhost:' + (process.env.PORT || 3002))
})