diff --git a/package-lock.json b/package-lock.json index ecbd0dd..dace99a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,11 +11,13 @@ "dependencies": { "body-parser": "^2.1.0", "express": "^5.0.1", + "morgan": "^1.10.0", "ws": "^8.18.1" }, "devDependencies": { "@types/body-parser": "^1.19.5", "@types/express": "^5.0.0", + "@types/morgan": "^1.9.9", "@types/node": "^22.13.4", "@types/ws": "^8.5.14", "nodemon": "^3.1.9", @@ -153,6 +155,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/morgan": { + "version": "1.9.9", + "resolved": "https://registry.npmmirror.com/@types/morgan/-/morgan-1.9.9.tgz", + "integrity": "sha512-iRYSDKVaC6FkGSpEVVIvrRGw0DfJMiQzIn3qr2G5B3C//AWkulhXgaBd7tS9/J79GWSYMTHGs7PfI5b3Y8m+RQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/node": { "version": "22.13.4", "resolved": "https://registry.npmmirror.com/@types/node/-/node-22.13.4.tgz", @@ -277,6 +289,24 @@ "dev": true, "license": "MIT" }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/binary-extensions": { "version": "2.3.0", "resolved": "https://registry.npmmirror.com/binary-extensions/-/binary-extensions-2.3.0.tgz", @@ -1026,6 +1056,49 @@ "node": "*" } }, + "node_modules/morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmmirror.com/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "license": "MIT", + "dependencies": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/morgan/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmmirror.com/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/morgan/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmmirror.com/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/morgan/node_modules/on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", @@ -1129,6 +1202,15 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/on-headers/-/on-headers-1.0.2.tgz", + "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmmirror.com/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index 30585ec..db1ecb2 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,13 @@ "dependencies": { "body-parser": "^2.1.0", "express": "^5.0.1", + "morgan": "^1.10.0", "ws": "^8.18.1" }, "devDependencies": { "@types/body-parser": "^1.19.5", "@types/express": "^5.0.0", + "@types/morgan": "^1.9.9", "@types/node": "^22.13.4", "@types/ws": "^8.5.14", "nodemon": "^3.1.9", diff --git a/src/index.ts b/src/index.ts index 448abf6..080ca1c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,10 +2,12 @@ import express from "express"; import { Server } from "ws"; import http from "http"; import bodyParser from "body-parser"; +import morgan from 'morgan'; import cmdRouter from "./routes/cmd"; import debugRouter from "./routes/debug"; import authRouter from "./routes/auth"; +import measureRouter from "./routes/measure"; // import { defaultStatus, StatusDatagram } from "./types/wsTypes"; import { wsSend } from "./utils/wss"; @@ -15,6 +17,7 @@ const app = express(); app.use(express.static("public")); app.use(bodyParser.urlencoded()); app.use(bodyParser.json()); +app.use(morgan('combined')); const server = http.createServer(app); // 在HTTP服务器上初始化WebSocket服务器 @@ -45,9 +48,6 @@ wss.on("connection", ws => { console.log("Client disconnected"); }); }); -function getCurrContext() { - return app.locals["context"] as ContextMessage["data"]; -} app.locals["wss"] = wss; app.locals["context"] = defaultContext; @@ -58,6 +58,7 @@ app.locals["context"] = defaultContext; app.use("/api/debug", debugRouter); app.use("/api/cmd", cmdRouter); app.use("/auth", authRouter); +app.use("/measurement-task", measureRouter); //@ts-ignore app.use((err, req, res, next) => { diff --git a/src/routes/measure.ts b/src/routes/measure.ts new file mode 100644 index 0000000..033a933 --- /dev/null +++ b/src/routes/measure.ts @@ -0,0 +1,87 @@ +import express from "express"; +import { delay } from "../utils/helper"; +import { wsSend } from "../utils/wss"; +const router = express.Router(); + +import points from "../utils/measure.json"; +let ptIndex = 0; +let intervalId: ReturnType; + +router.post("/cache-measurement", async (req, res) => { + await delay(100); + // setTimeout(() => { + // wsSend(req.app.locals["wss"], { + // type: "cmd", + // data: { + // commandId: req.body.commandId, + // status: "D0000", + // }, + // }); + // }, 2000); + res.json({ status: 0 }); +}); + +router.post("/start-measurement", (req, res) => { + setTimeout(() => { + ptIndex = 0; + wsSend(req.app.locals["wss"], { + messageType: "EVENT", + data: { + event: "START_RECORD_SIG", + }, + path: "/measurement-task/get-task-state", + }); + }); + intervalId = setInterval(() => { + if (ptIndex >= points.length) { + clearInterval(intervalId); + // ptIndex = 0; + return; + } + wsSend(req.app.locals["wss"], { + messageType: "EVENT", + data: { + x: points[ptIndex].x, + y: points[ptIndex].y, + }, + path: "/measurement-task/profile-record-ctrl-sig", + }); + ptIndex = ptIndex + 2; + }, 10); + + res.json({ status: 0 }); +}); +router.post("/stop-measurement", async (req, res) => { + ptIndex = 0; + clearInterval(intervalId); + + res.json({ status: 0 }); +}); +router.post("/analyze-measurement", async (req, res) => { + await delay(100); + // setTimeout(() => { + // wsSend(req.app.locals["wss"], { + // type: "cmd", + // data: { + // commandId: req.body.commandId, + // status: "D0000", + // }, + // }); + // }, 2000); + res.json({ status: 0 }); +}); + +router.post("/save-report", async (req, res) => { + await delay(100); + // setTimeout(() => { + // wsSend(req.app.locals["wss"], { + // type: "cmd", + // data: { + // commandId: req.body.commandId, + // status: "D0000", + // }, + // }); + // }, 2000); + res.json({ status: 0 }); +}); +export default router;