fix:温湿度pm从接口取
This commit is contained in:
@ -316,6 +316,8 @@ export default {
|
|||||||
acStatusList: ['待机', '启动中', '运行中', '关机中', '连接中'],
|
acStatusList: ['待机', '启动中', '运行中', '关机中', '连接中'],
|
||||||
// 空调控制相关
|
// 空调控制相关
|
||||||
acControlLoading: false, // 空调控制按钮加载状态
|
acControlLoading: false, // 空调控制按钮加载状态
|
||||||
|
// 数据获取定时器
|
||||||
|
dataFetchInterval: null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onLoad() {
|
onLoad() {
|
||||||
@ -351,17 +353,24 @@ export default {
|
|||||||
if (this.debugInterval) {
|
if (this.debugInterval) {
|
||||||
clearInterval(this.debugInterval)
|
clearInterval(this.debugInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理数据获取定时器
|
||||||
|
if (this.dataFetchInterval) {
|
||||||
|
clearInterval(this.dataFetchInterval)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
init() {
|
init() {
|
||||||
// 获取最新空调温度
|
// 获取最新空调温度
|
||||||
this.getLatestAirConditionerTemperature()
|
this.getLatestAirConditionerTemperature()
|
||||||
// 获取最新温湿度数据
|
// 获取最新温湿度数据
|
||||||
this.getLatestWsdData()
|
this.getLatestEnvironmentData()
|
||||||
// 首次进入系统时创建启动事件
|
// 首次进入系统时创建启动事件
|
||||||
this.createStartupEventIfNeeded()
|
this.createStartupEventIfNeeded()
|
||||||
// 获取温湿度区间设置
|
// 获取温湿度区间设置
|
||||||
this.loadWsdSettings()
|
this.loadWsdSettings()
|
||||||
|
// 启动定时获取环境数据
|
||||||
|
this.startDataFetching()
|
||||||
},
|
},
|
||||||
// 首次进入系统时创建启动事件
|
// 首次进入系统时创建启动事件
|
||||||
async createStartupEventIfNeeded() {
|
async createStartupEventIfNeeded() {
|
||||||
@ -470,26 +479,75 @@ export default {
|
|||||||
this.targetHumidity = 0;
|
this.targetHumidity = 0;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// 获取最新温湿度数据
|
// 获取最新环境数据(温湿度、PM2.5)
|
||||||
async getLatestWsdData() {
|
async getLatestEnvironmentData() {
|
||||||
try {
|
try {
|
||||||
|
console.log('📊 开始获取最新环境数据...')
|
||||||
const res = await wsdApi.getLatest();
|
const res = await wsdApi.getLatest();
|
||||||
if (res.status === 'success') {
|
console.log('📊 接口返回数据:', res)
|
||||||
this.updateEnvironmentData({
|
|
||||||
deviceType: 'WSD',
|
if (res && res.status === 'success') {
|
||||||
temperature: res.wd || 0,
|
// 直接更新页面数据,不再依赖MQTT数据格式
|
||||||
humidity: res.sd || 0,
|
const temperature = res.wd;
|
||||||
cleanliness: res.pm || 0,
|
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) {
|
} catch (error) {
|
||||||
uni.showToast({
|
console.error('❌ 获取环境数据失败:', error)
|
||||||
title: '获取温湿度失败',
|
// 不显示错误提示,避免频繁弹窗
|
||||||
icon: 'none',
|
|
||||||
duration: 2000
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 启动定时获取环境数据
|
||||||
|
startDataFetching() {
|
||||||
|
// 清除已存在的定时器
|
||||||
|
if (this.dataFetchInterval) {
|
||||||
|
clearInterval(this.dataFetchInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每3秒获取一次最新环境数据
|
||||||
|
this.dataFetchInterval = setInterval(() => {
|
||||||
|
this.getLatestEnvironmentData()
|
||||||
|
}, 5000)
|
||||||
|
|
||||||
|
console.log('🔄 已启动环境数据定时获取,间隔3秒')
|
||||||
|
},
|
||||||
|
|
||||||
// 初始化MQTT监听
|
// 初始化MQTT监听
|
||||||
initMqttListener() {
|
initMqttListener() {
|
||||||
// 监听数据更新
|
// 监听数据更新
|
||||||
@ -520,46 +578,29 @@ export default {
|
|||||||
}, 10000) // 每10秒检查一次
|
}, 10000) // 每10秒检查一次
|
||||||
},
|
},
|
||||||
|
|
||||||
// 更新环境数据
|
// 更新环境数据(仅处理MQTT的空调数据)
|
||||||
updateEnvironmentData(data) {
|
updateEnvironmentData(data) {
|
||||||
console.log('============data', data)
|
console.log('============MQTT数据', data)
|
||||||
// 处理WSD设备的数据
|
|
||||||
if (data.deviceType === 'WSD') {
|
// 只处理空调AC数据,温湿度和PM25数据改为从接口获取
|
||||||
if (data.temperature !== undefined) {
|
if (data.deviceType === 'AC') {
|
||||||
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') {
|
|
||||||
// 处理空调AC数据
|
// 处理空调AC数据
|
||||||
if (data.faultStatus !== undefined) {
|
if (data.faultStatus !== undefined) {
|
||||||
this.acFaultStatus = data.faultStatus // 空调故障
|
this.acFaultStatus = data.faultStatus // 空调故障
|
||||||
}
|
}
|
||||||
if (data.rawData.status !== undefined) {
|
if (data.rawData && data.rawData.status !== undefined) {
|
||||||
this.acStatus = data.rawData.status
|
this.acStatus = data.rawData.status
|
||||||
}
|
}
|
||||||
if (data.rawData.ctrlword !== undefined) {
|
if (data.rawData && data.rawData.ctrlword !== undefined) {
|
||||||
this.acCtrlword = data.rawData.ctrlword
|
this.acCtrlword = data.rawData.ctrlword
|
||||||
}
|
}
|
||||||
} else if (data.deviceType === 'PM25') {
|
|
||||||
// 处理PM25数据
|
// 只对空调数据进行报警检查
|
||||||
if (data.rawData.PM25 !== undefined) {
|
this.checkAlerts(data)
|
||||||
this.cleanliness = data.rawData.PM25;
|
|
||||||
this.cleanlinessProgress = Math.min(Math.max(this.cleanliness, 0), 100)
|
|
||||||
}
|
|
||||||
} else {
|
} 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}`
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
|
||||||
},
|
},
|
||||||
|
|
||||||
// 检查报警条件
|
// 检查MQTT空调数据的报警条件
|
||||||
checkAlerts(mqttData) {
|
checkAlerts(mqttData) {
|
||||||
const currentTime = this.formatDateTime(new Date())
|
const currentTime = this.formatDateTime(new Date())
|
||||||
console.log('====mqttData', mqttData)
|
console.log('====MQTT空调数据报警检查', mqttData)
|
||||||
|
|
||||||
// 只处理WSD设备的数据
|
// 只处理空调AC设备的数据
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mqttData.deviceType === 'AC') {
|
if (mqttData.deviceType === 'AC') {
|
||||||
// 3. 空调故障报警:acFaultStatus为1
|
// 空调故障报警:acFaultStatus为1
|
||||||
if (this.acFaultStatus === 1) {
|
if (this.acFaultStatus === 1) {
|
||||||
const alert = {
|
const alert = {
|
||||||
// content: "空调故障,需要手动设置温度",
|
|
||||||
content: "空调报警",
|
content: "空调报警",
|
||||||
category: "空调故障",
|
category: "空调故障",
|
||||||
alertTime: currentTime,
|
alertTime: currentTime,
|
||||||
@ -676,6 +636,85 @@ export default {
|
|||||||
this.logAlert(alert)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numValue < 30) {
|
if (numValue < 10) {
|
||||||
this.humidityValidationMessage = '湿度不能低于30%'
|
this.humidityValidationMessage = '湿度不能低于10%'
|
||||||
this.humidityValidationClass = 'error'
|
this.humidityValidationClass = 'error'
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -893,13 +932,12 @@ export default {
|
|||||||
// 发送空调参数到MQTT
|
// 发送空调参数到MQTT
|
||||||
this.sendAirConditionerParams()
|
this.sendAirConditionerParams()
|
||||||
|
|
||||||
// 湿度变化后检查报警(使用当前页面数据模拟MQTT数据)
|
// 湿度变化后检查报警(使用当前页面数据)
|
||||||
const mockMqttData = {
|
this.checkAlertsFromApiData({
|
||||||
deviceType: 'WSD',
|
|
||||||
temperature: this.temperature,
|
temperature: this.temperature,
|
||||||
humidity: this.humidity
|
humidity: this.humidity,
|
||||||
}
|
pm25: this.cleanliness
|
||||||
this.checkAlerts(mockMqttData)
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
// 空调温度更新
|
// 空调温度更新
|
||||||
@ -907,13 +945,12 @@ export default {
|
|||||||
// 发送空调参数到MQTT
|
// 发送空调参数到MQTT
|
||||||
this.sendAirConditionerParams()
|
this.sendAirConditionerParams()
|
||||||
|
|
||||||
// 温度变化后检查报警(使用当前页面数据模拟MQTT数据)
|
// 温度变化后检查报警(使用当前页面数据)
|
||||||
const mockMqttData = {
|
this.checkAlertsFromApiData({
|
||||||
deviceType: 'WSD',
|
|
||||||
temperature: this.temperature,
|
temperature: this.temperature,
|
||||||
humidity: this.humidity
|
humidity: this.humidity,
|
||||||
}
|
pm25: this.cleanliness
|
||||||
this.checkAlerts(mockMqttData)
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
// 发送空调参数
|
// 发送空调参数
|
||||||
|
|||||||
Reference in New Issue
Block a user