Browse Source

添加mobile路由

master
zhangjiming 4 months ago
parent
commit
15a8c3a726
  1. 4
      src/index.ts
  2. 70
      src/routes/mobile.ts
  3. 95
      src/utils/wss.ts

4
src/index.ts

@ -1,5 +1,4 @@
import express from "express"; import express from "express";
import { Server } from "ws";
import http from "http"; import http from "http";
import bodyParser from "body-parser"; import bodyParser from "body-parser";
import morgan from "morgan"; import morgan from "morgan";
@ -15,6 +14,7 @@ import measureAnalysisRouter from "./routes/measureAnalysis";
import measureDataRouter from "./routes/measureData"; import measureDataRouter from "./routes/measureData";
import railRouter from "./routes/rail"; import railRouter from "./routes/rail";
import calibrationRouter from "./routes/calibration"; import calibrationRouter from "./routes/calibration";
import mobileRouter from "./routes/mobile";
// import { defaultStatus, StatusDatagram } from "./types/wsTypes"; // import { defaultStatus, StatusDatagram } from "./types/wsTypes";
import { WsProxy } from "./utils/wss"; import { WsProxy } from "./utils/wss";
@ -50,6 +50,8 @@ app.locals["measure"] = defaultMeasureState;
// res.send("Hello World!"); // res.send("Hello World!");
// }); // });
app.use("/api/mobile", mobileRouter);
app.use("/api/cmd", cmdRouter); app.use("/api/cmd", cmdRouter);
app.use("/api/auth", authRouter); app.use("/api/auth", authRouter);
app.use("/api/measurement-analysis", measureAnalysisRouter); app.use("/api/measurement-analysis", measureAnalysisRouter);

70
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<typeof setInterval>;
let intervalId2: ReturnType<typeof setInterval>;
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;

95
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 { 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<string, any> | 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));
}
});
} }
});
} }
}
} }
Loading…
Cancel
Save