2025-04-16 02:11:26 +01:00

71 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import mqttTool from '@/utils/mqttTool.js'
import sysTopics from '@/utils/sysTopics.js'
export default{
mqttPublish:function(model,device) {
let topic = "";
let message = ""
switch (model.type) {
case 1:
if (device.status == 3) {
// 属性,在线模式
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.propertyOnlineSend;
} else if (device.isShadow) {
// 属性,离线模式
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.propertyOfflineSend;
}
message = `[{"id":"${model.id}","value":"${model.vaule}","remark":"${model.name}"}]`;
break;
case 2:
if (device.status == 3) {
// 功能,在线模式
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.functionOnlineSend;;
} else if (device.isShadow) {
// 功能,离线模式
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.functionOfflineSend;;
}
message = `[{"id":"${model.id}","value":"${model.vaule}","remark":"${model.name}"}]`;
break;
case 3:
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.otaSend;
message = model.message;
break;
case 4:
topic = "/" + device.productId + "/" + device.serialNumber + sysTopics.monitorSend;
message = model.message;
break;
default:
break;
}
if (topic != "") {
// 发布
mqttTool.publish(topic, message, model.name).then(res => {
uni.showToast({title:'发布成功',icon:'success'})
}).catch(res => {
uni.showToast({title:'发布失败',icon:'error'})
});
}
},
//isOpen true 发送监测命令false关闭监测命令
monitor:function(device,isOpen){
//监测间隔时间
let intervalTime=1000;//毫秒
if (device.status != 3) {
modal.alertError("设备不在线,下发指令失败");
return;
}
let name = isOpen?'实时监测开启':'实时监测关闭';
let interval = isOpen?intervalTime:0;
this.mqttPublish({name,type:4,message:`{"interval":${interval}}`},device);//毫秒
},
//ota升级
ota:async function(device){
const {data} = await uni.$u.http.get('/iot/firmware/getLatest/' + device.deviceId)
if(data){
this.mqttPublish({name:"ota升级",type:3,message:`[{"version":"${data.version}","downloadUrl":"${uni.$u.http.config.baseURL+data.filePath}"}]`},device);
}else{
modal.alertError("没有最新固件版本");
}
}
}