This commit is contained in:
2026-11-19 09:18:17 +08:00
parent 08888ab612
commit 21b3dbaddb
24 changed files with 449 additions and 220 deletions

View File

@@ -30,7 +30,7 @@ public class Constant {
public static final Long hostOfflineInterval = 10000L; // 单位毫秒
// ==================== 远程控制 ====================
public static final String protocol_version = "1.4.6";
public static final String protocol_version = "1.4.7";
public static final String remote_control_msg_prefix = "remote-msg";
public static final String remote_control_cmd_prefix = "remote-cmd";
}

View File

@@ -60,4 +60,12 @@ public class DevicePlanTask extends OrgBaseDO {
@TableField(exist = false)
private double taskTime; // s
@TableField(exist = false)
private double totalTaskDistance; // 记录当前任务的累计里程
@TableField(exist = false)
private double totalTaskTime; // s
@TableField(exist = false)
private double progress;
}

View File

@@ -25,7 +25,7 @@ public class DeviceRunStatistics implements Serializable {
private Double distance; //作业距离
private Double time; //时长 分钟
private Double time; //时长
private String deviceId;

View File

@@ -56,6 +56,8 @@ public class Alarm extends TenantBaseDO {
@Excel(name = "设备名称")
private String deviceName;
private String deviceAlias;
@ApiModelProperty("设备类型:1-逆变器 2-汇流箱 3-组串 4-气象站 5-电表 6-智能汇流箱 7-箱变 8-SVG")
@Excel(name = "设备类型", readConverterExp = "1=逆变器,2=汇流箱,3=组串,4=气象站,5=电表,6=智能汇流箱,7=箱变,8=SVG")
private ErrorSource deviceType;

View File

@@ -31,6 +31,7 @@ import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.LocalDateTime;
@@ -135,8 +136,8 @@ public class NettyDevice extends Connector {
public void updateWorkArea() {
double taskDistance = task.getTaskDistance();
double taskTime = task.getTaskTime();
double taskDistance = task.getTotalTaskDistance();
double taskTime = task.getTotalTaskTime();
double workArea = taskDistance * 0.8;
LambdaQueryWrapper<DeviceRunStatistics> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DeviceRunStatistics::getDeviceId, task.getDeviceId());
@@ -144,15 +145,20 @@ public class NettyDevice extends Connector {
.selectOne(queryWrapper);
if (existing != null) {
existing.setDistance(
existing.getDistance() + taskDistance);
BigDecimal.valueOf(existing.getDistance())
.add(BigDecimal.valueOf(taskDistance))
.doubleValue());
existing.setWorkArea(
existing.getWorkArea() + workArea);
existing.setTime(existing.getTime() + taskTime); // 累加时长,单位为分钟
BigDecimal.valueOf(existing.getWorkArea())
.add(BigDecimal.valueOf(workArea))
.doubleValue());
existing.setTime(existing.getTime() + taskTime); // 累加时长,单位为s
} else {
existing = new DeviceRunStatistics();
existing.setDistance(taskDistance);
existing.setWorkArea(workArea);
existing.setTime(taskTime); // 累加时长,单位为分钟
existing.setTime(taskTime); // 累加时长,单位为s
existing.setDeviceId(task.getDeviceId());
}
runStatisticsMapper.saveOrUpdate(existing);
@@ -190,7 +196,7 @@ public class NettyDevice extends Connector {
task = null;
currentTaskId = null;
currentPoint = null;
locationQueue.clear();
// locationQueue.clear();
executingTask = false;
return finishId;
}
@@ -236,7 +242,7 @@ public class NettyDevice extends Connector {
task = null;
currentTaskId = null;
currentPoint = null;
locationQueue.clear();
// locationQueue.clear();
executingTask = false;
return taskId;
}
@@ -256,7 +262,7 @@ public class NettyDevice extends Connector {
task = null;
currentTaskId = null;
currentPoint = null;
locationQueue.clear();
// locationQueue.clear();
executingTask = false;
return taskId;
}
@@ -333,7 +339,7 @@ public class NettyDevice extends Connector {
// todo 优化
public synchronized boolean pauseTask() throws MqttException {
if (executingTask) {
task.setTaskStaus(DeviceTaskStaus.PAUSE);
// task.setTaskStaus(DeviceTaskStaus.PAUSE);
HostWorkRecordCreateDTO dto = new HostWorkRecordCreateDTO();
dto.setAction("pause");
dto.setCommand_id(task.getId() + "_" + System.currentTimeMillis());
@@ -361,7 +367,7 @@ public class NettyDevice extends Connector {
// todo 优化
public synchronized boolean recoveryTask() throws InterruptedException, MqttException {
if (task.getTaskStaus().equals(DeviceTaskStaus.PAUSE)) {
task.setTaskStaus(DeviceTaskStaus.PAUSE);
// task.setTaskStaus(DeviceTaskStaus.EXECUTING);
HostWorkRecordCreateDTO dto = new HostWorkRecordCreateDTO();
dto.setAction("resume");
dto.setCommand_id(task.getId() + "_" + System.currentTimeMillis());
@@ -410,10 +416,11 @@ public class NettyDevice extends Connector {
if (current >= 10) {
futureHolder[0].cancel(false);
log.warn("任务执行达到最大次数,停止执行");
executingTask = false;
task.setTaskStaus(DeviceTaskStaus.FAILED);
devicePlanTaskMapper.saveOrUpdate(task);
task = null;
// executingTask = false;
// task.setTaskStaus(DeviceTaskStaus.FAILED);
// devicePlanTaskMapper.saveOrUpdate(task);
// task = null;
failedTask();
}
} catch (Exception e) {

View File

@@ -23,4 +23,6 @@ public class AlarmHandleDTO {
@ApiModelProperty("处理结果")
private AlarmHandleResult handleResult;
private String deviceId;
}

View File

@@ -23,4 +23,6 @@ public class DeviceMapPushDTO {
private String ms; // 运动状态 moving|stopped|blocked
private Boolean e; // 急停状态
}

View File

@@ -1,5 +1,8 @@
package com.maibu.core.business.dto;
import java.util.List;
import com.maibu.core.business.alarm_center.Alarm;
import com.maibu.core.enums.MesType;
import lombok.Data;
@@ -7,11 +10,13 @@ import lombok.Data;
@Data
public class WebOlineStatusMessageDTO {
private String deviceId;
private String deviceAlias;
private Integer onlineStatus;
private Integer hasError;
private String errorCode;
private String description;
private MesType type;
private List<Alarm> alarms;
private Long time;
}

View File

@@ -1,7 +1,6 @@
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;

View File

@@ -32,6 +32,7 @@ public class GlobalMemory {
public static ConcurrentHashMap<String, CompletableFuture<Boolean>> modeFutureMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, CompletableFuture<Boolean>> eStopFutureMap = new ConcurrentHashMap<>();
// public static MqttClientUtil mqttClientUtil;
public static CustomMqttDeviceMonitor customMqttDeviceMonitor = null; // 这里面包含了 连我们自己的mqtt 和外部的一些设备,自己的用clientId

View File

@@ -48,41 +48,41 @@ public class SiteMemory {
public SysSite sysSite;
public static final ConcurrentHashMap<String, MqttClient> mqttClientMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, MqttClient> mqttClientMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<Long, OrderModel> orderModelMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<Long, OrderModel> orderModelMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, AlarmOrderConfig> orderConfigMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, AlarmOrderConfig> orderConfigMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, DeviceRunParam> deviceRunParamMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, DeviceRunParam> deviceRunParamMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Device> deviceMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, IoTCommonDevice> commonDeviceMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, IoTCommonDevice> commonDeviceMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Long, Product> productMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Product> productMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Long, DeviceModel> deviceModelMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, DeviceModel> deviceModelMap = new ConcurrentHashMap<>();
// key deviceId
public static ConcurrentHashMap<String, List<DevicePlan>> devicePlanMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, List<DevicePlan>> devicePlanMap = new ConcurrentHashMap<>();
// key deviceId prepare
public static ConcurrentHashMap<String, List<DevicePlanTask>> devicePlanTaskPrepareMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, List<DevicePlanTask>> devicePlanTaskPrepareMap = new ConcurrentHashMap<>();
// key deviceId execute
public static ConcurrentHashMap<String, DevicePlanTask> devicePlanTaskExecuteMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, DevicePlanTask> devicePlanTaskExecuteMap = new ConcurrentHashMap<>();
// 记录 设备上线后的运行数据 下线后清除并保存到数据库
public static ConcurrentHashMap<String, DeviceRunStatistics> deviceRunStatisticsMap = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, DeviceRunStatistics> deviceRunStatisticsMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, DeviceMapPushDTO> deviceMapPushMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, DeviceMapPushDTO> deviceMapPushMap = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, VehicleMessage> vehicleMessageMap = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, VehicleMessage> vehicleMessageMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<RuleType, List<IotAutoRule>> iotAutoRuleMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<RuleType, List<IotAutoRule>> iotAutoRuleMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, RegionConsecutiveTaskRecord> regionConsecutiveTaskMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, RegionConsecutiveTaskRecord> regionConsecutiveTaskMap = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, List<AlarmMessage>> alarmMap = new ConcurrentHashMap<>();
public final ConcurrentHashMap<String, List<AlarmMessage>> alarmMap = new ConcurrentHashMap<>();
public void saveAlarm(String deviceSn, AlarmMessage alarmMessage) {
if (deviceSn == null || alarmMessage == null) {
@@ -206,14 +206,14 @@ public class SiteMemory {
return vehicleMessageMap.get(deviceId);
}
public void saveVehicleSystemMode(String deviceId, Integer systemMode) {
public synchronized void saveVehicleSystemMode(String deviceId, Integer systemMode) {
VehicleMessage vehicleMessage = getVehicleMessage(deviceId);
if (vehicleMessage != null) {
vehicleMessage.setSystemMode(systemMode);
}
}
public void saveVehicleMessage(String deviceId, VehicleMessage vehicleMessage) {
public synchronized void saveVehicleMessage(String deviceId, VehicleMessage vehicleMessage) {
VehicleMessage oldVehicle = getVehicleMessage(deviceId);
if (oldVehicle != null) {
vehicleMessage.setSystemMode(oldVehicle.getSystemMode());

View File

@@ -1,9 +1,12 @@
package com.maibu.mqtt;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import org.eclipse.paho.client.mqttv3.MqttException;
@@ -289,8 +292,10 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
dto.setMs(vehicleMessage.getMotion());
dto.setB(vehicleMessage.getBattery_percentage());
dto.setSm(vehicleMessage.getSystemMode());
dto.setE(vehicleMessage.getEmergency_stop());
}
dto.setWk(nettyDevice.getTask() != null ? nettyDevice.getTask().getWorkName() : "");
dto.setP(nettyDevice.getTask() != null ? nettyDevice.getTask().getProgress() : 0d);
siteMemory.saveMapPushDTO(deviceId, dto);
}
}
@@ -298,7 +303,8 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
return webStatusMessageDTO;
}
public WebStatusMessageDTO createWebDeviceRealtimeMessage(HostVehicleRelTimeDTO vehicleRelTimeDTO) {
public WebStatusMessageDTO createWebDeviceRealtimeMessage(HostVehicleRelTimeDTO vehicleRelTimeDTO,
Integer systemMode) {
// todo 状态消息
WebStatusMessageDTO webStatusMessageDTO = new WebStatusMessageDTO();
@@ -466,6 +472,12 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
angular_speed.setUnit("");
transferData.add(angular_speed);
DeviceStatusDetail system_mode = new DeviceStatusDetail();
system_mode.setName("systemMode");
system_mode.setValue(String.valueOf(systemMode));
system_mode.setUnit("");
transferData.add(system_mode);
webStatusMessageDTO.setData(transferData);
webStatusMessageDTO.setType(MesType.deviceInfo);
return webStatusMessageDTO;
@@ -480,24 +492,36 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
HostVehicleRelTimeDTO vehicleRelTimeDTO = JsonUtils.parseObject(payload, HostVehicleRelTimeDTO.class);
if (vehicleRelTimeDTO != null) {
// todo 更新里程数 里程数 * 割幅0.8
vehicleRelTimeDTO.getData().getTotal_work_distance();
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
Integer systemMode = null;
if (nettyDevice != null) {
Long orgId = nettyDevice.getDevice().getOrgId();
Long siteId = nettyDevice.getDevice().getSiteId();
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
if (siteMemory != null) {
DeviceRunStatistics deviceRunStatistics = siteMemory.getRunStatistics(deviceId);
if (deviceRunStatistics == null) {
deviceRunStatistics = new DeviceRunStatistics();
}
deviceRunStatistics.setDeviceId(deviceId);
deviceRunStatistics.setDistance(vehicleRelTimeDTO.getData().getTotal_work_distance());
deviceRunStatistics.setWorkArea(vehicleRelTimeDTO.getData().getTotal_work_distance() * 0.8);
siteMemory.saveRunStatistics(deviceRunStatistics);
// DeviceRunStatistics deviceRunStatistics =
// siteMemory.getRunStatistics(deviceId);
// if (deviceRunStatistics == null) {
// deviceRunStatistics = new DeviceRunStatistics();
// }
// deviceRunStatistics.setDeviceId(deviceId);
// deviceRunStatistics.setDistance(vehicleRelTimeDTO.getData().getTotal_work_distance());
// deviceRunStatistics.setWorkArea(vehicleRelTimeDTO.getData().getTotal_work_distance()
// * 0.8);
// siteMemory.saveRunStatistics(deviceRunStatistics);
siteMemory.saveVehicleMessage(deviceId, vehicleRelTimeDTO.getData());
systemMode = siteMemory.getVehicleMessage(deviceId).getSystemMode();
Integer oldSystemMode = siteMemory.getVehicleMessage(deviceId).getControl_mode();
Integer controlMode = vehicleRelTimeDTO.getData().getControl_mode();
if (Objects.equals(oldSystemMode, 0) && !Objects.equals(controlMode, 0)) {
// 清除 systemcode;
siteMemory.saveVehicleSystemMode(deviceId, null);
}
// 故障处理
Long errorCode = vehicleRelTimeDTO.getData().getError_code();
if (errorCode != null && errorCode != 0) {
@@ -510,7 +534,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
}
}
WebStatusMessageDTO dto = createWebDeviceRealtimeMessage(vehicleRelTimeDTO);
WebStatusMessageDTO dto = createWebDeviceRealtimeMessage(vehicleRelTimeDTO, systemMode);
List<NettyDevice> controlMasters = deviceSessionManager
.getAllSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(controlMasters)) {
@@ -736,20 +760,34 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
HostTaskStatusDTO dto = JsonUtils.parseObject(payload, HostTaskStatusDTO.class);
if (dto != null && !StringUtils.isEmpty(dto.getStatus())) {
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
if (nettyDevice == null)
return;
double progress = 0d;
if ("completed".equals(dto.getStatus())) {
Long taskId = nettyDevice.finishTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.FINISH.toString());
progress = 100d;
}
if ("cancelled".equals(dto.getStatus())) {
// todo 记录累计里程
if (nettyDevice != null && nettyDevice.getTask() != null) {
DevicePlanTask task = nettyDevice.getTask();
task.setTotalTaskDistance(dto.getData().getTotal_distance_m());
task.setTotalTaskTime(dto.getData().getExecution_time_s());
}
Long taskId = nettyDevice.canceledTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.CANCELED.toString());
}
// if("paused".equals(dto.getStatus())){
// NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
// nettyDevice.finishTask();
// }
if ("paused".equals(dto.getStatus())) {
if (nettyDevice != null && nettyDevice.getTask() != null) {
DevicePlanTask task = nettyDevice.getTask();
if (!DeviceTaskStaus.PAUSE.equals(task.getTaskStaus())) {
task.setTaskStaus(DeviceTaskStaus.PAUSE);
}
}
}
if ("failed".equals(dto.getStatus())) {
Long taskId = nettyDevice.failedTask();
pushTaskChangeMessage(deviceId, taskId, DeviceTaskStaus.FAILED.toString());
@@ -763,20 +801,23 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
// 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());
if (!DeviceTaskStaus.EXECUTING.equals(task.getTaskStaus())) {
task.setTaskStaus(DeviceTaskStaus.EXECUTING);
}
task.setTaskDistance(dto.getData().getDistance_traveled());
task.setTaskTime(dto.getData().getElapsed_s());
}
}
// 任务进度 百分比
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());
}
if (progress != 100d) {
progress = BigDecimal.valueOf(dto.getData().getProgress())
.multiply(BigDecimal.valueOf(100))
.setScale(2, RoundingMode.HALF_UP)
.doubleValue();
}
if (nettyDevice.getTask() != null) {
nettyDevice.getTask().setProgress(progress);
}
}
@@ -956,7 +997,7 @@ public class HostMessageHandler implements CustomMqttMessageHandler {
log.debug("【模式切换回复】deviceId={}, payload={}", deviceId, payload);
HostModeChangeReplyDTO data = JsonUtils.parseObject(payload, HostModeChangeReplyDTO.class);
if (data != null && data.getAccepted() != null) {
if (data != null && data.getAccepted() != null && data.getAccepted()) {
NettyDevice nettyDevice = deviceSessionManager.getDevice(deviceId);
if (nettyDevice != null && nettyDevice.getDevice() != null) {
@@ -1002,25 +1043,12 @@ 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);
CompletableFuture<Boolean> future = GlobalMemory.eStopFutureMap.get(deviceId);
if (future != null && data != null) {
future.complete(data.getAccepted());

View File

@@ -13,7 +13,7 @@ public class MqttTopic {
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
public static final String DEVICE_TASK_ARRIVE_TOPIC = "task/%s/arrive";
/** 通用设备主题,建议使用 MOWER_* 系列主题 */
public static final String DEVICE_ERROR_PUSH_TOPIC = "device/%s/error";
public static final String DEVICE_ERROR_PUSH_TOPIC = "device/%s/error/post";
public static final String DEVICE_ROUTE_PUSH_TOPIC = "device/%s/route/post";
public static final String DEVICE_STATUS_PUSH_TOPIC = "device/%s/status/change";
@@ -131,7 +131,7 @@ public class MqttTopic {
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";
public static final String MOWER_ESTOP_REPLY = "mower/+/control/emergency_stop/reply";
/**
* 格式化 Topic 模板,将 %s 替换为设备ID