From a7010092483445898d52cf09add5f6019fccb0ed Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Fri, 7 Mar 2025 10:35:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=88=86=E6=9E=90?= =?UTF-8?q?=E8=A7=92=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/auth.ts | 14 +++++++------- src/routes/measure.ts | 10 +++++++++- src/types/wsTypes.ts | 26 ++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 33e9131..7df3086 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -9,11 +9,11 @@ router.post("/login", async (req, res) => { loginFlag: true, loginUser: { id: 0, - account: "Demo", - nickname: "Demo", - password: "Demo", + account: "admin", + nickname: "admin", + password: "", userRole: "Admin", - isBuiltInUser: false, + isBuiltInUser: true, }, }; @@ -24,15 +24,15 @@ router.post("/login", async (req, res) => { }); await delay(200); - + res.json({ status: 0, data: { id: 3, //数据主键id createTime: "2025-03-03 17:51:13", //数据创建时间 updateTime: "2025-03-03 19:22:35", //数据更新时间 - account: "test001", //用户账户 - nickname: "测试账户001", //用户昵称 + account: "admin", //用户账户 + nickname: "admin", //用户昵称 password: null, usrRole: "Admin", //用户角色,可用值:User,Admin,Dev isBuiltInUser: false, //是否内置用户(内置用户不可删除) diff --git a/src/routes/measure.ts b/src/routes/measure.ts index 033a933..bb550ed 100644 --- a/src/routes/measure.ts +++ b/src/routes/measure.ts @@ -68,7 +68,15 @@ router.post("/analyze-measurement", async (req, res) => { // }, // }); // }, 2000); - res.json({ status: 0 }); + + 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 b8aec4b..452ecab 100644 --- a/src/types/wsTypes.ts +++ b/src/types/wsTypes.ts @@ -18,8 +18,15 @@ export type TrackRecordSig = { }; export const defaultContext: ContextMessage["data"] = { - loginFlag: false, - loginUser: {}, + loginFlag: true, + loginUser: { + id: 3, //数据主键id + account: "test001", //用户账户 + nickname: "测试账户001", //用户昵称 + userRole: "User", //用户角色,可用值:User,Admin,Dev + isBuiltInUser: false, //是否内置用户(内置用户不可删除) + }, + newMeasureAfterSave: false, }; export type ContextMessage = { @@ -34,8 +41,23 @@ export type ContextMessage = { userRole: "Admin" | "User" | "Dev"; isBuiltInUser: boolean; }>; + newMeasureAfterSave: boolean; }; path: "/deviceContext"; }; +export type MeasureState = { + messageType: "EVENT"; + data: { + taskStatus: "IDLE" | "MEASURING" | "WAITING_FOR_MEASURING" | "FINISHED"; + measureSideCnt: 0 | 1 | 2; //已测量数量,0,1,2 最多两边(左边和右边) + isMeasuringLeftEnd: boolean; //测量左侧完成 + isMeasuringRightEnd: boolean; //测量右侧完成 + motionlessSigFlag: boolean; //滑轮质心是否静止 + inStartMeasuringPos: boolean; //是否在允许开始测量的位置 + // profileRecordDescription: null; //用户填写的新测量信息 + }; + path: "/measurement-task/get-task-state"; +}; + export type Datagram = TrackRecordSig | TaskState | ContextMessage; From 911eb34abc586a2bb5895f9077d8718ab6ad73ae Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Fri, 7 Mar 2025 11:29:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E9=87=8F?= =?UTF-8?q?=E7=8A=B6=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;