-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (68 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
66
67
68
69
70
71
72
73
74
const config = require("./config");
const fs = require("fs");
const moment = require("moment");
const Cayenne = require("cayennejs");
const nodemailer = require("nodemailer");
const Raspistill = require("node-raspistill").Raspistill;
const raspistill = new Raspistill(config.raspistill);
const SlackClient = require("node-slack-upload");
const slack = new SlackClient(config.slackToken);
const cayenneClient = new Cayenne.MQTT({
username: config.cayenne.username,
password: config.cayenne.password,
clientId: config.cayenne.clientId
});
const transporter = nodemailer.createTransport(config.nodemailer);
const fileName = `${config.raspistill.fileName}.${config.raspistill.encoding}`;
cayenneClient.connect((err, mqttClient) => {
cayenneClient.on("cmd1", function(data) {
console.info(
`Username ${data.meta.username} requested channel ${data.meta.channel}`
);
if (data.meta.username === config.cayenne.username) {
console.info("Taking a photo..");
raspistill.takePhoto().then(photo => {
console.info("Photo taken!");
if (config.sendEmail) {
console.info("Attaching to e-mail..");
config.mailOptions.attachments = [
{
filename: fileName,
content: fs.createReadStream(`./photos/${fileName}`)
}
];
console.info("Attached!");
console.info("Sending e-mail..");
transporter.sendMail(config.mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.info("Message sent: %s", info.messageId);
});
}
if (config.uploadFile) {
console.info("Uploading file..");
config.slackUploadConfig.file = fs.createReadStream(
`./photos/${fileName}`
);
config.slackUploadConfig.initialComment = `Date and time: ${moment().format(
"MMMM Do YYYY, h:mm:ss a"
)}`;
slack.uploadFile(config.slackUploadConfig, function(err, data) {
if (err) {
console.error(`Failed uploading file to Slack: ${err}`);
} else {
console.info("File uploaded!");
console.info(
`Public permalink (works only on public channels): ${data.file.permalink}`
);
console.info(
`Private permalink (works on all channels, but requires slack login and access): ${data.file.permalink_public}`
);
}
});
}
});
}
});
});