#优化更新 连接上位机相关

This commit is contained in:
2026-09-09 09:01:54 +08:00
parent fda1060b38
commit 4ee3f6be90
21 changed files with 330 additions and 218 deletions

View File

@@ -0,0 +1,9 @@
package com.maibu.constant;
public class NettyCacheKey {
public static final String deviceRunningStatusKey = "running_status:";
public static final String latestPositonKey = "latest_position:";
}

View File

@@ -13,6 +13,8 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.maibu.annotation.Excel;
import com.maibu.core.domain.BaseDO;
import com.maibu.core.host.LocationMessage;
import com.maibu.core.host.VehicleMessage;
import com.maibu.influxdb.MowerRealTimeData;
import io.swagger.annotations.ApiModel;
@@ -72,7 +74,6 @@ public class Device extends BaseDO {
@Excel(name = "产品名称")
private String productName;
/**
* 租户名称
*/
@@ -87,7 +88,6 @@ public class Device extends BaseDO {
@Excel(name = "固件版本")
private BigDecimal firmwareVersion;
/**
* WIFI固件版本
*/
@@ -95,7 +95,6 @@ public class Device extends BaseDO {
@Excel(name = "HTTP推送固件版本")
private BigDecimal wirelessVersion;
/**
* 设备状态(1-未激活,2-禁用,3-在线,4-离线)
*/
@@ -111,7 +110,7 @@ public class Device extends BaseDO {
private Integer onlineStatus;
@TableField(exist = false)
private Integer feStatus; //前端大屏显示 1任务中/2空闲
private Integer feStatus; // 前端大屏显示 1任务中/2空闲
/**
* wifi信号强度(信号极好4格[-55— 0],信号好3格[-70— -55],信号一般2格[-85— -70],信号差1格[-100— -85])
@@ -194,7 +193,6 @@ public class Device extends BaseDO {
@ApiModelProperty("设备摘要")
private String summary;
/**
* 是否是模拟设备
*/
@@ -206,12 +204,12 @@ public class Device extends BaseDO {
@ApiModelProperty("子设备地址")
private Integer slaveId;
@TableField(updateStrategy = FieldStrategy.IGNORED)
private Long siteId;
@TableField(updateStrategy = FieldStrategy.IGNORED)
private Long orgId;
/**
* 租户ID
*/
@@ -220,7 +218,6 @@ public class Device extends BaseDO {
@TableField(updateStrategy = FieldStrategy.IGNORED)
private Long tenantId;
@TableField(exist = false)
private DeviceRunningStatusHistory lastRunningStatus;
@@ -228,12 +225,19 @@ public class Device extends BaseDO {
private MowerRealTimeData lastMowerRealTimeData;
@TableField(exist = false)
private Map<Integer,Integer> statusStatistics;
private Map<Integer, Integer> statusStatistics;
@TableField(exist = false)
private DeviceRunParam runParam;
private boolean hasRegistered = false;
private boolean hasHost = false;
@TableField(exist = false)
private LocationMessage locationMessage;
@TableField(exist = false)
private VehicleMessage vehicleMessage;
}

View File

@@ -1,18 +1,14 @@
package com.maibu.core.business;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.maibu.core.domain.BaseDO;
import com.maibu.core.enums.ConnectType;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)

View File

@@ -58,7 +58,7 @@ public class DeviceRunParam extends BaseDO {
private String engineType;//发动机类型:honda-本田发动机
@Excel(name = "是否搭载上位机",cellType = Excel.ColumnType.NUMERIC)
private Integer hasHost;//是否搭载上位机
private Integer hasHost = 1;//是否搭载上位机
@Excel(name = "设备能力列表")
@TableField(exist = false)

View File

@@ -138,24 +138,6 @@ public class NettyDevice extends Connector {
return finishId;
}
// public synchronized boolean cancelTask() {
// if (task != null) {
// sendPathCommand((byte) 0x01, (short) 0, 0.00d, 0.00d, (short) 0);
// task.setTaskStaus(DeviceTaskStaus.CANCELED);
// task.setUpdateTime(LocalDateTime.now());
// devicePlanTaskMapper.saveOrUpdate(task);
// SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(),
// task.getSiteId());
// siteMemory.removeDevicePlanPrepare(task);
// siteMemory.removeDevicePlanExecute(task.getDeviceId());
// task = null;
// currentTaskId = null;
// currentPoint = null;
// locationQueue.clear();
// executingTask = false;
// }
// return true;
// }
public synchronized boolean cancelingTask() throws MqttException {
if (task != null) {
@@ -200,6 +182,24 @@ public class NettyDevice extends Connector {
return true;
}
public synchronized boolean failedTask() {
if (task != null) {
task.setTaskStaus(DeviceTaskStaus.FAILED);
task.setUpdateTime(LocalDateTime.now());
devicePlanTaskMapper.saveOrUpdate(task);
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
siteMemory.removeDevicePlanPrepare(task);
siteMemory.removeDevicePlanExecute(task.getDeviceId());
GlobalMemory.taskPathMap.remove(task.getId());
task = null;
currentTaskId = null;
currentPoint = null;
locationQueue.clear();
executingTask = false;
}
return true;
}
public void sendPathCommand(byte commandType, short pointCounts, double lat, double lng, short speed) {
RoutePlanSendEntity routePlanSendEntity = new RoutePlanSendEntity();
routePlanSendEntity.setCommandType(commandType);
@@ -296,29 +296,6 @@ public class NettyDevice extends Connector {
}
}
// public void startTask() {
// try {
// if (executingTask)
// return;
// if (task != null) {
// Long routeId = task.getRouteId();
// WorkRecord workRecord = workRecordMapper.selectById(routeId);
// if (workRecord != null) {
// PlanPath planPath = workRecord.getJsonData();
// if (planPath != null && !CollectionUtils.isEmpty(planPath.getPath())) {
// planPath.getPath().forEach(x -> {
// locationQueue.offer(x);
// });
// }
// }
// task.setStartTime(LocalDateTime.now());
// }
// executingTask = true;
// sendNextPoint();
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
public void startTask() {
try {

View File

@@ -1,14 +1,26 @@
package com.maibu.core.host;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.maibu.core.domain.OrgNoUserDO;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
public class LocationMessage {
@EqualsAndHashCode(callSuper = true)
@TableName("iot_host_location_message")
public class LocationMessage extends OrgNoUserDO {
@TableId(type = IdType.AUTO)
private Long id;
private String deviceId;
private double latitude;
private double longitude;
private double yaw;//目标航向,正北0°、顺时针正、0-360°
private double yaw;// 目标航向,正北0°、顺时针正、0-360°
private String fix_status;
private double altitude;

View File

@@ -6,8 +6,6 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.util.CollectionUtils;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import com.maibu.influxdb.inter.InfluxField;

View File

@@ -0,0 +1,14 @@
package com.maibu.mapper;
import org.springframework.stereotype.Repository;
import com.maibu.core.host.LocationMessage;
import com.maibu.mybatis.mapper.BaseMapperX;
/**
* 设备历史状态
*/
@Repository
public interface HostLocationMapper extends BaseMapperX<LocationMessage> {
}

View File

@@ -262,6 +262,13 @@ public class DeviceSessionManager {
for (Connector connected : new HashSet<>(nettyDevice.getConnectedConnectors())) {
nettyDevice.removeConnectedDevice(connected);
}
// 清理channel和状态
nettyDevice.setChannel(null);
Channel channel = channelMap.remove(deviceId);
if (channel != null) {
channelToDeviceIdMap.remove(channel);
channel.close();
}
} else {
// 如果是下位机,则保留绑定关系,只清理channel和状态
nettyDevice.setChannel(null);

View File

@@ -3,17 +3,20 @@ package com.maibu.mqtt;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maibu.constant.Constant;
import com.maibu.constant.NettyCacheKey;
import com.maibu.core.business.device.DeviceStatusDetail;
import com.maibu.core.business.device.NettyDevice;
import com.maibu.core.business.dto.WebStatusMessageDTO;
import com.maibu.core.business.inter.WebsocketMesDispather;
import com.maibu.core.enums.MesType;
import com.maibu.core.host.*;
import com.maibu.core.redis.RedisCache;
import com.maibu.memory.DeviceSessionManager;
import com.maibu.memory.GlobalMemory;
import com.maibu.utils.StringUtils;
import com.maibu.utils.json.JsonUtils;
import com.maibu.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
@@ -34,6 +37,8 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
private final String mqttClientId = "maibu-mqtt-client";
private final RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
@Override
public String getHandlerType() {
return Constant.mqttHandleKey;
@@ -131,6 +136,12 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
// GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
// String.format(MqttTopic.DEVICE_LOCATION_TOPIC, deviceId),
// JsonUtils.toJsonString(dto));
// todo 保存到内存
String key = NettyCacheKey.latestPositonKey + deviceId;
LocationMessage locationMessage = locationRelTimeDTO.getData();
locationMessage.setDeviceId(deviceId);
redisCache.setCacheObject(key, locationMessage);
}
} catch (Exception e) {
log.error("位置消息解析失败: deviceId={}, payload={}, error={}", deviceId, payload, e.getMessage());
@@ -242,14 +253,6 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
}
}
/**
* 处理状态变更事件
* topic: mower/{deviceId}/event/status/post
*/
private void handleStatusPost(String deviceId, String topic, String payload) {
log.info("【状态变更】deviceId={}, payload={}", deviceId, payload);
}
/**
* 处理心跳
* topic: mower/{deviceId}/event/heartbeat/post
@@ -291,10 +294,9 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
// if("failed".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
if ("failed".equals(dto.getStatus())) {
nettyDevice.failedTask();
}
// if("accepted".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();