-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.js
47 lines (37 loc) · 1.1 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
var server = require('devify-server').CoapBroker;
// The email utility
var email = require('./utilities/email');
// Sendgrid credentials.
var fs = require('fs');
var credentials = JSON.parse(fs.readFileSync(__dirname + '/config.json'));
// Last time we have sent an email.
var last = 0;
var onmessage = function(message) {
// Parse strings to JSON object.
var obj = JSON.parse(message.data);
// Put a timestamp in the message
var now = Math.floor(Date.now() / 1000);
obj.timestamp = now;
// Don't send too many emails.
if (now - last < 60)
// We have sent an email in 60 seconds.
return;
email({
credentials: credentials,
from: 'jollen <[email protected]>',
to: 'jollen <[email protected]>',
replyTo: 'jollen <[email protected]>',
subject: 'ESP8266 Air Quality',
text: JSON.stringify(message.data),
success: function(message) {
last = now;
console.log('Email sent: ' + last);
},
error: function(err) {
console.log('Email failed: ' + err);
}
});
};
server.start({
onmessage: onmessage,
});