Files
controller-wisetopvue/src/components/debug/Debug.vue
T
2022-06-29 11:42:03 +08:00

116 lines
3.2 KiB
Vue

<template>
<va-card class="flex xl3 md3 sm12 xs12" title="MQTT">
<div class="row ma-1 text-center">
<va-button @click="getControllerID">Controller-ID</va-button>
<div class="flex-center">
{{ controllerID }}
</div>
</div>
<div class="row ma-1 text-start" v-if="controllerID !== ''">
<va-button @click="getHardwareInfo">HardwareInfo</va-button>
<div class="">
{{ responseMessage }}
</div>
</div>
</va-card>
</template>
<script>
import api from '@/data/api'
export default {
name: 'Developer',
components: {
},
data () {
return {
controllerID: '',
responseMessage: '',
}
},
mqtt: {
async '+/get_device_info_all/+' (data, topic) {
console.log('get_device_info_all', String.fromCharCode.apply(null, data))
},
async '+/hardware_device/+' (data, topic) {
console.log('hardware_device', String.fromCharCode.apply(null, data))
},
async '+/hardware_datetime/+' (data, topic) {
console.log('hardware_datetime', String.fromCharCode.apply(null, data))
},
async '+/hardware_info/+' (data, topic) {
console.log('hardware_info', String.fromCharCode.apply(null, data))
this.responseMessage = String.fromCharCode.apply(null, data)
},
async '+/broadcast/+' (data, topic) {
console.log('broadcast', String.fromCharCode.apply(null, data))
},
},
methods: {
mqttPub: function (topic, mes) {
this.$mqtt.publish(topic, mes)
},
mqttSub: function (val) {
this.$mqtt.subscribe(val)
},
mqttUnSub: function (val) {
this.$mqtt.unsubscribe(val)
},
pageMqttSub: function (id) {
this.mqttSub(id + '/hardware_datetime/+')
this.mqttSub(id + '/hardware_device/+')
this.mqttSub(id + '/hardware_info/+')
this.mqttSub(id + '/broadcast')
this.mqttSub(id + '/get_device_info_all/+')
},
pageMqttUnSub: function (id) {
this.mqttUnSub(id + '/hardware_datetime/+')
this.mqttUnSub(id + '/hardware_device/+')
this.mqttUnSub(id + '/hardware_info/+')
this.mqttUnSub(id + '/broadcast')
this.mqttUnSub(id + '/get_device_info_all/+')
},
getControllerID: async function () {
try {
const response = await api.controller.getAll()
if (response.status === 200) {
if (response.data.length > 0) {
this.controllerID = response.data[0].mqtt_id
this.pageMqttSub(this.controllerID)
return true
}
this.controllerID = 'No Controller Info'
return false
}
this.controllerID = 'Wrong status ' + response.status
} catch (e) {
console.log(e)
this.controllerID = e
}
},
getHardwareDatetime: function () {
this.mqttPub(this.controllerID + '_user', JSON.stringify({
header: 'hardware_datetime/0',
content: String(Date.now()),
}))
},
getHardwareInfo: function () {
this.mqttPub(this.controllerID + '_user', JSON.stringify({
header: 'hardware_info/0',
}))
},
},
async mounted () {
},
destroyed () {
if (this.controllerID !== '') {
this.pageMqttUnSub(this.controllerID)
}
},
}
</script>
<style>
</style>