-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (105 loc) · 3.51 KB
/
server.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
const fs = require('fs');
const http = require('http');
const shell = require('shelljs');
var RES;
// User-specific variables:
const user = "pi";
var shutdownpassword = "";
var normalDays = {
list : ["Mon", "Tue", "Wed", "Thu", "Fri"],
startHour : 17,
startMin : 0,
endHour : 20,
endMin : 0
};
var otherDays = {
list : ["Sat"],
startHour : 8,
startMin : 30,
endHour : 15,
endMin : 30
}
var lightMode = "incandescent"; // Use "fluorescent", "incandescent", or "sun", depending on lighting conditions.
var time = {
hour : parseInt(Date.call().slice(16,18)),
minute : parseInt(Date.call().slice(19,21)),
get : function() {
return 60*parseInt(Date.call().slice(16,18)) + parseInt(Date.call().slice(19,21));
},
a : function(obj) {
return (60*obj.startHour + obj.startMin);
},
b : function(obj) {
return (60*obj.endHour + obj.endMin);
},
name : function() {
return (Date.call().slice(4, 21)).replaceAll(" ","_");
}
}
function photo(fileName = "") {
if (fileName == "") {
shell.exec("raspistill -o /home/" + user + "/Desktop/Timelapse/" + time.name() + ".jpg -awb " + lightMode);
} else {
shell.exec("raspistill -o /home/" + user + "/Desktop/Timelapse/" + fileName + ".jpg -awb " + lightMode);
}
}
var running = setInterval(function() {
if ((normalDays.list.includes(Date.call().slice(0,3)) && time.get() >= time.a(normalDays) && time.get() <= time.b(normalDays)) || (otherDays.list.includes(Date.call().slice(0,3)) && time.get() >= time.a(otherDays) && time.get() <= time.b(otherDays))) {
photo();
}
}, 60000);
http.createServer(function (req, res) {
var dir = req.url;
if (dir == "/live.jpg") {
RES = res;
shell.exec("raspistill -o /home/" + user + "/Desktop/Timelapse/" + fileName + ".jpg -awb " + lightMode);
setTimeout(function() {
fs.readFile("/home/" + user + "/Desktop/Timelapse/live.jpg", function (err, data) {
if (err) {
RES.writeHead(204);
RES.end();
} else {
RES.writeHead(200);
RES.end(data);
}
});
}, 3000);
} else {
if (dir.indexOf("/home") != -1) {
dir = "/home/" + user + "/Raspberry-Pi-Time-Lapse/index.html";
} else {
dir = "/home/" + user + "/Desktop/Timelapse" + dir;
}
fs.readFile(dir, function (err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end("<body style='text-align:center;background-color:black'><h1 style='color:cyan'><b>404</b></h1><h2 style='color:red'>File not found.</h2><h1 style='font-size:200px;color:white'>):</h1></body>");
} else {
res.writeHead(200);
res.end(data);
}
});
}
if (req.url.indexOf('?') != -1) {
if (req.url.toLowerCase().indexOf("?stop") != -1) {
clearInterval(running);
}
if (req.url.toLowerCase().indexOf("?start") != -1) {
clearInterval(running);
running = setInterval(function() {
if ((normalDays.list.includes(Date.call().slice(0,3)) && time.get() > time.a(normalDays) && time.get() < time.b(normalDays)) || (otherDays.list.includes(Date.call().slice(0,3)) && time.get() > time.a(otherDays) && time.get() < time.b(otherDays))) {
photo();
}
}, 60000);
}
if (req.url.toLowerCase().indexOf("?shutdown"+shutdownpassword) != -1) {
shell.exec("sudo shutdown -h now");
}
if (req.url.toLowerCase().indexOf("?reboot") != -1) {
shell.exec("sudo reboot");
}
if (req.url.toLowerCase().indexOf("?pic") != -1) {
photo();
}
}
}).listen(8000);