From fe06b9677d585ec8fb57d1e6043610aab800a883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=89=E6=B5=A9=E8=8C=B9?= Date: Thu, 9 Oct 2025 18:27:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B8=A9=E6=B9=BF=E5=BA=A6pm=E4=BB=8E?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/environment/index.vue | 321 ++++++++++++++++++-------------- 1 file changed, 179 insertions(+), 142 deletions(-) diff --git a/src/pages/environment/index.vue b/src/pages/environment/index.vue index a08c296..4bc4d40 100644 --- a/src/pages/environment/index.vue +++ b/src/pages/environment/index.vue @@ -316,6 +316,8 @@ export default { acStatusList: ['待机', '启动中', '运行中', '关机中', '连接中'], // 空调控制相关 acControlLoading: false, // 空调控制按钮加载状态 + // 数据获取定时器 + dataFetchInterval: null } }, onLoad() { @@ -351,17 +353,24 @@ export default { if (this.debugInterval) { clearInterval(this.debugInterval) } + + // 清理数据获取定时器 + if (this.dataFetchInterval) { + clearInterval(this.dataFetchInterval) + } }, methods: { init() { // 获取最新空调温度 this.getLatestAirConditionerTemperature() // 获取最新温湿度数据 - this.getLatestWsdData() + this.getLatestEnvironmentData() // 首次进入系统时创建启动事件 this.createStartupEventIfNeeded() // 获取温湿度区间设置 this.loadWsdSettings() + // 启动定时获取环境数据 + this.startDataFetching() }, // 首次进入系统时创建启动事件 async createStartupEventIfNeeded() { @@ -470,26 +479,75 @@ export default { this.targetHumidity = 0; } }, - // 获取最新温湿度数据 - async getLatestWsdData() { + // 获取最新环境数据(温湿度、PM2.5) + async getLatestEnvironmentData() { try { + console.log('📊 开始获取最新环境数据...') const res = await wsdApi.getLatest(); - if (res.status === 'success') { - this.updateEnvironmentData({ - deviceType: 'WSD', - temperature: res.wd || 0, - humidity: res.sd || 0, - cleanliness: res.pm || 0, + console.log('📊 接口返回数据:', res) + + if (res && res.status === 'success') { + // 直接更新页面数据,不再依赖MQTT数据格式 + const temperature = res.wd; + const humidity = res.sd; + const pm25 = res.pm; + + // 更新温度数据 + if (temperature !== undefined && temperature !== 0) { + this.temperature = parseFloat(temperature.toFixed(1)) + this.temperatureProgress = Math.min(Math.max(this.temperature, 0), 100) + } + + // 更新湿度数据 + if (humidity !== undefined && humidity !== 0) { + this.humidity = parseFloat(humidity.toFixed(1)) + this.humidityProgress = Math.min(Math.max(this.humidity, 0), 100) + } + + // 更新PM2.5数据 + if (pm25 !== undefined && pm25 !== 0) { + this.cleanliness = parseFloat(pm25.toFixed(1)) + this.cleanlinessProgress = Math.min(Math.max(this.cleanliness, 0), 100) + } + + // 更新最后更新时间 + this.lastUpdate = new Date().toLocaleString('zh-CN') + + console.log('✅ 环境数据更新成功:', { + temperature: this.temperature, + humidity: this.humidity, + pm25: this.cleanliness, + time: this.lastUpdate + }) + + // 检查报警条件 + this.checkAlertsFromApiData({ + temperature: this.temperature, + humidity: this.humidity, + pm25: this.cleanliness }) } } catch (error) { - uni.showToast({ - title: '获取温湿度失败', - icon: 'none', - duration: 2000 - }) + console.error('❌ 获取环境数据失败:', error) + // 不显示错误提示,避免频繁弹窗 } }, + + // 启动定时获取环境数据 + startDataFetching() { + // 清除已存在的定时器 + if (this.dataFetchInterval) { + clearInterval(this.dataFetchInterval) + } + + // 每3秒获取一次最新环境数据 + this.dataFetchInterval = setInterval(() => { + this.getLatestEnvironmentData() + }, 5000) + + console.log('🔄 已启动环境数据定时获取,间隔3秒') + }, + // 初始化MQTT监听 initMqttListener() { // 监听数据更新 @@ -520,46 +578,29 @@ export default { }, 10000) // 每10秒检查一次 }, - // 更新环境数据 + // 更新环境数据(仅处理MQTT的空调数据) updateEnvironmentData(data) { - console.log('============data', 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) - } - - if (data.humidity !== undefined) { - this.humidity = parseFloat(data.humidity.toFixed(1)) - this.humidityProgress = Math.min(Math.max(this.humidity, 0), 100) - } - - this.lastUpdate = data.time || new Date().toLocaleString('zh-CN') - - // 检查报警条件,传入MQTT原始数据 - } else if (data.deviceType === 'AC') { + console.log('============MQTT数据', data) + + // 只处理空调AC数据,温湿度和PM25数据改为从接口获取 + if (data.deviceType === 'AC') { // 处理空调AC数据 if (data.faultStatus !== undefined) { this.acFaultStatus = data.faultStatus // 空调故障 } - if (data.rawData.status !== undefined) { + if (data.rawData && data.rawData.status !== undefined) { this.acStatus = data.rawData.status } - if (data.rawData.ctrlword !== undefined) { + if (data.rawData && data.rawData.ctrlword !== undefined) { this.acCtrlword = data.rawData.ctrlword } - } else if (data.deviceType === 'PM25') { - // 处理PM25数据 - if (data.rawData.PM25 !== undefined) { - this.cleanliness = data.rawData.PM25; - this.cleanlinessProgress = Math.min(Math.max(this.cleanliness, 0), 100) - } + + // 只对空调数据进行报警检查 + this.checkAlerts(data) } else { - console.log('⚠️ 非WSD、AC、PM25设备数据,跳过更新:', data.deviceType) - // 处理其他设备数据 + console.log('⚠️ 非AC设备数据,跳过MQTT处理:', data.deviceType) + // WSD和PM25数据现在从接口获取,不再通过MQTT处理 } - this.checkAlerts(data) }, // 格式化日期时间为指定格式 @@ -574,97 +615,16 @@ export default { return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` }, - // 检查报警条件 + // 检查MQTT空调数据的报警条件 checkAlerts(mqttData) { const currentTime = this.formatDateTime(new Date()) - console.log('====mqttData', mqttData) + console.log('====MQTT空调数据报警检查', mqttData) - // 只处理WSD设备的数据 - if (mqttData.deviceType === 'WSD') { - const mqttTemperature = mqttData.temperature - const mqttHumidity = mqttData.humidity - - if (mqttTemperature !== undefined && mqttTemperature !== 0 && this.temperatureRange.min !== 0 && this.temperatureRange.max !== 0) { - // 1. 温度报警:使用环境控制设置的区间(使用MQTT原始数据) - if (mqttTemperature < this.temperatureRange.min || mqttTemperature > this.temperatureRange.max) { - const alert = { - // content: `温度传感器异常,读数${mqttTemperature}°C超出控制范围${this.temperatureRange.min}°C-${this.temperatureRange.max}°C`, - content: `温度超出范围,${mqttTemperature}°C超出控制范围${this.temperatureRange.min}°C-${this.temperatureRange.max}°C`, - category: "温度报警", - alertTime: currentTime, - level: "中危", - action: "检查温度", - actionTime: currentTime, - deviceId: "WSD_001" - } - this.logAlert(alert) - } - } - - if (mqttHumidity !== undefined && mqttHumidity !==0 && this.humidityRange.min !==0 && this.humidityRange.max !==0) { - // 2. 湿度报警:使用环境控制设置的区间(使用MQTT原始数据) - if (mqttHumidity < this.humidityRange.min || mqttHumidity > this.humidityRange.max) { - const alert = { - // content: `湿度传感器异常,读数${mqttHumidity}%超出控制范围${this.humidityRange.min}%-${this.humidityRange.max}%`, - content: `湿度超出范围,${mqttHumidity}%超出控制范围${this.humidityRange.min}%-${this.humidityRange.max}%`, - category: "湿度报警", - alertTime: currentTime, - level: "中危", - action: "检查湿度", - actionTime: currentTime, - deviceId: "WSD_001" - } - this.logAlert(alert) - } - } - - // 4. 温度偏差报警:|温湿度计温度 - 空调设定温度| / 空调设定温度 > 30%(使用MQTT原始数据) - if (mqttTemperature !== undefined && this.targetTemperature > 0) { - const temperatureDiff = Math.abs(mqttTemperature - this.targetTemperature) - const deviationPercent = (temperatureDiff / this.targetTemperature) * 100 - - if (deviationPercent > 30) { - const alert = { - // content: `温度偏差过大,实际${mqttTemperature}°C与设定${this.targetTemperature}°C偏差${deviationPercent.toFixed(1)}%`, - content: `温度偏差过大,${mqttTemperature}°C与设定${this.targetTemperature}°C偏差${deviationPercent.toFixed(1)}%`, - category: "温度控制异常", - alertTime: currentTime, - level: "中危", - action: "调整空调设定温度", - actionTime: currentTime, - deviceId: "AC_001" - } - this.logAlert(alert) - } - } - - // 5. 湿度偏差报警:|温湿度计湿度 - 空调设定湿度| / 空调设定湿度 > 30%(使用MQTT原始数据) - if (mqttHumidity !== undefined && this.targetHumidity > 0) { - const humidityDiff = Math.abs(mqttHumidity - this.targetHumidity) - const deviationPercent = (humidityDiff / this.targetHumidity) * 100 - - if (deviationPercent > 30) { - const alert = { - // content: `湿度偏差过大,实际${mqttHumidity}%与设定${this.targetHumidity}%偏差${deviationPercent.toFixed(1)}%`, - content: `湿度偏差过大,${mqttHumidity}%与设定${this.targetHumidity}%偏差${deviationPercent.toFixed(1)}%`, - category: "湿度控制异常", - alertTime: currentTime, - level: "中危", - action: "调整空调设定湿度", - actionTime: currentTime, - deviceId: "AC_001" - } - this.logAlert(alert) - } - } - return - } - + // 只处理空调AC设备的数据 if (mqttData.deviceType === 'AC') { - // 3. 空调故障报警:acFaultStatus为1 + // 空调故障报警:acFaultStatus为1 if (this.acFaultStatus === 1) { const alert = { - // content: "空调故障,需要手动设置温度", content: "空调报警", category: "空调故障", alertTime: currentTime, @@ -676,6 +636,85 @@ export default { this.logAlert(alert) } } + // 注意:WSD和PM25数据的报警检查现在在checkAlertsFromApiData方法中处理 + }, + + // 检查从接口获取的数据的报警条件 + checkAlertsFromApiData(apiData) { + const currentTime = this.formatDateTime(new Date()) + console.log('====API数据报警检查', apiData) + + const { temperature, humidity, pm25 } = apiData + + // 1. 温度报警:使用环境控制设置的区间 + if (temperature !== undefined && temperature !== 0 && this.temperatureRange.min !== 0 && this.temperatureRange.max !== 0) { + if (temperature < this.temperatureRange.min || temperature > this.temperatureRange.max) { + const alert = { + content: `温度超出范围,${temperature}°C超出控制范围${this.temperatureRange.min}°C-${this.temperatureRange.max}°C`, + category: "温度报警", + alertTime: currentTime, + level: "中危", + action: "检查温度", + actionTime: currentTime, + deviceId: "WSD_001" + } + this.logAlert(alert) + } + } + + // 2. 湿度报警:使用环境控制设置的区间 + if (humidity !== undefined && humidity !== 0 && this.humidityRange.min !== 0 && this.humidityRange.max !== 0) { + if (humidity < this.humidityRange.min || humidity > this.humidityRange.max) { + const alert = { + content: `湿度超出范围,${humidity}%超出控制范围${this.humidityRange.min}%-${this.humidityRange.max}%`, + category: "湿度报警", + alertTime: currentTime, + level: "中危", + action: "检查湿度", + actionTime: currentTime, + deviceId: "WSD_001" + } + this.logAlert(alert) + } + } + + // 3. 温度偏差报警:|温湿度计温度 - 空调设定温度| / 空调设定温度 > 30% + if (temperature !== undefined && this.targetTemperature > 0) { + const temperatureDiff = Math.abs(temperature - this.targetTemperature) + const deviationPercent = (temperatureDiff / this.targetTemperature) * 100 + + if (deviationPercent > 30) { + const alert = { + content: `温度偏差过大,${temperature}°C与设定${this.targetTemperature}°C偏差${deviationPercent.toFixed(1)}%`, + category: "温度控制异常", + alertTime: currentTime, + level: "中危", + action: "调整空调设定温度", + actionTime: currentTime, + deviceId: "AC_001" + } + this.logAlert(alert) + } + } + + // 4. 湿度偏差报警:|温湿度计湿度 - 空调设定湿度| / 空调设定湿度 > 30% + if (humidity !== undefined && this.targetHumidity > 0) { + const humidityDiff = Math.abs(humidity - this.targetHumidity) + const deviationPercent = (humidityDiff / this.targetHumidity) * 100 + + if (deviationPercent > 30) { + const alert = { + content: `湿度偏差过大,${humidity}%与设定${this.targetHumidity}%偏差${deviationPercent.toFixed(1)}%`, + category: "湿度控制异常", + alertTime: currentTime, + level: "中危", + action: "调整空调设定湿度", + actionTime: currentTime, + deviceId: "AC_001" + } + this.logAlert(alert) + } + } }, // 记录报警到控制台并调用创建告警接口 @@ -865,8 +904,8 @@ export default { return } - if (numValue < 30) { - this.humidityValidationMessage = '湿度不能低于30%' + if (numValue < 10) { + this.humidityValidationMessage = '湿度不能低于10%' this.humidityValidationClass = 'error' return } @@ -893,13 +932,12 @@ export default { // 发送空调参数到MQTT this.sendAirConditionerParams() - // 湿度变化后检查报警(使用当前页面数据模拟MQTT数据) - const mockMqttData = { - deviceType: 'WSD', + // 湿度变化后检查报警(使用当前页面数据) + this.checkAlertsFromApiData({ temperature: this.temperature, - humidity: this.humidity - } - this.checkAlerts(mockMqttData) + humidity: this.humidity, + pm25: this.cleanliness + }) }, // 空调温度更新 @@ -907,13 +945,12 @@ export default { // 发送空调参数到MQTT this.sendAirConditionerParams() - // 温度变化后检查报警(使用当前页面数据模拟MQTT数据) - const mockMqttData = { - deviceType: 'WSD', + // 温度变化后检查报警(使用当前页面数据) + this.checkAlertsFromApiData({ temperature: this.temperature, - humidity: this.humidity - } - this.checkAlerts(mockMqttData) + humidity: this.humidity, + pm25: this.cleanliness + }) }, // 发送空调参数