From dd72766fb8459bc784f2de08d94737b0191c11f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B4=9B=E5=B8=8C=E9=9B=85?= Date: Tue, 7 May 2024 21:28:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A8=A1=E6=8B=9F=E5=99=A8?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/mqtt/dataLib.ts | 31 ++++++++++++++-- src/views/mqtt/index.vue | 74 ++++++++++++++++++++++++++++++++++----- 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/src/views/mqtt/dataLib.ts b/src/views/mqtt/dataLib.ts index e5b01a3..be1d0ef 100644 --- a/src/views/mqtt/dataLib.ts +++ b/src/views/mqtt/dataLib.ts @@ -1,4 +1,31 @@ -export type DataLibContent = Array> +export interface DriveStruct { + ptcType: 'motor' | 'non-motor' + ptcId: number + global_track_id: string + timeStamp: number + secMark: number + pos_lat: number + pos_lon: number + heading: number + source: number + width: number + length: number + height: number + speed: number + lane: number + x: number + y: number +} + +export type DeviceStruct = { + content: Array + dev_pos: { + lat: number + lon: number + } +} + +export type DataLibContent = Array> export interface DataLib { data: () => Promise @@ -10,7 +37,7 @@ async function importLib(url: string) { const url1 = `${baseUrl}/home${url}` const resp = await fetch(url1) if (!resp.ok) { - throw new Error(resp.text()) + throw new Error(await resp.text()) } return resp.json() } diff --git a/src/views/mqtt/index.vue b/src/views/mqtt/index.vue index 4a023d4..031e58e 100644 --- a/src/views/mqtt/index.vue +++ b/src/views/mqtt/index.vue @@ -19,7 +19,17 @@ const password = $toRef(mqttStore, 'password') const clientId = $toRef(mqttStore, 'clientId') let loading = $ref(false) -let status = $ref(false) +const publishing = reactive({ + status: false, + index: 0, + count: 1, + startTime: 0, + startTimestamp: 0, + lastTime: 0, + lastTimestamp: 0, + onlineCars: 0, + percentage: 0 +}) const active = $ref('CLC_track') function connect() { @@ -29,11 +39,11 @@ function connect() { clientId }) mqttClient.on('connect', (it) => { - status = true + publishing.status = true }) mqttClient.on('error', (it) => { - status = false + publishing.status = false console.error(it) }) } @@ -42,7 +52,7 @@ async function disconnect() { jobCancel.value?.() await mqttClient?.endAsync() mqttClient = null - status = false + publishing.status = false } async function publish() { @@ -50,6 +60,8 @@ async function publish() { try { loading = true jsonModule = await (dataLib[active] as DataLib).data() + loading = false + // @ts-ignore } catch (e: Error) { loading = false console.error(e) @@ -58,15 +70,33 @@ async function publish() { } const list = jsonModule let i = 0 - const next = () => list[(i = i++ % list.length)] + function next() { + i++ + if (i >= list.length) i = 0 + return list[i] + } ;(async () => { let exit = false jobCancel.value = () => { exit = true jobCancel.value = null + publishing.startTimestamp = 0 } + publishing.startTime = Date.now() + publishing.count = list.length while (!exit) { const item = next() + publishing.lastTime = Date.now() + if (item[0]?.content[0]) { + publishing.lastTimestamp = item[0].content[0].timeStamp + if (publishing.startTimestamp === 0) { + publishing.startTimestamp = publishing.lastTimestamp + } + } + publishing.onlineCars = item[0]?.content?.length ?? 0 + publishing.index = i + publishing.percentage = Number(((publishing.index / (publishing.count | 1)) * 100).toFixed(2)) + const json = JSON.stringify(item) mqttClient!.publish(topic, json, {}) @@ -77,7 +107,7 @@ async function publish() {