Browse Source

船舶回放v1

CzRger 1 year ago
parent
commit
30ed494da5
1 changed files with 38 additions and 23 deletions
  1. 38 23
      src/ship-playback/routers/ws-ship-playback.js

+ 38 - 23
src/ship-playback/routers/ws-ship-playback.js

@@ -1,4 +1,7 @@
-const {randomNum} = require("../../util");
+const {randomNum, YMDHms} = require("../../util");
+
+const mockPoint = new Map()
+
 const wsShipPlayback = (WebSocket) => {
 // 服务器端监听连接事件
   WebSocket.on('connection', (ws) => {
@@ -6,14 +9,9 @@ const wsShipPlayback = (WebSocket) => {
     // 监听客户端发送的消息
     ws.on('message', (message) => {
       const params = JSON.parse(message)
-      const sendResult = () => {
-        const result = initPointsFeature(5000)
-        ws.send(JSON.stringify(result))
-      }
-      sendResult()
-      setInterval(() => {
-        sendResult()
-      }, 1000)
+      console.log(params)
+      mockPoint.clear()
+      sendResult(ws, params)
     });
     // 监听连接断开事件
     ws.on('close', () => {
@@ -21,21 +19,38 @@ const wsShipPlayback = (WebSocket) => {
     });
   });
 }
-
-const initPointsFeature = (total = 1) => {
-  const arr = []
-  for (let i = 0; i < total; i++) {
-    arr.push({
-      type: 'Feature',
-      geometry: {
-        type: 'Point',
-        coordinates: [randomNum(107, 112, 6), randomNum(17, 20, 6)]
-      },
-      properties: {
-        title: i
+const delayLoop = (s) => {
+  return new Promise(resolve => {
+    setTimeout(resolve, s * 1000);
+  });
+}
+const sendResult = async (ws, params) => {
+  let result = []
+  let step = 0
+  for (let t = new Date(params.timeArea[0]).getTime(); t < new Date(params.timeArea[1]).getTime(); t += 1000) {
+    for (let i = 0; i < params.total; i++) {
+      if (!mockPoint.has(i)) {
+        mockPoint.set(i, [randomNum(107.61380654131777, 108.17960243975527, 6), randomNum(18.069308855109945, 18.857577898078695, 6)])
+      } else {
+        mockPoint.set(i, [mockPoint.get(i)[0] + 0.001, mockPoint.get(i)[1] + 0.0005])
       }
-    })
+      result.push({
+        id: i,
+        wkt: `POINT(${mockPoint.get(i)[0]} ${mockPoint.get(i)[1]})`,
+        time: YMDHms(t),
+        cogs: randomNum(0, 360, 1)
+      })
+    }
+    step++
+    if (step === 3600) {
+      // await delayLoop(3)
+      ws.send(JSON.stringify(result))
+      result = []
+      step = 0
+    }
+  }
+  if (result.length > 0) {
+    ws.send(JSON.stringify(result))
   }
-  return arr
 }
 module.exports = wsShipPlayback