This commit is contained in:
2026-11-10 16:40:15 +08:00
parent 99c7a3ab87
commit c3a2aee8fb
36 changed files with 932 additions and 262 deletions

View File

@@ -29,6 +29,9 @@ public class DevicePlanTask extends OrgBaseDO {
@TableField(exist = false)
private String deviceAlias; // 别名
@TableField(exist = false)
private String workName; // 路径规划名称
private DeviceTaskStaus taskStaus; // 任务状态
@TableField(exist = false)

View File

@@ -21,7 +21,7 @@ public class WorkRecord extends OrgBaseDO {
private String workName;
private Long userId;
private String mode;// 导航任务模式 single_point|coverage
private String mode;// 导航任务模式 single_point|coverage |predefined_path
// private String jsonData;
@@ -31,4 +31,6 @@ public class WorkRecord extends OrgBaseDO {
private PlanPath jsonData; // 计划
private String imgUrl;
private String fileUrl; // 存放的 路径规划的id
}

View File

@@ -8,6 +8,7 @@ import com.maibu.core.business.path.RoutePlanSendEntity;
import com.maibu.core.enums.ConnectorType;
import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.core.enums.RouteType;
import com.maibu.core.host.HostModeChangeDTO;
import com.maibu.core.host.HostWorkRecordCreateDTO;
import com.maibu.core.host.MutiPointEntity;
import com.maibu.core.host.PointEntity;
@@ -149,7 +150,6 @@ public class NettyDevice extends Connector {
regionConsecutiveTaskRecordMapper.saveOrUpdate(record);
}
siteMemory.removeDevicePlanExecute(task.getDeviceId());
GlobalMemory.taskPathMap.remove(task.getId());
task = null;
@@ -293,11 +293,26 @@ public class NettyDevice extends Connector {
// }
// todo 优化
public synchronized boolean pauseTask() {
public synchronized boolean pauseTask() throws MqttException {
if (executingTask) {
task.setTaskStaus(DeviceTaskStaus.PAUSE);
sendPathCommand((byte) 0x01, (short) 2, 0.00d, 0.00d, (short) 0);
// executingTask = false;
HostWorkRecordCreateDTO dto = new HostWorkRecordCreateDTO();
dto.setAction("pause");
dto.setCommand_id(task.getId() + "_" + System.currentTimeMillis());
dto.setTask_id(String.valueOf(task.getId()));
dto.setSn(task.getDeviceId());
// TODO 存内存
WorkRecord workRecord = workRecordMapper.selectById(task.getRouteId());
log.debug("发送开始任务 workRecord {}", workRecord);
if (workRecord != null) {
dto.setMode(workRecord.getMode());
}
dto.setTimestamp(System.currentTimeMillis());
dto.setMessage_id(task.getId() + "_" + System.currentTimeMillis());
dto.setProtocol_version(Constant.protocol_version);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
String.format(MqttTopic.MOWER_TASK_ROUTE_SET, task.getDeviceId()),
JsonUtils.toJsonString(dto));
return true;
} else {
return false;
@@ -306,15 +321,26 @@ public class NettyDevice extends Connector {
}
// todo 优化
public synchronized boolean recoveryTask() throws InterruptedException {
public synchronized boolean recoveryTask() throws InterruptedException, MqttException {
if (task.getTaskStaus().equals(DeviceTaskStaus.PAUSE)) {
task.setTaskStaus(DeviceTaskStaus.EXECUTING);
executingTask = true;
sendPathCommand((byte) 0x01, (short) 3, 0.00d, 0.00d, (short) 0);
// Thread.sleep(1000);
// sendPathCommand((byte) 0x01, (short) 1, currentPoint.getLat(),
// currentPoint.getLng(), (short) 0);
// sendNextPoint();
task.setTaskStaus(DeviceTaskStaus.PAUSE);
HostWorkRecordCreateDTO dto = new HostWorkRecordCreateDTO();
dto.setAction("resume");
dto.setCommand_id(task.getId() + "_" + System.currentTimeMillis());
dto.setTask_id(String.valueOf(task.getId()));
dto.setSn(task.getDeviceId());
// TODO 存内存
WorkRecord workRecord = workRecordMapper.selectById(task.getRouteId());
log.debug("发送开始任务 workRecord {}", workRecord);
if (workRecord != null) {
dto.setMode(workRecord.getMode());
}
dto.setTimestamp(System.currentTimeMillis());
dto.setMessage_id(task.getId() + "_" + System.currentTimeMillis());
dto.setProtocol_version(Constant.protocol_version);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
String.format(MqttTopic.MOWER_TASK_ROUTE_SET, task.getDeviceId()),
JsonUtils.toJsonString(dto));
return true;
} else {
return false;
@@ -372,7 +398,7 @@ public class NettyDevice extends Connector {
}
}
public void sendNavigationCommand() throws MqttException {
public synchronized void sendNavigationCommand() throws MqttException {
log.debug("发送开始任务 taskStatus:{},routeId:{}", task.getTaskStaus(), task.getRouteId());
if (!task.getTaskStaus().equals(DeviceTaskStaus.EXECUTING))
@@ -404,13 +430,6 @@ public class NettyDevice extends Connector {
}
if (workRecord.getMode().equals("coverage")) {
List<MutiPointEntity> boundary_points = new ArrayList<>();
// workRecord.getJsonData().getPath().forEach(x -> {
// MutiPointEntity mutiPoint = new MutiPointEntity();
// mutiPoint.setLongitude(x.getLng());
// mutiPoint.setLatitude(x.getLat());
// mutiPoint.setIndex();
// boundary_points.add(mutiPoint);
// });
List<LatAndLngEntity> path = workRecord.getJsonData().getPath();
for (int i = 0; i < path.size(); i++) {
@@ -424,8 +443,59 @@ public class NettyDevice extends Connector {
}
point.setBoundary_points(boundary_points);
}
if (workRecord.getMode().equals("predefined_path")) {
point.setPath_id(workRecord.getId());
point.setUrl(workRecord.getFileUrl());
point.setVersion("v2");
}
}
}
point.setExecute(true);
dto.setData(point);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
String.format(MqttTopic.MOWER_TASK_ROUTE_SET, task.getDeviceId()),
JsonUtils.toJsonString(dto));
}
}
public synchronized void sendPredefined(WorkRecord workRecord) throws MqttException {
log.debug("发送开始任务 taskStatus:{},routeId:{}", task.getTaskStaus(), task.getRouteId());
if (task != null)
return;
if (workRecord != null) {
HostWorkRecordCreateDTO dto = new HostWorkRecordCreateDTO();
dto.setAction("start");
dto.setCommand_id(task.getId() + "_" + System.currentTimeMillis());
dto.setTask_id(String.valueOf(task.getId()));
dto.setSn(task.getDeviceId());
dto.setMode(workRecord.getMode());
dto.setTimestamp(System.currentTimeMillis());
dto.setMessage_id("predefined" + "_" + System.currentTimeMillis());
dto.setProtocol_version(Constant.protocol_version);
PointEntity point = new PointEntity();
List<MutiPointEntity> boundary_points = new ArrayList<>();
List<LatAndLngEntity> path = workRecord.getJsonData().getPath();
for (int i = 0; i < path.size(); i++) {
LatAndLngEntity x = path.get(i);
MutiPointEntity mutiPoint = new MutiPointEntity();
mutiPoint.setLongitude(x.getLng());
mutiPoint.setLatitude(x.getLat());
mutiPoint.setIndex(i);
boundary_points.add(mutiPoint);
}
point.setBoundary_points(boundary_points);
point.setPath_id(workRecord.getId());
point.setVersion("v2");
point.setExecute(false);
dto.setData(point);
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
String.format(MqttTopic.MOWER_TASK_ROUTE_SET, task.getDeviceId()),
@@ -461,4 +531,28 @@ public class NettyDevice extends Connector {
JsonUtils.toJsonString(payload));
}
/**
* 下发模式切换指令
*
* @param deviceId
* @param controlMode
* @throws MqttException
*/
public void modeChage(String deviceId, Integer controlMode) throws MqttException {
HostModeChangeDTO payload = new HostModeChangeDTO();
payload.setSn(deviceId);
payload.setTimestamp(System.currentTimeMillis());
payload.setMessage_id("modeChange_" + System.currentTimeMillis());
payload.setCommand_id("modeChangeCommand_ " + System.currentTimeMillis());
payload.setProtocol_version(Constant.protocol_version);
Map<String, Object> data = new HashMap<>();
data.put("control_mode", controlMode);
payload.setData(data);
GlobalMemory.customMqttDeviceMonitor.publish(
mqttClientId,
String.format(MqttTopic.MOWER_MODE_CHANGE_SET, deviceId),
JsonUtils.toJsonString(payload));
}
}

View File

@@ -0,0 +1,29 @@
package com.maibu.core.business.dto;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.maibu.core.enums.DeviceTaskStaus;
import lombok.Data;
@Data
public class ConsecutiveTaskQueryDTO {
private Long siteId;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime endTime;
private String deviceId;
private DeviceTaskStaus taskStatus;
}

View File

@@ -12,6 +12,8 @@ public class DeviceMapPushDTO {
private double a; // altitude
private String s;// status 运行状态
private Long t; // taskId 任务id
private double p; // 任务进度
private String wk; // 任务进度
private double b; // 电量
private boolean ha = false;// hasAlarm 有无告警

View File

@@ -15,7 +15,7 @@ public class MowerCommandDispather {
@Resource
private ApplicationContext applicationContext;
public Long dispathDeviceTask(DeviceTaskCommandDTO dto) {
public Long dispathDeviceTask(DeviceTaskCommandDTO dto) throws Exception {
Map<String, MowerCommandInter> beanMap = applicationContext.getBeansOfType(MowerCommandInter.class);
// 遍历
Long id = null;

View File

@@ -4,6 +4,6 @@ import com.maibu.core.business.dto.DeviceTaskCommandDTO;
public interface MowerCommandInter {
Long createDeviceTask(DeviceTaskCommandDTO dto);
Long createDeviceTask(DeviceTaskCommandDTO dto) throws Exception;
}

View File

@@ -0,0 +1,16 @@
package com.maibu.core.host;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class HostModeChangeDTO extends HostBase {
private String command_id;
private Map<String, Object> data; // map key control_mode 2=自动任务,3=远程遥控
}

View File

@@ -0,0 +1,16 @@
package com.maibu.core.host;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class HostModeChangeReplyDTO extends HostBase {
private Boolean accepted;
private String code;
private String message;
}

View File

@@ -1,12 +1,25 @@
package com.maibu.core.host;
import lombok.Data;
@Data
public class HostTaskStatusDetailDTO {
private float progress;
private double progress;// 已完成作业路径比例,转移段持平且任务内不回退。规划完成前的 0 不代表已开始覆盖。
private double distance_remaining; // 整任务剩余作业路径
private Integer number_of_recoveries;// 恢复行为次数,不代表障碍数量。
private double elapsed_s;
private double eta_s;
private Integer current_cell_index;
private Integer total_cells;
private double distance_traveled;
private double speed;
private Integer task_phase;
private String error_code;
private String error_message;
private double total_distance_m;
private double distance_traveled_m;
private double execution_time_s;
private Integer cells_completed;
}

View File

@@ -1,20 +1,20 @@
package com.maibu.core.host;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class HostWorkRecordCreateDTO extends HostBase {
public class HostWorkRecordCreateDTO extends HostBase {
private String task_id; //任务唯一编号
private String command_id;//指令唯一编号
private String mode;//导航任务模式 single_point|coverage
private String action;//任务动作 start/pause/resume/cancel
// private String frame_id; //WGS84
private String task_id; // 任务唯一编号
private String command_id;// 指令唯一编号
private String mode;// 导航任务模式 single_point|coverage
private String action;// 任务动作 start/pause/resume/cancel
// private String frame_id; //WGS84
private PointEntity data;
}

View File

@@ -9,7 +9,7 @@ import lombok.Data;
@Data
public class ObstacleDetectionData {
private MesType mesType;
private MesType type;
/**
* 融合状态

View File

@@ -9,12 +9,16 @@ public class PointEntity {
private double latitude;
private double longitude;
private double yaw;//目标航向,正北0°、顺时针正、0-360°
private double yaw;// 目标航向,正北0°、顺时针正、0-360°
private List<MutiPointEntity> boundary_points;
private double cell_size;//覆盖分辨率,单位m
private double cell_size;// 覆盖分辨率,单位m
private double angle;//覆盖作业方向,单位度
private double angle;// 覆盖作业方向,单位度
private String url; // minio 存放文件的地址
private Long path_id; // 路径id 系统保存用的routeId;
private String version;
private boolean execute = true; // 是直接执行还是创建自定义任务
}

View File

@@ -17,9 +17,10 @@ import com.maibu.mybatis.mapper.BaseMapperX;
@Repository
public interface DevicePlanTaskMapper extends BaseMapperX<DevicePlanTask> {
@Select("SELECT t.*, d.device_alias " +
@Select("SELECT t.*, iwr.work_name,d.device_alias " +
"FROM iot_device_task t " +
"LEFT JOIN iot_device d ON d.device_id = t.device_id " +
"LEFT JOIN iot_device d ON d.serial_number = t.device_id " +
"left join iot_work_record iwr on t.route_id = iwr.id " +
"${ew.customSqlSegment}")
List<DevicePlanTask> selectTaskHistoryWithDeviceAlias(
@Param(Constants.WRAPPER) Wrapper<DevicePlanTask> wrapper);

View File

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
@Component
@@ -29,6 +30,8 @@ public class GlobalMemory {
public static InfluxDBUtil influxDBUtil;
public static ConcurrentHashMap<String, CompletableFuture<Boolean>> modeFutureMap = new ConcurrentHashMap<>();
// public static MqttClientUtil mqttClientUtil;
public static CustomMqttDeviceMonitor customMqttDeviceMonitor = null; // 这里面包含了 连我们自己的mqtt 和外部的一些设备,自己的用clientId
@@ -114,8 +117,11 @@ public class GlobalMemory {
MqttTopic.MOWER_WILDCARD_REALTIME, // 状态消息
MqttTopic.MOWER_WILDCARD_TASK_TARGET_REPLY, // 路径规划下发回复
MqttTopic.MOWER_OTA_RESULT_POST, // 在线升级
MqttTopic.MOWER_OBSTACLE
));
MqttTopic.MOWER_OBSTACLE, // 障碍物
MqttTopic.MOWER_MODE_REPLY, // 模式切换回复
MqttTopic.MOWER_FAULT_POST, // 故障上报
MqttTopic.MOWER_FAULT_RECOVERY_REPLY // 故障恢复回复
));
deviceConfig.setHandlerType(Constant.mqttHandleKey); // 唯一的 用于处理上位机mqtt的
customMqttDeviceMonitor.registerDevice(deviceConfig);
}

View File

@@ -1,15 +1,12 @@
package com.maibu.memory;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -75,11 +72,11 @@ public class SiteMemory {
// 记录 设备上线后的运行数据 下线后清除并保存到数据库
public static ConcurrentHashMap<String, DeviceRunStatistics> deviceRunStatisticsMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, DeviceMapPushDTO> deviceMapPushMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, DeviceMapPushDTO> deviceMapPushMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, VehicleMessage> vehicleMessageMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<RuleType, IotAutoRule> iotAutoRuleMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<RuleType, List<IotAutoRule>> iotAutoRuleMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, RegionConsecutiveTaskRecord> regionConsecutiveTaskMap = new ConcurrentHashMap<>();
@@ -109,20 +106,21 @@ public class SiteMemory {
return record;
}
public List<IotAutoRule> getAllIotAutoRule() {
return new ArrayList<>(iotAutoRuleMap.values());
}
public IotAutoRule getIotAutoRule(RuleType ruleType) {
public List<IotAutoRule> getIotAutoRule(RuleType ruleType) {
return iotAutoRuleMap.get(ruleType);
}
public void saveIotAutoRule(RuleType ruleType, IotAutoRule iotAutoRule) {
iotAutoRuleMap.put(ruleType, iotAutoRule);
List<IotAutoRule> list = iotAutoRuleMap.computeIfAbsent(
ruleType, k -> new ArrayList<>());
list.removeIf(item -> Objects.equals(item.getId(), iotAutoRule.getId()));
list.add(iotAutoRule);
}
public void removeIotAutoRule(RuleType ruleType) {
iotAutoRuleMap.remove(ruleType);
public void removeIotAutoRule(IotAutoRule iotAutoRule) {
List<IotAutoRule> list = iotAutoRuleMap.get(iotAutoRule.getRuleType());
list.removeIf(i -> iotAutoRule.getId().equals(i.getId()));
}
public List<VehicleMessage> getAllVehicleMessage() {
@@ -154,6 +152,10 @@ public class SiteMemory {
return deviceMapPushMap;
}
public DeviceMapPushDTO getDeviceMapPusById(String deviceId) {
return deviceMapPushMap.get(deviceId);
}
public void saveMapPushDTO(String deviceId, DeviceMapPushDTO dto) {
deviceMapPushMap.put(deviceId, dto);
}

View File

@@ -28,6 +28,7 @@ import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* mqtt相关的 上位机处理器
@@ -111,6 +112,15 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
case "obstacle_post":
handleObstaclePost(deviceId, topic, payload);
break;
case "mode_reply":
handleModeReply(deviceId, topic, payload);
break;
case "fault_post":
handleFaultPost(deviceId, topic, payload);
break;
case "fault_recovery_reply":
handleFaultRecoveryReply(deviceId, topic, payload);
break;
default:
log.debug("割草机消息: deviceId={}, topic={}, action={}", deviceId, topic, action);
@@ -255,6 +265,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
dto.setMs(vehicleMessage.getMotion());
dto.setB(vehicleMessage.getBattery_percentage());
}
dto.setWk(nettyDevice.getTask() != null ? nettyDevice.getTask().getWorkName() : "");
siteMemory.saveMapPushDTO(deviceId, dto);
}
}
@@ -560,6 +571,16 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
// 任务进度 百分比
Long siteId = nettyDevice.getDevice().getSiteId();
Long orgId = nettyDevice.getDevice().getOrgId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
DeviceMapPushDTO pushDto = siteMemory.getDeviceMapPusById(deviceId);
if (pushDto != null && dto.getData() != null) {
pushDto.setP(dto.getData().getProgress());
}
}
}
}
@@ -700,7 +721,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
List<NettyDevice> controlMasters = deviceSessionManager
.getAllSlaveControl(deviceId);
data.setMesType(MesType.detectionReport);
data.setType(MesType.detectionReport);
if (!CollectionUtils.isEmpty(controlMasters)) {
for (NettyDevice x : controlMasters) {
@@ -732,6 +753,44 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
}
private void handleModeReply(String deviceId, String topic, String payload) {
log.debug("【模式切换回复】deviceId={}, payload={}", deviceId, payload);
HostModeChangeReplyDTO data = JsonUtils.parseObject(payload, HostModeChangeReplyDTO.class);
CompletableFuture<Boolean> future = GlobalMemory.modeFutureMap.get(deviceId);
if (future != null && data != null) {
future.complete(data.getAccepted());
}
}
private void handleFaultPost(String deviceId, String topic, String payload) {
log.debug("【故障上报】deviceId={}, payload={}", deviceId, payload);
// HostFaultPostDTO data = JsonUtils.parseObject(payload,
// HostFaultPostDTO.class);
// CompletableFuture<Boolean> future = GlobalMemory.modeFutureMap.get(deviceId);
// if (future != null && data != null) {
// future.complete(data.getAccepted());
// }
}
private void handleFaultRecoveryReply(String deviceId, String topic, String payload) {
log.debug("【故障恢复回复】deviceId={}, payload={}", deviceId, payload);
// HostFaultRecoveryReplyDTO data = JsonUtils.parseObject(payload,
// HostFaultRecoveryReplyDTO.class);
// CompletableFuture<Boolean> future = GlobalMemory.modeFutureMap.get(deviceId);
// if (future != null && data != null) {
// future.complete(data.getAccepted());
// }
}
/**
* 从 Topic 中解析 deviceId
* Topic 格式:mower/{deviceId}/...
@@ -782,10 +841,16 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
return "control_reply";
if (remaining.equals("task/target/reply"))
return "route_reply";
if (remaining.equals("/ota/result/post"))
if (remaining.equals("ota/result/post"))
return "upgrade_reply";
if (remaining.equals("/perception/obstacle/post"))
if (remaining.equals("perception/obstacle/post"))
return "obstacle_post";
if (remaining.equals("control/mode/set/reply"))
return "mode_reply";
if (remaining.equals("event/fault/post"))
return "fault_post";
if (remaining.equals("fault/recovery/reply"))
return "fault_recovery_reply";
return null;
}

View File

@@ -109,9 +109,24 @@ public class MqttTopic {
public static final String MOWER_HEARTBEAT = "mower/+/event/heartbeat/post";
//障碍物
// 障碍物
public static final String MOWER_OBSTACLE = "mower/+/perception/obstacle/post";
/** 模式切换指令 无遥才能下发(平台->设备) */
public static final String MOWER_MODE_CHANGE_SET = "mower/%s/control/mode/set";
// 模式切换回复
public static final String MOWER_MODE_REPLY = "mower/+/control/mode/reply";
// 故障上报
public static final String MOWER_FAULT_POST = "mower/+/event/fault/post";
// 故障恢复 (平台->设备)
public static final String MOWER_FAULT_RECOVERY = "mower/%s/fault/recovery/set";
// 故障恢复回复
public static final String MOWER_FAULT_RECOVERY_REPLY = "mower/+/fault/recovery/reply";
/**
* 格式化 Topic 模板,将 %s 替换为设备ID
*