-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (64 loc) · 2.53 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
//Initialise required components
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
//Set up firebase function to allow emails to be sent
exports.sendEmail = functions.database.ref('serverData2/timeStamp')
.onWrite(event =>{
const time = event.data.val()
if(time > 5000){
console.log("LONG MOTION")
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'fit3140.team2424@gmail.com',
pass: 'WatchedPotsNeverBoil'
}
});
var mailOptions = {
from: 'fit3140.team2424@gmail.com',
to: 'fit3140.team2424@gmail.com',
subject: 'Long motion detected: firebase',
text: 'Dear user,' + "\n" + "\n" + 'A long motion was detected by firebase.' + "\n" + "\n" + "Regards, " + "\n" + "Management team."
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
console.log("An email to notify a long motion was detected, has been sent.")
}
})
//Set up email when motion change found
exports.sendEmailMotionChange = functions.database.ref('serverData2/timeStamp')
.onWrite(event =>{
const time = event.data.val()
return event.data.ref.parent.parent.once("value").then(snap => {
const post = snap.val()
const long = post.serverData3.longMotion
const short = post.serverData3.shortMotion
console.log("A change in motion was detected.")
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'fit3140.team2424@gmail.com',
pass: 'WatchedPotsNeverBoil'
}
});
var mailOptions = {
from: 'fit3140.team2424@gmail.com',
to: 'fit3140.team2424@gmail.com',
subject: 'Motion change detected: firebase',
text:'Dear user,' + "\n" + "\n" + 'A change in motion was detected by firebase. Here are the changes: ' + "\n" + "\n" + 'Number of long motions are: ' + long + "\n" + "Number of short motions are: " + short + "\n" + "\n" + "Regards, " + "\n" + "Management team."
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
console.log("An email to notify a change in motion was detected, has been sent.")
})
})