4877保存已选的配组
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
169
utils/MqHelper.js
Normal file
169
utils/MqHelper.js
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user