更新路径规划任务推送消息

This commit is contained in:
2026-06-11 17:12:55 +08:00
parent 9657fb42f6
commit 3879cd0067
24 changed files with 257 additions and 68 deletions

View File

@@ -89,6 +89,7 @@ public class NettyDevice extends Connector {
} }
public synchronized Long finishTask() { public synchronized Long finishTask() {
sendPathCommand((byte) 0x01, (short) 0, 0.00d, 0.00d, (short) 0);
long finishId = task.getId(); long finishId = task.getId();
task.setTaskStaus(DeviceTaskStaus.FINISH); task.setTaskStaus(DeviceTaskStaus.FINISH);
task.setFinishTime(LocalDateTime.now()); task.setFinishTime(LocalDateTime.now());

View File

@@ -0,0 +1,7 @@
package com.maibu.core.business.inter;
public interface DeviceWebSocketInterace {
void sendWebsoketMes(String key, String data);
}

View File

@@ -0,0 +1,24 @@
package com.maibu.core.business.inter;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class WebsocketMesDispather {
@Resource
private ApplicationContext applicationContext;
public void dispather(String key, String data) {
Map<String, DeviceWebSocketInterace> beanMap = applicationContext.getBeansOfType(DeviceWebSocketInterace.class);
// 遍历
beanMap.values().forEach(x -> {
x.sendWebsoketMes(key, data);
});
}
}

View File

@@ -54,6 +54,13 @@ public class BaseController {
PageUtils.startPage(); PageUtils.startPage();
} }
/**
* 设置请求分页数据
*/
protected void startPostPage(Integer pageNum,Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
}
/** /**
* 设置请求排序数据 * 设置请求排序数据
*/ */

View File

@@ -0,0 +1,17 @@
package com.maibu.core.enums;
public enum MesType {
AUTH, // 登录连接权限web->be
deviceInfo, // 设备状态 be->web
path, // 路径下发 web->be
have_logged_in, // 消息
device_status_changed, // 上下线状态变化
device_error_push, // 错误推送
device_task_change, // 任务状态变化推送
device_task_arrive_point, // 任务点位到达
heartbeat,
remoteControl,
detectionReport, // 障碍物消息
switchPermission, // 切换控制申请
switchResult;// 切换结果
}

View File

@@ -17,5 +17,11 @@ public enum RespondCode {
/** /**
* 任务状态变化推送 * 任务状态变化推送
*/ */
device_task_change device_task_change,
/**
* 路径规划到达点位
*/
device_task_arrive_point
} }

View File

