#update
This commit is contained in:
@@ -30,7 +30,7 @@ public class Constant {
|
||||
public static final Long hostOfflineInterval = 10000L; // 单位毫秒
|
||||
|
||||
// ==================== 远程控制 ====================
|
||||
public static final String protocol_version = "1.4.5";
|
||||
public static final String protocol_version = "1.4.6";
|
||||
public static final String remote_control_msg_prefix = "remote-msg";
|
||||
public static final String remote_control_cmd_prefix = "remote-cmd";
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ public class NettyCacheKey {
|
||||
|
||||
public static final String deviceRunningStatusKey = "running_status:";
|
||||
|
||||
public static final String vehicleMessageKey = "vehicle_message:";
|
||||
|
||||
public static final String latestPositonKey = "latest_position:";
|
||||
|
||||
}
|
||||
|
||||
@@ -55,4 +55,9 @@ public class DevicePlanTask extends OrgBaseDO {
|
||||
@TableField(exist = false)
|
||||
private RestPoint backTaskPoint;
|
||||
|
||||
@TableField(exist = false)
|
||||
private double taskDistance; // 记录当前任务的累计里程
|
||||
@TableField(exist = false)
|
||||
private double taskTime; // s
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class DeviceRunStatistics implements Serializable {
|
||||
|
||||
private Double distance; //作业距离
|
||||
|
||||
private Long time; //时长 分钟
|
||||
private Double time; //时长 分钟
|
||||
|
||||
private String deviceId;
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.maibu.core.business;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DeviceStatusRecordDTO {
|
||||
private List<DeviceRunningStatusHistory> histories;
|
||||
|
||||
private long lastRecordTime;
|
||||
}
|
||||
|
||||
@@ -24,4 +24,6 @@ public class ErrorIdentificationStandard extends OrgBaseDO {
|
||||
private String errorCode;
|
||||
private ErrorLevel errorLevel;
|
||||
private String suggestion;
|
||||
|
||||
private int codeIndex; //对应解析code某一位
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.maibu.core.business;
|
||||
|
||||
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
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("iot_error_record")
|
||||
public class ErrorRecord extends OrgNoUserDO {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String deviceId;
|
||||
private String errorDescription;
|
||||
private String errorName;
|
||||
private Long standardId;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class Alarm extends TenantBaseDO {
|
||||
@Excel(name = "告警内容")
|
||||
private String alarmContent;
|
||||
|
||||
private String timeValues; //告警时上报的值
|
||||
private String timeValues; // 告警时上报的值
|
||||
|
||||
@ApiModelProperty("设备ID")
|
||||
@Excel(name = "设备ID", cellType = Excel.ColumnType.NUMERIC)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.maibu.core.business.device;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.core.business.*;
|
||||
@@ -8,12 +9,14 @@ 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.HostEstopChangeDTO;
|
||||
import com.maibu.core.host.HostModeChangeDTO;
|
||||
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.DeviceRunStatisticsMapper;
|
||||
import com.maibu.mapper.RegionConsecutiveTaskRecordMapper;
|
||||
import com.maibu.mapper.WorkRecordMapper;
|
||||
import com.maibu.memory.GlobalMemory;
|
||||
@@ -77,6 +80,9 @@ public class NettyDevice extends Connector {
|
||||
@JsonIgnore
|
||||
private WorkRecordMapper workRecordMapper = SpringUtils.getBean(WorkRecordMapper.class);
|
||||
|
||||
@JsonIgnore
|
||||
private DeviceRunStatisticsMapper runStatisticsMapper = SpringUtils.getBean(DeviceRunStatisticsMapper.class);
|
||||
|
||||
@JsonIgnore
|
||||
private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
|
||||
|
||||
@@ -127,11 +133,40 @@ public class NettyDevice extends Connector {
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public void updateWorkArea() {
|
||||
|
||||
double taskDistance = task.getTaskDistance();
|
||||
double taskTime = task.getTaskTime();
|
||||
double workArea = taskDistance * 0.8;
|
||||
LambdaQueryWrapper<DeviceRunStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeviceRunStatistics::getDeviceId, task.getDeviceId());
|
||||
DeviceRunStatistics existing = runStatisticsMapper
|
||||
.selectOne(queryWrapper);
|
||||
if (existing != null) {
|
||||
existing.setDistance(
|
||||
existing.getDistance() + taskDistance);
|
||||
existing.setWorkArea(
|
||||
existing.getWorkArea() + workArea);
|
||||
existing.setTime(existing.getTime() + taskTime); // 累加时长,单位为分钟
|
||||
} else {
|
||||
existing = new DeviceRunStatistics();
|
||||
existing.setDistance(taskDistance);
|
||||
existing.setWorkArea(workArea);
|
||||
existing.setTime(taskTime); // 累加时长,单位为分钟
|
||||
existing.setDeviceId(task.getDeviceId());
|
||||
}
|
||||
runStatisticsMapper.saveOrUpdate(existing);
|
||||
}
|
||||
|
||||
public synchronized Long finishTask() {
|
||||
// sendPathCommand((byte) 0x01, (short) 0, 0.00d, 0.00d, (short) 0);
|
||||
long finishId = task.getId();
|
||||
task.setTaskStaus(DeviceTaskStaus.FINISH);
|
||||
task.setFinishTime(LocalDateTime.now());
|
||||
|
||||
// 更新累计里程
|
||||
updateWorkArea();
|
||||
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
|
||||
|
||||
@@ -190,6 +225,9 @@ public class NettyDevice extends Connector {
|
||||
Long taskId = task.getId();
|
||||
task.setTaskStaus(DeviceTaskStaus.CANCELED);
|
||||
task.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
// 更新累计里程
|
||||
updateWorkArea();
|
||||
devicePlanTaskMapper.saveOrUpdate(task);
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
|
||||
siteMemory.removeDevicePlanPrepare(task);
|
||||
@@ -555,4 +593,27 @@ public class NettyDevice extends Connector {
|
||||
JsonUtils.toJsonString(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* 急停更新
|
||||
*
|
||||
* @param deviceId
|
||||
* @throws MqttException
|
||||
*/
|
||||
public void eStopChange(String deviceId, Integer eStop) throws MqttException {
|
||||
HostEstopChangeDTO payload = new HostEstopChangeDTO();
|
||||
payload.setSn(deviceId);
|
||||
payload.setTimestamp(System.currentTimeMillis());
|
||||
payload.setMessage_id("eStopChange_" + System.currentTimeMillis());
|
||||
payload.setCommand_id("eStopChangeCommand_ " + System.currentTimeMillis());
|
||||
payload.setProtocol_version(Constant.protocol_version);
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("emergency_stop", eStop);
|
||||
payload.setData(data);
|
||||
|
||||
GlobalMemory.customMqttDeviceMonitor.publish(
|
||||
mqttClientId,
|
||||
String.format(MqttTopic.MOWER_ESTOP_SET, deviceId),
|
||||
JsonUtils.toJsonString(payload));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ public class DeviceMapPushDTO {
|
||||
|
||||
private Integer cm;// 控制模式 manual_remote|auto
|
||||
|
||||
private Integer sm;// 系统控制的模式 只有2,3 远程遥控 自动任务;
|
||||
|
||||
private String ms; // 运动状态 moving|stopped|blocked
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.maibu.core.business.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.maibu.core.host.VehicleMessage;
|
||||
|
||||
@Data
|
||||
public class VehicleMessageRecordDTO {
|
||||
private List<VehicleMessage> histories;
|
||||
|
||||
private long lastRecordTime;
|
||||
}
|
||||
@@ -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 HostEstopChangeDTO extends HostBase {
|
||||
|
||||
private String command_id;
|
||||
|
||||
private Map<String, Object> data; // map key control_mode 2=自动任务,3=远程遥控
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.maibu.core.host;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class HostFaultPostDTO extends HostBase {
|
||||
|
||||
private HostFaultPostDetail data;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.maibu.core.host;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class HostFaultPostDetail extends HostBase {
|
||||
|
||||
private Boolean active;
|
||||
private String fault_code;
|
||||
private String message;
|
||||
private String related_message_id;
|
||||
private String severity;
|
||||
private String source;
|
||||
|
||||
}
|
||||
@@ -7,10 +7,12 @@ import lombok.EqualsAndHashCode;
|
||||
@Data
|
||||
public class HostModeChangeReplyDTO extends HostBase {
|
||||
|
||||
private Boolean accepted;
|
||||
private Boolean accepted;
|
||||
|
||||
private String code;
|
||||
|
||||
private String message;
|
||||
|
||||
private Integer mode;
|
||||
|
||||
}
|
||||
|
||||
@@ -22,4 +22,6 @@ public class HostTaskStatusDetailDTO {
|
||||
private double distance_traveled_m;
|
||||
private double execution_time_s;
|
||||
private Integer cells_completed;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Data;
|
||||
@Data
|
||||
public class ObstacleDetectionData {
|
||||
|
||||
|
||||
private MesType type;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.maibu.core.host;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -8,6 +7,6 @@ import lombok.EqualsAndHashCode;
|
||||
@Data
|
||||
public class ObstaclePostDTO extends HostBase {
|
||||
|
||||
private ObstacleDetectionData fusion_obstacle;
|
||||
|
||||
private ObstacleDetectionData data;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ public class ParamDetail {
|
||||
private int chassis_lift; // 底盘升降 0:停止 1:升 2:降
|
||||
private int cutter; // 割刀加减速 0:停止 1:加速 2:减速
|
||||
private int ignition; // 点火熄火 0:停止 1:点火 2:熄火
|
||||
private int emergency_stop; // 急停 0:正常 1:急停
|
||||
// private int emergency_stop; // 急停 0:正常 1:急停
|
||||
private int valid_for_ms; // 指令有效时间,单位毫秒
|
||||
}
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
package com.maibu.core.host;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("iot_vehicle_message")
|
||||
public class VehicleMessage {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String deviceId;
|
||||
|
||||
private double cutter_speed;// 割刀实际转速
|
||||
@@ -41,4 +55,10 @@ public class VehicleMessage {
|
||||
private double linear_speed; // 实时线速度
|
||||
private double angular_speed; // 实时角速度
|
||||
|
||||
private Integer systemMode; // 当前 没有遥控器控制的模式 control_mode 为0,当前的系统控制的模式 只有2,3
|
||||
|
||||
private LocalDateTime createTime;
|
||||
|
||||
private Long error_code; // 暂时用于上报错误
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.maibu.mapper;
|
||||
|
||||
import com.maibu.core.business.DeviceRunStatistics;
|
||||
import com.maibu.mybatis.mapper.BaseMapperX;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* 设备历史状态
|
||||
*/
|
||||
@Repository
|
||||
public interface DeviceRunStatisticsMapper extends BaseMapperX<DeviceRunStatistics> {
|
||||
|
||||
}
|
||||
@@ -120,7 +120,8 @@ public class GlobalMemory {
|
||||
MqttTopic.MOWER_OBSTACLE, // 障碍物
|
||||
MqttTopic.MOWER_MODE_REPLY, // 模式切换回复
|
||||
MqttTopic.MOWER_FAULT_POST, // 故障上报
|
||||
MqttTopic.MOWER_FAULT_RECOVERY_REPLY // 故障恢复回复
|
||||
MqttTopic.MOWER_FAULT_RECOVERY_REPLY, // 故障恢复回复
|
||||
MqttTopic.MOWER_ESTOP_REPLY // 急停的回复
|
||||
));
|
||||
deviceConfig.setHandlerType(Constant.mqttHandleKey); // 唯一的 用于处理上位机mqtt的
|
||||
customMqttDeviceMonitor.registerDevice(deviceConfig);
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
@@ -23,6 +24,7 @@ import com.maibu.core.business.IoTCommonDevice;
|
||||
import com.maibu.core.business.OrderModel;
|
||||
import com.maibu.core.business.Product;
|
||||
import com.maibu.core.business.RegionConsecutiveTaskRecord;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
import com.maibu.core.business.dto.ConsecutiveTaskDTO;
|
||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
||||
import com.maibu.core.domain.entity.SysSite;
|
||||
@@ -80,6 +82,71 @@ public class SiteMemory {
|
||||
|
||||
public static final ConcurrentHashMap<String, RegionConsecutiveTaskRecord> regionConsecutiveTaskMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static final ConcurrentHashMap<String, List<AlarmMessage>> alarmMap = new ConcurrentHashMap<>();
|
||||
|
||||
public void saveAlarm(String deviceSn, AlarmMessage alarmMessage) {
|
||||
if (deviceSn == null || alarmMessage == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
alarmMap.compute(deviceSn, (key, list) -> {
|
||||
if (list == null) {
|
||||
list = new CopyOnWriteArrayList<>();
|
||||
}
|
||||
|
||||
list.add(alarmMessage);
|
||||
return list;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量替换设备告警
|
||||
*
|
||||
* @param deviceSn 设备SN
|
||||
* @param alarms 新的告警集合
|
||||
*/
|
||||
public void replaceAlarms(String deviceSn, List<AlarmMessage> alarms) {
|
||||
if (deviceSn == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (alarms == null || alarms.isEmpty()) {
|
||||
alarmMap.remove(deviceSn);
|
||||
return;
|
||||
}
|
||||
|
||||
alarmMap.put(deviceSn, new CopyOnWriteArrayList<>(alarms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备SN和告警编号查询告警
|
||||
*/
|
||||
public AlarmMessage getAlarm(String deviceSn, String alarmNo) {
|
||||
if (deviceSn == null || alarmNo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<AlarmMessage> list = alarmMap.get(deviceSn);
|
||||
if (list == null || list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return list.stream()
|
||||
.filter(x -> alarmNo.equals(x.getNo()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备SN删除所有告警
|
||||
*/
|
||||
public void removeAlarms(String deviceSn) {
|
||||
if (deviceSn == null) {
|
||||
return;
|
||||
}
|
||||
alarmMap.remove(deviceSn);
|
||||
}
|
||||
|
||||
public void saveRegionConsecutiveTask(String deviceId, ConsecutiveTaskDTO dto, String username) {
|
||||
RegionConsecutiveTaskRecord record = new RegionConsecutiveTaskRecord();
|
||||
record.setDeviceId(deviceId);
|
||||
@@ -139,7 +206,18 @@ public class SiteMemory {
|
||||
return vehicleMessageMap.get(deviceId);
|
||||
}
|
||||
|
||||
public void saveVehicleSystemMode(String deviceId, Integer systemMode) {
|
||||
VehicleMessage vehicleMessage = getVehicleMessage(deviceId);
|
||||
if (vehicleMessage != null) {
|
||||
vehicleMessage.setSystemMode(systemMode);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveVehicleMessage(String deviceId, VehicleMessage vehicleMessage) {
|
||||
VehicleMessage oldVehicle = getVehicleMessage(deviceId);
|
||||
if (oldVehicle != null) {
|
||||
vehicleMessage.setSystemMode(oldVehicle.getSystemMode());
|
||||
}
|
||||
vehicleMessage.setDeviceId(deviceId);
|
||||
vehicleMessageMap.put(deviceId, vehicleMessage);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,43 @@
|
||||
package com.maibu.mqtt;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.maibu.constant.Constant;
|
||||
import com.maibu.constant.NettyCacheKey;
|
||||
import com.maibu.core.business.DevicePlanTask;
|
||||
import com.maibu.core.business.DeviceRunStatistics;
|
||||
import com.maibu.core.business.DeviceRunningStatusHistory;
|
||||
import com.maibu.core.business.DeviceStatusRecordDTO;
|
||||
import com.maibu.core.business.ErrorIdentificationStandard;
|
||||
import com.maibu.core.business.ErrorRecord;
|
||||
import com.maibu.core.business.alarm_center.AlarmMessage;
|
||||
import com.maibu.core.business.device.DeviceStatusDetail;
|
||||
import com.maibu.core.business.device.NettyDevice;
|
||||
import com.maibu.core.business.dto.DeviceMapPushDTO;
|
||||
import com.maibu.core.business.dto.VehicleMessageRecordDTO;
|
||||
import com.maibu.core.business.dto.WebDeviceTaskStatusMessageDTO;
|
||||
import com.maibu.core.business.dto.WebMapMessageDTO;
|
||||
import com.maibu.core.business.dto.WebStatusMessageDTO;
|
||||
import com.maibu.core.business.inter.WebsocketMesDispather;
|
||||
import com.maibu.core.enums.DeviceTaskStaus;
|
||||
import com.maibu.core.enums.ErrorSource;
|
||||
import com.maibu.core.enums.MesType;
|
||||
import com.maibu.core.host.*;
|
||||
import com.maibu.core.enums.RespondCode;
|
||||
import com.maibu.core.host.HeartBeatDTO;
|
||||
import com.maibu.core.host.HostLocationRelTimeDTO;
|
||||
import com.maibu.core.host.HostModeChangeReplyDTO;
|
||||
import com.maibu.core.host.HostNavigationReplyDTO;
|
||||
import com.maibu.core.host.HostTaskStatusDTO;
|
||||
import com.maibu.core.host.HostVehicleRelTimeDTO;
|
||||
import com.maibu.core.host.LocationMessage;
|
||||
import com.maibu.core.host.ObstaclePostDTO;
|
||||
import com.maibu.core.host.VehicleMessage;
|
||||
import com.maibu.core.redis.RedisCache;
|
||||
import com.maibu.enums.DeviceStatusEnum;
|
||||
import com.maibu.memory.DeviceSessionManager;
|
||||
@@ -22,13 +46,8 @@ import com.maibu.memory.SiteMemory;
|
||||
import com.maibu.utils.StringUtils;
|
||||
import com.maibu.utils.json.JsonUtils;
|
||||
import com.maibu.utils.spring.SpringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* mqtt相关的 上位机处理器
|
||||
@@ -44,6 +63,8 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
|
||||
private final RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
|
||||
|
||||
private final Long recordInterval = 2000L;
|
||||
|
||||
@Override
|
||||
public String getHandlerType() {
|
||||
return Constant.mqttHandleKey;
|
||||
@@ -121,6 +142,9 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
case "fault_recovery_reply":
|
||||
handleFaultRecoveryReply(deviceId, topic, payload);
|
||||
break;
|
||||
case "eStop_reply":
|
||||
handleEstopReply(deviceId, topic, payload);
|
||||
break;
|
||||
|
||||
default:
|
||||
log.debug("割草机消息: deviceId={}, topic={}, action={}", deviceId, topic, action);
|
||||
@@ -264,6 +288,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
dto.setCm(vehicleMessage.getControl_mode());
|
||||
dto.setMs(vehicleMessage.getMotion());
|
||||
dto.setB(vehicleMessage.getBattery_percentage());
|
||||
dto.setSm(vehicleMessage.getSystemMode());
|
||||
}
|
||||
dto.setWk(nettyDevice.getTask() != null ? nettyDevice.getTask().getWorkName() : "");
|
||||
siteMemory.saveMapPushDTO(deviceId, dto);
|
||||
@@ -454,7 +479,6 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
try {
|
||||
HostVehicleRelTimeDTO vehicleRelTimeDTO = JsonUtils.parseObject(payload, HostVehicleRelTimeDTO.class);
|
||||
if (vehicleRelTimeDTO != null) {
|
||||
|
||||
// todo 更新里程数 里程数 * 割幅0.8
|
||||
vehicleRelTimeDTO.getData().getTotal_work_distance();
|
||||
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
|
||||
@@ -473,6 +497,16 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
siteMemory.saveRunStatistics(deviceRunStatistics);
|
||||
|
||||
siteMemory.saveVehicleMessage(deviceId, vehicleRelTimeDTO.getData());
|
||||
|
||||
// 故障处理
|
||||
Long errorCode = vehicleRelTimeDTO.getData().getError_code();
|
||||
if (errorCode != null && errorCode != 0) {
|
||||
List<ErrorIdentificationStandard> standards = parseErrorCode(errorCode, siteMemory);
|
||||
List<AlarmMessage> alMessages = parseAlarm(standards, deviceId, siteId, orgId);
|
||||
siteMemory.replaceAlarms(deviceId, alMessages);
|
||||
} else {
|
||||
siteMemory.replaceAlarms(deviceId, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -513,6 +547,164 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备故障码解析告警
|
||||
*
|
||||
* @param errorCode 设备上报的十进制故障掩码(uint32)
|
||||
* @param standards 故障标准配置集合
|
||||
* @param deviceName 设备名称
|
||||
* @param deviceSn 设备SN
|
||||
* @param siteId 场站ID
|
||||
* @param orgId 组织ID
|
||||
* @return 告警集合
|
||||
*/
|
||||
public static List<AlarmMessage> parseAlarm(
|
||||
List<ErrorIdentificationStandard> standards,
|
||||
String deviceId,
|
||||
Long siteId,
|
||||
Long orgId) {
|
||||
|
||||
List<AlarmMessage> result = new ArrayList<>();
|
||||
|
||||
if (standards == null || standards.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (ErrorIdentificationStandard standard : standards) {
|
||||
|
||||
if (standard == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AlarmMessage alarm = new AlarmMessage();
|
||||
|
||||
alarm.setNo(standard.getErrorCode());
|
||||
alarm.setName(standard.getErrorName());
|
||||
alarm.setLevel(standard.getErrorLevel());
|
||||
alarm.setContent(standard.getErrorDescription());
|
||||
alarm.setSuggestion(standard.getSuggestion());
|
||||
|
||||
alarm.setTime(LocalDateTime.now());
|
||||
alarm.setDeviceName(deviceId);
|
||||
alarm.setDeviceSn(deviceId);
|
||||
alarm.setSiteId(siteId);
|
||||
alarm.setOrgId(orgId);
|
||||
|
||||
result.add(alarm);
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void addStandards(int index, List<ErrorIdentificationStandard> errorList,
|
||||
List<ErrorIdentificationStandard> eStandards) {
|
||||
ErrorIdentificationStandard eStandard = eStandards.stream().filter(x -> x.getCodeIndex() == index).findFirst()
|
||||
.orElse(null);
|
||||
if (eStandard != null) {
|
||||
errorList.add(eStandard);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设备故障码解析故障信息
|
||||
*
|
||||
* @param errorCode 十进制故障码
|
||||
* @return 故障描述集合
|
||||
*/
|
||||
public List<ErrorIdentificationStandard> parseErrorCode(long errorCode, SiteMemory siteMemory) {
|
||||
List<ErrorIdentificationStandard> errorList = new ArrayList<>();
|
||||
|
||||
if (errorCode <= 0 || siteMemory == null) {
|
||||
return errorList;
|
||||
}
|
||||
List<ErrorIdentificationStandard> eStandards = siteMemory.getStandards().get(ErrorSource.MOWER);
|
||||
// bit0:遥控器链路超时
|
||||
if ((errorCode & (1L << 0)) != 0) {
|
||||
addStandards(0, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit2:整车控制器心跳反馈超时
|
||||
if ((errorCode & (1L << 2)) != 0) {
|
||||
addStandards(2, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit3:无效工作模式
|
||||
if ((errorCode & (1L << 3)) != 0) {
|
||||
addStandards(3, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit4:控制器请求慢停或停车
|
||||
if ((errorCode & (1L << 4)) != 0) {
|
||||
addStandards(4, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit5:底盘轮速反馈超时
|
||||
if ((errorCode & (1L << 5)) != 0) {
|
||||
addStandards(5, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit6:上位机控制指令超时
|
||||
if ((errorCode & (1L << 6)) != 0) {
|
||||
addStandards(6, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit7:左侧电机电流超限
|
||||
if ((errorCode & (1L << 7)) != 0) {
|
||||
addStandards(7, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit8:右侧电机电流超限
|
||||
if ((errorCode & (1L << 8)) != 0) {
|
||||
addStandards(8, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit9:母线电流超限
|
||||
if ((errorCode & (1L << 9)) != 0) {
|
||||
addStandards(9, errorList, eStandards);
|
||||
}
|
||||
|
||||
// bit10:俯仰倾角超限
|
||||
if ((errorCode & (1L << 10)) != 0) {
|
||||
addStandards(10, errorList, eStandards);
|
||||
}
|
||||
|
||||
return errorList;
|
||||
}
|
||||
|
||||
public void recordRealtimeMessage(String deviceId, VehicleMessage vehicleMessage) {
|
||||
String key = NettyCacheKey.vehicleMessageKey + deviceId;
|
||||
VehicleMessageRecordDTO recordDto = redisCache.getCacheObject(key);
|
||||
boolean record = false;
|
||||
List<VehicleMessage> histories;
|
||||
if (recordDto == null) {
|
||||
recordDto = new VehicleMessageRecordDTO();
|
||||
histories = new ArrayList<>();
|
||||
record = true;
|
||||
} else {
|
||||
long lastRecordTime = recordDto.getLastRecordTime();
|
||||
histories = recordDto.getHistories();
|
||||
if (System.currentTimeMillis() - lastRecordTime >= recordInterval) {
|
||||
record = true;
|
||||
}
|
||||
}
|
||||
if (record) {
|
||||
if (histories == null) {
|
||||
histories = new ArrayList<>();
|
||||
}
|
||||
vehicleMessage.setCreateTime(LocalDateTime.now());
|
||||
histories.add(vehicleMessage);
|
||||
recordDto.setHistories(histories);
|
||||
recordDto.setLastRecordTime(System.currentTimeMillis());
|
||||
redisCache.setCacheObject(key, recordDto);
|
||||
|
||||
// todo 记录到InfluxDB 是否删除redis中的记录 目前先保留最近一次的记录在redis中
|
||||
// MowerRealTimeData data = processInfluxData(deviceID, split);
|
||||
// String line = InfluxSqlBuilder.buildLine(data);
|
||||
// GlobalMemory.influxDBUtil.write(line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理心跳
|
||||
* topic: mower/{deviceId}/event/heartbeat/post
|
||||
@@ -567,10 +759,15 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
|
||||
// nettyDevice.finishTask();
|
||||
// }
|
||||
// if("running".equals(dto.getStatus())){
|
||||
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
|
||||
// nettyDevice.finishTask();
|
||||
// }
|
||||
if ("running".equals(dto.getStatus())) {
|
||||
// todo 记录累计里程
|
||||
if (nettyDevice != null && nettyDevice.getTask() != null) {
|
||||
DevicePlanTask task = nettyDevice.getTask();
|
||||
task.setTaskDistance(dto.getData().getDistance_traveled_m());
|
||||
task.setTaskTime(dto.getData().getExecution_time_s());
|
||||
}
|
||||
|
||||
}
|
||||
// 任务进度 百分比
|
||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||||
@@ -717,16 +914,18 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
|
||||
log.debug("【障碍物提示】deviceId={}, payload={}", deviceId, payload);
|
||||
|
||||
ObstacleDetectionData data = JsonUtils.parseObject(payload, ObstacleDetectionData.class);
|
||||
ObstaclePostDTO data = JsonUtils.parseObject(payload, ObstaclePostDTO.class);
|
||||
if (data == null || data.getData() == null)
|
||||
return;
|
||||
|
||||
List<NettyDevice> controlMasters = deviceSessionManager
|
||||
.getAllSlaveControl(deviceId);
|
||||
data.setType(MesType.detectionReport);
|
||||
data.getData().setType(MesType.detectionReport);
|
||||
if (!CollectionUtils.isEmpty(controlMasters)) {
|
||||
|
||||
for (NettyDevice x : controlMasters) {
|
||||
try {
|
||||
websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(data));
|
||||
websocketMesDispather.dispather(x.getConnectorId(), JsonUtils.toJsonString(data.getData()));
|
||||
} catch (Exception e) {
|
||||
log.error("实时数据websocket推送失败: connectorId={}, error={}", x.getConnectorId(),
|
||||
e.getMessage(), e);
|
||||
@@ -738,7 +937,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
if (device != null && device.getDevice() != null) {
|
||||
Long siteId = device.getDevice().getSiteId();
|
||||
websocketMesDispather.mapMessageDispather(siteId,
|
||||
JsonUtils.toJsonString(data));
|
||||
JsonUtils.toJsonString(data.getData()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("实时地图数据websocket推送失败: siteId={}, error={}", e);
|
||||
@@ -746,7 +945,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
try {
|
||||
GlobalMemory.customMqttDeviceMonitor.publish(mqttClientId,
|
||||
String.format(MqttTopic.DEVICE_OBSTACLE_PUSH_TOPIC, deviceId),
|
||||
JsonUtils.toJsonString(data));
|
||||
JsonUtils.toJsonString(data.getData()));
|
||||
} catch (MqttException e) {
|
||||
log.error("实时数据websocket推送失败: deviceId={}, error={}", deviceId, e.getMessage(), e);
|
||||
}
|
||||
@@ -757,6 +956,18 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
log.debug("【模式切换回复】deviceId={}, payload={}", deviceId, payload);
|
||||
|
||||
HostModeChangeReplyDTO data = JsonUtils.parseObject(payload, HostModeChangeReplyDTO.class);
|
||||
if (data != null && data.getAccepted() != null) {
|
||||
|
||||
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
|
||||
if (nettyDevice != null && nettyDevice.getDevice() != null) {
|
||||
Long siteId = nettyDevice.getDevice().getSiteId();
|
||||
Long orgId = nettyDevice.getDevice().getOrgId();
|
||||
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
||||
if (siteMemory != null) {
|
||||
siteMemory.saveVehicleSystemMode(deviceId, data.getMode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CompletableFuture<Boolean> future = GlobalMemory.modeFutureMap.get(deviceId);
|
||||
|
||||
@@ -791,6 +1002,31 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
// }
|
||||
}
|
||||
|
||||
private void handleEstopPost(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());
|
||||
// }
|
||||
}
|
||||
|
||||
private void handleEstopReply(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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Topic 中解析 deviceId
|
||||
* Topic 格式:mower/{deviceId}/...
|
||||
@@ -845,12 +1081,14 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
|
||||
return "upgrade_reply";
|
||||
if (remaining.equals("perception/obstacle/post"))
|
||||
return "obstacle_post";
|
||||
if (remaining.equals("control/mode/set/reply"))
|
||||
if (remaining.equals("control/mode/reply"))
|
||||
return "mode_reply";
|
||||
if (remaining.equals("event/fault/post"))
|
||||
return "fault_post";
|
||||
if (remaining.equals("fault/recovery/reply"))
|
||||
return "fault_recovery_reply";
|
||||
if (remaining.equals("control/emergency_stop/reply"))
|
||||
return "eStop_reply";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -127,6 +127,12 @@ public class MqttTopic {
|
||||
// 故障恢复回复
|
||||
public static final String MOWER_FAULT_RECOVERY_REPLY = "mower/+/fault/recovery/reply";
|
||||
|
||||
/** 设置无遥控器时候的急停(平台->设备) */
|
||||
public static final String MOWER_ESTOP_SET = "mower/%s/control/emergency_stop/set";
|
||||
|
||||
/** 设置无遥控器时候的急停回复(设备->平台) */
|
||||
public static final String MOWER_ESTOP_REPLY = "mower/%s/control/emergency_stop/reply";
|
||||
|
||||
/**
|
||||
* 格式化 Topic 模板,将 %s 替换为设备ID
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user