-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageMailer2.js
288 lines (227 loc) · 7.11 KB
/
ImageMailer2.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
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
"use strict";
var http = require('http');
var request = require('request');
var mailer = require('nodemailer');
var fs = require('fs');
var transporter
var ImageMailerConfig
var EventSchedule
var LastInterval
function initializeMailer(service,username,password) {
transporter = mailer.createTransport({
service : service
, auth : {
user : username
, pass : password
}
})
}
function writeLogEntry(level,module,message) {
module = ( message === undefined) ? module : module + ": " + message
if ((ImageMailerConfig === undefined) || (level <= ImageMailerConfig.logLevel)) {
console.log(new Date().toISOString() + ": ImageManager." + module);
}
}
// Create a new object, that prototypally inherits from the Error constructor
function getImageError(requestOptions,error) {
return {
name : 'ImageError'
, message : 'Unexpected Error while Accessing Camera'
, details : requestOptions
, error : error
}
}
function processSnapshot(requestOptions, response, body, resolve, reject) {
var moduleId = 'processSnapshot()';
var responseDetails = {
module : moduleId
, requestOptions : requestOptions
, statusCode : response.statusCode
, statusText : http.STATUS_CODES[response.statusCode]
, contentType : response.headers["content-type"]
, headers : response.headers
, body : response.body
, elapsedTime : response.elapsedTime
}
if ((response.statusCode === 200) || (response.statusCode === 201)) {
resolve(response);
}
else {
writeLogEntry(3,moduleId,'Error. Status code = ' + sodaResponse.statusCode);
response.stack = new Error().stack
reject(getImageError(responseDetails));
}
}
function captureSnapshot(camera) {
var moduleId = 'captureSnapshot()';
var requestOptions = {
method : 'GET'
, uri : camera.hostname + camera.snapshotPath
, qs : camera.qs ? camera.qs : {}
, encoding : null
, time : true
};
switch (camera.authenticationMode) {
case 'basic':
requestOptions.headers = {"Authorization" : "Basic " + new Buffer(camera.username + ":" + camera.password).toString("base64")}
break;
case 'foscam' :
requestOptions.qs.usr = camera.username;
requestOptions.qs.pwd = camera.password;
break;
default:
}
return new Promise(function(resolve, reject) {
writeLogEntry(3,'Executing Promise');
request(requestOptions, function(error, response, body) {
if (error) {
reject(getImageError(requestOptions,error));
}
else {
processSnapshot(requestOptions, response, body, resolve, reject);
}
})
});
}
function mailSnapshot(data,sender,recipient,subject,filenamePrefix) {
var moduleId = "mailSnapshot()"
var timestamp = new Date();
var timestampSuffix = timestamp.toISOString()
timestampSuffix = timestampSuffix.replace(/-/g,'')
timestampSuffix = timestampSuffix.replace(/:/g,'')
timestampSuffix = timestampSuffix.replace(/\./g,'')
var mailOptions = {
from : sender
, to : recipient
, subject : subject
, text: 'Image Attached'
, attachments: [{
filename : filenamePrefix + '-' + timestampSuffix + '.jpg'
, content : data
, contentType : 'image/jpeg'
}]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if (error) {
writeLogEntry(0,moduleId,'Error: ' + error);
}
else {
writeLogEntry(0,moduleId,'Message sent. Status: ' + info.response);
}
})
}
var emailSnapshot = function () {
var moduleId = "emailSnapshot";
try {
const camera = ImageMailerConfig[ImageMailerConfig.activeCameras[0]]
captureSnapshot(camera).then(function(resp){
writeLogEntry(1,moduleId,"Bytes Recieved: " + resp.body.length);
mailSnapshot(resp.body,ImageMailerConfig.email.sender,ImageMailerConfig.email.recipient,ImageMailerConfig.email.subject,ImageMailerConfig.email.filenamePrefix)
}).catch(function(e) {
if (e instanceof Error) {
writeLogEntry(0,moduleId, e.stack)
}
else {
writeLogEntry(0,moduleId, "Error : " + JSON.stringify(e));
}
});
} catch (e) {
writeLogEntry(0,moduleId,"Uncaught Error");
writeLogEntry(0,moduleId,e.stack);
}
}
function processEvents(callback) {
var moduleId = "processEvents";
var now = new Date();
try {
if (EventSchedule.length > 0) {
var now = new Date();
while (EventSchedule[0] <= now) {
var nextEvent = EventSchedule.shift();
if (nextEvent > LastInterval) {
writeLogEntry(2,moduleId,"Processing Event Scheduled for " + nextEvent.toISOString());
callback();
}
else {
writeLogEntry(0,moduleId,"Skipping Event Scheduled for " + nextEvent.toISOString());
}
}
}
if (LastInterval.getHours() > now.getHours()) {
loadConfigurationFile();
}
} catch (e) {
writeLogEntry(0,moduleId,e.stack)
}
LastInterval = now;
}
function convertToDate(time) {
var moduleId = "convertToDate"
var hms = time.split(":");
while (hms.length < 2) {
hms.push("0");
}
var target = new Date()
target.setHours(parseInt(hms[0]));
target.setMinutes(parseInt(hms[1]));
target.setSeconds(0);
target.setMilliseconds(0);
return target;
}
function orderEvents(times) {
var moduleId = "orderEvents";
EventSchedule = new Array(times.length);
writeLogEntry(2,moduleId,'Scheduling ' + EventSchedule.length + " events.");
for (var i = 0; i < times.length; i++) {
EventSchedule[i] = convertToDate(times[i])
writeLogEntry(2,moduleId,"Event scheduled for " + EventSchedule[i].toISOString());
}
EventSchedule.sort(function(a,b) {return a-b});
}
function loadConfigurationFile() {
var moduleId = "loadConfigurationFile";
var configData = fs.readFileSync(__dirname + '/ImageMailer.json');
ImageMailerConfig = JSON.parse(configData);
LastInterval = new Date();
orderEvents(ImageMailerConfig.schedule.times);
}
function startIntervalTimer(callback) {
var moduleId = "startIntervalTimer";
var startTime = new Date();
startTime.setSeconds(0);
startTime.setMilliseconds(0);
startTime.setTime(startTime.getTime() + (60 * 1000));
var startTimeWait = startTime - (new Date()).getTime();
writeLogEntry(2,moduleId,"Starting Interval Timer in " + startTimeWait + "ms.")
setTimeout(
function() {
setInterval(processEvents,60000,callback)
writeLogEntry(0,moduleId,"Interval Timer Active.")
},
startTimeWait
)
}
function main() {
var moduleId = "main"
try {
loadConfigurationFile();
initializeMailer(ImageMailerConfig.email.service,ImageMailerConfig.email.sender,ImageMailerConfig.email.password);
startIntervalTimer(emailSnapshot);
} catch (e) {
writeLogEntry(0,moduleId,"Uncaught Error");
writeLogEntry(0,moduleId,e.stack);
}
}
function test() {
var moduleId = "test"
try {
loadConfigurationFile();
initializeMailer(ImageMailerConfig.email.service,ImageMailerConfig.email.sender,ImageMailerConfig.email.password);
emailSnapshot()
} catch (e) {
writeLogEntry(0,moduleId,"Uncaught Error");
writeLogEntry(0,moduleId,e.stack);
}
}
main();