From 6414bf3d7ad398cb5849ba792a664b0d9167ca4f Mon Sep 17 00:00:00 2001 From: liub Date: Wed, 3 Dec 2025 11:42:05 +0800 Subject: [PATCH] =?UTF-8?q?4877=E4=BF=9D=E5=AD=98=E5=B7=B2=E9=80=89?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/4877/BJQ4877.vue | 49 +++++++++++- utils/MqHelper.js | 169 +++++++++++++++++++++++++++++++++++++++++ utils/mqtt.js | 2 + 3 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 utils/MqHelper.js diff --git a/pages/4877/BJQ4877.vue b/pages/4877/BJQ4877.vue index 4beeee4..825a758 100644 --- a/pages/4877/BJQ4877.vue +++ b/pages/4877/BJQ4877.vue @@ -255,7 +255,8 @@ import { colors as groupColors } from '@/api/4877/BJQ4877.js'; - + + import MqTool from '@/utils/MqHelper.js' const pagePath = "/pages/4877/BJQ4877"; @@ -264,6 +265,7 @@ var recei = null; var interval = null; var slidTime = null; + var mq=null; export default { data() { return { @@ -389,12 +391,19 @@ onUnload() { console.log("页面卸载,释放资源"); ble.removeAllCallback(pagePath); + if(mq){ + mq.unSubscribes(); + mq.disconnect(); + } + ble=null; + mq=null; clearInterval(interval); }, onLoad: function() { these = this; recei = BleReceive.getBleReceive(); ble = BleTool.getBleTool(); + mq=MqTool.getMqTool(); this.dic.gropus = []; @@ -429,6 +438,14 @@ } var device = data.data; these.device = device; + + let arr=[{topic:'C/4877_Groups_'+these.device.id,callback:these.getCheckedColors}]; + mq.init().then(res=>{ + mq.subscribes(arr).catch(ex=>{ + console.error("ex=",ex); + }); + }) + let f = ble.data.LinkedList.find((v) => { if (v.macAddress == device.deviceMac) { // console.log("找到设备了", v); @@ -477,6 +494,30 @@ }, methods: { + getCheckedColors(rec){ + console.error("收到MQ消息:",rec); + try{ + + let str=rec.receive.payloadString; + let arr=JSON.parse(str); + + + let groups = groupColors.filter((v,index) => { + if(arr[index]===1){ + return true; + } + return false; + }); + + these.dic.groups = groups; + + + }catch(err){ + + } + + + }, ShowChannelEdit() { this.Status.ShowEditChannel = true; this.showPop({ @@ -596,7 +637,11 @@ return; } - + mq.sendData('C/4877_Groups_'+these.device.id,JSON.stringify(arr),true).then(res=>{ + console.log("发送成功,",res) + }).catch(err=>{ + console.error("err=",err); + }); var json = { ins_GroupType: arr } diff --git a/utils/MqHelper.js b/utils/MqHelper.js new file mode 100644 index 0000000..ccbf7d8 --- /dev/null +++ b/utils/MqHelper.js @@ -0,0 +1,169 @@ +import MqttClient from '@/utils/mqtt.js'; + + +class MqHelper { + constructor() { + this.instance = null; + this.subTopics=[]; + } + + timeout(ms) { + if (!ms) { + ms = 200; + } + return new Promise((succ, err) => { + setTimeout(() => { + err({ + code: -1, + errMsg: '超时了' + }) + }, ms); + }); + + } + //打开mq连接,topics:要订阅的主题列表格式{topic:'主题',callback:fn}或此格式的数组 + init(topics) { + let connect = () => { + return new Promise((resolve, reject) => { + this.instance = new MqttClient(); + this.instance.connect((res) => { + if(topics){ + console.error("连接成功开始订阅消息",topics); + setTimeout(this.subscribes, 50, topics) + } + + resolve(); + }); + }); + } + + let p1 = connect(); + let p2 = this.timeout(500); + + return new Promise((resolve, reject) => { + Promise.race([p1, p2]).then(()=>{ + resolve() + }).catch(ex=>{ + this.instance=null; + reject(ex); + }); + }); + + } + //订阅消息 + subscribes(topics) { + return new Promise((resolve,reject)=>{ + + + if(!this.instance || !topics){ + reject(); + return; + } + if (!Array.isArray(topics)) { + topics = [topics]; + } + for (let i = 0; i < topics.length; i++) { + let item = topics[i]; + let f=this.subTopics.find(v=>{ + return v.topic===item.topic; + }); + if(!f){ + this.subTopics.push(item); + } + this.instance.subscribe(item.topic, (payload, receive) => { + try { + console.log("开始处理回调") + item.callback({payload:payload,receive:receive}); + console.log("处理回调成功") + } catch (err) { + console.error("订阅消息回调出现异常,",err); + } + }); + + console.error("订阅消息成功"); + } + resolve(); + }); + + } + //取消订阅消息 单个或者数组 + unSubscribes(topics){ + if(!this.instance || !topics){ + return; + } + if (!Array.isArray(topics)) { + topics = [topics]; + } + + for (let i = 0; i < topics.length; i++) { + let item = topics[i]; + this.instance.unsubscribe(item.topic); + this.subTopics.find((v,index)=>{ + if(v.topic===item.topic){ + this.subTopics.splice(index,1); + return true; + } + return false; + }); + } + + } + //发送消息,主题 消息内容 是否保留消息 + sendData(topic,msg,retained){ + return new Promise((resolve,reject)=>{ + if(!this.instance || !topic){ + reject("MQTT未连接或无主题无法发布消息") + return + } + if(msg===null || msg===undefined){ + reject("msg无内容") + return; + } + + if(!retained){ + retained=false; + }else{ + retained=true; + } + + try { + let flag = this.instance.publish(topic, msg, retained); + if (flag) { + resolve({ + code: 200, + msg: 'MQTT直连发送成功' + }); + return; + } + + reject("MQTT未连接,无法发布消息"); + } catch (error) { + + reject(error); + } + }); + + + } + //断开连接 + disconnect(){ + if(!this.instance){ + return + } + this.instance.disconnect() + } + +} + + + +export default { + getMqTool: function(found, receive) { + + let instance = new MqHelper(); + + + + return instance; + } +} \ No newline at end of file diff --git a/utils/mqtt.js b/utils/mqtt.js index eb8d287..2297fa2 100644 --- a/utils/mqtt.js +++ b/utils/mqtt.js @@ -286,7 +286,9 @@ class MqttClient { const mqttMessage = new Paho.Message(message); mqttMessage.destinationName = topic; mqttMessage.qos = 1; + console.log("typeof(retained)=",typeof(retained)) if(typeof(retained)==='boolean'){ + console.log("retained=",retained) mqttMessage.retained=retained; }