112 lines
4.2 KiB
JavaScript
112 lines
4.2 KiB
JavaScript
const http = require("http");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
const DB = path.join(__dirname, "data.json");
|
|
|
|
const CAMS = [
|
|
{ id: "cam1", name: "Вход · Камера 1" },
|
|
{ id: "cam2", name: "Приёмная · Камера 2" },
|
|
{ id: "cam3", name: "Парковка · Камера 3" },
|
|
{ id: "cam4", name: "Коридор · Камера 4" }
|
|
];
|
|
|
|
const DEFAULT_DATA = { incidents: [], log: [] };
|
|
|
|
function load() {
|
|
try { return { ...DEFAULT_DATA, ...JSON.parse(fs.readFileSync(DB, "utf8")) }; }
|
|
catch (_) { return { ...DEFAULT_DATA }; }
|
|
}
|
|
function save(d) { fs.writeFileSync(DB, JSON.stringify(d, null, 2)); }
|
|
|
|
function body(req) {
|
|
return new Promise((resolve) => {
|
|
let s = "";
|
|
req.on("data", (c) => (s += c));
|
|
req.on("end", () => { try { resolve(JSON.parse(s || "{}")); } catch (_) { resolve({}); } });
|
|
});
|
|
}
|
|
|
|
function readReq() {
|
|
const u = new URL(req.url, "http://x");
|
|
return u.searchParams;
|
|
}
|
|
|
|
const server = http.createServer(async (req, res) => {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
if (req.method === "OPTIONS") { res.writeHead(204); return res.end(); }
|
|
|
|
const url = req.url.split("?")[0];
|
|
|
|
if (req.method === "GET" && url === "/api/cams") {
|
|
const data = load();
|
|
const byCam = {};
|
|
data.incidents.forEach((i) => { if (!byCam[i.camId]) byCam[i.camId] = []; byCam[i.camId].push(i); });
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(CAMS.map((c) => {
|
|
const list = (byCam[c.id] || []).slice().reverse().filter(i => i.status !== "dismissed");
|
|
return { ...c, events: list };
|
|
})));
|
|
}
|
|
|
|
if (req.method === "GET" && url === "/api/incidents") {
|
|
const data = load();
|
|
data.incidents.sort((a, b) => b.at.localeCompare(a.at));
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(data.incidents));
|
|
}
|
|
|
|
if (req.method === "POST" && url === "/api/incidents") {
|
|
const it = await body(req);
|
|
const data = load();
|
|
const rec = {
|
|
id: "INC-" + (data.incidents.length + 1),
|
|
camId: String(it.camId || "cam1"),
|
|
type: String(it.type || "conflict"),
|
|
level: it.level === "high" ? "high" : "medium",
|
|
at: new Date().toISOString(),
|
|
status: "new",
|
|
note: String(it.note || "")
|
|
};
|
|
data.incidents.push(rec);
|
|
data.log.push({ when: Date.now(), what: "новый инцидент " + rec.id + " на " + rec.camId, who: "система" });
|
|
save(data);
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(rec));
|
|
}
|
|
|
|
if (req.method === "POST" && url === "/api/incidents/status") {
|
|
const it = await body(req);
|
|
const data = load();
|
|
const rec = data.incidents.find((i) => i.id === String(it.id));
|
|
if (rec) {
|
|
rec.status = it.status === "ok" ? "ok" : "dismissed";
|
|
data.log.push({ when: Date.now(), what: "инцидент " + rec.id + " → " + rec.status, who: "оператор" });
|
|
save(data);
|
|
}
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(rec || { ok: false }));
|
|
}
|
|
|
|
if (req.method === "GET" && url === "/api/log") {
|
|
const data = load();
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
return res.end(JSON.stringify(data.log.slice(-50).reverse()));
|
|
}
|
|
|
|
let file = req.url === "/" ? "/index.html" : req.url.split("?")[0];
|
|
const full = path.join(__dirname, file);
|
|
if (full.startsWith(__dirname) && fs.existsSync(full) && fs.statSync(full).isFile()) {
|
|
const ext = path.extname(full);
|
|
const types = { ".css": "text/css", ".js": "application/javascript", ".html": "text/html", ".svg": "image/svg+xml", ".png": "image/png", ".woff2": "font/woff2", ".json": "application/json" };
|
|
res.writeHead(200, { "Content-Type": (types[ext] || "text/plain") + "; charset=utf-8" });
|
|
return res.end(fs.readFileSync(full));
|
|
}
|
|
res.writeHead(404); res.end("Not found");
|
|
});
|
|
|
|
server.listen(PORT, () => console.log("VisionGuard server on port " + PORT));
|