@@ -10,13 +10,11 @@ import com.maibu.utils.sql.SqlUtil;
* *
* @author ruoyi * @author ruoyi
*/ */
public class PageUtils extends PageHelper public class PageUtils extends PageHelper {
{
/** /**
* 设置请求分页数据 * 设置请求分页数据
*/ */
public static void startPage() public static void startPage() {
{
PageDomain pageDomain = TableSupport.buildPageRequest(); PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum(); Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize(); Integer pageSize = pageDomain.getPageSize();
@@ -28,8 +26,7 @@ public class PageUtils extends PageHelper
/** /**
* 清理分页的线程变量 * 清理分页的线程变量
*/ */
public static void clearPage() public static void clearPage() {
{
PageHelper.clearPage(); PageHelper.clearPage();
} }
} }

View File

@@ -1,5 +1,6 @@
package com.maibu.controller; package com.maibu.controller;
import com.github.pagehelper.PageHelper;
import com.maibu.core.business.DevicePlan; import com.maibu.core.business.DevicePlan;
import com.maibu.core.controller.BaseController; import com.maibu.core.controller.BaseController;
import com.maibu.core.domain.AjaxResult; import com.maibu.core.domain.AjaxResult;
@@ -87,13 +88,13 @@ public class DeviceTaskController extends BaseController {
@PostMapping("/deviceTaskPool") @PostMapping("/deviceTaskPool")
public TableDataInfo deviceTaskPool(@RequestBody DeviceTaskQueryDTO dto) { public TableDataInfo deviceTaskPool(@RequestBody DeviceTaskQueryDTO dto) {
startPage(); startPostPage(dto.getPageNum(),dto.getPageSize());
return getDataTable(deviceTaskService.deviceTaskPool(dto)); return getDataTable(deviceTaskService.deviceTaskPool(dto));
} }
@PostMapping("/getSiteDeviceTaskHistory") @PostMapping("/getSiteDeviceTaskHistory")
public TableDataInfo getSiteDeviceTaskHistory(@RequestBody DeviceTaskQueryDTO dto) { public TableDataInfo getSiteDeviceTaskHistory(@RequestBody DeviceTaskQueryDTO dto) {
startPage(); startPostPage(dto.getPageNum(),dto.getPageSize());
return getDataTable(deviceTaskService.getSiteDeviceTaskHistory(dto)); return getDataTable(deviceTaskService.getSiteDeviceTaskHistory(dto));
} }

View File

@@ -1,12 +1,14 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.domain.BaseEntity;
import com.maibu.core.enums.DeviceTaskStaus; import com.maibu.core.enums.DeviceTaskStaus;
import lombok.Data;
import java.time.LocalDateTime; import lombok.Data;
import lombok.EqualsAndHashCode;
@Data @Data
public class DeviceTaskQueryDTO { @EqualsAndHashCode(callSuper = true)
public class DeviceTaskQueryDTO extends BaseEntity {
private Long userId; private Long userId;
// private LocalDateTime startTime; // private LocalDateTime startTime;

View File

@@ -1,5 +1,6 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.enums.DeviceTaskStaus; import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.core.enums.RespondCode; import com.maibu.core.enums.RespondCode;
import lombok.Data; import lombok.Data;
@@ -15,4 +16,6 @@ public class DeviceTaskReportDTO {
private DeviceTaskStaus status; private DeviceTaskStaus status;
private LatAndLngEntity entity;
} }

View File

@@ -0,0 +1,20 @@
package com.maibu.dto;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.enums.MesType;
import lombok.Data;
@Data
public class DeviceTaskStatusMessageDTO {
private MesType type;
private String deviceId;
private Long taskId;
private String status;
private LatAndLngEntity entity;
}

View File

@@ -1,14 +1,46 @@
package com.maibu.netty.handler; package com.maibu.netty.handler;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.maibu.common.CommandConstant; import com.maibu.common.CommandConstant;
import com.maibu.common.Constant; import com.maibu.common.Constant;
import com.maibu.core.business.Device; import com.maibu.core.business.Device;
import com.maibu.core.business.device.NettyDevice; import com.maibu.core.business.device.NettyDevice;
import com.maibu.core.business.inter.DeviceWebSocketInterace;
import com.maibu.core.business.inter.WebsocketMesDispather;
import com.maibu.core.business.path.LatAndLngEntity; import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.domain.entity.SysUser; import com.maibu.core.domain.entity.SysUser;
import com.maibu.core.domain.entity.UserClient; import com.maibu.core.domain.entity.UserClient;
import com.maibu.core.enums.*; import com.maibu.core.enums.CommandRequestType;
import com.maibu.dto.*; import com.maibu.core.enums.ConnectionCloseReason;
import com.maibu.core.enums.ConnectorStatus;
import com.maibu.core.enums.ConnectorType;
import com.maibu.core.enums.DeviceStatus;
import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.core.enums.MesType;
import com.maibu.core.enums.RespondCode;
import com.maibu.dto.DeviceLoginResponseDTO;
import com.maibu.dto.DeviceRequestDTO;
import com.maibu.dto.DeviceRespondDTO;
import com.maibu.dto.DeviceStatusChangeDTO;
import com.maibu.dto.DeviceTaskReportDTO;
import com.maibu.dto.DeviceTaskStatusMessageDTO;
import com.maibu.manager.DeviceSessionManager; import com.maibu.manager.DeviceSessionManager;
import com.maibu.mapper.DeviceRunStatisticsMapper; import com.maibu.mapper.DeviceRunStatisticsMapper;
import com.maibu.service.IDeviceService; import com.maibu.service.IDeviceService;
@@ -16,28 +48,14 @@ import com.maibu.service.ISysUserClientService;
import com.maibu.service.ISysUserService; import com.maibu.service.ISysUserService;
import com.maibu.utils.CommandUtils; import com.maibu.utils.CommandUtils;
import com.maibu.utils.json.JsonUtils; import com.maibu.utils.json.JsonUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
@Component @Component
@ChannelHandler.Sharable @ChannelHandler.Sharable
@@ -63,6 +81,9 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
@Autowired @Autowired
private DeviceRunStatisticsMapper deviceRunStatisticsMapper; private DeviceRunStatisticsMapper deviceRunStatisticsMapper;
@Resource
private WebsocketMesDispather websocketMesDispather;
public DeviceConnectHandler(DeviceSessionManager sessionManager) { public DeviceConnectHandler(DeviceSessionManager sessionManager) {
this.sessionManager = sessionManager; this.sessionManager = sessionManager;
} }
@@ -317,29 +338,13 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
String deviceId = channel.attr(Constant.ATT_DEVICE_ID).get(); String deviceId = channel.attr(Constant.ATT_DEVICE_ID).get();
NettyDevice device = sessionManager.getDevice(deviceId); NettyDevice device = sessionManager.getDevice(deviceId);
if (CollectionUtils.isEmpty(device.getLocationQueue())) { if (CollectionUtils.isEmpty(device.getLocationQueue())) {
Long taskId = device.finishTask(); // 推送到点
// todo 推送信息给前端 pushArriveData(device, deviceId);
DeviceTaskReportDTO changeDTO = new DeviceTaskReportDTO(); // 推送完成+执行结束更新内存
changeDTO.setDeviceId(deviceId); pushFinishData(device, deviceId);
changeDTO.setStatus(DeviceTaskStaus.FINISH);
changeDTO.setEvent(RespondCode.device_task_change);
changeDTO.setTaskId(taskId);
// ByteBuf buf = ctx.alloc().buffer();
ByteBuf buf = Unpooled.buffer();
CommandUtils.buildCommand(buf, changeDTO, CommandConstant.interaction);
List<NettyDevice> controlMasters = sessionManager
.getAllSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {
if (x.getChannel() != null && x.getChannel().isActive()) {
x.getChannel().writeAndFlush(buf);
logger.info("任务完成状态推送 task:{},device:{}", taskId, deviceId);
}
});
}
} else { } else {
pushArriveData(device, deviceId);
device.sendNextPoint(); device.sendNextPoint();
//todo 推送到点给前端
} }
} }
} }
@@ -350,6 +355,65 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
} }
} }
public void pushFinishData(NettyDevice device, String deviceId) {
Long taskId = device.finishTask();
// todo 推送信息给前端
DeviceTaskStatusMessageDTO changeDTO = new DeviceTaskStatusMessageDTO();
changeDTO.setDeviceId(deviceId);
changeDTO.setStatus(DeviceTaskStaus.FINISH.toString());
changeDTO.setType(MesType.device_task_change);
changeDTO.setTaskId(taskId);
// ByteBuf buf = ctx.alloc().buffer();
// ByteBuf buf = Unpooled.buffer();
// CommandUtils.buildCommand(buf, changeDTO, CommandConstant.interaction);
List<NettyDevice> controlMasters = sessionManager
.getAllSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {
websocketMesDispather.dispather(x.getConnectorId(),
JsonUtils.toJsonString(changeDTO));
// String content = buf.toString(CharsetUtil.UTF_8);
logger.info("任务完成状态推送 task:{},device:{},content:{}", taskId, deviceId,
JsonUtils.toJsonString(changeDTO));
// if (x.getChannel() != null && x.getChannel().isActive()) {
// x.getChannel().writeAndFlush(buf);
// String content = buf.toString(CharsetUtil.UTF_8);
// logger.info("任务完成状态推送 task:{},device:{},content:{}", taskId, deviceId,
// content);
// }
});
}
}
public void pushArriveData(NettyDevice device, String deviceId) {
Long taskId = device.getTask() == null ? null : device.getTask().getId();
// todo 推送信息给前端
DeviceTaskStatusMessageDTO reportDTO = new DeviceTaskStatusMessageDTO();
reportDTO.setDeviceId(deviceId);
reportDTO.setTaskId(taskId);
reportDTO.setEntity(device.getCurrentPoint());
reportDTO.setType(MesType.device_task_arrive_point);
// ByteBuf buf = ctx.alloc().buffer();
// ByteBuf buf = Unpooled.buffer();
// CommandUtils.buildCommand(buf, reportDTO, CommandConstant.interaction);
List<NettyDevice> controlMasters = sessionManager
.getAllSlaveControl(deviceId);
if (!CollectionUtils.isEmpty(controlMasters)) {
controlMasters.forEach(x -> {
websocketMesDispather.dispather(x.getConnectorId(),
JsonUtils.toJsonString(reportDTO));
logger.info("任务点位到达推送 task:{},device:{},content:{}", taskId, deviceId,
JsonUtils.toJsonString(reportDTO));
// if (x.getChannel() != null && x.getChannel().isActive()) {
// x.getChannel().writeAndFlush(buf);
// String content = buf.toString(CharsetUtil.UTF_8);
// logger.info("任务点位到达推送 task:{},device:{},content:{}", taskId, deviceId,
// content);
// }
});
}
}
/** /**
* 处理反馈 待扩展 * 处理反馈 待扩展
*/ */

