-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CameraServer.js
62 lines (54 loc) · 2.37 KB
/
CameraServer.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
"use strict";
let ScreenshotHelper = require("./ScreenshotHelper");
let http = require("http");
let mjpegServer = require("mjpeg-server");
module.exports = CameraServer;
function CameraServer(hap, conf, log) {
this.hap = hap;
this.log = log;
this.conf = conf;
this.screenshotHelper = new ScreenshotHelper(log, this.conf.url, this.conf.chromiumPath, this.conf.ignoreHTTPSErrors, this.conf.jsFile);
let networkTimeout = this.conf.timeout || 10000;
let renderTimeout = this.conf.renderTimeout || 1;
this.screenshotHelper.getPage(1920, 1080, networkTimeout, renderTimeout).then(page => {
this.page = page;
});
this.openRequests = [];
http.createServer((req, res) => {
if (req.url.endsWith("still")) {
this.log.debug("Still image request for " + this.conf.url);
this.screenshotHelper.getScreenshot(1920, 1080, networkTimeout, renderTimeout).then(img => {
const openRequest = mjpegServer.createReqHandler(req, res);
openRequest.write(img);
});
} else {
const pos = this.openRequests.length;
this.log.debug("Image Stream opened for client " + pos + " for " + this.conf.url);
this.openRequests[pos] = mjpegServer.createReqHandler(req, res);
res.on("close", ()=>{
this.log.debug("Image Stream closed for client " + pos + " for " + this.conf.url);
this.openRequests.splice(pos, 1);
})
}
}).listen(this.conf.livePort || 8554);
setInterval(() => {
if (this.openRequests.length > 0 && !!this.page) {
this.screenshotHelper.makeScreenshot(this.page, false).then(img => {
for (let i = 0; i < this.openRequests.length; i++) {
const openRequest = this.openRequests[i];
openRequest.write(img);
}
});
}
}, (this.conf.liveSnapshotInterval || 1000));
this.log(this.conf.liveRefreshInterval)
if (this.conf.liveRefreshInterval > 0) {
setInterval(() => {
if (this.openRequests.length > 0 && !!this.page) {
this.log.debug("Refreshing page");
let networkTimeout = this.conf.timeout || 10000;
this.screenshotHelper.refresh(this.page, networkTimeout);
}
}, (this.conf.liveRefreshInterval));
}
}