diff --git a/src/index.ts b/src/index.ts index a1978a1..e754b03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ import measureDataRouter from "./routes/measureData" // 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")); @@ -29,7 +29,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()); @@ -45,6 +44,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"); }); @@ -52,6 +59,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/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..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,7 +110,18 @@ 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 56c656a..f100720 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,6 +41,7 @@ export type ContextMessage = { userRole: "Admin" | "User" | "Dev"; isBuiltInUser: boolean; }>; + newMeasureAfterSave: boolean; }; path: "/deviceContext"; }; @@ -49,4 +57,34 @@ export type ChannelMessage = { path: "/subdevice/uartchanel/get-channel-state"; }; -export type Datagram = TrackRecordSig | TaskState | ContextMessage | ChannelMessage; +export const taskStatusDescMap: { [k in MeasureState["data"]["taskStatus"]]: string } = { + IDLE: "空闲", + MEASURING: "测量中", + WAITING_FOR_MEASURING: "等待测量", + FINISHED: "测量完成", +}; + +export type MeasureState = { + messageType: "STATE"; + 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 const defaultMeasureState = { + taskStatus: "IDLE", + measureSideCnt: 0, //已测量数量,0,1,2 最多两边(左边和右边) + isMeasuringLeftEnd: false, //测量左侧完成 + isMeasuringRightEnd: false, //测量右侧完成 + motionlessSigFlag: true, //滑轮质心是否静止 + inStartMeasuringPos: true, //是否在允许开始测量的位置 +}; + +export type Datagram = TrackRecordSig | TaskState | ContextMessage | MeasureState | ChannelMessage;