提交 b9d82e14 作者: 翁进城

优化MQTT模块代码

上级 de3e06c3
import mqtt from 'mqtt'; import mqtt from 'mqtt';
import mqttOrders from "./mqttOrders"; import orders from "./orders";
function uint8array2json(uint8array) { function uint8array2json(uint8array) {
if (!uint8array || !uint8array.length) { if (!uint8array || !uint8array.length) {
...@@ -22,7 +22,7 @@ export default { ...@@ -22,7 +22,7 @@ export default {
type(如258): {} type(如258): {}
} */ } */
}, },
mqttOrders, //所有指令 orders, //所有指令
}, },
mutations: { mutations: {
setClient(state, data) { setClient(state, data) {
...@@ -35,7 +35,11 @@ export default { ...@@ -35,7 +35,11 @@ export default {
state.url = data; state.url = data;
}, },
setDataSet(state, data) { setDataSet(state, data) {
state.dataSet = data; let temp = {};
Object.keys(data).forEach((key) => {
temp[key] = data[key];
});
state.dataSet = temp;
}, },
}, },
actions: { actions: {
...@@ -62,11 +66,7 @@ export default { ...@@ -62,11 +66,7 @@ export default {
state.dataSet[deviceHardId] = {}; state.dataSet[deviceHardId] = {};
} }
state.dataSet[deviceHardId][data.type] = data; state.dataSet[deviceHardId][data.type] = data;
let temp = {}; commit("setDataSet", state.dataSet);
Object.keys(state.dataSet).forEach((key) => {
temp[key] = state.dataSet[key];
});
commit("setDataSet", temp);
}); });
/** /**
...@@ -116,6 +116,10 @@ export default { ...@@ -116,6 +116,10 @@ export default {
if (client) { if (client) {
client.unsubscribe(topic); client.unsubscribe(topic);
} }
if (state.dataSet[deviceHardId]) {
state.dataSet[deviceHardId] = undefined;
commit("setDataSet", state.dataSet);
}
}, },
/** /**
* 发布 * 发布
...@@ -146,14 +150,14 @@ export default { ...@@ -146,14 +150,14 @@ export default {
* @param {Function} param1.callback 成功回调 * @param {Function} param1.callback 成功回调
*/ */
order({ dispatch }, { topic, order, data, callback }) { order({ dispatch }, { topic, order, data, callback }) {
if (mqttOrders[order] === undefined) { if (orders[order] === undefined) {
throw "该命令不存在!"; throw "该命令不存在!";
} }
dispatch("publish", { dispatch("publish", {
topic, topic,
data: { data: {
type: mqttOrders[order], type: orders[order],
data, data,
callback, callback,
}, },
......
...@@ -4,8 +4,15 @@ export default ` ...@@ -4,8 +4,15 @@ export default `
<el-form> <el-form>
<el-form-item label="无人机id"> <el-form-item label="无人机id">
<el-input v-model="deviceHardId"></el-input> <el-input v-model="deviceHardId"></el-input>
<el-button @click="onSubscribe">订阅</el-button> </el-form-item>
<el-form-item label="操作">
<el-button @click="subscribe">订阅</el-button>
<el-button @click="unsubscribe">取消</el-button> <el-button @click="unsubscribe">取消</el-button>
<el-button @click="publish({type: orders.继续航线任务})">通过原始type发送指令</el-button>
<el-button @click="order({order: '继续航线任务'})">通过关键字发送指令</el-button>
</el-form-item>
<el-form-item label="支持的指令">
{{ orders }}
</el-form-item> </el-form-item>
</el-form> </el-form>
<div> <div>
...@@ -25,6 +32,12 @@ export default { ...@@ -25,6 +32,12 @@ export default {
computed: { computed: {
text(){ text(){
return this.$store.state.MMCMQTT.dataSet; return this.$store.state.MMCMQTT.dataSet;
},
orders(){
return this.$store.state.MMCMQTT.orders;
},
topic(){
return \`PX4/RECEIVE/\${this.deviceHardId}\`;
} }
}, },
async created(){ async created(){
...@@ -38,9 +51,9 @@ export default { ...@@ -38,9 +51,9 @@ export default {
console.log('mqtt连接成功'); console.log('mqtt连接成功');
}, },
methods: { methods: {
onSubscribe(){ subscribe(){
this.$store.dispatch('MMCMQTT/subscribe', { this.$store.dispatch('MMCMQTT/subscribe', {
topic: \`PX4/RECEIVE/\${this.deviceHardId}\`, topic: this.topic,
callback(){ callback(){
console.log('订阅成功') console.log('订阅成功')
} }
...@@ -48,7 +61,27 @@ export default { ...@@ -48,7 +61,27 @@ export default {
}, },
unsubscribe(){ unsubscribe(){
this.$store.dispatch('MMCMQTT/unsubscribe', { this.$store.dispatch('MMCMQTT/unsubscribe', {
topic: \`PX4/RECEIVE/\${this.deviceHardId}\` topic: this.topic
})
},
publish({ type }){
this.$store.dispatch('MMCMQTT/publish', {
topic: this.topic,
data: {
type
},
callback(){
alert('发送成功')
}
})
},
order( { order } ){
this.$store.dispatch('MMCMQTT/order', {
topic: this.topic,
order,
callback(){
alert('发送成功')
}
}) })
} }
} }
......
...@@ -7,7 +7,11 @@ ...@@ -7,7 +7,11 @@
<el-form-item label="操作"> <el-form-item label="操作">
<el-button @click="subscribe">订阅</el-button> <el-button @click="subscribe">订阅</el-button>
<el-button @click="unsubscribe">取消</el-button> <el-button @click="unsubscribe">取消</el-button>
<el-button @click="publish({type: mqttOrders.继续航线任务})">继续飞行</el-button> <el-button @click="publish({type: orders.继续航线任务})">通过原始type发送指令</el-button>
<el-button @click="order({order: '继续航线任务'})">通过关键字发送指令</el-button>
</el-form-item>
<el-form-item label="支持的指令">
{{ orders }}
</el-form-item> </el-form-item>
</el-form> </el-form>
<div> <div>
...@@ -28,8 +32,8 @@ export default { ...@@ -28,8 +32,8 @@ export default {
text(){ text(){
return this.$store.state.MMCMQTT.dataSet; return this.$store.state.MMCMQTT.dataSet;
}, },
mqttOrders(){ orders(){
return this.$store.state.MMCMQTT.mqttOrders; return this.$store.state.MMCMQTT.orders;
}, },
topic(){ topic(){
return `PX4/RECEIVE/${this.deviceHardId}`; return `PX4/RECEIVE/${this.deviceHardId}`;
...@@ -69,6 +73,15 @@ export default { ...@@ -69,6 +73,15 @@ export default {
alert('发送成功') alert('发送成功')
} }
}) })
},
order( { order } ){
this.$store.dispatch('MMCMQTT/order', {
topic: this.topic,
order,
callback(){
alert('发送成功')
}
})
} }
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论