From 6017f46b762663b9393cdae8422e0de1ed3db218 Mon Sep 17 00:00:00 2001
From: guifei zhu <guifeizhu@guifeideiMac.local>
Date: 星期三, 27 十一月 2024 13:30:06 +0800
Subject: [PATCH] 添加mqtt数据接口

---
 pages/device/control.vue |  199 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 171 insertions(+), 28 deletions(-)

diff --git a/pages/device/control.vue b/pages/device/control.vue
index 12746c5..9a54dfc 100644
--- a/pages/device/control.vue
+++ b/pages/device/control.vue
@@ -37,10 +37,17 @@
 							</view>
 							<view class="h-tr h-tr-2">
 								<view class="h-td">
-									<u-tag size="mini" text="鍦ㄧ嚎" type="success" plain plainFill></u-tag>
+									<u-tag v-if="connected" size="mini" text="鍦ㄧ嚎" type="success" plain
+										plainFill></u-tag>
+									<u-tag v-else size="mini" text="绂荤嚎" type="error" plain plainFill></u-tag>
 								</view>
 								<view class="h-td">
-									<u-tag size="mini" text="姝e湪骞茬嚗" type="success" plain plainFill></u-tag>
+									<template v-if="runStatu">
+										<u-tag size="mini" text="姝e湪骞茬嚗" type="success" plain plainFill></u-tag>
+									</template>
+									<template v-else>
+										<u-tag size="mini" text="寰呮満" type="error" plain plainFill></u-tag>
+									</template>
 								</view>
 								<view class="h-td">{{$lget(model,'windTemp')}}</view>
 								<view class="h-td">{{$lget(model,'windTemp')}}</view>
@@ -74,7 +81,7 @@
 
 							</view>
 							<view class="h-tr h-tr-2">
-								<view class="h-td">鐢熷湴榛�</view>
+								<view class="h-td">{{$lget(model,'herbName')}}</view>
 								<view class="h-td">{{$lget(model,'envTemp') == "" ? "--" :  $lget(model,'envTemp')}}m鲁
 								</view>
 								<view class="h-td">{{$lget(model,'envHum') == "" ? "--" :$lget(model,'envHum') }}kg
@@ -136,11 +143,12 @@
 				<uni-section title="鎸囦护" type="line">
 					<view class="flex justify-center padding-lg bg-white">
 						<view class="margin-right-sm">
-							<u-button text="寮�鏈�" type="success" @click="$u.throttle(showModal(), 500)"></u-button>
+							<u-button :disabled="connected" text="寮�鏈�" type="success"
+								@click="$u.throttle(showModal(1), 500)"></u-button>
 						</view>
-
 						<view class="margin-left-sm">
-							<u-button text="鍏虫満" type="error" :disabled="true"></u-button>
+							<u-button :disabled="!connected" text="鍏虫満" type="error"
+								@click="$u.throttle(showModal(2), 500)"></u-button>
 						</view>
 
 
@@ -150,8 +158,8 @@
 
 				<uni-section title="鎺у埗鍙�" type="line" class="margin-top-sm ">
 					<view class="padding-left-sm padding-right-sm padding-bottom-sm">
-						<view class="text-sm text-gray">12:00:20: <寮�鏈�>鎸囦护鎿嶄綔鎴愬姛</view>
-						<view class="text-sm text-gray  margin-top-sm">12:00:10: 鍙戦��<寮�鏈�>鎸囦护</view>
+						<view class="text-sm text-gray" :class="{'margin-top-sm':index>0}"  v-for="(log,index) in logList">{{log}}</view>
+						<!-- <view class="text-sm text-gray  margin-top-sm">12:00:10: 鍙戦��<寮�鏈�>鎸囦护</view> -->
 					</view>
 
 				</uni-section>
@@ -162,15 +170,15 @@
 		</swiper>
 
 
-
-
-
-		<u-modal :show="show" title="楠岃瘉浜岀骇瀵嗙爜" @confirm="confirm" :showCancelButton="true" @cancel="show=false"
-			ref="uModal" :asyncClose="true">
+		<u-modal :show="show" @confirm="confirm" :showCancelButton="true" @cancel="show=false" ref="uModal"
+			:asyncClose="true">
 
 			<slot>
