暂停和倒带控制器

This commit is contained in:
洛洛希雅 2024-05-08 14:39:22 +08:00
parent 10a871fb20
commit 52bcd69f41

View File

@ -19,7 +19,7 @@ const password = $toRef(mqttStore, 'password')
const clientId = $toRef(mqttStore, 'clientId') const clientId = $toRef(mqttStore, 'clientId')
let loading = $ref(false) let loading = $ref(false)
const publishing = reactive({ const publish = reactive({
status: false, status: false,
index: 0, index: 0,
count: 1, count: 1,
@ -27,8 +27,17 @@ const publishing = reactive({
startTimestamp: 0, startTimestamp: 0,
lastTime: 0, lastTime: 0,
lastTimestamp: 0, lastTimestamp: 0,
lastRawTimestamp: 0,
timeOffset: 0,
timeTicking: 1,
onlineCars: 0, onlineCars: 0,
percentage: 0 percentage: 0,
pause: false,
rawTime: true,
range: {
start: 0,
end: 0
}
}) })
const active = $ref('CLC_track') const active = $ref('CLC_track')
@ -39,11 +48,11 @@ function connect() {
clientId clientId
}) })
mqttClient.on('connect', (it) => { mqttClient.on('connect', (it) => {
publishing.status = true publish.status = true
}) })
mqttClient.on('error', (it) => { mqttClient.on('error', (it) => {
publishing.status = false publish.status = false
console.error(it) console.error(it)
}) })
} }
@ -52,14 +61,14 @@ async function disconnect() {
jobCancel.value?.() jobCancel.value?.()
await mqttClient?.endAsync() await mqttClient?.endAsync()
mqttClient = null mqttClient = null
publishing.status = false publish.status = false
} }
onUnmounted(() => { onUnmounted(() => {
disconnect() disconnect()
}) })
async function publish() { async function doPublish() {
let jsonModule: DataLibContent let jsonModule: DataLibContent
try { try {
loading = true loading = true
@ -73,43 +82,74 @@ async function publish() {
return return
} }
const list = jsonModule const list = jsonModule
let i = 0 publish.index = 0
function next() { function next() {
i++ if (!publish.pause) {
if (i >= list.length) i = 0 publish.index += publish.timeTicking
return list[i] if (publish.index >= list.length) publish.index = 0
else if (publish.index < 0) publish.index = list.length - 1
}
return list[publish.index]
} }
let exit = false let exit = false
jobCancel.value = () => { jobCancel.value = () => {
exit = true exit = true
jobCancel.value = null jobCancel.value = null
publishing.startTimestamp = 0 publish.startTimestamp = 0
} }
publishing.startTime = Date.now() publish.startTime = Date.now()
publishing.count = list.length publish.count = list.length
publish.pause = false
publish.rawTime = true
// //
const interval = setInterval(() => { const interval = setInterval(() => {
if (exit) { if (exit) {
clearInterval(interval) clearInterval(interval)
return return
} }
const item = next() // if (publishing.pause) return
publishing.lastTime = Date.now() publish.lastTime = Date.now()
let item = next()
//
if (item[0]?.content[0]) { if (item[0]?.content[0]) {
publishing.lastTimestamp = item[0].content[0].timeStamp //
if (publishing.startTimestamp === 0) { publish.lastRawTimestamp = item[0].content[0].timeStamp
publishing.startTimestamp = publishing.lastTimestamp //
if (!publish.rawTime) {
if (publish.pause) publish.timeOffset += 100
else if (publish.timeTicking < 0) publish.timeOffset += 200
const offset = publish.timeOffset
publish.lastTimestamp = publish.lastRawTimestamp + publish.timeOffset
//
item = JSON.parse(JSON.stringify(item))
const list = item[0].content
for (const it of list) it.timeStamp = it.timeStamp + offset
} else {
publish.lastTimestamp = publish.lastRawTimestamp
}
if (publish.startTimestamp === 0) {
publish.startTimestamp = publish.lastTimestamp
} }
} }
publishing.onlineCars = item[0]?.content?.length ?? 0 publish.onlineCars = item[0]?.content?.length ?? 0
publishing.index = i publish.percentage = Number(((publish.index / (publish.count | 1)) * 100).toFixed(2))
publishing.percentage = Number(((publishing.index / (publishing.count | 1)) * 100).toFixed(2))
const json = JSON.stringify(item) const json = JSON.stringify(item)
mqttClient!.publish(topic, json, {}) mqttClient!.publish(topic, json, {})
}, 100) }, 100)
} }
function pause() {
publish.pause = true
publish.rawTime = false
}
function reverse() {
publish.timeTicking = -1
publish.rawTime = false
}
</script> </script>
<template> <template>
@ -152,31 +192,43 @@ async function publish() {
<el-input v-model="password" /> <el-input v-model="password" />
</el-form-item> </el-form-item>
<el-form-item v-if="jobCancel" label="状态"> <el-form-item v-if="jobCancel" label="状态">
<el-descriptions border direction="vertical"> <el-descriptions border direction="vertical" column="4">
<el-descriptions-item label="进度">{{ publishing.index }} / {{ publishing.count }}</el-descriptions-item> <el-descriptions-item label="进度">{{ publish.index }} / {{ publish.count }}</el-descriptions-item>
<el-descriptions-item label="运行时间"> <el-descriptions-item label="运行时间">
{{ (publishing.lastTime - publishing.startTime) / 1000 }} {{ (publish.lastTime - publish.startTime) / 1000 }}
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="数据运行时间"> <el-descriptions-item label="数据运行时间">
{{ (publishing.lastTimestamp - publishing.startTimestamp) / 1000 }} {{ (publish.lastTimestamp - publish.startTimestamp) / 1000 }}
</el-descriptions-item>
<el-descriptions-item label="原始时间">
<template v-if="publish.rawTime">正常</template>
<template v-else>已禁用</template>
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="数据起始时间戳"> <el-descriptions-item label="数据起始时间戳">
{{ publishing.startTimestamp }} {{ publish.startTimestamp }}
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="数据当前时间戳"> <el-descriptions-item label="数据当前时间戳">
{{ publishing.lastTimestamp }} {{ publish.lastTimestamp }}
</el-descriptions-item>
<el-descriptions-item label="数据原始时间戳">
{{ publish.lastRawTimestamp }}
</el-descriptions-item> </el-descriptions-item>
<el-descriptions-item label="活跃车辆"> <el-descriptions-item label="活跃车辆">
{{ publishing.onlineCars }} {{ publish.onlineCars }}
</el-descriptions-item> </el-descriptions-item>
</el-descriptions> </el-descriptions>
<el-progress flex-grow text-inside stroke-width="20" :percentage="publishing.percentage" /> <el-progress flex-grow text-inside :stroke-width="20" :percentage="publish.percentage" />
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button v-if="!publishing.status" type="primary" @click="connect">连接</el-button> <el-button v-if="!publish.status" type="primary" @click="connect">连接</el-button>
<el-button v-else type="danger" @click="disconnect">断开</el-button> <el-button v-else type="danger" @click="disconnect">断开</el-button>
<el-button v-if="!jobCancel" type="primary" :disabled="!publishing.status" @click="publish">发送</el-button> <el-button v-if="!jobCancel" type="primary" :disabled="!publish.status" @click="doPublish">发送</el-button>
<el-button v-else type="danger" :disabled="!publishing.status" @click="() => jobCancel?.()">停止</el-button> <template v-else>
<el-button type="danger" :disabled="!publish.status" @click="() => jobCancel?.()">停止</el-button>
<el-button v-if="!publish.pause" type="warning" @click="pause">暂停</el-button>
<el-button v-else type="success" @click="publish.pause = false">继续</el-button>
<el-button type="danger" @mousedown="reverse" @mouseup="publish.timeTicking = 1">倒带</el-button>
</template>
</el-form-item> </el-form-item>
</el-form> </el-form>
</el-main> </el-main>