119 lines
3.1 KiB
Vue
119 lines
3.1 KiB
Vue
<script>
|
||
import indexUtil from "@/utils/indexUtil.js"
|
||
import dc from "@/utils/deviceUtil/dc.js"
|
||
import mqttUtil from "@/utils/mqttUtil.js"
|
||
|
||
export default {
|
||
data() {
|
||
return {
|
||
indexData: new indexUtil(this.indexDataHandler), //全局地块列表管理
|
||
dc: new dc(),
|
||
mqtt: new mqttUtil(),
|
||
mqttObj: {
|
||
topics: []
|
||
},
|
||
}
|
||
},
|
||
globalData: {},
|
||
onLaunch: function() {
|
||
console.log('App Launch')
|
||
// #ifdef H5
|
||
this.mqtt = new mqttUtil();
|
||
// #endif
|
||
|
||
// 监听mqtt连接命令
|
||
uni.$on("mqtt-link", () => {
|
||
this.mqttLink();
|
||
});
|
||
// 监听mqtt连接订阅命令
|
||
uni.$on("mqtt-subscribes", topics => {
|
||
this.mqttSubscribes();
|
||
});
|
||
},
|
||
onShow: function() {
|
||
console.log('App Show')
|
||
},
|
||
onHide: function() {
|
||
console.log('App Hide')
|
||
},
|
||
methods: {
|
||
//监听indexData的回调
|
||
indexDataHandler(e, data) {
|
||
// console.error("indexDataHandler_1:", {
|
||
// e: e,
|
||
// data: data
|
||
// });
|
||
if (e == this.indexData.eventType.land) {
|
||
// console.error("地块更新通知:", this.indexData.selectItem)
|
||
|
||
this.dc.setLandItem(this.indexData.selectItem, null);
|
||
}
|
||
if (e == this.indexData.eventType.device) {
|
||
// console.error("设备更新通知:", this.indexData.device)
|
||
|
||
this.dc.setStations(this.indexData.device.stations);
|
||
this.dc.setFlapValves(this.indexData.device.flapValves);
|
||
this.dc.setValves(this.indexData.selectItem.id, this.indexData.device.waterOutlets);
|
||
}
|
||
uni.hideLoading();
|
||
// console.error("indexDataHandler_2:", {
|
||
// indexData: this.indexData,
|
||
// dc: this.dc,
|
||
// })
|
||
},
|
||
setTime: function() {
|
||
console.error(this.globalData.num);
|
||
// setTimeout(() => {
|
||
// this.globalData.num++
|
||
// console.error(this.globalData.num);
|
||
// this.setTime();
|
||
// }, 1000)
|
||
},
|
||
mqttLink() {
|
||
// 处理 wx.connectSocket promisify 兼容问题,强制返回 SocketTask
|
||
uni.connectSocket = (function(connectSocket) {
|
||
return function(options) {
|
||
console.log(options)
|
||
options.success = options.success || function() {}
|
||
return connectSocket.call(this, options)
|
||
}
|
||
})(uni.connectSocket)
|
||
|
||
console.error("mqttLink");
|
||
// #ifdef H5
|
||
this.mqtt.link();
|
||
this.mqtt.client.on("connect", error => {
|
||
console.log("连接成功")
|
||
console.error("client:", this.mqtt.client);
|
||
})
|
||
this.mqtt.client.on("error", error => {
|
||
console.log("连接异常")
|
||
})
|
||
this.mqtt.client.on("reconnect", error => {
|
||
console.log("重新连接")
|
||
})
|
||
this.mqtt.client.on("message", (topic, message) => {
|
||
console.log("接受消息")
|
||
console.log("message:", message.toString())
|
||
this.mqttSendMsg(message);
|
||
})
|
||
// #endif
|
||
|
||
},
|
||
mqttSubscribes(topics) {
|
||
this.mqttObj.topics = topics;
|
||
console.error("mqttSubscribes:", topics);
|
||
},
|
||
mqttSendMsg(msg) {
|
||
uni.$emit("mqtt-msg", msg);
|
||
},
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss">
|
||
@import '@/uni_modules/uview-plus/index.scss';
|
||
@import '@/static/scss/index.scss';
|
||
@import '@/static/alifont/iconfont.css';
|
||
@import '@/static/scss/app.css';
|
||
</style> |