feat:日志报警页面更新
This commit is contained in:
@ -52,7 +52,8 @@
|
||||
<view class="detail-progress-fill temperature-progress" :style="{ width: temperatureProgress + '%' }"></view>
|
||||
</view>
|
||||
<view class="detail-range">
|
||||
<text class="detail-range-text">{{ temperatureRange.min }}°C - {{ temperatureRange.max }}°C</text>
|
||||
<!-- <text class="detail-range-text">{{ temperatureRange.min }}°C - {{ temperatureRange.max }}°C</text> -->
|
||||
<text class="detail-range-text">{{ 0 }}°C - {{ 100 }}°C</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@ -66,7 +67,8 @@
|
||||
<view class="detail-progress-fill humidity-progress" :style="{ width: humidityProgress + '%' }"></view>
|
||||
</view>
|
||||
<view class="detail-range">
|
||||
<text class="detail-range-text">{{ humidityRange.min }}% - {{ humidityRange.max }}%</text>
|
||||
<!-- <text class="detail-range-text">{{ humidityRange.min }}% - {{ humidityRange.max }}%</text> -->
|
||||
<text class="detail-range-text">{{ 0 }}% - {{ 100 }}%</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@ -80,7 +82,8 @@
|
||||
<view class="detail-progress-fill cleanliness-progress" :style="{ width: cleanlinessProgress + '%' }"></view>
|
||||
</view>
|
||||
<view class="detail-range">
|
||||
<text class="detail-range-text">暂无数据</text>
|
||||
<!-- <text class="detail-range-text">暂无数据</text> -->
|
||||
<text class="detail-range-text">{{ 0 }}% - {{ 100 }}%</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@ -121,12 +124,28 @@
|
||||
|
||||
<view class="control-ranges">
|
||||
<view class="range-item">
|
||||
<text class="range-label">温度控制</text>
|
||||
<text class="range-value">{{ temperatureRange.min }}°C - {{ temperatureRange.max }}°C</text>
|
||||
<view class="range-header">
|
||||
<text class="range-label">温度控制</text>
|
||||
<view class="range-status" :class="getTemperatureStatus().class">
|
||||
<text class="status-text">{{ getTemperatureStatus().text }}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="range-content">
|
||||
<text class="current-value">当前: {{ temperature }}°C</text>
|
||||
<text class="range-value">范围: {{ temperatureRange.min }}°C - {{ temperatureRange.max }}°C</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="range-item">
|
||||
<text class="range-label">湿度控制</text>
|
||||
<text class="range-value">{{ humidityRange.min }}% - {{ humidityRange.max }}%</text>
|
||||
<view class="range-header">
|
||||
<text class="range-label">湿度控制</text>
|
||||
<view class="range-status" :class="getHumidityStatus().class">
|
||||
<text class="status-text">{{ getHumidityStatus().text }}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="range-content">
|
||||
<text class="current-value">当前: {{ humidity }}%</text>
|
||||
<text class="range-value">范围: {{ humidityRange.min }}% - {{ humidityRange.max }}%</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@ -202,7 +221,7 @@
|
||||
<script>
|
||||
import mqttDataManager from '@/utils/mqttDataManager.js'
|
||||
import { manualReconnect, sendMqttData } from '@/utils/sendMqtt.js'
|
||||
import { thDataApi } from '@/utils/api.js'
|
||||
import { thDataApi, alertApi, eventApi } from '@/utils/api.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@ -215,33 +234,56 @@ export default {
|
||||
cleanlinessProgress: 0,
|
||||
lastUpdate: '暂无数据',
|
||||
temperatureRange: {
|
||||
min: 0,
|
||||
max: 100
|
||||
min: 25,
|
||||
max: 35
|
||||
},
|
||||
humidityRange: {
|
||||
min: 0,
|
||||
max: 100
|
||||
min: 40,
|
||||
max: 70
|
||||
},
|
||||
tempSettings: {
|
||||
min: 25,
|
||||
max: 35
|
||||
},
|
||||
humiditySettings: {
|
||||
min: 0,
|
||||
max: 100
|
||||
min: 40,
|
||||
max: 70
|
||||
},
|
||||
connectionStatus: {
|
||||
isConnected: false,
|
||||
lastUpdate: null
|
||||
},
|
||||
targetTemperature: 30,
|
||||
targetHumidity: 50, // 空调设定湿度
|
||||
// 报警相关数据
|
||||
acFaultStatus: 0, // 空调故障状态,1表示故障
|
||||
alertHistory: [], // 报警历史记录
|
||||
// 系统启动事件相关
|
||||
hasCreatedStartupEvent: false, // 是否已创建启动事件
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
console.log('环境参数页面加载')
|
||||
|
||||
// 从本地存储读取是否已创建启动事件的状态
|
||||
const hasCreatedStartupEvent = uni.getStorageSync('hasCreatedStartupEvent')
|
||||
if (hasCreatedStartupEvent) {
|
||||
this.hasCreatedStartupEvent = true
|
||||
console.log('📱 从本地存储读取到启动事件状态: 已创建')
|
||||
}
|
||||
|
||||
this.initMqttListener()
|
||||
// 获取最新空调温度
|
||||
this.getLatestAirConditionerTemperature()
|
||||
// 首次进入系统时创建启动事件
|
||||
this.createStartupEventIfNeeded()
|
||||
},
|
||||
onShow() {
|
||||
console.log('📱 环境参数页面显示,触发页面更新')
|
||||
// 只有在非首次显示时才重新获取最新空调温度
|
||||
if (this.hasCreatedStartupEvent) {
|
||||
this.getLatestAirConditionerTemperature()
|
||||
}
|
||||
},
|
||||
onUnload() {
|
||||
// console.log('🔌 环境参数页面卸载,清理资源...')
|
||||
@ -265,6 +307,67 @@ export default {
|
||||
// console.log('✅ 环境参数页面资源清理完成')
|
||||
},
|
||||
methods: {
|
||||
// 首次进入系统时创建启动事件
|
||||
async createStartupEventIfNeeded() {
|
||||
// 检查是否已经创建过启动事件
|
||||
if (this.hasCreatedStartupEvent) {
|
||||
console.log('✅ 启动事件已存在,跳过创建')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('🚀 首次进入系统,创建启动事件...')
|
||||
|
||||
const currentTime = this.formatDateTime(new Date())
|
||||
const startupEvent = {
|
||||
eventType: "设备重启",
|
||||
eventTime: currentTime,
|
||||
status: "已完成",
|
||||
description: "设备正常重启维护",
|
||||
deviceId: "AC_001"
|
||||
}
|
||||
|
||||
console.log('📤 提交启动事件:', startupEvent)
|
||||
const response = await eventApi.create(startupEvent)
|
||||
console.log('✅ 启动事件创建成功:', response)
|
||||
|
||||
// 标记已创建启动事件
|
||||
this.hasCreatedStartupEvent = true
|
||||
|
||||
// 可以保存到本地存储,避免刷新页面后重复创建
|
||||
uni.setStorageSync('hasCreatedStartupEvent', true)
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 启动事件创建失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 获取温度控制状态
|
||||
getTemperatureStatus() {
|
||||
if (this.temperature === 0) {
|
||||
return { text: '无数据', class: 'no-data' }
|
||||
} else if (this.temperature >= this.temperatureRange.min && this.temperature <= this.temperatureRange.max) {
|
||||
return { text: '正常', class: 'normal' }
|
||||
} else if (this.temperature < this.temperatureRange.min) {
|
||||
return { text: '偏低', class: 'low' }
|
||||
} else {
|
||||
return { text: '偏高', class: 'high' }
|
||||
}
|
||||
},
|
||||
|
||||
// 获取湿度控制状态
|
||||
getHumidityStatus() {
|
||||
if (this.humidity === 0) {
|
||||
return { text: '无数据', class: 'no-data' }
|
||||
} else if (this.humidity >= this.humidityRange.min && this.humidity <= this.humidityRange.max) {
|
||||
return { text: '正常', class: 'normal' }
|
||||
} else if (this.humidity < this.humidityRange.min) {
|
||||
return { text: '偏低', class: 'low' }
|
||||
} else {
|
||||
return { text: '偏高', class: 'high' }
|
||||
}
|
||||
},
|
||||
|
||||
// 获取最新空调温度
|
||||
async getLatestAirConditionerTemperature() {
|
||||
try {
|
||||
@ -347,6 +450,11 @@ export default {
|
||||
updateEnvironmentData(data) {
|
||||
// console.log('🌡️ 环境参数页面更新数据:', data)
|
||||
|
||||
// 处理空调故障状态
|
||||
if (data.deviceType === 'AC' && data.faultStatus !== undefined) {
|
||||
this.acFaultStatus = data.faultStatus
|
||||
}
|
||||
|
||||
// 只处理WSD设备的数据
|
||||
if (data.deviceType === 'WSD') {
|
||||
if (data.temperature !== undefined) {
|
||||
@ -363,6 +471,9 @@ export default {
|
||||
|
||||
this.lastUpdate = data.time || new Date().toLocaleString('zh-CN')
|
||||
|
||||
// 检查报警条件,传入MQTT原始数据
|
||||
this.checkAlerts(data)
|
||||
|
||||
// console.log('✅ 环境数据更新完成:', {
|
||||
// temperature: this.temperature,
|
||||
// humidity: this.humidity,
|
||||
@ -373,6 +484,137 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
// 格式化日期时间为指定格式
|
||||
formatDateTime(date) {
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
const hours = String(date.getHours()).padStart(2, '0')
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
|
||||
},
|
||||
|
||||
// 检查报警条件
|
||||
checkAlerts(mqttData) {
|
||||
const currentTime = this.formatDateTime(new Date())
|
||||
|
||||
// 只处理WSD设备的数据
|
||||
if (mqttData.deviceType !== 'WSD') {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取MQTT原始数据
|
||||
const mqttTemperature = mqttData.temperature
|
||||
const mqttHumidity = mqttData.humidity
|
||||
|
||||
// 1. 温度报警:使用环境控制设置的区间(使用MQTT原始数据)
|
||||
if (mqttTemperature !== undefined && (mqttTemperature < this.temperatureRange.min || mqttTemperature > this.temperatureRange.max)) {
|
||||
const alert = {
|
||||
// content: `温度传感器异常,读数${mqttTemperature}°C超出控制范围${this.temperatureRange.min}°C-${this.temperatureRange.max}°C`,
|
||||
content: `温度超出控制范围`,
|
||||
category: "传感器故障",
|
||||
alertTime: currentTime,
|
||||
level: "高危",
|
||||
action: "检查温度传感器连接",
|
||||
actionTime: currentTime,
|
||||
deviceId: "WSD_001"
|
||||
}
|
||||
this.logAlert(alert)
|
||||
}
|
||||
|
||||
// 2. 湿度报警:使用环境控制设置的区间(使用MQTT原始数据)
|
||||
if (mqttHumidity !== undefined && (mqttHumidity < this.humidityRange.min || mqttHumidity > this.humidityRange.max)) {
|
||||
const alert = {
|
||||
// content: `湿度传感器异常,读数${mqttHumidity}%超出控制范围${this.humidityRange.min}%-${this.humidityRange.max}%`,
|
||||
content: `温度超出控制范围`,
|
||||
category: "传感器故障",
|
||||
alertTime: currentTime,
|
||||
level: "高危",
|
||||
action: "检查湿度传感器连接",
|
||||
actionTime: currentTime,
|
||||
deviceId: "WSD_001"
|
||||
}
|
||||
this.logAlert(alert)
|
||||
}
|
||||
|
||||
// 3. 空调故障报警:acFaultStatus为1
|
||||
if (this.acFaultStatus === 1) {
|
||||
const alert = {
|
||||
// content: "空调故障,需要手动设置温度",
|
||||
content: "空调报警",
|
||||
category: "设备故障",
|
||||
alertTime: currentTime,
|
||||
level: "高危",
|
||||
action: "手动调节空调温度",
|
||||
actionTime: currentTime,
|
||||
deviceId: "AC_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: `温度偏差过大`,
|
||||
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: `湿度偏差过大`,
|
||||
category: "湿度控制异常",
|
||||
alertTime: currentTime,
|
||||
level: "中危",
|
||||
action: "调整空调设定湿度",
|
||||
actionTime: currentTime,
|
||||
deviceId: "AC_001"
|
||||
}
|
||||
this.logAlert(alert)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 记录报警到控制台并调用创建告警接口
|
||||
async logAlert(alert) {
|
||||
console.log('🚨 报警触发:', JSON.stringify(alert, null, 2))
|
||||
this.alertHistory.push(alert)
|
||||
|
||||
// 调用创建告警接口
|
||||
try {
|
||||
console.log('📤 正在创建告警记录...')
|
||||
const response = await alertApi.create(alert)
|
||||
console.log('✅ 告警记录创建成功:', response)
|
||||
} catch (error) {
|
||||
console.error('❌ 告警记录创建失败:', error)
|
||||
}
|
||||
|
||||
// 限制报警历史记录数量,避免内存溢出
|
||||
if (this.alertHistory.length > 100) {
|
||||
this.alertHistory = this.alertHistory.slice(-50)
|
||||
}
|
||||
},
|
||||
|
||||
// 降低目标温度
|
||||
decreaseTemperature() {
|
||||
if (this.targetTemperature > 16) {
|
||||
@ -409,8 +651,16 @@ export default {
|
||||
// duration: 1500
|
||||
// })
|
||||
|
||||
// 发送空调参数到MQTT
|
||||
this.sendAirConditionerParams()
|
||||
// 发送空调参数到MQTT
|
||||
this.sendAirConditionerParams()
|
||||
|
||||
// 温度变化后检查报警(使用当前页面数据模拟MQTT数据)
|
||||
const mockMqttData = {
|
||||
deviceType: 'WSD',
|
||||
temperature: this.temperature,
|
||||
humidity: this.humidity
|
||||
}
|
||||
this.checkAlerts(mockMqttData)
|
||||
},
|
||||
|
||||
// 发送空调参数
|
||||
@ -869,25 +1119,68 @@ export default {
|
||||
}
|
||||
|
||||
.range-item {
|
||||
padding: 20rpx;
|
||||
background: #f8fbff;
|
||||
border-radius: 12rpx;
|
||||
border-left: 4rpx solid #4a90e2;
|
||||
}
|
||||
|
||||
.range-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 15rpx 20rpx;
|
||||
// background: #f8fbff;
|
||||
border-radius: 12rpx;
|
||||
// border-left: 4rpx solid #4a90e2;
|
||||
margin-bottom: 12rpx;
|
||||
}
|
||||
|
||||
.range-label {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.range-status {
|
||||
padding: 6rpx 12rpx;
|
||||
border-radius: 20rpx;
|
||||
font-size: 22rpx;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.range-status.normal {
|
||||
background-color: #e8f5e8;
|
||||
color: #4caf50;
|
||||
}
|
||||
|
||||
.range-status.low {
|
||||
background-color: #fff3e0;
|
||||
color: #ff9800;
|
||||
}
|
||||
|
||||
.range-status.high {
|
||||
background-color: #ffebee;
|
||||
color: #f44336;
|
||||
}
|
||||
|
||||
.range-status.no-data {
|
||||
background-color: #f5f5f5;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.range-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8rpx;
|
||||
}
|
||||
|
||||
.current-value {
|
||||
font-size: 26rpx;
|
||||
color: #666;
|
||||
color: #4a90e2;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.range-value {
|
||||
font-size: 30rpx;
|
||||
color: #4a90e2;
|
||||
font-weight: 500;
|
||||
font-size: 24rpx;
|
||||
color: #666;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.settings-button {
|
||||
|
||||
Reference in New Issue
Block a user