feat:对接参数记录echart、空调度数获取设置
This commit is contained in:
@ -202,6 +202,7 @@
|
||||
<script>
|
||||
import mqttDataManager from '@/utils/mqttDataManager.js'
|
||||
import { manualReconnect, sendMqttData } from '@/utils/sendMqtt.js'
|
||||
import { thDataApi } from '@/utils/api.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@ -233,110 +234,140 @@ export default {
|
||||
isConnected: false,
|
||||
lastUpdate: null
|
||||
},
|
||||
targetTemperature: 30
|
||||
targetTemperature: 30,
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
console.log('环境参数页面加载')
|
||||
this.initMqttListener()
|
||||
// 获取最新空调温度
|
||||
this.getLatestAirConditionerTemperature()
|
||||
},
|
||||
onUnload() {
|
||||
console.log('🔌 环境参数页面卸载,清理资源...')
|
||||
// console.log('🔌 环境参数页面卸载,清理资源...')
|
||||
|
||||
// 页面卸载时移除监听器
|
||||
if (this.dataUpdateHandler) {
|
||||
mqttDataManager.removeListener('dataUpdate', this.dataUpdateHandler)
|
||||
console.log('✅ 数据更新监听器已移除')
|
||||
// console.log('✅ 数据更新监听器已移除')
|
||||
}
|
||||
if (this.statusUpdateHandler) {
|
||||
mqttDataManager.removeListener('connectionStatus', this.statusUpdateHandler)
|
||||
console.log('✅ 状态更新监听器已移除')
|
||||
// console.log('✅ 状态更新监听器已移除')
|
||||
}
|
||||
|
||||
// 清理调试定时器
|
||||
if (this.debugInterval) {
|
||||
clearInterval(this.debugInterval)
|
||||
console.log('✅ 调试定时器已清理')
|
||||
// console.log('✅ 调试定时器已清理')
|
||||
}
|
||||
|
||||
console.log('✅ 环境参数页面资源清理完成')
|
||||
// console.log('✅ 环境参数页面资源清理完成')
|
||||
},
|
||||
methods: {
|
||||
// 获取最新空调温度
|
||||
async getLatestAirConditionerTemperature() {
|
||||
try {
|
||||
console.log('🌡️ 开始获取最新空调温度...')
|
||||
const res = await thDataApi.getLatest();
|
||||
if (res.status === 'success') {
|
||||
this.targetTemperature = res.temperature;
|
||||
// this.humidity = res.humidity;
|
||||
// this.updateEnvironmentData({
|
||||
// temperature: res.temperature,
|
||||
// humidity: res.humidity,
|
||||
// deviceType: 'WSD'
|
||||
// })
|
||||
}
|
||||
// {
|
||||
// "status": "success",
|
||||
// "message": "获取成功",
|
||||
// "temperature": 25.5,
|
||||
// "humidity": 60.2,
|
||||
// "deviceId": "TH_SENSOR_001",
|
||||
// "createTime": "2025-09-30T15:55:13"
|
||||
// }
|
||||
console.log('✅ 最新空调温度数据:', res)
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 获取最新空调温度失败:', error)
|
||||
}
|
||||
},
|
||||
// 初始化MQTT监听
|
||||
initMqttListener() {
|
||||
console.log('🔧 环境参数页面开始初始化MQTT监听...')
|
||||
// console.log('🔧 环境参数页面开始初始化MQTT监听...')
|
||||
|
||||
// 监听数据更新
|
||||
this.dataUpdateHandler = (data) => {
|
||||
console.log('📨 环境参数页面收到MQTT数据:', data)
|
||||
// console.log('📨 环境参数页面收到MQTT数据:', data)
|
||||
this.updateEnvironmentData(data)
|
||||
}
|
||||
mqttDataManager.addListener('dataUpdate', this.dataUpdateHandler)
|
||||
|
||||
// 监听连接状态
|
||||
this.statusUpdateHandler = (status) => {
|
||||
console.log('🔄 环境参数页面连接状态更新:', status)
|
||||
// console.log('🔄 环境参数页面连接状态更新:', status)
|
||||
const wasConnected = this.connectionStatus.isConnected
|
||||
this.connectionStatus = status
|
||||
|
||||
// 只在状态发生变化时显示提示(避免重复提示)
|
||||
if (wasConnected !== status.isConnected) {
|
||||
if (status.isConnected) {
|
||||
console.log('✅ MQTT连接状态: 已连接')
|
||||
// 不显示Toast,因为sendMqtt.js中已经显示了
|
||||
} else {
|
||||
console.log('❌ MQTT连接状态: 未连接')
|
||||
// 不显示Toast,因为sendMqtt.js中已经显示了
|
||||
}
|
||||
// if (status.isConnected) {
|
||||
// console.log('✅ MQTT连接状态: 已连接')
|
||||
// // 不显示Toast,因为sendMqtt.js中已经显示了
|
||||
// } else {
|
||||
// console.log('❌ MQTT连接状态: 未连接')
|
||||
// // 不显示Toast,因为sendMqtt.js中已经显示了
|
||||
// }
|
||||
}
|
||||
}
|
||||
mqttDataManager.addListener('connectionStatus', this.statusUpdateHandler)
|
||||
|
||||
// 获取初始数据
|
||||
const lastData = mqttDataManager.getLastData()
|
||||
console.log('📊 获取初始数据:', lastData)
|
||||
// console.log('📊 获取初始数据:', lastData)
|
||||
if (lastData.timestamp) {
|
||||
this.updateEnvironmentData(lastData)
|
||||
}
|
||||
|
||||
// 获取初始连接状态
|
||||
this.connectionStatus = mqttDataManager.getConnectionStatus()
|
||||
console.log('🔍 初始连接状态:', this.connectionStatus)
|
||||
// console.log('🔍 初始连接状态:', this.connectionStatus)
|
||||
|
||||
// 定期检查连接状态(用于调试)
|
||||
this.debugInterval = setInterval(() => {
|
||||
const currentStatus = mqttDataManager.getConnectionStatus()
|
||||
console.log('🔍 定期检查连接状态:', currentStatus)
|
||||
// console.log('🔍 定期检查连接状态:', currentStatus)
|
||||
}, 10000) // 每10秒检查一次
|
||||
|
||||
console.log('✅ 环境参数页面MQTT监听初始化完成')
|
||||
// console.log('✅ 环境参数页面MQTT监听初始化完成')
|
||||
},
|
||||
|
||||
// 更新环境数据
|
||||
updateEnvironmentData(data) {
|
||||
console.log('🌡️ 环境参数页面更新数据:', data)
|
||||
// console.log('🌡️ 环境参数页面更新数据:', data)
|
||||
|
||||
// 只处理WSD设备的数据
|
||||
if (data.deviceType === 'WSD') {
|
||||
if (data.temperature !== undefined) {
|
||||
this.temperature = parseFloat(data.temperature.toFixed(1))
|
||||
this.temperatureProgress = Math.min(Math.max(this.temperature, 0), 100)
|
||||
console.log('✅ 温度已更新:', this.temperature)
|
||||
// console.log('✅ 温度已更新:', this.temperature)
|
||||
}
|
||||
|
||||
if (data.humidity !== undefined) {
|
||||
this.humidity = parseFloat(data.humidity.toFixed(1))
|
||||
this.humidityProgress = Math.min(Math.max(this.humidity, 0), 100)
|
||||
console.log('✅ 湿度已更新:', this.humidity)
|
||||
// console.log('✅ 湿度已更新:', this.humidity)
|
||||
}
|
||||
|
||||
this.lastUpdate = data.time || new Date().toLocaleString('zh-CN')
|
||||
|
||||
console.log('✅ 环境数据更新完成:', {
|
||||
temperature: this.temperature,
|
||||
humidity: this.humidity,
|
||||
lastUpdate: this.lastUpdate
|
||||
})
|
||||
// console.log('✅ 环境数据更新完成:', {
|
||||
// temperature: this.temperature,
|
||||
// humidity: this.humidity,
|
||||
// lastUpdate: this.lastUpdate
|
||||
// })
|
||||
} else {
|
||||
console.log('⚠️ 非WSD设备数据,跳过更新:', data.deviceType)
|
||||
}
|
||||
@ -372,18 +403,18 @@ export default {
|
||||
|
||||
// 显示温度变化提示
|
||||
showTemperatureChangeToast() {
|
||||
uni.showToast({
|
||||
title: `目标温度: ${this.targetTemperature}°C`,
|
||||
icon: 'success',
|
||||
duration: 1500
|
||||
})
|
||||
// uni.showToast({
|
||||
// title: `目标温度: ${this.targetTemperature}°C`,
|
||||
// icon: 'success',
|
||||
// duration: 1500
|
||||
// })
|
||||
|
||||
// 发送空调参数到MQTT
|
||||
this.sendAirConditionerParams()
|
||||
},
|
||||
|
||||
// 发送空调参数
|
||||
sendAirConditionerParams() {
|
||||
async sendAirConditionerParams() {
|
||||
const airConditionerData = {
|
||||
"TagValue": this.targetTemperature,
|
||||
"TagName": "JS_COD",
|
||||
@ -397,10 +428,28 @@ export default {
|
||||
|
||||
if (success) {
|
||||
console.log('✅ 空调参数发送请求已提交')
|
||||
|
||||
// 发送成功后调用提交温湿度数据API
|
||||
await this.submitTemperatureData()
|
||||
|
||||
} else {
|
||||
console.log('❌ 空调参数发送失败')
|
||||
}
|
||||
},
|
||||
|
||||
// 提交温湿度数据
|
||||
async submitTemperatureData() {
|
||||
try {
|
||||
const temperatureData = {
|
||||
temperature: this.targetTemperature,
|
||||
deviceId: "TH_SENSOR_001",
|
||||
humidity: 0,
|
||||
}
|
||||
await thDataApi.submit(temperatureData)
|
||||
} catch (error) {
|
||||
console.error('❌ 温湿度数据提交失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 手动重连MQTT
|
||||
manualReconnect() {
|
||||
|
||||
Reference in New Issue
Block a user