From 3cbea81b9a25fb23bf1e1f6a790dd098ab246388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=AD=A3=E6=98=93?= Date: Thu, 15 May 2025 17:56:20 +0800 Subject: [PATCH] 123 --- src/utils/mqtt/emqxUtil.js | 4 +- .../workorder/components/WorkOrderCard.vue | 1 + .../backend/touchScreen/workorder/index.vue | 103 +++++++++++++++--- 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/src/utils/mqtt/emqxUtil.js b/src/utils/mqtt/emqxUtil.js index 55e204d..7839e52 100644 --- a/src/utils/mqtt/emqxUtil.js +++ b/src/utils/mqtt/emqxUtil.js @@ -4,9 +4,9 @@ export const title = 'shgg_mes' // 云服务器环境 // const url = 'wss://ff6bab7a.ala.cn-hangzhou.emqxsl.cn:8084/mqtt'; // 本地环境 -//const url = 'ws://192.168.23.165:8083/mqtt' +const url = 'ws://192.168.23.165:8083/mqtt' // 线上环境 -const url = 'ws://192.168.60.251:8083/mqtt' +//const url = 'ws://192.168.60.251:8083/mqtt' // 创建客户端实例 const options = { clean: true, diff --git a/src/views/qualityManagement/backend/touchScreen/workorder/components/WorkOrderCard.vue b/src/views/qualityManagement/backend/touchScreen/workorder/components/WorkOrderCard.vue index dd9ca4b..f43dc05 100644 --- a/src/views/qualityManagement/backend/touchScreen/workorder/components/WorkOrderCard.vue +++ b/src/views/qualityManagement/backend/touchScreen/workorder/components/WorkOrderCard.vue @@ -259,6 +259,7 @@ export default { this.updateFormData() } else { this.showErrorMessage(3, res.data) + this.updateFormData() } }) }, diff --git a/src/views/qualityManagement/backend/touchScreen/workorder/index.vue b/src/views/qualityManagement/backend/touchScreen/workorder/index.vue index b7fa03d..488e9bf 100644 --- a/src/views/qualityManagement/backend/touchScreen/workorder/index.vue +++ b/src/views/qualityManagement/backend/touchScreen/workorder/index.vue @@ -46,6 +46,10 @@ export default { }, messageClass: 'text-green', message: '正常', + // 用于存储已处理消息的Map,键为LabelCode,值为时间戳 + processedMessages: new Map(), + // 去重时间窗口(毫秒),可根据实际需求调整 + deduplicationWindow: 1 * 500, } }, created() { @@ -174,25 +178,98 @@ export default { that.mqttClient.on('message', function (topic, message) { if (topic === _topic.SiteComLabelCode) { const objData = emqxUtil.parseJSON(message) + console.log(objData) + that.doMqttAction(objData) } }) }, - // 执行mqtt消息动作 doMqttAction(objData) { - const that = this - const { SiteNo, ComNo, LabelCode, Time } = objData - if (!LabelCode) { - this.showWarningMessage(2, '扫描结果为空!') - return + try { + const { SiteNo, ComNo, LabelCode, Time } = objData + + if (this.isDuplicateMessage(LabelCode, Time)) { + console.log(`重复消息被过滤: ${LabelCode}`) + return + } + + this.recordProcessedMessage(LabelCode, Time) + + if (!LabelCode) { + this.showWarningMessage(2, '扫描结果为空!') + return + } + + if (this.$refs.WorkOrderCardRef && typeof this.$refs.WorkOrderCardRef.setMqttMessage === 'function') { + this.$refs.WorkOrderCardRef.setMqttMessage(LabelCode, ComNo, SiteNo) + } else { + console.log('错误: WorkOrderCardRef 或 setMqttMessage 方法不存在') + } + } catch (error) { + console.log('错误: 处理MQTT消息时发生错误:', error) + } + }, + + isDuplicateMessage(labelCode, messageTime) { + try { + if (!messageTime) return false + + const messageTimestamp = this.parseTimestamp(messageTime) + if (!messageTimestamp) return false + + const lastProcessedTime = this.processedMessages.get(labelCode) + + return lastProcessedTime && messageTimestamp - lastProcessedTime <= this.deduplicationWindow + } catch (error) { + console.log('错误: 检查重复消息时发生错误:', error) + return false + } + }, + + recordProcessedMessage(labelCode, messageTime) { + try { + const timestamp = this.parseTimestamp(messageTime) + if (timestamp) { + this.processedMessages.set(labelCode, timestamp) + setTimeout(() => this.cleanupExpiredMessages(), 0) + } + } catch (error) { + console.log('错误: 记录已处理消息时发生错误:', error) + } + }, + + parseTimestamp(timeString) { + try { + if (!timeString) return null + + const date = this.$dayjs(timeString, 'YYYY-MM-DD HH:mm:ss') + + if (!date.isValid()) { + console.log(`警告: 无效的日期格式: ${timeString}`) + return null + } + + return date.valueOf() + } catch (error) { + console.log(`错误: 解析时间戳失败 (${timeString}):`, error) + return null + } + }, + + cleanupExpiredMessages() { + try { + const now = Date.now() + const expirationThreshold = now - this.deduplicationWindow + + for (const [labelCode, timestamp] of this.processedMessages.entries()) { + if (timestamp < expirationThreshold) { + this.processedMessages.delete(labelCode) + } + } + console.log(`清理过期消息完成,剩余记录: ${this.processedMessages.size}`) + } catch (error) { + console.log('错误: 清理过期消息时发生错误:', error) } - // TODO 打印 - // const _topic = emqxUtil.BackEndTopic - // const jsonStr = { - // Path: 'C:\\Users\\Administrator\\Desktop\\label\\DOAN_Test\\奥迪PPC模板\\02S外标签.btw', - // } - // that.mqttClient.publish(_topic.SiteLabelPrint + '/' + SiteNo, JSON.stringify(jsonStr)) - this.$refs.WorkOrderCardRef.setMqttMessage(LabelCode, ComNo, SiteNo) }, }, }