修改队列和启动重连

This commit is contained in:
2026-06-01 15:51:12 +08:00
parent 562881cc6c
commit 92165c914a
3 changed files with 73 additions and 39 deletions
+42 -2
View File
@@ -25,13 +25,50 @@ const PRIORITY = {
const rs485Queue = []
let isRS485Sending = false
const RS485_SEND_DELAY = 200 // 每条指令间隔200ms,确保总线空闲
const MAX_QUEUE_SIZE = 3 // 队列最大长度
/**
* 将任务按优先级插入队列
* 高优先级(0)插入到队列前面低优先级任务之前
* 低优先级(1)追加到队列末尾
* 规则:
* 1. 队列最大长度为3
* 2. 重复指令(相同cmd)不入队,直接丢弃
* 3. 队列满时,高优先级任务挤掉低优先级任务,否则踢掉最老的(队首)
*/
const enqueueTask = (task) => {
// 规则1: 去重 - 已存在相同cmd的任务则丢弃
const isDuplicate = rs485Queue.some(t => t.cmd === task.cmd)
if (isDuplicate) {
console.warn('[RS485队列] 丢弃重复任务:', task.cmd)
return false
}
// 规则2: 队列满了,先腾位置
if (rs485Queue.length >= MAX_QUEUE_SIZE) {
if (task.priority === PRIORITY.HIGH) {
// 高优先级:优先挤掉队列中最后一个低优先级任务
let lastLowIndex = -1
for (let i = rs485Queue.length - 1; i >= 0; i--) {
if (rs485Queue[i].priority === PRIORITY.LOW) {
lastLowIndex = i
break
}
}
if (lastLowIndex !== -1) {
console.warn('[RS485队列] 高优挤掉低优:', rs485Queue[lastLowIndex].cmd)
rs485Queue.splice(lastLowIndex, 1)
} else {
// 没有低优先级可挤,踢掉队首最老的
console.warn('[RS485队列] 满,踢掉队首:', rs485Queue[0].cmd)
rs485Queue.shift()
}
} else {
// 低优先级:直接踢掉队首最老的
console.warn('[RS485队列] 满,踢掉队首:', rs485Queue[0].cmd)
rs485Queue.shift()
}
}
// 规则3: 按优先级插入
if (task.priority === PRIORITY.HIGH) {
let insertIndex = rs485Queue.findIndex(t => t.priority === PRIORITY.LOW)
if (insertIndex === -1) {
@@ -42,6 +79,9 @@ const enqueueTask = (task) => {
} else {
rs485Queue.push(task)
}
console.log(`[RS485队列] 入队成功, 长度:${rs485Queue.length}, cmd:${task.cmd}`)
return true
}
const processRS485Queue = async () => {