This repository was archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (65 loc) · 1.92 KB
/
app.js
File metadata and controls
73 lines (65 loc) · 1.92 KB
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
var Gpio = require('onoff').Gpio;
var exec = require('child_process').exec;
var PIR_PIN = 18;
var motionSensor = new Gpio(PIR_PIN, 'in', 'both');
var dateLastSensed;
var timeoutDisplay;
var TIME_TO_TURN_SCREEN_OFF = 120;
motionSensor.watch(function (err, value) {
if (err) {
exit();
}
console.log('Detected at ' + (new Date()).toDateString());
if (shouldDisplayTurnOn()) {
turnDisplayOn();
if (timeoutDisplay) {
window.clearTimeout(timeoutDisplay);
}
timeoutDisplay = setTimeout(turnOffDisplayAndCloseBrowser, TIME_TO_TURN_SCREEN_OFF * 1000);
}
else {
if (timeoutDisplay) {
window.clearTimeout(timeoutDisplay);
}
timeoutDisplay = setTimeout(turnOffDisplayAndCloseBrowser, TIME_TO_TURN_SCREEN_OFF * 1000);
}
});
function shouldDisplayTurnOn() {
var date = new Date();
var turnDisplayOn = false;
if (dateLastSensed === null || dateLastSensed === undefined) {
dateLastSensed = date;
turnDisplayOn = true;
}
if (dateLastSensed - date > TIME_TO_TURN_SCREEN_OFF * 1000) {
turnDisplayOn = true;
}
dateLastSensed = date;
return turnDisplayOn;
}
function turnDisplayOn() {
exec('/opt/vc/bin/tvservice -p', puts);
// Wait 2 seconds to open browser
setTimeout(function () {
exec('chromium --kiosk --noerrdialogs --incognito /home/pi/mirror/MirrorPage.html', puts);
// Wait more to play audio
setTimeout(function () {
exec('omxplayer /home/pi/mirror/HelloIAmBaymax.mp3', puts);
}, 2500);
}, 2000);
}
function turnOffDisplayAndCloseBrowser() {
exec('pkill chromium', puts);
exec('/opt/vc/bin/tvservice -o', puts);
}
function puts(error, stdout, stderr) {
if (error) {
console.log(error);
}
console.log(stdout);
}
function exit() {
motionSensor.unexport();
process.exit();
}
process.on('SIGINT', exit);