openCountdown/index.js

160 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-02-23 21:55:13 +01:00
const express = require("express");
const fs = require("fs");
const bodyParser = require("body-parser");
const app = express();
app.use(express.static("static"));
app.use(express.static("node_modules"));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true,
})
);
currentState = {
mode: "clock",
2022-02-24 19:11:13 +01:00
countdownGoal: new Date().getTime(),
showMilliSeconds: true,
2022-02-24 20:17:24 +01:00
defaultFullScreen: true,
timeAmountInital: 0,
timerRunState: true,
2022-02-24 21:08:31 +01:00
pauseMoment: 0,
showTimeOnCountdown: true,
message: "",
showMessage: false,
2022-02-24 22:18:21 +01:00
messageAppearTime: 0,
showProgressbar: true,
2022-02-25 17:22:59 +01:00
colorSegments: {20000: "#FFAE00", 5000: "#ff0000", "START": "yellow"},
textColors: {},
srvTime: 0,
enableColoredText: true,
debug: false
2022-02-23 21:55:13 +01:00
};
2022-02-25 17:22:59 +01:00
currentState.textColors = currentState.colorSegments
2022-02-23 21:55:13 +01:00
app.get("/", function (req, res) {
2022-02-25 22:24:45 +01:00
const data = fs.readFileSync("templates/newAdminPanel.html", "utf8");
res.send(data);
});
app.get("/old", function (req, res) {
2022-02-23 21:55:13 +01:00
const data = fs.readFileSync("templates/adminPanel.html", "utf8");
res.send(data);
});
2022-02-25 22:24:45 +01:00
2022-02-23 21:55:13 +01:00
app.get("/timer", function (req, res) {
const data = fs.readFileSync("templates/timerPage.html", "utf8");
res.send(data);
});
app.get("/api/v1/data", function (req, res) {
2022-02-25 17:22:59 +01:00
currentState.srvTime = new Date().getTime()
2022-02-23 21:55:13 +01:00
res.json(currentState);
});
app.get("/api/v1/set/mode", function (req, res) {
currentState.mode = req.query.mode;
res.json({ status: "ok" });
});
2022-02-25 17:22:59 +01:00
app.get("/api/v1/set/layout/showMillis", function (req, res) {
2022-02-24 19:11:13 +01:00
currentState.showMilliSeconds = (req.query.show === 'true');
res.json({ status: "ok" });
});
2022-02-23 22:17:12 +01:00
app.get("/api/v1/set/timerGoal", function (req, res) {
2022-02-25 17:22:59 +01:00
currentState.countdownGoal = new Date(parseInt(req.query.time)).getTime(); // ToDO error handling
2022-02-23 21:55:13 +01:00
res.json({ status: "ok" });
});
2022-02-24 19:11:13 +01:00
app.get("/api/v1/set/addMillisToTimer", function (req, res) {
2022-02-24 20:17:24 +01:00
currentState.timeAmountInital = req.query.time;
2022-02-24 19:11:13 +01:00
currentState.countdownGoal = new Date().getTime() + parseInt(req.query.time)
res.json({ status: "ok" });
2022-02-24 21:08:31 +01:00
});
2022-02-23 21:55:13 +01:00
2022-02-24 20:17:24 +01:00
app.get("/api/v1/ctrl/timer/pause", function (req, res) {
currentState.timerRunState = false;
currentState.pauseMoment = new Date().getTime();
res.json({ status: "ok" });
});
app.get("/api/v1/ctrl/timer/play", function (req, res) {
2022-02-26 23:22:53 +01:00
if(currentState.timerRunState == false){
currentState.timerRunState = true
currentState.countdownGoal += new Date().getTime() - currentState.pauseMoment;
}
2022-02-24 20:17:24 +01:00
res.json({ status: "ok" });
});
app.get("/api/v1/ctrl/timer/restart", function (req, res) {
currentState.countdownGoal = new Date().getTime() + parseInt(currentState.timeAmountInital)
res.json({ status: "ok" });
});
2022-02-25 17:22:59 +01:00
app.get("/api/v1/set/layout/showTime", function (req, res) {
2022-02-24 21:08:31 +01:00
currentState.showTimeOnCountdown = (req.query.show === 'true');
res.json({ status: "ok" });
});
2022-02-25 17:22:59 +01:00
app.get("/api/v1/set/progressbar/show", function (req, res) {
2022-02-24 22:18:21 +01:00
currentState.showProgressbar = (req.query.show === 'true');
res.json({ status: "ok" });
});
2022-02-25 17:22:59 +01:00
app.get("/api/v1/set/progressbar/colors", function (req, res) {
try {
currentState.colorSegments = JSON.parse(req.query.colors);
res.json({ status: "ok" });
} catch (error) {
res.json({ status: "error", message: error });
}
});
app.get("/api/v1/set/text/colors", function (req, res) {
try {
if(req.query.copy === "true"){
currentState.textColors = currentState.colorSegments;
} else {
currentState.textColors = JSON.parse(req.query.colors);
}
res.json({ status: "ok" });
} catch (error) {
res.json({ status: "error", message: error });
}
});
app.get("/api/v1/set/text/enableColoring", function (req, res) {
currentState.enableColoredText = (req.query.enable === 'true');
});
2022-02-24 22:18:21 +01:00
2022-02-24 21:08:31 +01:00
app.get("/api/v1/ctrl/message/show", function (req, res) {
currentState.message = req.query.msg
currentState.showMessage = true
currentState.messageAppearTime = new Date().getTime()
res.json({ status: "ok" });
});
2022-02-25 17:22:59 +01:00
app.get("/api/v1/debug", function (req, res) {
currentState.debug = (req.query.enable === 'true');
res.json({ status: "ok" });
});
2022-02-24 21:08:31 +01:00
app.get("/api/v1/ctrl/message/hide", function (req, res) {
currentState.showMessage = false
res.json({ status: "ok" });
});
2022-02-23 22:17:12 +01:00
app.listen(3005);
console.log("Server running on port 3005");