diff --git a/src/index.ts b/src/index.ts index 8698308..8f309de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ import express from "express"; -import { Server } from "ws"; import http from "http"; import bodyParser from "body-parser"; import morgan from "morgan"; @@ -15,6 +14,7 @@ import measureAnalysisRouter from "./routes/measureAnalysis"; import measureDataRouter from "./routes/measureData"; import railRouter from "./routes/rail"; import calibrationRouter from "./routes/calibration"; +import mobileRouter from "./routes/mobile"; // import { defaultStatus, StatusDatagram } from "./types/wsTypes"; import { WsProxy } from "./utils/wss"; @@ -50,6 +50,8 @@ app.locals["measure"] = defaultMeasureState; // res.send("Hello World!"); // }); +app.use("/api/mobile", mobileRouter); + app.use("/api/cmd", cmdRouter); app.use("/api/auth", authRouter); app.use("/api/measurement-analysis", measureAnalysisRouter); diff --git a/src/routes/mobile.ts b/src/routes/mobile.ts new file mode 100644 index 0000000..85594d7 --- /dev/null +++ b/src/routes/mobile.ts @@ -0,0 +1,70 @@ +import express from "express"; +import { delay } from "../utils/helper"; +const router = express.Router(); + +import points from "../utils/measure.json"; +import { WsProxy } from "../utils/wss"; +let ptIndex = 0; +let intervalId1: ReturnType; +let intervalId2: ReturnType; + +router.post("/startMeasure", (req, res) => { + setTimeout(() => { + ptIndex = 0; + WsProxy.sendFunc({ + func: "measureTaskEvent", + data: { event: "START_RECORD_LEFT" }, + }); + }, 10); + intervalId1 = setInterval(() => { + if (ptIndex >= points.length / 2) { + clearInterval(intervalId1); + WsProxy.sendFunc({ + func: "measureTaskEvent", + data: { event: "FINISH_RECORD_LEFT" }, + }); + setTimeout(() => { + WsProxy.sendFunc({ + func: "measureTaskEvent", + data: { event: "START_RECORD_RIGHT" }, + }); + intervalId2 = setInterval(() => { + if (ptIndex >= points.length) { + clearInterval(intervalId2); + ptIndex = 0; + WsProxy.sendFunc({ + func: "measureTaskEvent", + data: { event: "FINISH_RECORD_RIGHT" }, + }); + return; + } + WsProxy.sendFunc({ + func: "measurePointEvent", + data: { + x: points[ptIndex].x, + y: points[ptIndex].y + 1, + side: "right", + }, + }); + ptIndex = ptIndex + 2; + }, 10); + }, 2000); + return; + } + // 从中心向左边画 + const tempIdx = Math.max(Math.floor(points.length / 2) - ptIndex, 0); + WsProxy.sendFunc({ + func: "measurePointEvent", + data: { + x: points[tempIdx].x, + y: points[tempIdx].y + 1, + side: "left", + }, + }); + ptIndex = ptIndex + 2; + }, 10); + + res.json({ success: true }); +}); + +export default router; diff --git a/src/utils/wss.ts b/src/utils/wss.ts index 4c35d11..5d9edd2 100644 --- a/src/utils/wss.ts +++ b/src/utils/wss.ts @@ -1,50 +1,65 @@ -import http from 'http'; -import { Server, WebSocket } from 'ws'; -import { Datagram } from '../types/wsTypes'; +import http from "http"; +import { Server, WebSocket } from "ws"; +import { Datagram } from "../types/wsTypes"; export class WsProxy { - public static server: Server; + public static server: Server; - private static actionsWhenClientConnect: ((ws: WebSocket) => void)[] = []; + private static actionsWhenClientConnect: ((ws: WebSocket) => void)[] = []; - static addActionForClientConnect(act: (ws: WebSocket) => void) { - this.actionsWhenClientConnect.push(act); - } - static init(server: http.Server) { - this.server = new Server({ server }); - this.onConnection(); - } + static addActionForClientConnect(act: (ws: WebSocket) => void) { + this.actionsWhenClientConnect.push(act); + } + static init(server: http.Server) { + this.server = new Server({ server }); + this.onConnection(); + } + + private static onConnection() { + this.server.on("connection", ws => { + console.log("Client connected"); + // 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()); + // } + // }); + // }); + this.actionsWhenClientConnect.forEach(act => act(ws)); - private static onConnection() { - this.server.on('connection', ws => { - console.log('Client connected'); - // 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()); - // } - // }); - // }); - this.actionsWhenClientConnect.forEach(act => act(ws)); + ws.on("close", () => { + console.log("Client disconnected"); + }); + }); + } - ws.on('close', () => { - console.log('Client disconnected'); - }); - }); - } + public static send(data: Datagram, ws?: WebSocket) { + // DeviceContext + if (ws) { + ws.send(JSON.stringify(data)); + } else { + this.server.clients.forEach(ws => { + if (ws.readyState === ws.OPEN) { + ws.send(JSON.stringify(data)); + } + }); + } + } - public static send(data: Datagram, ws?: WebSocket) { - // DeviceContext - if (ws) { - ws.send(JSON.stringify(data)); - } else { - this.server.clients.forEach(ws => { - if (ws.readyState === ws.OPEN) { - ws.send(JSON.stringify(data)); + public static sendFunc( + data: { func: string; data: Record | any[] }, + ws?: WebSocket + ) { + if (ws) { + ws.send(JSON.stringify(data)); + } else { + this.server.clients.forEach(ws => { + if (ws.readyState === ws.OPEN) { + ws.send(JSON.stringify(data)); + } + }); } - }); } - } }