1
0
forked from dyf/dyf-vue-ui

6170控制面板代码优化

This commit is contained in:
fengerli
2025-10-13 17:01:11 +08:00
parent 84af6f5cfe
commit aa7229e21b
2 changed files with 5 additions and 8 deletions

View File

@ -592,17 +592,15 @@ const getMainLightModeLabel = (mode: any) => {
// 处理设备消息 // 处理设备消息
const handleDeviceMessage = (msg: any) => { const handleDeviceMessage = (msg: any) => {
try { try {
// 解析设备消息(假设格式为 { state: [类型, 模式值, 亮度, 续航...] }
const payloadObj = JSON.parse(msg.payload.toString()); const payloadObj = JSON.parse(msg.payload.toString());
const deviceState = payloadObj.state; // 设备状态数组 const deviceState = payloadObj.state; // 设备状态数组
if (!Array.isArray(deviceState)) { if (!Array.isArray(deviceState)) {
return; return;
} }
// 用switch处理不同的消息类型deviceState[0]
switch (deviceState[0]) { switch (deviceState[0]) {
case 1: case 1:
// 类型1灯光主键 // 类型1灯光主键
const lightModeId = getMainLightModeLabel(deviceState[1]); // 获取模式ID如'strong' const lightModeId = getMainLightModeLabel(deviceState[1]);
const brightness = deviceState[2]; // 亮度值 const brightness = deviceState[2]; // 亮度值
const batteryTime = deviceState[3]; // 续航时间 const batteryTime = deviceState[3]; // 续航时间
console.log('灯光模式消息:', { 模式ID: lightModeId, 亮度: brightness, 续航: batteryTime }); console.log('灯光模式消息:', { 模式ID: lightModeId, 亮度: brightness, 续航: batteryTime });
@ -625,7 +623,7 @@ const handleDeviceMessage = (msg: any) => {
break; break;
case 12: case 12:
// 灯光主键 // 灯光主键
const lightModeIdA = getMainLightModeLabel(deviceState[1]); // 获取模式ID如'strong' const lightModeIdA = getMainLightModeLabel(deviceState[1]);
if (lightModeIdA !== 'unknown') { if (lightModeIdA !== 'unknown') {
lightModes.value.forEach(mode => { lightModes.value.forEach(mode => {
const isActive = mode.id === lightModeIdA; const isActive = mode.id === lightModeIdA;
@ -650,7 +648,6 @@ const handleDeviceMessage = (msg: any) => {
break; break;
default: default:
// 其他类型消息(不处理,仅打印)
console.log('未处理的消息类型:', deviceState[0]); console.log('未处理的消息类型:', deviceState[0]);
break; break;
} }
@ -658,7 +655,7 @@ const handleDeviceMessage = (msg: any) => {
} }
}; };
onMounted(async () => { onMounted(async () => {
await getList(); // 先获取设备信息 await getList();
// 连接mqtt // 连接mqtt
onConnect(async () => { onConnect(async () => {
const deviceImei = deviceDetail.value.deviceImei; const deviceImei = deviceDetail.value.deviceImei;
@ -684,7 +681,7 @@ onMounted(async () => {
handleDeviceMessage(msg); handleDeviceMessage(msg);
}); });
onError((err) => { onError((err) => {
console.error('MQTT连接失败原因:', err.message); // 关键:打印连接失败的具体原因 console.error('MQTT连接失败原因:', err.message);
}); });
connect(); connect();
}); });