-				<view>
-					<u--input style="width: 100%;" placeholder="璇疯緭鍏ヤ簩绾у瘑鐮�" border="surround" password
+				<view style="width: 100%;height:100%;">
+					<view style="color:red">
+						杩滅▼鎺у埗鎿嶄綔鍙兘瀛樺湪瀹夊叏椋庨櫓锛岃璋ㄦ厧鎿嶄綔
+					</view>
+					<u--input style="width: 100%;" placeholder="璇疯緭鍏ヤ簩绾у畨鍏ㄥ瘑鐮�" border="surround" password
 						clearable></u--input>
 				</view>
 			</slot>
@@ -187,6 +195,14 @@
 	export default {
 		data() {
 			return {
+				//涓婁釜椤甸潰浼犻�掕繃鏉ョ殑鏁版嵁
+				option: {},
+				//鎿嶄綔鎸囦护log
+				logList: [],
+				//褰撳墠璁惧杩炴帴鐘舵��
+				connected: false,
+				//寮�鍏虫満鎸囦护 1-寮�鏈� 2-鍏虫満
+				cmd: -1,
 				show: false,
 				swiperList: ["鐩戞祴", "鎺у埗"],
 				swiperCurrent: 0,
@@ -221,8 +237,8 @@
 				notice: "2023-03-1812:00:001鍙锋満鍙颁綆娓╂姤璀�",
 				model: {},
 				queryParam: {
-					machineid: "GM001",
-					tenantid: 1003
+					machineid: "",
+					tenantid: null
 				},
 				bannerList: [{
 					imageUrl: '../../static/image/zcy_gzj1.png'
@@ -232,11 +248,21 @@
 			}
 		},
 		onShow() {
+			console.info("onShow")
 			this.startTimer()
 
 		},
 
-		onLoad(e) {
+		onLoad(option) {
+			const userinfo = uni.getStorageSync('userinfo');
+			this.option = option
+			this.option.tenantid = userinfo.loginTenantId
+			this.option.username = userinfo.username
+
+
+			//鍒濆鍖栨煡璇㈠弬鏁�
+			this.queryParam.machineid = option.code
+			this.queryParam.tenantid = userinfo.loginTenantId
 
 		},
 		onBackPress() {
@@ -254,31 +280,131 @@
 		},
 		onReady() {
 			//this.getServerData()
+			// this.$mqttTool.message().then(res => {
+			// 	console.error("瑙f瀽鏁版嵁")
+			// 	if (res.topic == this.$constant.SERVICE_RES_EQU_STATU) {
+			// 		const msgstr = res.message.toString();
+			// 		const message = JSON.parse(msgstr);
+			// 		console.error(message)
+			// 		console.error(message.success)
+			// 	}
+			// })
+			let _this = this
+			this.$mqttTool.client.on('message', function(topic, message, buffer) {
+
+				console.error("瑙f瀽鏁版嵁")
+				const msg = JSON.parse(message);
+				console.error(topic)
+				console.error(msg)
+				console.error(msg.success)
+				if (topic == _this.$constant.SERVICE_RES_EQU_STATU.replace('equ', _this.deviceId)) {
+					if (msg.success) {
+						_this.connected = msg.connected
+					} else {
+						_this.connected = false
+					}
+				} else if (topic == _this.$constant.SERVICE_RES_EQU_CMD.replace('equ', _this.deviceId)) {
+					//鍙戦�佹寚浠ゆ敹鍒板搷搴�
+					//娣诲姞log
+					let log = this.getCurrentTime() +'杩斿洖锛�' +  + '鎸囦护' ;
+					this.logList.push(log)
+					
+					setTimeout(() => {
+						// 3绉掑悗鑷姩鍏抽棴
+						uni.showToast({
+							title: msg.msg,
+							icon: 'none',
+							mask: true
+						});
+						_this.show = false;
+					}, 1000)
+				}
+			})
+
+			this.queryEquStatu()
 
 		},
 		methods: {
-			showModal() {
+			//MQTT
+			queryEquStatu() {
+
+				//1.鏌ヨ璁惧瀹炴椂鐘舵��
+				const message = {
+					clientId: this.option.clientId,
+					timestamp: new Date(),
+					req: this.deviceId
+				}
+				let opts = {
+					topic: this.$constant.MOBILE_QUERY_EQU_STATU,
+					message: JSON.stringify(message),
+				}
+
+				this.$mqttTool.publish(opts).then(res => {
+					console.error(res)
+				})
+
+			},
+
+			sendEquCmd() {
+				//娣诲姞log
+				let log = this.getCurrentTime() +'鍙戦��' + (this.cmd == 1 ? "锛氥�婂紑鏈恒��" : "锛氥�婂叧鏈恒��") + '鎸囦护' ;
+				this.logList.push(log)
+		 
+				//1.鍙戦�佸紑鍏虫満鎸囦护 1-寮�鏈�  2-鍏虫満
+				const message = {
+					clientId: this.option.clientId,
+					cmd: this.cmd,
+					timestamp: new Date(),
+					req: this.deviceId
+				}
+				let opts = {
+					topic: this.$constant.MOBILE_REQ_EQU_CMD,
+					message: JSON.stringify(message),
+				}
+
+				this.$mqttTool.publish(opts).then(res => {
+					console.error(res)
+				})
+
+			},
+
+			showModal(cmd) {
 				this.show = true;
+				this.cmd = cmd;
 			},
 			confirm() {
-				setTimeout(() => {
-					// 3绉掑悗鑷姩鍏抽棴
-					uni.showToast({
-						title: '鎿嶄綔鎴愬姛~',
-						icon: 'none',
-						mask: true
-					});
-					this.show = false;
-				}, 3000)
+				this.sendEquCmd()
+				// setTimeout(() => {
+				// 	// 3绉掑悗鑷姩鍏抽棴
+				// 	uni.showToast({
+				// 		title: '鎿嶄綔鎴愬姛~',
+				// 		icon: 'none',
+				// 		mask: true
+				// 	});
+				// 	this.show = false;
+				// }, 3000)
+
+
+
+
 			},
 			startTimer() {
 				console.info("鐩戞帶椤甸潰鏄剧ず")
 				this.getRealData()
+				//涓嶉棿鏂煡璇㈠緟鎿嶄綔骞茬嚗鏈虹殑鐘舵�侊紝瀹炴椂鏇存柊
+				//this.queryEquStatu()
 				if (!this.$monitorTimer) {
 					this.$monitorTimer = setInterval(() => {
 						console.info("瀹氭椂鍣ㄥ伐浣�")
 						console.info(this.$monitorTimer)
 						this.getRealData()
+
+						/**
+						 * TODO 浣跨敤last_will浠f浛杞
+						 */
+						//涓嶉棿鏂煡璇㈠緟鎿嶄綔骞茬嚗鏈虹殑鐘舵�侊紝瀹炴椂鏇存柊   
+						//this.queryEquStatu()
+
 					}, 1000 * 12)
 				}
 
@@ -303,6 +429,20 @@
 				this.swiperCurrent = e.detail.current;
 				console.log(e.detail.current)
 				this.$refs.tabs.unlockDx();
+			},
+			getCurrentTime() {
+				// 鑾峰彇褰撳墠鏃堕棿  
+				let now = new Date();
+				// 鏍煎紡鍖栨椂闂�  
+				let year = now.getFullYear();
+				let month = (now.getMonth() + 1).toString().padStart(2, '0');
+				let day = now.getDate().toString().padStart(2, '0');
+				let hours = now.getHours().toString().padStart(2, '0');
+				let minutes = now.getMinutes().toString().padStart(2, '0');
+				let seconds = now.getSeconds().toString().padStart(2, '0');
+
+				// 杩斿洖缁撴灉  
+				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
 			},
 			getServerData() {
 				//妯℃嫙浠庢湇鍔″櫒鑾峰彇鏁版嵁鏃剁殑寤舵椂
@@ -452,6 +592,9 @@
 			}
 		},
 		computed: {
+			deviceId() {
+				return uni.getStorageSync(this.$constant.DEVICE_ID);
+			},
 			//杩愯鐘舵��
 			runStatu() {
 				let stop = this.$lget(this.model, 'stop');

--
Gitblit v1.9.3