forked from tomasholderness/PiThermServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
304 lines (263 loc) · 9 KB
/
server.js
File metadata and controls
304 lines (263 loc) · 9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// server.js - NodeJS server for the PiThermServer project.
/*
Parses data from DS18B20 temperature sensor and serves as a JSON object.
Uses node-static module to serve a plot of current temperature (uses highcharts).
Tom Holderness 03/01/2013
Ref: www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/
*/
// Load node modules
var fs = require('fs'),
sys = require('sys'),
http = require('http'),
sqlite3 = require('sqlite3'),
Sendgrid = require('sendgrid'),
nodestatic = require('node-static');
var sendgrid;
if (process.env.SENDGRID_USER && process.env.SENDGRID_PASSWORD) {
sendgrid = Sendgrid(process.env.SENDGRID_USER, process.env.SENDGRID_PASSWORD);
console.log('done');
}
// Setup static server for current directory
var staticServer = new nodestatic.Server(".");
// Setup database connection for logging
var db = new sqlite3.Database('./piTemps.db');
// default to 60 minute notification throttle
var notificationThrottle = process.env.NOTIFICATION_THROTTLE || 60;
var lastNotificationTime = 0;
function sendOkayNotification(record) {
var subject = 'Temperature is back within desired range';
var text = 'Current temperature is ' + record.celsius + ' C';
lastNotificationTime = 0;
sendNotification(subject, text);
}
function sendFailNotification(failedRecords) {
var text;
var subject;
var temp = failedRecords[failedRecords.length - 1].celsius + ' C';
if (failedRecords.length === 1) {
text = 'Last temperature reading was out of desired range: ' + temp;
subject = 'Temperature has gone out of desired range';
lastNotificationTime = Date.now();
} else {
text = 'Last ' + failedRecords.length + ' temperature readings were out of range \n' +
'Current temperature is ' + temp;
subject = 'Temperature is still out of desired range';
lastNotificationTime = Date.now();
}
lastNotificationTime = Date.now();
sendNotification(subject, text);
}
function sendNotification(subject, text) {
if (!sendgrid) {
console.log('Not sending notification: No sendgrid credentials');
return;
}
var toEmail = process.env.NOTIFICATION_TO_EMAIL;
var fromEmail = process.env.NOTIFICATION_FROM_EMAIL;
if (!toEmail || !fromEmail) {
console.log('Not sending notification: Notification email not specified');
return;
}
var email = new sendgrid.Email({
from: fromEmail,
subject: subject,
text: text
});
toEmail.split(',').forEach(email.addTo, email);
sendgrid.send(email, function(err, json) {
if (err) {
console.log('Sendgrid error: ' + JSON.stringify(err));
return;
}
console.log('Sent notification email to ' + toEmail);
});
}
// Write a single temperature record in JSON format to database table.
function insertTemp(data){
// data is a javascript object
var statement = db.prepare("INSERT INTO temperature_records VALUES (?, ?)");
// Insert values into prepared statement
statement.run(data.temperature_record[0].unix_time, data.temperature_record[0].celsius);
// Execute the statement
statement.finalize();
}
// Read current temperature from sensor
function readTemp(callback){
fs.readFile('/sys/bus/w1/devices/' + process.env.SENSOR_ID + '/w1_slave', function(err, buffer) {
if (err) {
console.error(err);
process.exit(1);
}
// Read data from file (using fast node ASCII encoding).
var data = buffer.toString('ascii').split(" "); // Split by space
// Extract temperature from string and divide by 1000 to give celsius
var temp = parseFloat(data[data.length-1].split("=")[1])/1000.0;
// Round to one decimal place
temp = Math.round(temp * 10) / 10;
// Add date/time to temperature
var data = {
temperature_record:[{
unix_time: Date.now(),
celsius: temp
}]};
// Execute call back with data
callback(data);
});
};
// Create a wrapper function which we'll use specifically for logging
function logTemp(interval) {
function readAndNotify() {
readTemp(function(data) {
insertTemp(data);
var record = data.temperature_record[0];
console.log(record.unix_time + ' ' + record.celsius);
if (record.celsius < process.env.RANGE_MIN || record.celsius > process.env.RANGE_MAX) {
selectRecentFailures(function(err, records) {
if (err) {
console.log('error: ' + JSON.stringify(err));
}
if (Date.now() > lastNotificationTime + notificationThrottle * 60 * 1000) {
sendFailNotification(records);
}
});
} else {
if (lastNotificationTime) {
sendOkayNotification(record);
}
}
});
}
readAndNotify(insertTemp);
setInterval(readAndNotify, interval, insertTemp);
};
function selectLastSuccessful(callback) {
db.all("SELECT * FROM temperature_records WHERE celsius >= ? AND celsius <= ? ORDER BY unix_time DESC LIMIT 1;",
process.env.RANGE_MIN, process.env.RANGE_MAX, function(err, rows) {
if (err) {
return callback(err);
} else if (rows.length === 0) {
return callback(null, null);
}
callback(null, rows[0]);
});
}
function selectSince(time, callback) {
db.all("SELECT * FROM temperature_records WHERE unix_time > ? ORDER BY unix_time ASC;",
time, function(err, rows) {
if (err) {
return callback(err);
}
callback(null, rows);
});
}
function selectRecentFailures(callback) {
selectLastSuccessful(function(err, record) {
if (err) {
return callback(err);
}
if (!record) {
return selectSince(0, callback);
}
selectSince(record.unix_time, callback);
});
}
// Get temperature records from database
function selectTemp(num_records, start_date, callback){
// - Num records is an SQL filter from latest record back trough time series,
// - start_date is the first date in the time-series required,
// - callback is the output function
db.all("SELECT * FROM (SELECT * FROM temperature_records WHERE unix_time > (strftime('%s',?)*1000) ORDER BY unix_time DESC LIMIT ?) ORDER BY unix_time;", start_date, num_records,
function(err, rows){
if (err){
response.writeHead(500, { "Content-type": "text/html" });
response.end(err + "\n");
console.log('Error serving querying database. ' + err);
return;
}
data = {temperature_record:[rows]}
callback(data);
});
};
// Setup node http server
var server = http.createServer(function(request, response) {
// Grab the URL requested by the client and parse any query options
var url = require('url').parse(request.url, true);
var pathfile = url.pathname;
var query = url.query;
// Test to see if it's a database query
if (pathfile == '/temperature_query.json'){
// Test to see if number of observations was specified as url query
if (query.num_obs){
var num_obs = parseInt(query.num_obs);
}
else{
// If not specified default to 20. Note use -1 in query string to get all.
var num_obs = -1;
}
if (query.start_date){
var start_date = query.start_date;
}
else{
var start_date = '1970-01-01T00:00';
}
// Send a message to console log
console.log('Database query request from '+ request.connection.remoteAddress +' for ' + num_obs + ' records from ' + start_date+'.');
// call selectTemp function to get data from database
selectTemp(num_obs, start_date, function(data){
response.writeHead(200, { "Content-type": "application/json" });
response.end(JSON.stringify(data), "ascii");
});
return;
}
// Test to see if it's a request for current temperature
if (pathfile == '/temperature_now.json') {
readTemp(function(data){
response.writeHead(200, { "Content-type": "application/json" });
response.end(JSON.stringify(data), "ascii");
});
return;
}
// Handler for favicon.ico requests
if (pathfile == '/favicon.ico'){
response.writeHead(200, {'Content-Type': 'image/x-icon'});
response.end();
// Optionally log favicon requests.
//console.log('favicon requested');
return;
} else {
// Print requested file to terminal
console.log('Request from '+ request.connection.remoteAddress +' for: ' + pathfile);
// Serve file using node-static
staticServer.serve(request, response, function (err, result) {
if (err) {
// Log the error
sys.error("Error serving " + request.url + " - " + err.message);
// Respond to the client
response.writeHead(err.status, err.headers);
response.end('Error 404 - file not found');
return;
}
return;
});
}
});
if (!process.env.SENSOR_ID) {
throw new Error('SENSOR_ID environment variable must be defined');
}
console.log('Temp Sensor: ' + process.env.SENSOR_ID);
// Start temperature logging (every 5 min).
var msecs = process.env.LOG_INTERVAL || (60 * 5) * 1000; // log interval duration in milliseconds
logTemp(msecs);
// Send a message to console
console.log('Server is logging to database at '+msecs+'ms intervals');
// Enable server
var httpPort = process.env.HTTP_PORT;
server.listen(httpPort)
server.on('error', function(err) {
console.log('ERROR: ' + JSON.stringify(err, null, ' '));
server.close();
process.exit(1);
});
server.on('listening', function() {
console.log('Server listening on localhost:' + httpPort);
});