CzRger 1 år sedan
förälder
incheckning
0218fee6fc
3 ändrade filer med 82 tillägg och 12 borttagningar
  1. 2 0
      package.json
  2. 2 2
      src/app.js
  3. 78 10
      src/ship-playback/routers/ws-ship-playback.js

+ 2 - 0
package.json

@@ -13,9 +13,11 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "@turf/turf": "^6.5.0",
     "body-parser": "^1.20.2",
     "connect-multiparty": "^2.2.0",
     "cors": "^2.8.5",
+    "elasticsearch": "^16.7.3",
     "express": "^4.18.2",
     "mycp": "^1.0.3",
     "mysql": "^2.18.1",

+ 2 - 2
src/app.js

@@ -32,5 +32,5 @@ app.listen(port, () => {
 // 引入ws模块
 const WebSocket = require('ws');
 // 创建WebSocket服务器
-const wsShipPlayback = require('./ship-playback/routers/ws-ship-playback');
-wsShipPlayback(new WebSocket.Server({ port: 18062 }))
+const {wsShipPlayback} = require('./ship-playback/routers/ws-ship-playback');
+wsShipPlayback(new WebSocket.Server({ port: 18062 }))

+ 78 - 10
src/ship-playback/routers/ws-ship-playback.js

@@ -1,18 +1,27 @@
 const {randomNum, YMDHms} = require("../../util");
+const turf = require('@turf/turf')
 
 const mockPoint = new Map()
 
 const wsShipPlayback = (WebSocket) => {
 // 服务器端监听连接事件
-  WebSocket.on('connection', (ws) => {
+  WebSocket.on('connection', (ws, req) => {
     console.log('18062:WebSocket连接成功');
-    // 监听客户端发送的消息
-    ws.on('message', (message) => {
-      const params = JSON.parse(message)
-      console.log(params)
-      mockPoint.clear()
-      sendResult(ws, params)
-    });
+    if (req.url === '/random-line') {
+      // 监听客户端发送的消息
+      ws.on('message', (message) => {
+        const params = JSON.parse(message)
+        console.log(params)
+        sendResultRandomLine(ws, params)
+      });
+    } else {
+      // 监听客户端发送的消息
+      ws.on('message', (message) => {
+        const params = JSON.parse(message)
+        console.log(params)
+        sendResult(ws, params)
+      });
+    }
     // 监听连接断开事件
     ws.on('close', () => {
       console.log('18062:WebSocket关闭成功');
@@ -25,6 +34,7 @@ const delayLoop = (s) => {
   });
 }
 const sendResult = async (ws, params) => {
+  mockPoint.clear()
   let result = []
   let step = 0
   for (let t = new Date(params.timeArea[0]).getTime(); t < new Date(params.timeArea[1]).getTime(); t += 1000) {
@@ -32,7 +42,7 @@ const sendResult = async (ws, params) => {
       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])
+        mockPoint.set(i, [mockPoint.get(i)[0] + 0.00001, mockPoint.get(i)[1] + 0.000005])
       }
       result.push({
         id: i,
@@ -53,4 +63,62 @@ const sendResult = async (ws, params) => {
     ws.send(JSON.stringify(result))
   }
 }
-module.exports = wsShipPlayback
+const sendResultRandomLine = async (ws, params) => {
+  mockPoint.clear()
+  let result = []
+  let step = 0
+  let lineCount = 0
+  for (let t = new Date(params.timeArea[0]).getTime(); t <= new Date(params.timeArea[1]).getTime(); t += 1000) {
+    const obj = {
+      timestamp: t,
+      lines: {
+        type: 'FeatureCollection',
+        features: []
+      },
+      points: {
+        type: 'FeatureCollection',
+        features: []
+      }
+    }
+    lineCount++
+    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), [Number(mockPoint.get(i)[mockPoint.get(i).length - 1][0]) + 0.001, Number(mockPoint.get(i)[mockPoint.get(i).length - 1][1]) + 0.0005]])
+      }
+      if (lineCount > 1) {
+        const lineGeo = turf.feature({
+          "type": "LineString",
+          "coordinates": mockPoint.get(i)
+        }, {
+          id: i,
+        })
+        obj.lines.features.push(lineGeo)
+      }
+      const pointGeo = turf.feature({
+        "type": "Point",
+        "coordinates": mockPoint.get(i)[mockPoint.get(i).length - 1]
+      }, {
+        id: i,
+        time: YMDHms(t),
+        cogs: randomNum(0, 360, 1)
+      })
+      obj.points.features.push(pointGeo)
+    }
+    result.push(obj)
+    step++
+    if (step === params.split) {
+      // await delayLoop(3)
+      ws.send(JSON.stringify(result))
+      result = []
+      step = 0
+    }
+  }
+  if (result.length > 0) {
+    ws.send(JSON.stringify(result))
+  }
+}
+module.exports = {
+  wsShipPlayback,
+}