-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
132 lines (124 loc) · 3.53 KB
/
node_helper.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
/* eslint-disable prettier/prettier */
const NodeWebcam = require( "node-webcam" );
const moment = require("moment");
const path = require("path");
const exec = require("child_process").exec;
var nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
var log = () => {
//do nothing
};
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function() {
this.devices = [];
this.device = false;
},
initialize: function(payload) {
this.config = payload;
if (payload.debug) {
log = (...args) => {
console.log("[SELFIE]", ...args);
};
}
if (payload.useWebEndpoint) {
log("Web server endpoint is activated:", `/${payload.useWebEndpoint} [POST]`);
this.expressApp.use(bodyParser.json());
this.expressApp.use(bodyParser.urlencoded({extended: true}));
this.expressApp.post("/" + payload.useWebEndpoint, (req, res) => {
log("External request arrives from", req.ip);
this.sendSocketNotification("WEB_REQUEST", req.body);
res.status(200).send({status: 200});
});
}
var Webcam = NodeWebcam.create({});
Webcam.list((list)=>{
log("Searching camera devices...");
if (!list || list.length <= 0) {
log ("Cannot find any camera in this computer.");
return;
}
this.devices.concat(list);
log("Detected devices:", list);
if (payload.device) {
var idx = list.indexOf(payload.device);
if (idx !== -1) {
this.device = list[idx];
log(`'${payload.device}' will be used.`);
} else {
log(`Cannot find '${payload.device}' in the list. '${list[0]}' be selected as default.`);
}
} else {
log(`Default camera '${list[0]}' will be used.`);
}
this.sendSocketNotification("INITIALIZED");
});
},
socketNotificationReceived: function(noti, payload) {
if (payload.debug) log("Notification received: " + noti);
if (noti == "INIT") {
this.initialize(payload);
}
if (noti == "SHOOT") {
console.log('shoot payload:', payload)
this.shoot(payload);
}
if (noti == "EMPTY") {
var dir = path.resolve(__dirname, "photos");
exec(`rm ${dir}/*.jpg`, (err, sto, ste)=>{
log("Cleaning directory:", dir);
if (err) this.log("Error:", err);
if (sto) this.log(sto);
if (ste) this.log(ste);
});
}
},
shoot: function(payload) {
var uri = moment().format("YYMMDD_HHmmss") + ".jpg";
var filename = path.resolve(__dirname, "photos", uri);
var opts = Object.assign ({
width: this.config.width,
height: this.config.height,
quality: this.config.quality,
delay: 0,
saveShots: true,
output: "jpeg",
device: this.device,
callbackReturn: "location",
verbose: this.config.debug
}, (payload.options) ? payload.options : {});
NodeWebcam.capture(filename, opts, (err, data)=>{
if (err) log("Error:", err);
log("Photo is taken:", data);
this.sendSocketNotification("SHOOT_RESULT", {
path: data,
uri: uri,
session: payload.session
});
this.sendMail(data);
});
},
sendMail: function(filepath) {
if (this.config.sendMail && typeof this.config.sendMail == "object") {
try {
var transport = nodemailer.createTransport(this.config.sendMail.transport);
var msg = Object.assign({}, this.config.sendMail.message, {attachments: [{path: filepath}]});
transport.sendMail(msg, (err)=>{
if (err) {
log("Failed to send mail.");
log("Error:", err);
return;
}
log("Email sent.");
return;
});
} catch (e) {
log("Invalid mail account configuration.");
log("Error:", e);
return;
}
} else {
return;
}
}
});