-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
91 lines (79 loc) · 2.64 KB
/
app.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
const TEN_SECONDS = 10000;
const CACHE_TIME = 86400000 * 30 // the time you want it to be cached in milliseconds
// Require express
const express = require('express');
const app = express();
// Additional required modules
const {
isRunning,
createFolderIfNotExists,
getImages,
generateThumbnails,
deleteImageAndThumbnail
} = require('./functions');
const path = require('path');
const shell = require('shelljs');
const { clearInterval } = require('timers');
// create /public/images path if it does not exist
createFolderIfNotExists();
// Set up the server
app.engine('.html', require('ejs').__express);
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public'), {
maxAge: CACHE_TIME
}));
// Without this you would need to
// supply the extension to res.render()
// ex: res.render('users.html').
app.set('view engine', 'html');
// Paths
app.get('/', function (req, res) {
generateThumbnails();
const birds = getImages();
res.render('index', { birds });
});
app.delete('/:id', function (req, res) {
const { id } = req.params;
deleteImageAndThumbnail(id);
res.send('ok');
});
// Background process
function backgroundProcess() {
// check if libcamera is running
isRunning('libcamera-detect', (running) => {
console.log(`libcamera-detect health check: ${running}`);
if (!running) {
console.log('Launching libcamera-detect process...');
// Run background process
const eventDate = new Date().toISOString().replace(/:/g, '-');
shell.exec(`libcamera-detect -t 0 -o ./public/images/bird-${eventDate}-%06d.jpg --lores-width 800 --lores-height 600 --post-process-file object_detect_tf.json --object bird`,
{ silent: true, async: true },
function (code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
});
console.log('Process launched.');
}
});
}
let detectLoop;
/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
// create a loop to check if libcamera is running
detectLoop = setInterval(backgroundProcess, TEN_SECONDS);
// run it on the first start of the app
backgroundProcess();
// log to console to let us know it's working
console.log('Express started on http://localhost:3000');
}
// Graceful shutdown
process.on('SIGINT', () => {
clearInterval(detectLoop);
process.exit();
});
process.on('SIGTERM', () => {
clearInterval(detectLoop);
process.exit();
});