设备控制修复

master
lld 2025-12-25 19:00:45 +08:00
parent a18f888531
commit 1884eb0f9d
1 changed files with 41 additions and 33 deletions

View File

@ -20,8 +20,8 @@
<text class="card-main">卷被开</text>
<text class="card-sub">{{ show.jbk }}</text>
</view>
<view class="card-icon" :class="{ active: status.jbk === '运行' }">
<uni-icons :type=" (status.jbk === '运行')?'circle':'circle-filled'" size="24" color="#fff" />
<view class="card-icon" :class="{ active: status.jbk === 1 }">
<uni-icons :type=" (status.jbk === 1)?'circle':'circle-filled'" size="24" color="#fff" />
</view>
</view>
</template>
@ -284,8 +284,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷被开</text>
<text class="card-sub">{{ show.jbk }}</text>
</view>
<view class="card-icon" :class="{ active: status.jbk === '运行' }">
<uni-icons :type=" (status.jbk === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jbk === 1 }">
<uni-icons :type=" (status.jbk === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -295,8 +295,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷被关</text>
<text class="card-sub">{{ show.jbg }}</text>
</view>
<view class="card-icon" :class="{ active: status.jbg === '运行' }">
<uni-icons :type=" (status.jbg === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jbg === 1 }">
<uni-icons :type=" (status.jbg === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -306,8 +306,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜1开</text>
<text class="card-sub">{{ show.jm1k }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm1k === '运行' }">
<uni-icons :type="(status.jm1k === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm1k === 1 }">
<uni-icons :type="(status.jm1k === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -317,8 +317,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜1关</text>
<text class="card-sub">{{ show.jm1g }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm1g === '运行' }">
<uni-icons :type="(status.jm1g === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm1g === 1 }">
<uni-icons :type="(status.jm1g === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -328,8 +328,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜2开</text>
<text class="card-sub">{{ show.jm2k }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm2k === '运行' }">
<uni-icons :type="(status.jm2k === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm2k === 1 }">
<uni-icons :type="(status.jm2k === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -339,8 +339,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜2关</text>
<text class="card-sub">{{ show.jm2g }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm2g === '运行' }">
<uni-icons :type="(status.jm2g === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm2g === 1 }">
<uni-icons :type="(status.jm2g === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
<!-- 卷膜3开卡片 -->
@ -349,8 +349,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜3开</text>
<text class="card-sub">{{ show.jm3k }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm3k === '运行' }">
<uni-icons :type="(status.jm3k === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm3k === 1 }">
<uni-icons :type="(status.jm3k === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
@ -360,8 +360,8 @@ $uni-success: #18bc37 !default;
<text class="card-main">卷膜3关</text>
<text class="card-sub">{{ show.jm3g }}</text>
</view>
<view class="card-icon" :class="{ active: status.jm3g === '运行' }">
<uni-icons :type="(status.jm3g === '运行')?'circle':'circle-filled'" size="24" color="#fff"/>
<view class="card-icon" :class="{ active: status.jm3g === 1 }">
<uni-icons :type="(status.jm3g === 1)?'circle':'circle-filled'" size="24" color="#fff"/>
</view>
</view>
</view>
@ -387,6 +387,7 @@ export default {
publishTopic: '/down',
title:'',
message: {},
connected:false,
//
show: {
jbk: "暂停",
@ -398,6 +399,7 @@ export default {
jm3k: "暂停",
jm3g: "暂停"
},
deviceType: '',
status: {
jbk: 0,
jbg: 0,
@ -412,7 +414,7 @@ export default {
},
onLoad() {
this.publishTopic = "dtu/"+this.imei+this.publishTopic;
this.subscribeTopic = "dtu/"+this.imei+this.subscribeTopic;
this.mqttConfig.subscribeTopic = "dtu/"+this.imei+this.mqttConfig.subscribeTopic;
this.title="设备【"+this.imei+"】";
if (this.status.jbk===1) {
this.status.jbg = 0;
@ -449,6 +451,10 @@ export default {
//
handleCardClick(status, type) {
if (!this.connected) {
// mqtt
this.connectMqtt()
}
uni.showModal({
title: '操作提示:',
content: '确定'+(status===1?"运行":"暂停")+'设备?',
@ -457,17 +463,14 @@ export default {
success: (res) =>{
if (res.confirm) {
console.info("操作功能:【"+type+"】,变更状态为:"+ status)
if (!this.connected) {
// mqtt
this.connectMqtt()
}
//
this.message = JSON.stringify({[type]: status})
console.info("指令:"+this.message)
//
this.publishMessage();
//
this.ackMessage(type);
this.deviceType=type;
}
}
})
@ -489,9 +492,11 @@ export default {
this.client.on('connect', () => {
this.connected = true
this.client.subscribe(this.mqttConfig.subscribeTopic, { qos: 0 })
this.addMessage('已连接到MQTT服务器')
})
this.client.on("message",this.ackMessage);
this.client.on('error', (err) => {
this.addMessage(`控制失败: ${err.message}`)
@ -517,7 +522,7 @@ export default {
},
publishMessage() {
console.info("开始发布消息")
console.info("开始发布消息",this.connected,this.publishTopic,this.message)
if (!this.connected || !this.publishTopic || !this.message) {
uni.showToast({
title: '控制设备失败',
@ -538,27 +543,28 @@ export default {
this.message = {};
},
ackMessage(type) {
ackMessage(topic, payload) {
console.info("处理回执消息")
this.client.on("message", (topic, payload) => {
// console.info(""+topic,payload)
// 1. dtu/xxx/up
if (topic !== this.status.subscribeTopic) return;
if (topic !== this.mqttConfig.subscribeTopic) return;
// 2.
let msgData = {};
try {
msgData = JSON.parse(payload.toString());
msgData = JSON.parse(payload);
} catch (e) {
console.error("消息解析失败:", e);
return;
}
// console.info(""+msgData)
// 3.
if (msgData.prop && "suc" in msgData) {
// 👉
this.handleCommandAck(msgData,type);
this.handleCommandAck(msgData,this.deviceType);
}
});
},
addMessage(content) {
@ -569,6 +575,7 @@ export default {
},
//
handleCommandAck(ackData,type) {
console.info("回执消息11111"+ackData)
// jm2ksuc
const commandField = Object.keys(ackData.prop)[0]; // "jm2k"
const commandValue = ackData.prop[commandField]; // 0
@ -578,6 +585,7 @@ export default {
this.status[type] = this.status[type] === 0 ? 1 : 0;
this.show[type] = this.status[type] === 0 ? "运行" : "暂停";
}
this.deviceType='';
// /
console.log(`指令[${commandField}=${commandValue}]执行${isSuccess ? "成功" : "失败"}`);
// msgIdUI