View File

@@ -1,16 +0,0 @@
package com.maibu.dto;
public enum MesType {
AUTH, //登录连接权限web->be
deviceInfo, //设备状态 be->web
path,//路径下发 web->be
have_logged_in,//消息
device_status_changed,//上下线状态变化
device_error_push, //错误推送
device_task_change, //任务状态变化推送
heartbeat,
remoteControl,
detectionReport,//障碍物消息
switchPermission,//切换控制申请
switchResult;//切换结果
}

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -1,15 +1,20 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data
public class WebDeviceTaskStatusMessageDTO { public class WebDeviceTaskStatusMessageDTO {
private MesType event; private MesType type;
private String deviceId; private String deviceId;
private Long taskId; private Long taskId;
private String status; private String status;
private LatAndLngEntity entity;
} }

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -4,6 +4,8 @@ import lombok.Data;
import java.util.List; import java.util.List;
import com.maibu.core.enums.MesType;
@Data @Data
public class WebStatusMessageDTO { public class WebStatusMessageDTO {
private List<DeviceStatusDetail> data; private List<DeviceStatusDetail> data;

View File

@@ -1,5 +1,7 @@
package com.maibu.dto; package com.maibu.dto;
import com.maibu.core.enums.MesType;
import lombok.Data; import lombok.Data;
@Data @Data

View File

@@ -3,6 +3,8 @@ package com.maibu.netty.handler;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.maibu.common.MiddleCommandConstant; import com.maibu.common.MiddleCommandConstant;
import com.maibu.common.MiddleConstant; import com.maibu.common.MiddleConstant;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.enums.MesType;
import com.maibu.core.enums.RespondCode; import com.maibu.core.enums.RespondCode;
import com.maibu.dto.*; import com.maibu.dto.*;
import com.maibu.memory.MiddleGlobalMemory; import com.maibu.memory.MiddleGlobalMemory;
@@ -107,10 +109,20 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
String status = obj.get("status").toString(); String status = obj.get("status").toString();
WebDeviceTaskStatusMessageDTO dto = new WebDeviceTaskStatusMessageDTO(); WebDeviceTaskStatusMessageDTO dto = new WebDeviceTaskStatusMessageDTO();
dto.setDeviceId(deviceId); dto.setDeviceId(deviceId);
dto.setEvent(MesType.device_task_change); dto.setType(MesType.device_task_change);
dto.setTaskId(Long.valueOf(taskId)); dto.setTaskId(Long.valueOf(taskId));
dto.setStatus(status); dto.setStatus(status);
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto)); WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
}else if ("device_task_arrive_point".equals(eventType)) {
String taskId = obj.get("taskId").toString();
LatAndLngEntity position = JsonUtils.parseObject(obj.get("entity").toString(),
LatAndLngEntity.class);
WebDeviceTaskStatusMessageDTO dto = new WebDeviceTaskStatusMessageDTO();
dto.setDeviceId(deviceId);
dto.setType(MesType.device_task_arrive_point);
dto.setTaskId(Long.valueOf(taskId));
dto.setEntity(position);
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
} }
} else { } else {
/* 判断 respond 字段 */ /* 判断 respond 字段 */

View File

@@ -3,6 +3,7 @@ package com.maibu.websocket;
import com.maibu.common.CommandConstant; import com.maibu.common.CommandConstant;
import com.maibu.common.MiddleConstant; import com.maibu.common.MiddleConstant;
import com.maibu.core.enums.CommandRequestType; import com.maibu.core.enums.CommandRequestType;
import com.maibu.core.enums.MesType;
import com.maibu.dto.*; import com.maibu.dto.*;
import com.maibu.memory.MiddleGlobalMemory; import com.maibu.memory.MiddleGlobalMemory;
import com.maibu.netty.NettyClient; import com.maibu.netty.NettyClient;

View File

@@ -0,0 +1,24 @@
package com.maibu.websocket;
import com.maibu.core.business.inter.DeviceWebSocketInterace;
import com.maibu.memory.MiddleGlobalMemory;
import org.java_websocket.WebSocket;
import org.springframework.stereotype.Component;
@Component
public class WebsocketMesHandler implements DeviceWebSocketInterace {
@Override
public void sendWebsoketMes(String key, String data) {
MiddleGlobalMemory.onlineSockets.keySet().forEach(x -> {
// key: test:web:token
if (x.startsWith(key)) {
WebSocket webSocket = MiddleGlobalMemory.onlineSockets.get(x);
WebsocketHandler.sendMessageToClient(webSocket, data);
}
});
}
}