From e57699dbdf8881029c48c64fcaae507923e7cb2e Mon Sep 17 00:00:00 2001 From: rqian <1206436827@qq.com> Date: Tue, 20 Oct 2026 09:04:57 +0800 Subject: [PATCH] =?UTF-8?q?#=E6=9B=B4=E6=96=B0=E5=AE=9E=E6=97=B6=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=85=B7=E4=BD=93=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/business/dto/DeviceMapPushDTO.java | 16 +- .../com/maibu/core/host/VehicleMessage.java | 30 ++- .../java/com/maibu/memory/SiteMemory.java | 15 ++ .../com/maibu/mqtt/HostMessageHandler.java | 181 +++++++++++++++--- .../main/java/com/maibu/mqtt/MqttTopic.java | 4 +- .../maibu/service/DeviceThreadService.java | 5 +- .../java/com/maibu/utils/ControlUtils.java | 2 +- 7 files changed, 214 insertions(+), 39 deletions(-) diff --git a/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java b/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java index e0efb01..9ac2477 100644 --- a/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java +++ b/maibu-common/src/main/java/com/maibu/core/business/dto/DeviceMapPushDTO.java @@ -7,12 +7,14 @@ public class DeviceMapPushDTO { private String id; private double lat; private double lng; - private double y; //yaw - private String f; //fix_status - private double a; //altitude - private String s;//status 运行状态 - private Long t; //taskId 任务id - private double b; //电量 - private boolean ha = false;//hasAlarm 有无告警 + private double y; // yaw + private String f; // fix_status + private double a; // altitude + private String s;// status 运行状态 + private Long t; // taskId 任务id + private double b; // 电量 + private boolean ha = false;// hasAlarm 有无告警 + + private String cm;// 控制模式 manual_remote|auto } diff --git a/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java b/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java index 8a8d06b..dc4b601 100644 --- a/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java +++ b/maibu-common/src/main/java/com/maibu/core/host/VehicleMessage.java @@ -5,12 +5,30 @@ import lombok.Data; @Data public class VehicleMessage { - private double speed; - private double battery_percentage;// 电量 - private double battery_voltage;// 电压 - private String motion;// moving|stopped|blocked - private boolean emergency_stop; // 急停状态 + private double battery_percentage;// 实时电量百分比 + private double battery_voltage;// 实时电压 - private double distance; // 里程数 + private double left_motor_current;// 实时左电机电流 + private double right_motor_current;// 实时右电机电流 + private double left_motor_temperature;// 实时左电机温度 + private double right_motor_temperature;// 实时右电机温度 + private double main_temperature;// 实时主温度 + private double yaw;// 实时航向角度 + private double pitch;// 实时俯仰角度 + private String heading_status;// 实时航向状态 + private double total_work_area;// 累计作业面积 + private double total_work_duration;// 累计作业时长 + private double total_work_distance;// 累计作业里程 + private String motion;// moving|stopped|blocked 运动状态 + private Boolean emergency_stop; // 急停状态 + + private double left_target_rpm; // 实时左电机目标转速 + private double right_target_rpm;// 实时右电机目标转速 + private double left_measured_rpm;// 实时左电机测量转速 + private double right_measured_rpm;// 实时右电机测量转速 + private double left_motor_rpm;// 实时左电机转速 + private double right_motor_rpm;// 实时右电机转速 + private double roll;// 实时翻滚角度 + private String control_mode;// 遥控器拨杆对应的当前控制模式 } diff --git a/maibu-common/src/main/java/com/maibu/memory/SiteMemory.java b/maibu-common/src/main/java/com/maibu/memory/SiteMemory.java index 1653f11..2516624 100644 --- a/maibu-common/src/main/java/com/maibu/memory/SiteMemory.java +++ b/maibu-common/src/main/java/com/maibu/memory/SiteMemory.java @@ -16,6 +16,7 @@ import org.springframework.util.CollectionUtils; import com.maibu.core.domain.entity.SysSite; import com.maibu.core.enums.ErrorSource; +import com.maibu.core.host.VehicleMessage; import lombok.Data; import lombok.extern.slf4j.Slf4j; @@ -59,6 +60,20 @@ public class SiteMemory { public static ConcurrentHashMap deviceMapPushMap = new ConcurrentHashMap<>(); + public static ConcurrentHashMap vehicleMessageMap = new ConcurrentHashMap<>(); + + public VehicleMessage getVehicleMessage(String deviceId) { + return vehicleMessageMap.get(deviceId); + } + + public void saveVehicleMessage(String deviceId, VehicleMessage vehicleMessage) { + vehicleMessageMap.put(deviceId, vehicleMessage); + } + + public void removeVehicleMessage(String deviceId) { + vehicleMessageMap.remove(deviceId); + } + public Map getAllDeviceMapPush() { return deviceMapPushMap; } diff --git a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java index 90cbeef..924a574 100644 --- a/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java +++ b/maibu-common/src/main/java/com/maibu/mqtt/HostMessageHandler.java @@ -148,6 +148,15 @@ public class HostMessageHandler implements CustomMqttMessageHandler { locationMessage.setDeviceId(deviceId); redisCache.setCacheObject(key, locationMessage); + // todo 推送实时位置和状态消息到外部 + try { + GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, + String.format(MqttTopic.DEVICE_LOCATION_MESSAGE_TOPIC, deviceId), + JsonUtils.toJsonString(locationRelTimeDTO)); + } catch (MqttException e) { + log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); + } + } } catch (Exception e) { log.error("位置消息解析失败: deviceId={}, payload={}, error={}", deviceId, payload, e); @@ -227,7 +236,6 @@ public class HostMessageHandler implements CustomMqttMessageHandler { DeviceMapPushDTO dto = new DeviceMapPushDTO(); dto.setA(Double.parseDouble(altitude.getValue())); dto.setId(deviceId); - // dto.setB(); // dto.setHa(); dto.setF(fix_status.getValue()); dto.setS(status.getValue()); @@ -235,6 +243,12 @@ public class HostMessageHandler implements CustomMqttMessageHandler { dto.setLng(Double.parseDouble(lng.getValue())); dto.setT(nettyDevice.getCurrentTaskId()); dto.setY(Double.parseDouble(yaw.getValue())); + siteMemory.getVehicleMessage(deviceId); + VehicleMessage vehicleMessage = siteMemory.getVehicleMessage(deviceId); + if (vehicleMessage != null) { + dto.setCm(vehicleMessage.getControl_mode()); + dto.setB(vehicleMessage.getBattery_percentage()); + } siteMemory.saveMapPushDTO(deviceId, dto); } } @@ -248,17 +262,82 @@ public class HostMessageHandler implements CustomMqttMessageHandler { WebStatusMessageDTO webStatusMessageDTO = new WebStatusMessageDTO(); List transferData = new ArrayList<>(); DeviceStatusDetail voltage = new DeviceStatusDetail(); - voltage.setName("voltage"); - voltage.setValue(String.valueOf(vehicleRelTimeDTO.getData().getBattery_voltage())); + voltage.setName("battery_percentage"); + voltage.setValue(String.valueOf(vehicleRelTimeDTO.getData().getBattery_percentage())); voltage.setUnit(""); - transferData.add(voltage); - DeviceStatusDetail speed = new DeviceStatusDetail(); - speed.setName("speed"); - speed.setValue(String.valueOf(vehicleRelTimeDTO.getData().getSpeed())); - speed.setUnit(""); - transferData.add(speed); + DeviceStatusDetail battery_voltage = new DeviceStatusDetail(); + battery_voltage.setName("battery_voltage"); + battery_voltage.setValue(String.valueOf(vehicleRelTimeDTO.getData().getBattery_voltage())); + battery_voltage.setUnit(""); + transferData.add(battery_voltage); + + DeviceStatusDetail left_motor_current = new DeviceStatusDetail(); + left_motor_current.setName("left_motor_current"); + left_motor_current.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLeft_motor_current())); + left_motor_current.setUnit(""); + transferData.add(left_motor_current); + + DeviceStatusDetail right_motor_current = new DeviceStatusDetail(); + right_motor_current.setName("right_motor_current"); + right_motor_current.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRight_motor_current())); + right_motor_current.setUnit(""); + transferData.add(right_motor_current); + + DeviceStatusDetail left_motor_temperature = new DeviceStatusDetail(); + left_motor_temperature.setName("left_motor_temperature"); + left_motor_temperature.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLeft_motor_temperature())); + left_motor_temperature.setUnit(""); + transferData.add(left_motor_temperature); + + DeviceStatusDetail right_motor_temperature = new DeviceStatusDetail(); + right_motor_temperature.setName("right_motor_temperature"); + right_motor_temperature.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRight_motor_temperature())); + right_motor_temperature.setUnit(""); + transferData.add(right_motor_temperature); + + DeviceStatusDetail main_temperature = new DeviceStatusDetail(); + main_temperature.setName("main_temperature"); + main_temperature.setValue(String.valueOf(vehicleRelTimeDTO.getData().getMain_temperature())); + main_temperature.setUnit(""); + transferData.add(main_temperature); + + DeviceStatusDetail yaw = new DeviceStatusDetail(); + yaw.setName("yaw"); + yaw.setValue(String.valueOf(vehicleRelTimeDTO.getData().getYaw())); + yaw.setUnit(""); + transferData.add(yaw); + + DeviceStatusDetail pitch = new DeviceStatusDetail(); + pitch.setName("pitch"); + pitch.setValue(String.valueOf(vehicleRelTimeDTO.getData().getPitch())); + pitch.setUnit(""); + transferData.add(pitch); + + DeviceStatusDetail heading_status = new DeviceStatusDetail(); + heading_status.setName("heading_status"); + heading_status.setValue(String.valueOf(vehicleRelTimeDTO.getData().getHeading_status())); + heading_status.setUnit(""); + transferData.add(heading_status); + + DeviceStatusDetail total_work_area = new DeviceStatusDetail(); + total_work_area.setName("total_work_area"); + total_work_area.setValue(String.valueOf(vehicleRelTimeDTO.getData().getTotal_work_area())); + total_work_area.setUnit(""); + transferData.add(total_work_area); + + DeviceStatusDetail total_work_duration = new DeviceStatusDetail(); + total_work_duration.setName("total_work_duration"); + total_work_duration.setValue(String.valueOf(vehicleRelTimeDTO.getData().getTotal_work_duration())); + total_work_duration.setUnit(""); + transferData.add(total_work_duration); + + DeviceStatusDetail total_work_distance = new DeviceStatusDetail(); + total_work_distance.setName("total_work_distance"); + total_work_distance.setValue(String.valueOf(vehicleRelTimeDTO.getData().getTotal_work_distance())); + total_work_distance.setUnit(""); + transferData.add(total_work_distance); DeviceStatusDetail motion = new DeviceStatusDetail(); motion.setName("motion"); @@ -266,11 +345,59 @@ public class HostMessageHandler implements CustomMqttMessageHandler { motion.setUnit(""); transferData.add(motion); - DeviceStatusDetail percentage = new DeviceStatusDetail(); - percentage.setName("percentage"); - percentage.setValue(String.valueOf(vehicleRelTimeDTO.getData().getBattery_percentage())); - percentage.setUnit(""); - transferData.add(percentage); + DeviceStatusDetail emergency_stop = new DeviceStatusDetail(); + emergency_stop.setName("emergency_stop"); + emergency_stop.setValue(String.valueOf(vehicleRelTimeDTO.getData().getEmergency_stop())); + emergency_stop.setUnit(""); + transferData.add(emergency_stop); + + DeviceStatusDetail left_target_rpm = new DeviceStatusDetail(); + left_target_rpm.setName("left_target_rpm"); + left_target_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLeft_target_rpm())); + left_target_rpm.setUnit(""); + transferData.add(left_target_rpm); + + DeviceStatusDetail right_target_rpm = new DeviceStatusDetail(); + right_target_rpm.setName("right_target_rpm"); + right_target_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRight_target_rpm())); + right_target_rpm.setUnit(""); + transferData.add(right_target_rpm); + + DeviceStatusDetail left_measured_rpm = new DeviceStatusDetail(); + left_measured_rpm.setName("left_measured_rpm"); + left_measured_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLeft_measured_rpm())); + left_measured_rpm.setUnit(""); + transferData.add(left_measured_rpm); + + DeviceStatusDetail right_measured_rpm = new DeviceStatusDetail(); + right_measured_rpm.setName("right_measured_rpm"); + right_measured_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRight_measured_rpm())); + right_measured_rpm.setUnit(""); + transferData.add(right_measured_rpm); + + DeviceStatusDetail left_motor_rpm = new DeviceStatusDetail(); + left_motor_rpm.setName("left_motor_rpm"); + left_motor_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getLeft_motor_rpm())); + left_motor_rpm.setUnit(""); + transferData.add(left_motor_rpm); + + DeviceStatusDetail right_motor_rpm = new DeviceStatusDetail(); + right_motor_rpm.setName("right_motor_rpm"); + right_motor_rpm.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRight_motor_rpm())); + right_motor_rpm.setUnit(""); + transferData.add(right_motor_rpm); + + DeviceStatusDetail roll = new DeviceStatusDetail(); + roll.setName("roll"); + roll.setValue(String.valueOf(vehicleRelTimeDTO.getData().getRoll())); + roll.setUnit(""); + transferData.add(roll); + + DeviceStatusDetail control_mode = new DeviceStatusDetail(); + control_mode.setName("control_mode"); + control_mode.setValue(String.valueOf(vehicleRelTimeDTO.getData().getControl_mode())); + control_mode.setUnit(""); + transferData.add(control_mode); webStatusMessageDTO.setData(transferData); webStatusMessageDTO.setType(MesType.deviceInfo); @@ -287,7 +414,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { if (vehicleRelTimeDTO != null) { // todo 更新里程数 里程数 * 割幅0.8 - vehicleRelTimeDTO.getData().getDistance(); + vehicleRelTimeDTO.getData().getTotal_work_distance(); NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId); if (nettyDevice != null) { Long orgId = nettyDevice.getDevice().getOrgId(); @@ -299,9 +426,11 @@ public class HostMessageHandler implements CustomMqttMessageHandler { deviceRunStatistics = new DeviceRunStatistics(); } deviceRunStatistics.setDeviceId(deviceId); - deviceRunStatistics.setDistance(vehicleRelTimeDTO.getData().getDistance()); - deviceRunStatistics.setWorkArea(vehicleRelTimeDTO.getData().getDistance() * 0.8); + deviceRunStatistics.setDistance(vehicleRelTimeDTO.getData().getTotal_work_distance()); + deviceRunStatistics.setWorkArea(vehicleRelTimeDTO.getData().getTotal_work_distance() * 0.8); siteMemory.saveRunStatistics(deviceRunStatistics); + + siteMemory.saveVehicleMessage(deviceId, vehicleRelTimeDTO.getData()); } } @@ -318,6 +447,14 @@ public class HostMessageHandler implements CustomMqttMessageHandler { } } } + // todo 推送实时位置和状态消息到外部 + try { + GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId, + String.format(MqttTopic.DEVICE_VEHICLE_MESSAGE_TOPIC, deviceId), + JsonUtils.toJsonString(vehicleRelTimeDTO)); + } catch (MqttException e) { + log.error("位置实时数据推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); + } } } catch (Exception e) { log.error("实时消息解析失败: deviceId={}, payload={}, error={}", deviceId, payload, e.getMessage()); @@ -371,7 +508,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { try { websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(sendDto)); } catch (Exception e) { - log.error("实时数据websocket推送失败: connectorId={}, error={}", x.getConnectorId(), + log.error("实时数据websocket任务状态推送失败: connectorId={}, error={}", x.getConnectorId(), e.getMessage()); } } @@ -382,8 +519,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, taskId), JsonUtils.toJsonString(sendDto)); } catch (MqttException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("实时数据websocket任务状态推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } } @@ -451,7 +587,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(sendDto)); } catch (Exception e) { log.error("实时数据websocket推送失败: connectorId={}, error={}", x.getConnectorId(), - e.getMessage()); + e.getMessage(), e); } } } @@ -461,8 +597,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler { String.format(MqttTopic.DEVICE_TASK_STATUS_TOPIC, device.getTask().getId()), JsonUtils.toJsonString(sendDto)); } catch (MqttException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("实时数据websocket推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } } diff --git a/maibu-common/src/main/java/com/maibu/mqtt/MqttTopic.java b/maibu-common/src/main/java/com/maibu/mqtt/MqttTopic.java index 63b9c9e..4d2943f 100644 --- a/maibu-common/src/main/java/com/maibu/mqtt/MqttTopic.java +++ b/maibu-common/src/main/java/com/maibu/mqtt/MqttTopic.java @@ -5,7 +5,9 @@ public class MqttTopic { /** 通用设备主题,建议使用 MOWER_* 系列主题 */ public static final String DEVICE_STATUS_TOPIC = "device/%s/realTimeMessage"; - public static final String DEVICE_LOCATION_TOPIC = "device/%s/locationMessage"; + public static final String DEVICE_VEHICLE_MESSAGE_TOPIC = "device/%s/host/realTime/message"; + + public static final String DEVICE_LOCATION_MESSAGE_TOPIC = "device/%s/host/location/message"; /** 通用设备主题,建议使用 MOWER_* 系列主题 */ public static final String DEVICE_TASK_STATUS_TOPIC = "task/%s/status"; /** 通用设备主题,建议使用 MOWER_* 系列主题 */ diff --git a/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java b/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java index 5f31cf7..3c2f404 100644 --- a/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java +++ b/maibu-netty-server/src/main/java/com/maibu/service/DeviceThreadService.java @@ -350,6 +350,7 @@ public class DeviceThreadService { } // todo 删除实时消息推送里面的数据 siteMemory.removeMapPushDTO(deviceId); + siteMemory.removeVehicleMessage(deviceId); } statusChangePush(deviceId, siteId, 0); @@ -386,12 +387,14 @@ public class DeviceThreadService { if (slaveDevice != null) { slaveDevice.setOnlineStatus(1); slaveDevice.setDevice(device); + statusChangePush(deviceId, device.getSiteId(), 1); } } else { // todo 推送设备上线 nettyDevice.status(1); nettyDevice.setDevice(device); - statusChangePush(deviceId, nettyDevice.getDevice().getSiteId(), 1); + nettyDevice.setConnectorStatus(ConnectorStatus.ACTIVE); + statusChangePush(deviceId, device.getSiteId(), 1); } } }); diff --git a/maibu-web-middleware/src/main/java/com/maibu/utils/ControlUtils.java b/maibu-web-middleware/src/main/java/com/maibu/utils/ControlUtils.java index b2aee54..321e80d 100644 --- a/maibu-web-middleware/src/main/java/com/maibu/utils/ControlUtils.java +++ b/maibu-web-middleware/src/main/java/com/maibu/utils/ControlUtils.java @@ -65,7 +65,7 @@ public class ControlUtils { } public static byte[] buildControlCommand(LawnMoverProtocolEntity entity) { - ByteBuffer buffer = ByteBuffer.allocate(27); + ByteBuffer buffer = ByteBuffer.allocate(27); // 15 buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(entity.getHeader1());