91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
import {
|
|
Mqtt
|
|
} from '@/utils/mqtt.js'
|
|
import store from '@/store/index.js'
|
|
|
|
function init (pdaId, mqttConnection, subscription, publication) {
|
|
store.dispatch('savePdaId', pdaId)
|
|
store.dispatch('saveMqttConnection', mqttConnection)
|
|
store.dispatch('saveSubscription', subscription)
|
|
store.dispatch('savePublication', publication)
|
|
// store.dispatch('saveMqttClient', );
|
|
// pdaId = pdaId
|
|
// mqttConnection = mqttConnection
|
|
// console.log(mqttConnection);
|
|
// subscription = subscription
|
|
// publication = publication
|
|
connectMqtt()
|
|
}
|
|
|
|
function connectMqtt () {
|
|
let mqttClient = store.state.mqttClient
|
|
mqttClient = new Mqtt(store.state.mqttConnection)
|
|
mqttClient.createConnection()
|
|
mqttClient.client.on('connect', () => {
|
|
console.log('MQTT Connection succeeded!')
|
|
})
|
|
mqttClient.doSubscribe(store.state.subscription)
|
|
// 使用过程中的报错 断开连接 服务端出问题
|
|
mqttClient.client.on('error', error => {
|
|
console.log('Connection failed', error)
|
|
})
|
|
mqttClient.client.on('message', (topic, message) => {
|
|
mqttOnMessage(topic, message)
|
|
})
|
|
}
|
|
|
|
function disConnectMqtt () {
|
|
if (store.state.mqttConnection) {
|
|
store.state.mqttConnection.destroyConnection()
|
|
store.state.mqttConnection = null
|
|
}
|
|
}
|
|
|
|
function mqttOnMessage (topic, message) {
|
|
let pdaId = 'PDA_1010'
|
|
// console.log(topic, message.toString())
|
|
const arraysSignal = message.toString().split('|')
|
|
const mSource = arraysSignal[0] // MES
|
|
// const m_Command = arraysSignal[1]
|
|
const mOpName = arraysSignal[2]
|
|
switch (mSource) {
|
|
case 'BarCode':
|
|
switch (mOpName) {
|
|
case pdaId:
|
|
// const BarValue = arraysSignal[3]
|
|
connectMessage(arraysSignal[3], message)
|
|
// uni.$emit("connect-mqttOnMessage", arraysSignal[3], message)
|
|
// this.mqttSendMessage(`PDABarCode|Bar|${mOpName}|${BarValue}`)
|
|
break
|
|
default:
|
|
console.log(`消息工位号不匹配:${message.toString()};工位号:${pdaId}`)
|
|
break
|
|
}
|
|
break
|
|
default:
|
|
console.log(`消息来源不匹配:${message.toString()};工位号:${pdaId}`)
|
|
break
|
|
}
|
|
}
|
|
|
|
function connectMessage (val, msg) {
|
|
// window.dispatchEvent(new CustomEvent('onmessageMqtt', {
|
|
// detail: {
|
|
// data: val
|
|
// }
|
|
// }))
|
|
// window.addEventListener('onmessageMqtt', this.getMqttMessage)
|
|
store.dispatch('saveMqttMsg', val)
|
|
}
|
|
|
|
// function mqttSendMessage (message) {
|
|
// publication.payload = message
|
|
// mqttClient.doPublish(this.publication)
|
|
// }
|
|
export {
|
|
init,
|
|
mqttOnMessage,
|
|
disConnectMqtt,
|
|
connectMessage
|
|
}
|