123
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -259,6 +259,7 @@ export default {
|
||||
this.updateFormData()
|
||||
} else {
|
||||
this.showErrorMessage(3, res.data)
|
||||
this.updateFormData()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user