对接mqtt

This commit is contained in:
吉浩茹
2025-09-27 14:59:48 +08:00
parent 8f6dcca19f
commit e4ea3312b7
8 changed files with 1093 additions and 220 deletions

View File

@ -1,31 +1,65 @@
// MQTT配置文件
// 使用条件编译适配不同平台
let mqtturl;
// #ifdef H5
mqtturl = "ws://122.51.194.184:8083/mqtt";
// #endif
// #ifdef APP-PLUS || 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: 'ws://122.51.194.184:8083/mqtt', // WebSocket MQTT端口
// broker: 'mqtt://122.51.194.184:1883', // 标准MQTT端口
// EMQX服务器地址 - 使用条件编译选择协议
broker: mqtturl,
// 连接选项
options: {
clientId: 'mobile-inspection-system-' + Math.random().toString(16).substr(2, 8),
// 暂时不设置账号密码
// username: '',
// password: '',
keepalive: 60,
username: 'dmbroker',
password: 'qwer1234',
keepalive: 30,
clean: true,
reconnectPeriod: 5000,
connectTimeout: 30 * 1000,
reconnectPeriod: 1000,
connectTimeout: 5000,
protocolVersion: 4,
rejectUnauthorized: false,
},
// 主题配置
topics: {
// 设备数据主题
deviceData: 'hdydcj_01_UP',
// deviceData: 'hdydcj_01_down',
deviceData: 'HDYDCJ_01_DOWN',
// 设备类型
deviceTypes: {
WSD: '温湿度', // 温湿度传感器
AC: '空调', // 空调设备
PM: 'PM2.5', // PM2.5传感器
// SIMPLE: '简单数据', // 简单数据(如"11"、"22"
// 可以根据需要添加更多设备类型
}
}
@ -36,13 +70,45 @@ export const DataParser = {
// 解析设备数据
parseDeviceData(rawData) {
try {
const data = JSON.parse(rawData)
if (Array.isArray(data) && data.length > 0) {
return data[0] // 取第一个设备数据
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('解析设备数据失败:', error)
console.error('原始数据:', rawData)
return null
}
},