设备控制修复
parent
a18f888531
commit
1884eb0f9d
|
|
@ -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)
|
||||
// 拿到指令字段(如jm2k)和执行状态(suc)
|
||||
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 ? "成功" : "失败"}`);
|
||||
// (可匹配之前发布的指令msgId,更新UI状态)
|
||||
|
|
|
|||
Loading…
Reference in New Issue