-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpb-server.js
130 lines (111 loc) · 3.7 KB
/
pb-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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//--------------------------------------------------
// Photo-booth service
// by: Alexander Terczka <alex@mail.at>
//--------------------------------------------------
// --- settings
var slideshowdir = "images/slideshow/";
var inactive_slideshowdir = "images/inactive_slideshow/";
var port = 8082;
var use_gphoto2_as_webcam = true;
// --- requires
var fs = require('fs');
var express = require('express');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var app = express();
function read_slideshow_dir(dir,callback)
{
fs.readdir(dir,function (err,files) {
if (err) { throw new Error(err); }
callback(files);
});
}
//
// ---------- webcam process controll
// this process streams gphoto2 to webcam v4l2
// and is stopped for taking photos
//
var webcam_process=false;
var webcam_mode=false;
var last_attempt=0;
function start_webcam_mode()
{
if (!use_gphoto2_as_webcam) return;
webcam_mode=true;
keep_webcam_running();
}
function keep_webcam_running()
{
if (webcam_mode==false) return;
var now=new Date().getTime();
if (now-last_attempt<5000) // 5s
{ // try later again
setTimeout(keep_webcam_running,5000);
return;
}
last_attempt=now;
webcam_process=spawn("./start_webcam.sh",[],{ "stdio":"inherit" });
webcam_process.on("error",function (e) { console.log("Could not start webcam_process\n"); keep_webcam_running(); });
webcam_process.on("close",function (c) { console.log("Webcam_process terminated with "+c+"\n"); keep_webcam_running(); });
}
function stop_webcam_mode()
{
if (!use_gphoto2_as_webcam) return;
webcam_mode=false;
webcam_process=spawn("./stop_webcam.sh",[],{ "stdio":"inherit" });
}
//
// ---- API URLs
//
app.get("/capture",function(req,res) {
// Capture a photo using die capture shell script
// and send the name to the browser or error text
stop_webcam_mode();
setTimeout(function () {
console.log("Taking photo\n");
exec('./capture-photo.sh', function (error, stdout, stderr) {
if (error) res.send({error: "Capture Script Fehler"});
else if (stdout.trim()=="ERROR") res.send({error: "Kamera Fehler"});
else res.send({image:stdout.trim()});
start_webcam_mode();
});
},100);
});
app.get("/images.json",function(req,res) {
read_slideshow_dir(slideshowdir,function (data) { res.send(data); });
});
app.get("/inactive.images.json",function(req,res) {
read_slideshow_dir(inactive_slideshowdir,function (data) { res.send(data); });
});
app.get("/deactivate",function(req,res) {
var img=req.query.img;
if (img.match("^[a-zA-Z0-9][a-zA-Z.0-9_-]+$")==null) { res.send("invalid filename"); return; }
fs.renameSync(slideshowdir+img,inactive_slideshowdir+img);
res.send("OK");
});
app.get("/reactivate",function(req,res) {
var img=req.query.img;
if (img.match("^[a-zA-Z0-9][a-zA-Z.0-9_-]+$")==null) { res.send("invalid filename"); return; }
fs.renameSync(inactive_slideshowdir+img,slideshowdir+img);
res.send("OK");
});
//
// ---- Client Libraries
//
app.use("/jquery",express.static('node_modules/jquery/dist/'));
//
// ---- serve static files from "htdocs" files
//
app.get('/', function (req, res) { res.sendFile(__dirname+"/htdocs/index.html"); });
app.get('/admin', function (req, res) { res.sendFile(__dirname+"/htdocs/admin.html"); });
app.use(express.static('htdocs'));
app.use("/images",express.static('images/slideshow/'));
app.use("/inactive-images",express.static('images/inactive_slideshow/'));
//
// ---- Startup
//
//
// ---- GO!
//
app.listen(port, function () { console.log("Photo-booth service listening on port "+port+"!"); });
start_webcam_mode();