-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
119 lines (99 loc) · 3.6 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
111
112
113
114
115
116
117
118
119
// server.js
//require("console-stamp")(console, "[HH:MM:ss.l]");
//require("dotenv").config();
const fs = require("fs");
const util = require("util");
const path = require("path");
const morgan = require("morgan");
const express = require("express");
const os = require("os");
const { readFile, unlink } = require("fs").promises;
const { spawn } = require("child_process");
var control = require("./app/hwcontrol");
var app = express();
var app2 = express();
const httpServer = require("http").createServer(app);
const httpServer2 = require("http").createServer(app2);
var DEBUG = process.env.DEBUG != "true" ? false : true;
var AUTOREBOOT = process.env.AUTOREBOOT != "true" ? false : true;
var RUN_DRY = false;
var PORT = process.env.CONTROL_PORT || 3009;
var PORT2 = process.env.XSERVER_PORT || 3005;
var TOKEN = process.env.CONTROL_TOKEN || undefined;
var DISPLAY_COUNT = process.env.BALENA_DISPLAY_COUNT || 1;
// configuration ===============================================================
console.log("[Server] Start API Server");
if (!TOKEN || TOKEN == "" || TOKEN == "null" || TOKEN == undefined) {
console.log("[Server] No Token Set, API is not secure!");
}
if (DEBUG) {
var accessLogStream = fs.createWriteStream(path.join(__dirname, "access.log"), {
flags: "a",
});
app.use(morgan("combined", { stream: accessLogStream }));
}
app.use(express.json({ limit: "20mb" }));
app.use(express.urlencoded({ extended: true, limit: "20mb" }));
// routes ======================================================================
require("./app/routes.js")(app); // load our routes
const delay = (time) => new Promise((res) => setTimeout(res, time));
app2.get("/", async (req, res) => {
console.log("[SCREENSHOT] Screenshot request received.");
const fileName = process.hrtime.bigint() + ".png";
const filePath = path.join(os.tmpdir(), fileName);
try {
const child = spawn("scrot", [filePath]);
const statusCode = await new Promise((res, rej) => {
child.on("close", res);
});
if (statusCode != 0) {
return res.status(500).send("command exited with non-zero return code.");
}
const fileContents = await readFile(filePath);
res.set("Content-Type", "image/png");
return res.status(200).send(fileContents);
} catch (e) {
console.log("[SCREENSHOT] ERROR:");
console.log(e.toString());
return res.status(500).send("Error occurred in screenshot code.");
} finally {
try {
await unlink(filePath);
} catch (e) {
console.log("[SCREENSHOT] ERROR:");
console.log(e);
}
}
});
httpServer.listen(PORT, () => {
console.log("[Server] http control api : " + httpServer.address().address + ":" + PORT);
});
httpServer2.listen(PORT2, () => {
console.log("[Server] http screenshot api : " + httpServer2.address().address + ":" + PORT2 + "/screenshot");
});
init();
async function init() {
await control.init();
let displayCount = (await control.getDisplayCount()).data;
if (displayCount < 1) {
if (AUTOREBOOT) {
console.log("[MAIN] no display found - Start projectors and reboot.");
await delay(15000);
await control.setBalenaWake();
} else {
console.log("[MAIN] INIT - no display found");
}
} else {
if (AUTOREBOOT) {
console.log("[MAIN] INIT - display Count: " + displayCount + " autoreboot enabled");
} else {
console.log("[MAIN] INIT - display Count: " + displayCount + " autoreboot disabled");
}
//todo: anyway turn on displays here and setup xserver again
}
console.log("[INIT] Balena Control is Running...");
}
process.on("SIGINT", () => {
console.log("Bye bye!");
process.exit();
});