-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (140 loc) · 3.71 KB
/
server.js
File metadata and controls
164 lines (140 loc) · 3.71 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var spawn = require('child_process').spawn,
http = require('http'),
colors = require('colors'),
fs = require('fs'),
SerialPort = require('serialport').SerialPort;
var DEBUG_LEDSIGN = false;
var DEBUG_SAY = false;
var hosts = {
list: [],
sitesDown: [],
add: function(hostname, title) {
hosts.list.push({
hostname: hostname,
title: title,
isUp: null
});
},
checkIfUp: function () {
for (var index in hosts.list) {
(function(host) {
var request = http.request({
host: host.hostname,
headers: {'user-agent': 'Engage Web Pinger 1.0 (http://enga.ge)'},
method: 'HEAD',
port: 80,
path: '/',
agent: false
}, function(response) {
var statusCodeFirstChar = (typeof(response) != 'undefined') ? response.statusCode.toString().charAt(0) : null;
if (statusCodeFirstChar != '2' && statusCodeFirstChar != '3') {
console.log(('!!' + host.hostname + ' is down').red);
console.log("\t" + 'Status code: ' + response.statusCode);
if (host.isUp == true || host.isUp === null) {
hosts.siteDown(host);
}
host.isUp = false;
} else {
console.log(host.hostname + ' is up');
console.log("\t" + 'Status code: ' + response.statusCode);
if (host.isUp == false || host.isUp === null) {
hosts.siteUp(host);
}
host.isUp = true;
};
}).on('error', function (error) {
console.log(host.hostname + ' is down');
console.log("\t" + error);
if (host.isUp == true || host.isUp === null) {
hosts.siteDown(host);
}
host.isUp = false;
});
request.end();
})(hosts.list[index]);
}
setTimeout(hosts.checkIfUp, 15000);
},
siteUp: function (host) {
// Remove item from array
for (var i = 0; i < hosts.sitesDown.length; i++) {
if (hosts.sitesDown[i].hostname == host.hostname) {
hosts.sitesDown.splice(i, 1);
break;
}
}
if (hosts.sitesDown.length == 0 && host.isUp != null) {
say('Wooop! Everything\'s good! Just sit back and relax.');
sign.stop(false);
sign.walk(true);
}
},
siteDown: function (host) {
say('Warning! ' + host.title + ' is down!');
if (hosts.sitesDown.length <= 0) {
hosts.sitesDown.push(host);
sign.blinkStopSign();
sign.walk(false);
} else {
hosts.sitesDown.push(host);
}
}
}
var sign = {
stopStatus: null,
walkStatus: null,
blinkStopSign: function() {
if (sign.stopStatus === null) {
sign.stopStatus = false;
}
if (hosts.sitesDown.length > 0) {
sign.stop(!sign.stopStatus);
setTimeout(sign.blinkStopSign, 500);
} else {
sign.stop(false);
}
},
stop: function(turnOn) {
if (DEBUG_LEDSIGN) {
console.log('Turning stop sign ' + (turnOn ? 'on' : 'off'));
}
serialPort.write((turnOn ? 1 : 2) + "\n");
sign.stopStatus = turnOn;
},
walk: function(turnOn) {
if (DEBUG_LEDSIGN) {
console.log('Turning walk sign ' + (turnOn ? 'on' : 'off'));
}
serialPort.write((turnOn ? 3 : 4) + "\n");
sign.walkStatus = turnOn;
},
};
function say(str) {
if (DEBUG_SAY) {
console.log('Say: ' + str);
}
spawn('say', ['-v', 'Zarvox', '-r', 170, str]);
}
// Initialize serial port
var serialPort = new SerialPort('/dev/tty.usbserial-A800fcje', {
baudrate: 9600,
}).on('open', function () {
setTimeout(function () {
sign.walk(true);
hosts.checkIfUp();
}, 2000); // Give it a little bit of time to get ready
});
hosts.checkIfUp();
// Add hosts
fs.exists('./hosts.json', function (exists) {
if (!exists) {
console.log('!! Please rename hosts-sample.json to hosts.json and add the hosts that you want to monitor'.red);
process.exit(1);
} else {
var hostsList = require('./hosts.json');
for (var index in hostsList) {
var host = hostsList[index];
hosts.add(host.hostname, host.title);
}
}
});