From 911eb34abc586a2bb5895f9077d8718ab6ad73ae Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Fri, 7 Mar 2025 11:29:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E9=87=8F=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 12 ++++++++-- src/routes/measure.ts | 63 +++++++++++++++++++++++++++++++++++++++++++-------- src/types/wsTypes.ts | 20 ++++++++++++++-- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 080ca1c..800508f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import measureRouter from "./routes/measure"; // import { defaultStatus, StatusDatagram } from "./types/wsTypes"; import { wsSend } from "./utils/wss"; -import { ContextMessage, defaultContext } from "./types/wsTypes"; +import { ContextMessage, defaultContext, defaultMeasureState } from "./types/wsTypes"; const app = express(); app.use(express.static("public")); @@ -28,7 +28,6 @@ wss.on("connection", ws => { // ws.send("Welcome to the WebSocket server!"); // ws.on("message", message => { // console.log(`Received message: ${message}`); - // // 广播收到的消息给所有连接的客户端 // wss.clients.forEach(client => { // if (client.readyState === ws.OPEN) { // client.send(message.toString()); @@ -44,6 +43,14 @@ wss.on("connection", ws => { path: "/deviceContext", }) ); + // MeasureState + ws.send( + JSON.stringify({ + messageType: "EVENT", + data: app.locals["measure"], + path: "/measurement-task/get-task-state", + }) + ); ws.on("close", () => { console.log("Client disconnected"); }); @@ -51,6 +58,7 @@ wss.on("connection", ws => { app.locals["wss"] = wss; app.locals["context"] = defaultContext; +app.locals["measure"] = defaultMeasureState; // app.get("/", (req, res) => { // res.send("Hello World!"); diff --git a/src/routes/measure.ts b/src/routes/measure.ts index bb550ed..5d3e573 100644 --- a/src/routes/measure.ts +++ b/src/routes/measure.ts @@ -4,6 +4,7 @@ import { wsSend } from "../utils/wss"; const router = express.Router(); import points from "../utils/measure.json"; +import { MeasureState } from "../types/wsTypes"; let ptIndex = 0; let intervalId: ReturnType; @@ -18,6 +19,15 @@ router.post("/cache-measurement", async (req, res) => { // }, // }); // }, 2000); + + const measure: MeasureState["data"] = req.app.locals["measure"]; + measure.taskStatus = "IDLE"; + wsSend(req.app.locals["wss"], { + messageType: "STATE", + data: req.app.locals["measure"], + path: "/measurement-task/get-task-state", + }); + res.json({ status: 0 }); }); @@ -31,11 +41,27 @@ router.post("/start-measurement", (req, res) => { }, path: "/measurement-task/get-task-state", }); + + const measure: MeasureState["data"] = req.app.locals["measure"]; + measure.taskStatus = "MEASURING"; + wsSend(req.app.locals["wss"], { + messageType: "STATE", + data: req.app.locals["measure"], + path: "/measurement-task/get-task-state", + }); }); intervalId = setInterval(() => { if (ptIndex >= points.length) { clearInterval(intervalId); // ptIndex = 0; + const measure: MeasureState["data"] = req.app.locals["measure"]; + measure.taskStatus = "FINISHED"; + wsSend(req.app.locals["wss"], { + messageType: "STATE", + data: req.app.locals["measure"], + path: "/measurement-task/get-task-state", + }); + return; } wsSend(req.app.locals["wss"], { @@ -55,6 +81,22 @@ router.post("/stop-measurement", async (req, res) => { ptIndex = 0; clearInterval(intervalId); + wsSend(req.app.locals["wss"], { + messageType: "EVENT", + data: { + event: "END_RECORD_SIG", + }, + path: "/measurement-task/get-task-state", + }); + + const measure: MeasureState["data"] = req.app.locals["measure"]; + measure.taskStatus = "IDLE"; + wsSend(req.app.locals["wss"], { + messageType: "STATE", + data: req.app.locals["measure"], + path: "/measurement-task/get-task-state", + }); + res.json({ status: 0 }); }); router.post("/analyze-measurement", async (req, res) => { @@ -68,15 +110,18 @@ router.post("/analyze-measurement", async (req, res) => { // }, // }); // }, 2000); - - res.json({ status: 0, data: { - angles: [ - { x: 9.949007022412, y: -0.1650166186941, degree: 80, describe: "80°"}, - { x: 25.35, y: -2.184814802617, degree: 60, describe: "60°" }, - { x: -9.949007022412, y: -0.1650166186941, degree: 100, describe: "100°" }, - { x: -25.35, y: -2.184814802617, degree: 120, describe: "120°" }, - ] - }}); + + res.json({ + status: 0, + data: { + angles: [ + { x: 9.949007022412, y: -0.1650166186941, degree: 80, describe: "80°" }, + { x: 25.35, y: -2.184814802617, degree: 60, describe: "60°" }, + { x: -9.949007022412, y: -0.1650166186941, degree: 100, describe: "100°" }, + { x: -25.35, y: -2.184814802617, degree: 120, describe: "120°" }, + ], + }, + }); }); router.post("/save-report", async (req, res) => { diff --git a/src/types/wsTypes.ts b/src/types/wsTypes.ts index 452ecab..9f38594 100644 --- a/src/types/wsTypes.ts +++ b/src/types/wsTypes.ts @@ -46,8 +46,15 @@ export type ContextMessage = { path: "/deviceContext"; }; +export const taskStatusDescMap: { [k in MeasureState["data"]["taskStatus"]]: string } = { + IDLE: "空闲", + MEASURING: "测量中", + WAITING_FOR_MEASURING: "等待测量", + FINISHED: "测量完成", +}; + export type MeasureState = { - messageType: "EVENT"; + messageType: "STATE"; data: { taskStatus: "IDLE" | "MEASURING" | "WAITING_FOR_MEASURING" | "FINISHED"; measureSideCnt: 0 | 1 | 2; //已测量数量,0,1,2 最多两边(左边和右边) @@ -60,4 +67,13 @@ export type MeasureState = { path: "/measurement-task/get-task-state"; }; -export type Datagram = TrackRecordSig | TaskState | ContextMessage; +export const defaultMeasureState = { + taskStatus: "IDLE", + measureSideCnt: 0, //已测量数量,0,1,2 最多两边(左边和右边) + isMeasuringLeftEnd: false, //测量左侧完成 + isMeasuringRightEnd: false, //测量右侧完成 + motionlessSigFlag: true, //滑轮质心是否静止 + inStartMeasuringPos: true, //是否在允许开始测量的位置 +}; + +export type Datagram = TrackRecordSig | TaskState | ContextMessage | MeasureState;