feat:对接参数记录echart、空调度数获取设置
This commit is contained in:
151
src/utils/api.js
Normal file
151
src/utils/api.js
Normal file
@ -0,0 +1,151 @@
|
||||
/**
|
||||
* API接口封装
|
||||
* 统一管理所有API接口
|
||||
*/
|
||||
|
||||
import httpService from './http.js'
|
||||
|
||||
// 数据历史接口
|
||||
export const dataHistoryApi = {
|
||||
// 获取历史数据
|
||||
getHistory(params) {
|
||||
return httpService.post('/api/data/history', params)
|
||||
}
|
||||
}
|
||||
|
||||
// 设备相关接口
|
||||
export const deviceApi = {
|
||||
// 获取设备详情
|
||||
getDetail(deviceId) {
|
||||
return httpService.get(`/api/devices/${deviceId}`)
|
||||
},
|
||||
|
||||
// 获取设备列表
|
||||
getList(params = {}) {
|
||||
return httpService.get('/api/devices', params)
|
||||
},
|
||||
|
||||
// 更新设备状态
|
||||
updateStatus(deviceId, status) {
|
||||
return httpService.put(`/api/devices/${deviceId}/status`, { status })
|
||||
}
|
||||
}
|
||||
|
||||
// 环境参数接口
|
||||
export const environmentApi = {
|
||||
// 获取环境参数
|
||||
getParams(params = {}) {
|
||||
return httpService.get('/api/environment/params', params)
|
||||
},
|
||||
|
||||
// 获取环境参数历史
|
||||
getHistory(params) {
|
||||
return httpService.post('/api/environment/history', params)
|
||||
}
|
||||
}
|
||||
|
||||
// 报警相关接口
|
||||
export const alarmApi = {
|
||||
// 获取报警记录
|
||||
getRecords(params = {}) {
|
||||
return httpService.get('/api/alarms', params)
|
||||
},
|
||||
|
||||
// 获取报警统计
|
||||
getStatistics(params = {}) {
|
||||
return httpService.get('/api/alarms/statistics', params)
|
||||
},
|
||||
|
||||
// 处理报警
|
||||
handleAlarm(alarmId, action) {
|
||||
return httpService.put(`/api/alarms/${alarmId}/handle`, { action })
|
||||
}
|
||||
}
|
||||
|
||||
// 系统日志接口
|
||||
export const logApi = {
|
||||
// 获取系统日志
|
||||
getLogs(params = {}) {
|
||||
return httpService.get('/api/logs', params)
|
||||
},
|
||||
|
||||
// 获取日志统计
|
||||
getStatistics(params = {}) {
|
||||
return httpService.get('/api/logs/statistics', params)
|
||||
}
|
||||
}
|
||||
|
||||
// 用户相关接口
|
||||
export const userApi = {
|
||||
// 用户登录
|
||||
login(credentials) {
|
||||
return httpService.post('/api/auth/login', credentials)
|
||||
},
|
||||
|
||||
// 用户登出
|
||||
logout() {
|
||||
return httpService.post('/api/auth/logout')
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo() {
|
||||
return httpService.get('/api/user/info')
|
||||
},
|
||||
|
||||
// 更新用户信息
|
||||
updateUserInfo(userInfo) {
|
||||
return httpService.put('/api/user/info', userInfo)
|
||||
}
|
||||
}
|
||||
|
||||
// 温湿度数据接口
|
||||
export const thDataApi = {
|
||||
// 获取最新空调温度
|
||||
getLatest() {
|
||||
return httpService.get('/api/th/data/latest')
|
||||
},
|
||||
|
||||
// 提交温湿度数据
|
||||
submit(data) {
|
||||
return httpService.post('/api/th/data', data)
|
||||
}
|
||||
}
|
||||
|
||||
// 告警相关接口
|
||||
export const alertApi = {
|
||||
// 创建告警
|
||||
create(data) {
|
||||
return httpService.post('/api/alerts', data)
|
||||
},
|
||||
|
||||
// 分页获取告警
|
||||
getList(params = {}) {
|
||||
return httpService.get('/api/alerts', params)
|
||||
}
|
||||
}
|
||||
|
||||
// 事件相关接口
|
||||
export const eventApi = {
|
||||
// 创建事件
|
||||
create(data) {
|
||||
return httpService.post('/api/events', data)
|
||||
},
|
||||
|
||||
// 分页获取事件
|
||||
getList(params = {}) {
|
||||
return httpService.get('/api/events', params)
|
||||
}
|
||||
}
|
||||
|
||||
// 导出所有API
|
||||
export default {
|
||||
dataHistory: dataHistoryApi,
|
||||
device: deviceApi,
|
||||
environment: environmentApi,
|
||||
alarm: alarmApi,
|
||||
log: logApi,
|
||||
user: userApi,
|
||||
thData: thDataApi,
|
||||
alert: alertApi,
|
||||
event: eventApi
|
||||
}
|
||||
213
src/utils/dataHistoryExample.js
Normal file
213
src/utils/dataHistoryExample.js
Normal file
@ -0,0 +1,213 @@
|
||||
/**
|
||||
* 历史数据接口使用示例
|
||||
* 演示如何使用封装的历史数据API
|
||||
*/
|
||||
|
||||
import { dataHistoryApi } from './api.js'
|
||||
|
||||
// 使用示例类
|
||||
class DataHistoryExample {
|
||||
|
||||
// 示例1: 获取指定时间范围的历史数据
|
||||
async getHistoryData() {
|
||||
try {
|
||||
const params = {
|
||||
startTime: "2025-09-30 06:51:40",
|
||||
endTime: "2025-09-30 23:51:40"
|
||||
}
|
||||
|
||||
console.log('📊 请求历史数据:', params)
|
||||
|
||||
const response = await dataHistoryApi.getHistory(params)
|
||||
|
||||
console.log('✅ 历史数据获取成功:', response)
|
||||
return response
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 历史数据获取失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 示例2: 获取今天的历史数据
|
||||
async getTodayHistory() {
|
||||
try {
|
||||
const today = new Date()
|
||||
const startTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0)
|
||||
const endTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 59, 59)
|
||||
|
||||
const params = {
|
||||
startTime: this.formatDateTime(startTime),
|
||||
endTime: this.formatDateTime(endTime)
|
||||
}
|
||||
|
||||
console.log('📊 请求今天历史数据:', params)
|
||||
|
||||
const response = await dataHistoryApi.getHistory(params)
|
||||
|
||||
console.log('✅ 今天历史数据获取成功:', response)
|
||||
return response
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 今天历史数据获取失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 示例3: 获取最近7天的历史数据
|
||||
async getLastWeekHistory() {
|
||||
try {
|
||||
const endTime = new Date()
|
||||
const startTime = new Date(endTime.getTime() - 7 * 24 * 60 * 60 * 1000)
|
||||
|
||||
const params = {
|
||||
startTime: this.formatDateTime(startTime),
|
||||
endTime: this.formatDateTime(endTime)
|
||||
}
|
||||
|
||||
console.log('📊 请求最近7天历史数据:', params)
|
||||
|
||||
const response = await dataHistoryApi.getHistory(params)
|
||||
|
||||
console.log('✅ 最近7天历史数据获取成功:', response)
|
||||
return response
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 最近7天历史数据获取失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 示例4: 获取指定小时的历史数据
|
||||
async getHourlyHistory(date, hour) {
|
||||
try {
|
||||
const startTime = new Date(date)
|
||||
startTime.setHours(hour, 0, 0, 0)
|
||||
|
||||
const endTime = new Date(startTime)
|
||||
endTime.setHours(hour + 1, 0, 0, 0)
|
||||
|
||||
const params = {
|
||||
startTime: this.formatDateTime(startTime),
|
||||
endTime: this.formatDateTime(endTime)
|
||||
}
|
||||
|
||||
console.log('📊 请求指定小时历史数据:', params)
|
||||
|
||||
const response = await dataHistoryApi.getHistory(params)
|
||||
|
||||
console.log('✅ 指定小时历史数据获取成功:', response)
|
||||
return response
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 指定小时历史数据获取失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化日期时间
|
||||
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}`
|
||||
}
|
||||
|
||||
// 示例5: 在Vue组件中使用
|
||||
async useInVueComponent() {
|
||||
// 在Vue组件的methods中使用
|
||||
const methods = {
|
||||
async loadHistoryData() {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: '加载历史数据中...'
|
||||
})
|
||||
|
||||
const params = {
|
||||
startTime: "2025-09-30 06:51:40",
|
||||
endTime: "2025-09-30 23:51:40"
|
||||
}
|
||||
|
||||
// 使用全局注册的API
|
||||
const response = await this.$api.dataHistory.getHistory(params)
|
||||
|
||||
console.log('历史数据:', response)
|
||||
|
||||
// 处理响应数据
|
||||
if (response.code === 200) {
|
||||
this.historyData = response.data
|
||||
uni.showToast({
|
||||
title: '数据加载成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} else {
|
||||
throw new Error(response.message || '数据加载失败')
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('加载历史数据失败:', error)
|
||||
uni.showToast({
|
||||
title: error.message || '数据加载失败',
|
||||
icon: 'error'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return methods
|
||||
}
|
||||
}
|
||||
|
||||
// 创建实例
|
||||
const dataHistoryExample = new DataHistoryExample()
|
||||
|
||||
export default dataHistoryExample
|
||||
|
||||
// 使用示例
|
||||
/*
|
||||
// 1. 直接使用API
|
||||
import { dataHistoryApi } from '@/utils/api.js'
|
||||
|
||||
const getData = async () => {
|
||||
try {
|
||||
const response = await dataHistoryApi.getHistory({
|
||||
startTime: "2025-09-30 06:51:40",
|
||||
endTime: "2025-09-30 23:51:40"
|
||||
})
|
||||
console.log('历史数据:', response)
|
||||
} catch (error) {
|
||||
console.error('获取失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 在Vue组件中使用
|
||||
export default {
|
||||
methods: {
|
||||
async loadData() {
|
||||
try {
|
||||
const response = await this.$api.dataHistory.getHistory({
|
||||
startTime: "2025-09-30 06:51:40",
|
||||
endTime: "2025-09-30 23:51:40"
|
||||
})
|
||||
this.data = response.data
|
||||
} catch (error) {
|
||||
this.$toast(error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 使用示例类
|
||||
import dataHistoryExample from '@/utils/dataHistoryExample.js'
|
||||
|
||||
const example = new dataHistoryExample()
|
||||
example.getHistoryData()
|
||||
example.getTodayHistory()
|
||||
example.getLastWeekHistory()
|
||||
*/
|
||||
178
src/utils/http.js
Normal file
178
src/utils/http.js
Normal file
@ -0,0 +1,178 @@
|
||||
/**
|
||||
* HTTP请求工具类
|
||||
* 封装uni.request,提供统一的请求接口
|
||||
*/
|
||||
|
||||
class HttpService {
|
||||
constructor() {
|
||||
this.baseURL = 'http://110.40.171.179:7999'
|
||||
this.timeout = 10000
|
||||
this.defaultHeaders = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
|
||||
// 设置基础URL
|
||||
setBaseURL(url) {
|
||||
this.baseURL = url
|
||||
}
|
||||
|
||||
// 设置超时时间
|
||||
setTimeout(timeout) {
|
||||
this.timeout = timeout
|
||||
}
|
||||
|
||||
// 设置默认请求头
|
||||
setDefaultHeaders(headers) {
|
||||
this.defaultHeaders = { ...this.defaultHeaders, ...headers }
|
||||
}
|
||||
|
||||
// 通用请求方法
|
||||
request(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const {
|
||||
url,
|
||||
method = 'GET',
|
||||
data = {},
|
||||
headers = {},
|
||||
timeout = this.timeout
|
||||
} = options
|
||||
|
||||
// 构建完整URL
|
||||
const fullUrl = url.startsWith('http') ? url : `${this.baseURL}${url}`
|
||||
|
||||
// 合并请求头
|
||||
const requestHeaders = { ...this.defaultHeaders, ...headers }
|
||||
|
||||
console.log('🌐 HTTP请求:', {
|
||||
url: fullUrl,
|
||||
method,
|
||||
data,
|
||||
headers: requestHeaders
|
||||
})
|
||||
|
||||
uni.request({
|
||||
url: fullUrl,
|
||||
method,
|
||||
data,
|
||||
header: requestHeaders,
|
||||
timeout,
|
||||
success: (response) => {
|
||||
console.log('✅ HTTP响应:', response)
|
||||
|
||||
// 检查HTTP状态码
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
resolve(response.data)
|
||||
} else {
|
||||
reject(new Error(`HTTP ${response.statusCode}: ${response.data?.message || '请求失败'}`))
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('❌ HTTP请求失败:', error)
|
||||
reject(new Error(error.errMsg || '网络请求失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// GET请求
|
||||
get(url, params = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
// POST请求
|
||||
post(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
put(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
delete(url, params = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
data: params,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
upload(url, filePath, formData = {}, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullUrl = url.startsWith('http') ? url : `${this.baseURL}${url}`
|
||||
|
||||
console.log('📤 文件上传:', {
|
||||
url: fullUrl,
|
||||
filePath,
|
||||
formData
|
||||
})
|
||||
|
||||
uni.uploadFile({
|
||||
url: fullUrl,
|
||||
filePath,
|
||||
name: 'file',
|
||||
formData,
|
||||
header: { ...this.defaultHeaders, ...options.headers },
|
||||
success: (response) => {
|
||||
console.log('✅ 文件上传成功:', response)
|
||||
try {
|
||||
const data = JSON.parse(response.data)
|
||||
resolve(data)
|
||||
} catch (error) {
|
||||
resolve(response.data)
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('❌ 文件上传失败:', error)
|
||||
reject(new Error(error.errMsg || '文件上传失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
download(url, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullUrl = url.startsWith('http') ? url : `${this.baseURL}${url}`
|
||||
|
||||
console.log('📥 文件下载:', fullUrl)
|
||||
|
||||
uni.downloadFile({
|
||||
url: fullUrl,
|
||||
header: { ...this.defaultHeaders, ...options.headers },
|
||||
success: (response) => {
|
||||
console.log('✅ 文件下载成功:', response)
|
||||
resolve(response)
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('❌ 文件下载失败:', error)
|
||||
reject(new Error(error.errMsg || '文件下载失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 创建单例实例
|
||||
const httpService = new HttpService()
|
||||
|
||||
export default httpService
|
||||
@ -21,10 +21,10 @@ class MqttDataManager {
|
||||
|
||||
// 监听MQTT数据
|
||||
uni.$on('mqttData', this.handleMqttData.bind(this))
|
||||
console.log('✅ MQTT数据监听器已注册')
|
||||
// console.log('✅ MQTT数据监听器已注册')
|
||||
|
||||
// 立即创建MQTT连接,不使用延迟
|
||||
console.log('🔧 立即创建MQTT连接...')
|
||||
// console.log('🔧 立即创建MQTT连接...')
|
||||
createMqtt()
|
||||
|
||||
// 定期检查连接状态
|
||||
@ -34,10 +34,10 @@ class MqttDataManager {
|
||||
|
||||
// 如果连接状态发生变化,通知监听器
|
||||
if (wasConnected !== this.isConnected) {
|
||||
console.log('🔄 MQTT连接状态变化:', {
|
||||
wasConnected,
|
||||
isConnected: this.isConnected
|
||||
})
|
||||
// console.log('🔄 MQTT连接状态变化:', {
|
||||
// wasConnected,
|
||||
// isConnected: this.isConnected
|
||||
// })
|
||||
this.notifyListeners('connectionStatus', {
|
||||
isConnected: this.isConnected,
|
||||
lastUpdate: this.lastData.timestamp ? new Date(this.lastData.timestamp * 1000).toLocaleString('zh-CN') : null
|
||||
@ -45,7 +45,7 @@ class MqttDataManager {
|
||||
}
|
||||
}, 3000) // 改为3秒检查一次
|
||||
|
||||
console.log('✅ MQTT数据管理器初始化完成')
|
||||
// console.log('✅ MQTT数据管理器初始化完成')
|
||||
} catch (error) {
|
||||
console.error('❌ MQTT数据管理器初始化失败:', error)
|
||||
}
|
||||
@ -54,23 +54,23 @@ class MqttDataManager {
|
||||
// 处理MQTT数据
|
||||
handleMqttData(data) {
|
||||
try {
|
||||
console.log('📨 收到MQTT数据:', data)
|
||||
// console.log('📨 收到MQTT数据:', data)
|
||||
|
||||
// 更新连接状态
|
||||
this.isConnected = true
|
||||
|
||||
// 检查数据是否为数组
|
||||
if (Array.isArray(data)) {
|
||||
console.log('📋 收到数组数据,长度:', data.length)
|
||||
// console.log('📋 收到数组数据,长度:', data.length)
|
||||
|
||||
// 遍历数组中的每个设备数据
|
||||
data.forEach((deviceData, index) => {
|
||||
console.log(`📦 处理设备数据[${index}]:`, deviceData)
|
||||
// console.log(`📦 处理设备数据[${index}]:`, deviceData)
|
||||
this.processDeviceData(deviceData)
|
||||
})
|
||||
} else {
|
||||
// 单个设备数据
|
||||
console.log('📦 处理单个设备数据:', data)
|
||||
// console.log('📦 处理单个设备数据:', data)
|
||||
this.processDeviceData(data)
|
||||
}
|
||||
} catch (error) {
|
||||
@ -91,21 +91,21 @@ class MqttDataManager {
|
||||
const deviceDataContent = deviceData.Data
|
||||
const timestamp = deviceData.timestamp || Math.floor(Date.now() / 1000)
|
||||
|
||||
console.log(`🔍 处理设备类型: ${deviceType}`)
|
||||
console.log('设备数据:', deviceDataContent)
|
||||
console.log('时间戳:', timestamp)
|
||||
// console.log(`🔍 处理设备类型: ${deviceType}`)
|
||||
// console.log('设备数据:', deviceDataContent)
|
||||
// console.log('时间戳:', timestamp)
|
||||
|
||||
// 根据设备类型处理数据
|
||||
if (deviceType === 'WSD') {
|
||||
console.log('✅ 处理WSD设备数据 - 更新环境参数')
|
||||
// console.log('✅ 处理WSD设备数据 - 更新环境参数')
|
||||
this.processWSDData(deviceDataContent, timestamp)
|
||||
} else {
|
||||
console.log(`⚠️ 设备类型 ${deviceType} 暂不处理,仅打印到控制台`)
|
||||
console.log('设备详情:', {
|
||||
deviceType,
|
||||
data: deviceDataContent,
|
||||
timestamp: new Date(timestamp * 1000).toLocaleString('zh-CN')
|
||||
})
|
||||
// console.log(`⚠️ 设备类型 ${deviceType} 暂不处理,仅打印到控制台`)
|
||||
// console.log('设备详情:', {
|
||||
// deviceType,
|
||||
// data: deviceDataContent,
|
||||
// timestamp: new Date(timestamp * 1000).toLocaleString('zh-CN')
|
||||
// })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ 处理设备数据失败:', error)
|
||||
@ -116,12 +116,12 @@ class MqttDataManager {
|
||||
processWSDData(data, timestamp) {
|
||||
try {
|
||||
// 解析WSD数据 - 根据您提供的数据结构,WD是温度,SD是湿度
|
||||
const temperature = parseFloat(data.WD) || 0
|
||||
const humidity = parseFloat(data.SD) || 0
|
||||
const temperature = data.WD && parseFloat(data.WD);
|
||||
const humidity = data.SD && parseFloat(data.SD);
|
||||
|
||||
console.log('🌡️ WSD数据解析:')
|
||||
console.log('温度(WD):', temperature)
|
||||
console.log('湿度(SD):', humidity)
|
||||
// console.log('🌡️ WSD数据解析:')
|
||||
// console.log('温度(WD):', temperature)
|
||||
// console.log('湿度(SD):', humidity)
|
||||
|
||||
// 构建解析后的数据
|
||||
const parsedData = {
|
||||
@ -138,7 +138,7 @@ class MqttDataManager {
|
||||
// 通知所有监听器
|
||||
this.notifyListeners('dataUpdate', parsedData)
|
||||
|
||||
console.log('✅ WSD数据处理完成:', parsedData)
|
||||
// console.log('✅ WSD数据处理完成:', parsedData)
|
||||
} catch (error) {
|
||||
console.error('❌ 处理WSD数据失败:', error)
|
||||
}
|
||||
@ -154,7 +154,7 @@ class MqttDataManager {
|
||||
|
||||
// 检查数据结构
|
||||
if (!rawData || !rawData.Device || !rawData.Data) {
|
||||
console.warn('⚠️ 数据格式不符合预期:', rawData)
|
||||
// console.warn('⚠️ 数据格式不符合预期:', rawData)
|
||||
return null
|
||||
}
|
||||
|
||||
@ -171,17 +171,17 @@ class MqttDataManager {
|
||||
|
||||
switch (deviceType) {
|
||||
case 'WSD': // 温湿度传感器
|
||||
parsedData.temperature = parseFloat(deviceData.Temperature) || 0
|
||||
parsedData.humidity = parseFloat(deviceData.Humidity) || 0
|
||||
deviceData.Temperature && (parsedData.temperature = parseFloat(deviceData.Temperature))
|
||||
deviceData.Humidity && (parsedData.humidity = parseFloat(deviceData.Humidity))
|
||||
break
|
||||
|
||||
case 'AC': // 空调设备
|
||||
parsedData.temperature = parseFloat(deviceData.Temperature) || 0
|
||||
parsedData.humidity = parseFloat(deviceData.Humidity) || 0
|
||||
deviceData.Temperature && (parsedData.temperature = parseFloat(deviceData.Temperature))
|
||||
deviceData.Humidity && (parsedData.humidity = parseFloat(deviceData.Humidity))
|
||||
break
|
||||
|
||||
case 'PM': // PM2.5传感器
|
||||
parsedData.pm25 = parseFloat(deviceData.PM25) || 0
|
||||
deviceData.PM25 && (parsedData.pm25 = parseFloat(deviceData.PM25))
|
||||
break
|
||||
|
||||
default:
|
||||
|
||||
@ -122,7 +122,7 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
duration: 3000
|
||||
});
|
||||
} else {
|
||||
console.log("✅ MQTT订阅主题成功:", topicUrl);
|
||||
// console.log("✅ MQTT订阅主题成功:", topicUrl);
|
||||
}
|
||||
});
|
||||
});
|
||||
@ -137,13 +137,10 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
// 获取信息
|
||||
const mqttData = JSON.parse(message.toString());
|
||||
console.log('📋 解析后的数据:', mqttData);
|
||||
console.log('数据类型:', Array.isArray(mqttData) ? '数组' : '对象');
|
||||
|
||||
// 如果是数组,打印数组信息
|
||||
if (Array.isArray(mqttData)) {
|
||||
console.log('📋 数组长度:', mqttData.length);
|
||||
mqttData.forEach((item, index) => {
|
||||
console.log(`📦 数组[${index}]:`, item);
|
||||
if (item.Device) {
|
||||
console.log(`🔍 设备类型[${index}]: ${item.Device}`);
|
||||
}
|
||||
@ -221,16 +218,16 @@ const judgeBeat = () => {
|
||||
// 获取连接状态
|
||||
const getConnectionStatus = () => {
|
||||
if (!client) {
|
||||
console.log('🔍 连接状态检查: 客户端不存在');
|
||||
// console.log('🔍 连接状态检查: 客户端不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
const isConnected = client.connected;
|
||||
console.log('🔍 连接状态检查:', {
|
||||
clientExists: !!client,
|
||||
connected: isConnected,
|
||||
readyState: client.stream ? client.stream.readyState : 'unknown'
|
||||
});
|
||||
// console.log('🔍 连接状态检查:', {
|
||||
// clientExists: !!client,
|
||||
// connected: isConnected,
|
||||
// readyState: client.stream ? client.stream.readyState : 'unknown'
|
||||
// });
|
||||
|
||||
return isConnected;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user