Files
movecheck/src/config/mqtt.js
2025-09-29 23:53:09 +08:00

129 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// MQTT配置文件
// 使用条件编译适配不同平台
let mqtturl;
// #ifdef H5
mqtturl = "ws://122.51.194.184:8083/mqtt";
// #endif
// #ifdef APP-PLUS
mqtturl = "ws://122.51.194.184:8083/mqtt";
//#endif
// #ifdef MP-WEIXIN
mqtturl = "wx://122.51.194.184:8083/mqtt";
//#endif
// #ifdef MP-ALIPAY
mqtturl = "alis://122.51.194.184:8083/mqtt";
//#endif
console.log('🔧 MQTT环境检测:', {
mqtturl: mqtturl,
// #ifdef H5
platform: 'H5',
// #endif
// #ifdef APP-PLUS
platform: 'APP-PLUS',
// #endif
// #ifdef MP-WEIXIN
platform: 'MP-WEIXIN',
// #endif
// #ifdef MP-ALIPAY
platform: 'MP-ALIPAY',
// #endif
});
export const MQTT_CONFIG = {
// EMQX服务器地址 - 使用条件编译选择协议
broker: mqtturl,
// 连接选项
options: {
clientId: 'mobile-inspection-system-' + Math.random().toString(16).substr(2, 8),
username: 'dmbroker',
password: 'qwer1234',
keepalive: 30,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 5000,
protocolVersion: 4,
rejectUnauthorized: false,
},
// 主题配置
topics: {
// 设备数据主题
// deviceData: 'hdydcj_01_down',
deviceData: 'HDYDCJ_01_UP',
// 设备类型
deviceTypes: {
WSD: '温湿度', // 温湿度传感器
AC: '空调', // 空调设备
PM: 'PM2.5', // PM2.5传感器
// SIMPLE: '简单数据', // 简单数据(如"11"、"22"
// 可以根据需要添加更多设备类型
}
}
}
// 数据解析工具
export const DataParser = {
// 解析设备数据
parseDeviceData(rawData) {
try {
console.log('🔧 开始解析设备数据...')
console.log('原始数据:', rawData)
console.log('数据类型:', typeof rawData)
// 根据截图,数据可能是简单的数字(如"11"、"22"
// 先尝试解析为JSON如果失败则作为简单数据处理
let data
try {
data = JSON.parse(rawData)
console.log('解析后的JSON数据:', data)
console.log('是否为数组:', Array.isArray(data))
if (Array.isArray(data) && data.length > 0) {
console.log('✅ 返回数组第一个元素:', data[0])
return data[0] // 取第一个设备数据
} else if (data && typeof data === 'object') {
console.log('✅ 返回对象数据:', data)
return data // 直接返回对象
}
} catch (jsonError) {
console.log('⚠️ 不是JSON格式作为简单数据处理')
// 处理简单数据(如"11"、"22"
const simpleData = {
Device: 'SIMPLE',
timestamp: Math.floor(Date.now() / 1000),
Data: {
value: rawData,
type: 'simple_data'
}
}
console.log('✅ 返回简单数据:', simpleData)
return simpleData
}
console.log('⚠️ 数据格式不符合预期')
return null
} catch (error) {
console.error('❌ 解析设备数据失败:', error)
console.error('原始数据:', rawData)
return null
}
},
// 获取设备类型名称
getDeviceTypeName(deviceCode) {
return MQTT_CONFIG.topics.deviceTypes[deviceCode] || deviceCode
},
// 转换Unix时间戳为可读时间
formatTimestamp(timestamp) {
return new Date(timestamp * 1000).toLocaleString('zh-CN')
}
}