This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
80 lines (66 loc) · 1.83 KB
/
index.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
/**
* phant-monitor
* https://github.com/sparkfun/phant-monitor
*
* Copyright (c) 2014 SparkFun Electronics
* Licensed under the GPL v3 license.
*/
'use strict';
/**** Module dependencies ****/
var mailer = require('nodemailer'),
util = require('util'),
fs = require('fs');
/**** app prototype ****/
var app = Monitor.prototype;
/**** Expose Monitor ****/
exports = module.exports = Monitor;
function Monitor(options) {
if (! (this instanceof Monitor)) {
return new Monitor(options);
}
util._extend(this, options || {});
this.email_template = fs.readFileSync('./email_template', 'utf8');
// this sets up the smtp connection pool.
// better to do it for each message?
this.smtpTransport = mailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: 'gmail.user@gmail.com',
pass: 'userpass'
}
});
}
app.last_mailed = {};
app.run = function () {
setInterval(this.check.bind(this), this.interval);
};
app.check = function () {
var self = this;
this.metadata.all(function (err, streams) {
var now = new Date();
var then = null;
var age = self.age;
if(err || ! streams.length) {
return;
}
streams.forEach(function(stream) {
then = self.last_mailed[stream.id] || stream.last_push;
if ((now - then) > age) {
self.last_mailed[stream.id] = now;
console.log(util.format(self.email_template, stream.id));
// self.smtpTransport.sendMail({
// from: '',
// to: '',
// subject: '',
// text: util.format(self.email_template, stream.id)
// }, function(error, response){
// if (error) {
// console.log(error);
// } else {
// console.log("Message sent: " + response.message);
// }
// });
}
});
});
};