This commit is contained in:
2026-06-10 16:31:39 +08:00
parent 1ece1b2336
commit 9657fb42f6
11 changed files with 463 additions and 194 deletions

View File

@@ -1,12 +1,17 @@
package com.maibu.core.business.device;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.Queue;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.maibu.core.business.DevicePlanTask;
import com.maibu.core.business.DeviceRunStatistics;
import com.maibu.core.business.PlanPath;
import com.maibu.core.business.WorkRecord;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.business.path.RoutePlanSendEntity;
import com.maibu.core.enums.ConnectorType;
import com.maibu.core.enums.DeviceTaskStaus;
@@ -15,35 +20,28 @@ import com.maibu.mapper.WorkRecordMapper;
import com.maibu.memory.GlobalMemory;
import com.maibu.memory.SiteMemory;
import com.maibu.utils.spring.SpringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.maibu.core.business.path.LatAndLngEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.Queue;
@Slf4j
@EqualsAndHashCode(callSuper = true)
@Data
public class NettyDevice extends Connector {
private ConnectorType deviceType; // 设备角色类型:MASTER、SLAVE、GENERAL
private ConnectorType deviceType; // 设备角色类型:MASTER、SLAVE、GENERAL
private String currentSlaveId;// 当前对应的下位机 master
private Long currentTaskId; //当前执行的任务;
private Long currentTaskId; // 当前执行的任务;
private DevicePlanTask task; //当前执行的任务;
private DevicePlanTask task; // 当前执行的任务;
private Integer onlineStatus; //在线状态 0 离线 1在线
private Integer onlineStatus; // 在线状态 0 离线 1在线
private LatAndLngEntity currentPoint; //执行任务的当前点
private LatAndLngEntity currentPoint; // 执行任务的当前点
private Queue<LatAndLngEntity> locationQueue = new LinkedList<>();
@@ -69,41 +67,15 @@ public class NettyDevice extends Connector {
this.deviceType = deviceType;
}
// abaa 00 18 06 18 06 00 00 00 00 00 00 aaab
//
// ab aa 01 01 00 3c 88
// 2d e0 48 05 40 40
// 22 07 d0 42 c6 33
// 5e 40 e8 03 00 00 aa ab
private static final byte[] PACKET = {(byte) 0xAB, (byte) 0xAA,
(byte) 0x00, (byte) 0x18,(byte) 0x06,(byte) 0x18,(byte) 0x06,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0xAA, (byte) 0xAB};
private static final byte[] route = {(byte) 0xab, (byte) 0xaa,
(byte) 0x01, (byte) 0x01,(byte) 0x00,(byte) 0x3c,(byte) 0x88,
(byte) 0x2d, (byte) 0xe0,(byte) 0x48,(byte) 0x05,(byte) 0x40,(byte) 0x40,
(byte) 0x22, (byte) 0x07,(byte) 0xd0,(byte) 0x42,(byte) 0xc6,(byte) 0x33,
(byte) 0x5e,(byte) 0x40,(byte) 0xe8,(byte) 0x03,(byte) 0x00,(byte) 0x00,
(byte) 0xaa, (byte) 0xab};
//todo 重发
// todo 重发
public void sendNextPoint() {
if (task.getTaskStaus().equals(DeviceTaskStaus.PAUSE)) return;
if (task.getTaskStaus().equals(DeviceTaskStaus.PAUSE))
return;
if (!locationQueue.isEmpty()) {
LatAndLngEntity entity = locationQueue.poll();
if (entity != null) {
RoutePlanSendEntity routePlanSendEntity = new RoutePlanSendEntity();
routePlanSendEntity.setCommandType((byte) 0x01);
routePlanSendEntity.setPointCounts((short) 1);
routePlanSendEntity.setTargetLatitude(entity.getLat());
routePlanSendEntity.setTargetLongitude(entity.getLng());
routePlanSendEntity.setSpeed((short) 1000);
byte[] bytes = routePlanSendEntity.toBytes();
log.debug("下发路径的报文:{}", toHexString(bytes));
sendPathCommand((byte) 0x01, (short) 1, entity.getLat(), entity.getLng(), (short) 1000);
currentPoint = entity;
if (this.getChannel() != null && this.getChannel().isActive()) {
this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes));
}
}
}
}
@@ -116,21 +88,22 @@ public class NettyDevice extends Connector {
return sb.toString().trim();
}
public synchronized void finishTask() {
if (executingTask) {
task.setTaskStaus(DeviceTaskStaus.FINISH);
task.setFinishTime(LocalDateTime.now());
devicePlanTaskMapper.saveOrUpdate(task);
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
siteMemory.removeDevicePlanExecute(task.getDeviceId());
task = null;
currentTaskId = null;
executingTask = false;
}
public synchronized Long finishTask() {
long finishId = task.getId();
task.setTaskStaus(DeviceTaskStaus.FINISH);
task.setFinishTime(LocalDateTime.now());
devicePlanTaskMapper.saveOrUpdate(task);
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
siteMemory.removeDevicePlanExecute(task.getDeviceId());
task = null;
currentTaskId = null;
executingTask = false;
return finishId;
}
public synchronized boolean cancelTask() {
if (executingTask) {
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);
@@ -141,17 +114,30 @@ public class NettyDevice extends Connector {
currentTaskId = null;
locationQueue.clear();
executingTask = false;
return true;
} else {
return false;
}
return true;
}
public void sendPathCommand(byte coommadnType, short pointCounts, double lat, double lng, short speed) {
RoutePlanSendEntity routePlanSendEntity = new RoutePlanSendEntity();
routePlanSendEntity.setCommandType(coommadnType);
routePlanSendEntity.setPointCounts(pointCounts);
routePlanSendEntity.setTargetLatitude(lat);
routePlanSendEntity.setTargetLongitude(lng);
routePlanSendEntity.setSpeed(speed);
byte[] bytes = routePlanSendEntity.toBytes();
log.debug("下发路径的报文:{}", toHexString(bytes));
if (this.getChannel() != null && this.getChannel().isActive()) {
this.getChannel().writeAndFlush(Unpooled.wrappedBuffer(bytes));
}
}
//todo 优化
// todo 优化
public synchronized boolean pauseTask() {
if (executingTask) {
task.setTaskStaus(DeviceTaskStaus.PAUSE);
executingTask = false;
sendPathCommand((byte) 0x01, (short) 2, 0.00d, 0.00d, (short) 0);
// executingTask = false;
return true;
} else {
return false;
@@ -159,22 +145,25 @@ public class NettyDevice extends Connector {
}
//todo 优化
public synchronized boolean recoveryTask() {
if (!executingTask) {
// todo 优化
public synchronized boolean recoveryTask() throws InterruptedException {
if (task.getTaskStaus().equals(DeviceTaskStaus.PAUSE)) {
task.setTaskStaus(DeviceTaskStaus.EXECUTING);
executingTask = true;
sendNextPoint();
sendPathCommand((byte) 0x01, (short) 3, 0.00d, 0.00d, (short) 0);
Thread.sleep(1000);
sendPathCommand((byte) 0x01, (short) 1, currentPoint.getLat(), currentPoint.getLng(), (short) 0);
// sendNextPoint();
return true;
} else {
return false;
}
}
public void startTask() {
try {
if (executingTask) return;
if (executingTask)
return;
if (task != null) {
Long routeId = task.getRouteId();
WorkRecord workRecord = workRecordMapper.selectById(routeId);
@@ -196,4 +185,3 @@ public class NettyDevice extends Connector {
}
}

View File

@@ -13,5 +13,9 @@ public enum RespondCode {
/**
* 设备错误推送
*/
device_error_push
device_error_push,
/**
* 任务状态变化推送
*/
device_task_change
}