#update 设备在线升级的功能

This commit is contained in:
2026-09-23 14:38:12 +08:00
parent 5c0ee1f811
commit 25a05ae9d8
7 changed files with 140 additions and 22 deletions

View File

@@ -9,6 +9,7 @@ import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.core.host.HostWorkRecordCreateDTO;
import com.maibu.core.host.MutiPointEntity;
import com.maibu.core.host.PointEntity;
import com.maibu.core.ota.DeviceOtaFirmware;
import com.maibu.mapper.DevicePlanTaskMapper;
import com.maibu.mapper.WorkRecordMapper;
import com.maibu.memory.GlobalMemory;
@@ -26,10 +27,7 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -182,7 +180,7 @@ public class NettyDevice extends Connector {
return true;
}
public synchronized boolean failedTask() {
public synchronized boolean failedTask() {
if (task != null) {
task.setTaskStaus(DeviceTaskStaus.FAILED);
task.setUpdateTime(LocalDateTime.now());
@@ -216,7 +214,7 @@ public class NettyDevice extends Connector {
// 配置左右轮增益系数
public void sendWheelGainCoefficientsCommand(double leftForwardGain, double leftBackwardGain,
double rightForwardGain, double rightBackwardGain) {
double rightForwardGain, double rightBackwardGain) {
ByteBuffer buffer = ByteBuffer.allocate(39);
buffer.order(ByteOrder.LITTLE_ENDIAN);
@@ -403,4 +401,21 @@ public class NettyDevice extends Connector {
}
}
public void upgrade(String deviceId, DeviceOtaFirmware firmware) throws MqttException {
Map<String, Object> payload = new HashMap<>();
payload.put("action", "ota_upgrade");
payload.put("version", firmware.getVersion());
payload.put("file_url", firmware.getFileUrl());
payload.put("firmware_id", firmware.getId());
payload.put("sn", deviceId);
payload.put("timestamp", System.currentTimeMillis());
payload.put("message_id", firmware.getId() + "_" + System.currentTimeMillis());
GlobalMemory.customMqttDeviceMonitor.publish(
mqttClientId,
String.format(MqttTopic.MOWER_OTA_UPGRADE_SET, deviceId),
JsonUtils.toJsonString(payload)
);
}
}

View File

@@ -27,7 +27,7 @@ public class DeviceOtaOperateRecord extends BaseDO {
private OtaOperate operateType; //操作类型
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> deviceIds; //文件名
private List<String> deviceModels; //文件名
private String fileName; //名称

View File

@@ -111,7 +111,8 @@ public class GlobalMemory {
MqttTopic.MOWER_WILDCARD_TASK_STATUS,//任务状态
MqttTopic.MOWER_WILDCARD_ERROR,//错误
MqttTopic.MOWER_WILDCARD_REALTIME, //状态消息
MqttTopic.MOWER_WILDCARD_TASK_TARGET_REPLY //路径规划下发回复
MqttTopic.MOWER_WILDCARD_TASK_TARGET_REPLY, //路径规划下发回复
MqttTopic.MOWER_OTA_RESULT_POST
));
deviceConfig.setHandlerType(Constant.mqttHandleKey); //唯一的 用于处理上位机mqtt的
customMqttDeviceMonitor.registerDevice(deviceConfig);

View File

@@ -114,6 +114,9 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
case "route_reply":
handleRouteReply(deviceId, topic, payload);
break;
case "upgrade_reply":
handleUpgradeReply(deviceId, topic, payload);
break;
default:
log.debug("割草机消息: deviceId={}, topic={}, action={}", deviceId, topic, action);
break;
@@ -162,7 +165,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
}
public WebStatusMessageDTO createWebDeviceLocationMessage(HostLocationRelTimeDTO locationRelTimeDTO,
String deviceId) {
String deviceId) {
WebStatusMessageDTO webStatusMessageDTO = new WebStatusMessageDTO();
List<DeviceStatusDetail> transferData = new ArrayList<>();
@@ -447,6 +450,23 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
}
}
/**
* 处理控制指令响应
*/
private void handleUpgradeReply(String deviceId, String topic, String payload) {
// log.debug("【路径任务下发响应】deviceId={}, payload={}", deviceId, payload);
//
// // todo 更新任务接收状态
// HostNavigationReplyDTO replyDTO = JsonUtils.parseObject(payload, HostNavigationReplyDTO.class);
// NettyDevice device = deviceSessionManager.getDevice(deviceId);
// if (replyDTO.isAccepted()) {
// if (device != null && device.getTask() != null
// && String.valueOf(device.getTask().getId()).equals(replyDTO.getTask_id())) {
// device.getTask().setTaskAccepted(true);
// }
// }
}
/**
* 从 Topic 中解析 deviceId
* Topic 格式:mower/{deviceId}/...
@@ -497,6 +517,9 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
return "control_reply";
if (remaining.equals("task/target/reply"))
return "route_reply";
if (remaining.equals("/ota/result/post"))
return "upgrade_reply";
return null;
}

View File

@@ -64,15 +64,12 @@ public class MqttTopic {
public static final String MOWER_ERROR_POST = "mower/%s/event/error/post";
// ==================== OTA 固件升级 ====================
// /** 下发 OTA 升级指令(平台->设备) */
// public static final String MOWER_OTA_UPGRADE_SET =
// "mower/%s/ota/upgrade/set";
// /** OTA 升级进度上报(设备->平台) */
// public static final String MOWER_OTA_PROGRESS_POST =
// "mower/%s/ota/progress/post";
// /** OTA 升级结果上报(设备->平台) */
// public static final String MOWER_OTA_RESULT_POST =
// "mower/%s/ota/result/post";
/** 下发 OTA 升级指令(平台->设备) */
public static final String MOWER_OTA_UPGRADE_SET = "mower/%s/ota/upgrade/set";
/** OTA 升级进度上报(设备->平台) */
public static final String MOWER_OTA_PROGRESS_POST = "mower/+/ota/progress/post";
/** OTA 升级结果上报(设备->平台) */
public static final String MOWER_OTA_RESULT_POST = "mower/+/ota/result/post";
// ==================== 设备信息 ====================
// /** 查询设备信息(平台->设备) */