<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(this.mqttMsgHandler), //Mqtt
			}
		},
		globalData: {},
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef H5
			this.mqtt = new mqttUtil();
			// #endif

			// 监听mqtt连接命令
			uni.$on("mqtt-link", (userId) => {
				this.mqttLink(userId);
			});
			// 监听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) {
				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:", {
				// 	e: e,
				// 	data: data,
				// 	indexData: this.indexData,
				// 	dc: this.dc,
				// })
			},
			//Mqtt连接
			mqttLink(userId) {
				this.mqtt.link(userId);
			},
			//Mqtt连接
			mqttSubscribes(topics) {
				this.mqtt.subscribes(topics);
			},
			//Mqtt连接
			mqttUnsubscribes(topics) {
				this.mqtt.unsubscribes(topics);
			},
			//Mqtt消息回调
			mqttMsgHandler(topic, data) {
				console.error("mqttMsgHandler:", {
					topic: topic,
					data: data,
				});
			},
			//Mqtt断开连接
			mqttEnd() {
				this.mqtt.over();
			},

		}
	}
</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>