From b04ea364c6a15a25c46d0cbfbcd036e1f2abb8dc Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Mon, 15 Jan 2024 09:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E8=B0=83=E8=AF=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ats/message/FrameSchemaParse.java | 11 ++--- .../ats/message/XianOccMessagingClient.java | 49 +++++++++++++++++-- .../convertor/PlanScheduleConvertor.java | 6 ++- .../datasource/InUsedScheduleData.java | 37 ++++++++++++-- .../line3/device/ScheduleSubIdType.java | 6 +-- .../message/line3/rep/EntityParseUtil.java | 8 ++- .../line3/rep/HistoryScheduleResponse.java | 2 +- .../line3/rep/InusedScheduleResponse.java | 2 +- .../ats/warn/lost/SwitchLostTask.java | 7 ++- 9 files changed, 99 insertions(+), 29 deletions(-) diff --git a/src/main/java/club/joylink/xiannccda/ats/message/FrameSchemaParse.java b/src/main/java/club/joylink/xiannccda/ats/message/FrameSchemaParse.java index 193bba4..2c7c0bc 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/FrameSchemaParse.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/FrameSchemaParse.java @@ -60,9 +60,7 @@ public class FrameSchemaParse { private static void messageDecode(ByteBuf buf, List messages) throws Exception { int len = buf.getUnsignedShort(0); byte[] msgData = new byte[len + 2]; -// ByteBuf msgBuf = buf.readBytes(msgData); - ByteBuf msgBuf = Unpooled.wrappedBuffer(msgData); int msgId = msgBuf.getUnsignedShort(8); @@ -88,7 +86,7 @@ public class FrameSchemaParse { inByteBuf.markReaderIndex(); int totalReadables = inByteBuf.readableBytes(); if (totalReadables < 4) { - inByteBuf.resetReaderIndex(); +// inByteBuf.resetReaderIndex(); log.error("数据不全,当前可读[{}]", totalReadables); return false; } @@ -102,10 +100,9 @@ public class FrameSchemaParse { log.error("可读内容不足 sysId[{}] totalLen[{}] flag[{}] contentLen[{}] readableBytes[{}]", sysId, totalLength, flag, contentLen, readables); return false; } - byte[] bb = new byte[contentLen]; - inByteBuf.readBytes(bb); - packageData.writeBytes(inByteBuf); -// packageData.writeBytes(inByteBuf, contentLen); +// byte[] bb = new byte[contentLen]; +// inByteBuf.readBytes(bb); + packageData.writeBytes(inByteBuf, contentLen); return flag != Flag_Multi; } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java b/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java index 7722f40..f0ec8c4 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java @@ -4,13 +4,19 @@ import club.joylink.xiannccda.ats.message.line3.req.LoadHistoryTGDataRequest; import club.joylink.xiannccda.ats.message.line3.req.LoadHistoryTGDataRequest.ApplyTypeEnum; import java.time.LocalDateTime; import java.util.LinkedList; +import java.util.List; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; +import oshi.hardware.GlobalMemory; @Slf4j public class XianOccMessagingClient { @@ -93,6 +99,7 @@ public class XianOccMessagingClient { * 连接OCC服务 */ public void connect(boolean monitorHandwareChange) { + this.timeOutHandler.monitorHandwareChange = monitorHandwareChange; this.timeOutHandler.start(); } @@ -120,15 +127,18 @@ public class XianOccMessagingClient { static ScheduledExecutorService Executor = Executors.newSingleThreadScheduledExecutor(); private final LinkedList clientConnections = new LinkedList<>(); + private final SystemInfo systemInfo; -// private long[] ticks; + private boolean monitorHandwareChange; + + private long[] ticks; public void shutdown() { Executor.shutdownNow(); } public ConnectionTimeOutHandler() { -// this.systemInfo = new SystemInfo(); + this.systemInfo = new SystemInfo(); if (Executor.isShutdown()) { Executor = Executors.newSingleThreadScheduledExecutor(); } @@ -138,15 +148,46 @@ public class XianOccMessagingClient { this.clientConnections.add(conn); } + private String hardWareUserInfo() { + CentralProcessor cpu = systemInfo.getHardware().getProcessor(); + GlobalMemory memory = systemInfo.getHardware().getMemory(); + if (Objects.isNull(ticks)) { + ticks = cpu.getSystemCpuLoadTicks(); + } else { + double d2 = cpu.getSystemCpuLoadBetweenTicks(ticks); + ticks = cpu.getSystemCpuLoadTicks(); + long totalMemory = memory.getTotal(); + long availableMemory = memory.getAvailable(); + long usedMemory = totalMemory - availableMemory; + return String.format("当前CPU使用:%d%% 内存总内存:%s(MB) 使用:%s(MB) 可用:%s(MB)", Math.round(d2 * 100), (totalMemory / 1024 / 1024), (usedMemory / 1024 / 1024), + (availableMemory / 1024 / 1024)); + } + return null; + +/* GlobalMemory memory = systemInfo.getHardware().getMemory(); + long totalMemory = memory.getTotal(); + long availableMemory = memory.getAvailable(); + long usedMemory = totalMemory - availableMemory; + if (Objects.isNull(this.osProcess)) { + return String.format("内存总内存:%s(MB) 使用:%s(MB) 可用:%s(MB)", (totalMemory / 1024 / 1024), (usedMemory / 1024 / 1024), + (availableMemory / 1024 / 1024)); + } else { + OSProcess osp = this.systemInfo.getOperatingSystem().getProcess(this.processId); + + double d2 = this.osProcess.getProcessCpuLoadBetweenTicks(osp); + return String.format("当前CPU使用:%d%% 内存总内存:%s(MB) 使用:%s(MB) 可用:%s(MB)", Math.round(d2 * 100), (totalMemory / 1024 / 1024), (usedMemory / 1024 / 1024), + (availableMemory / 1024 / 1024)); + }*/ + } public void start() { Executor.scheduleWithFixedDelay(() -> { - /* if (this.monitorHandwareChange) { + if (this.monitorHandwareChange) { String moitorContent = this.hardWareUserInfo(); if (StringUtils.isNotEmpty(moitorContent)) { log.info(moitorContent); } - }*/ + } for (OccTcpClientConnection cc : this.clientConnections) { if (cc.connected) { long ctm = System.currentTimeMillis(); diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/PlanScheduleConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/PlanScheduleConvertor.java index 1e2f629..dd586a4 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/PlanScheduleConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/PlanScheduleConvertor.java @@ -26,9 +26,13 @@ public class PlanScheduleConvertor extends DefaultConvertor { @Override protected void runBefore(List builders) { for (Builder builder : builders) { - if ((builder instanceof Plan.Builder plan) && (plan.getActionId() == ScheduleSubIdType.HISTORY_BEGIN.val())) { + if ((builder instanceof Plan.Builder plan) + && (plan.getActionId() == ScheduleSubIdType.HISTORY_BEGIN.val() +// || plan.getActionId() == ScheduleSubIdType.BEGIN.val() + )) { InUsedScheduleData inUsedScheduleData = DeviceDataRepository.findDataSouce(String.valueOf(plan.getLineId()), DataTypeEnum.TRAIN_PLAN); inUsedScheduleData.clear(); + break; } } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/datasource/InUsedScheduleData.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/datasource/InUsedScheduleData.java index 2b32ebe..dec280f 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/datasource/InUsedScheduleData.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/datasource/InUsedScheduleData.java @@ -2,6 +2,7 @@ package club.joylink.xiannccda.ats.message.collect.datasource; import club.joylink.xiannccda.dto.protos.TrainShedule.Plan; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Lists; import com.google.common.collect.Table; @@ -34,13 +35,39 @@ public class InUsedScheduleData extends AbstractData { @Override public void addDevice(List dataList) { for (Builder builder : dataList) { + if (builder instanceof Plan.Builder planBuilder) { - List bList = trainSchedule.get(planBuilder.getTrainId(), planBuilder.getGlobalId()); - if (Objects.isNull(bList)) { - bList = Lists.newArrayList(); - trainSchedule.put(planBuilder.getTrainId(), planBuilder.getGlobalId(), bList); + if (StringUtils.isNotEmpty(planBuilder.getTrainId()) && StringUtils.isNotEmpty(planBuilder.getGlobalId())) { + List bList = trainSchedule.get(planBuilder.getTrainId(), planBuilder.getGlobalId()); + if (Objects.isNull(bList)) { + bList = Lists.newArrayList(); + trainSchedule.put(planBuilder.getTrainId(), planBuilder.getGlobalId(), bList); + } + if (bList.isEmpty()) { + bList.add(planBuilder); + + } else { + Plan.Builder oldPlan = bList.stream() + .filter(d -> d.getStationId() == planBuilder.getStationId() && d.getUpWay() == planBuilder.getUpWay() && planBuilder.getDTime() - planBuilder.getATime() > 0).findFirst().orElse(null); + if (Objects.isNull(oldPlan)) { + bList.add(planBuilder); + } else { + oldPlan.mergeFrom(planBuilder.build()); + } + /* boolean merged = false; + for (Plan.Builder oldPlan : bList) { + if (oldPlan.getStationId() == planBuilder.getStationId() && oldPlan.getUpWay() == planBuilder.getUpWay()) { + oldPlan.mergeFrom(planBuilder.build()); + merged = true; + break; + } + } + if (!merged) { + bList.add(planBuilder); + }*/ + } } - bList.add(planBuilder); + } } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/device/ScheduleSubIdType.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/device/ScheduleSubIdType.java index 4d49044..9eae476 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/device/ScheduleSubIdType.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/device/ScheduleSubIdType.java @@ -12,9 +12,9 @@ import java.util.Arrays; * 实际数据 发送 0 = 开始,1=传输中,2=结束 */ public enum ScheduleSubIdType { - BEGIN(0X0001), - RUNING(0X0002), - STOP(0X0003), + // BEGIN(0X0001), +// RUNING(0X0002), +// STOP(0X0003), HISTORY_BEGIN(0), HISTORY_RUN(1), HISTORY_STOP(2), diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/EntityParseUtil.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/EntityParseUtil.java index 550af8a..b455d2e 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/EntityParseUtil.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/EntityParseUtil.java @@ -28,11 +28,9 @@ public class EntityParseUtil { public static String convertStr(ByteBuf buf, int len) { - ByteBuf bb = buf.readBytes(len); - String str = new String(bb.array(), MessageCons.STRING_CHARSET).trim(); - bb.clear(); - bb.release(); - return str; + byte[] data = new byte[len]; + buf.readBytes(data); + return new String(data, MessageCons.STRING_CHARSET).trim(); } public interface ReadData { diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/HistoryScheduleResponse.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/HistoryScheduleResponse.java index 47566b8..87de636 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/HistoryScheduleResponse.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/HistoryScheduleResponse.java @@ -63,7 +63,7 @@ public class HistoryScheduleResponse extends MessageResponse { this.date = DateTimeUtil.convert(buf); this.subId = ScheduleSubIdType.of(buf.readShort()); - if (this.subId == ScheduleSubIdType.RUNING) { + if (this.subId == ScheduleSubIdType.HISTORY_RUN) { this.groupId = EntityParseUtil.convertStr(buf, 9); this.recCnt = buf.readShort(); this.entityList = EntityParseUtil.collect(this.recCnt, buf, HistoryScheduleEntity.class); diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/InusedScheduleResponse.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/InusedScheduleResponse.java index caf97f1..cb0d491 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/InusedScheduleResponse.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/rep/InusedScheduleResponse.java @@ -74,7 +74,7 @@ public class InusedScheduleResponse extends MessageResponse { this.lineId = buf.readShort(); this.date = DateTimeUtil.convert(buf); this.subId = ScheduleSubIdType.of(buf.readShort()); - if (this.subId == ScheduleSubIdType.RUNING) { + if (this.subId == ScheduleSubIdType.HISTORY_RUN) { this.trainId = EntityParseUtil.convertStr(buf, 9); this.tripCnt = buf.readShort(); diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/lost/SwitchLostTask.java b/src/main/java/club/joylink/xiannccda/ats/warn/lost/SwitchLostTask.java index 491c199..0e18d33 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/lost/SwitchLostTask.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/lost/SwitchLostTask.java @@ -52,9 +52,12 @@ public class SwitchLostTask implements AlertMonitoringTask { for (String lineId : lineCollSet) { DeviceStatusData deviceStatusData = DeviceDataRepository.findDataSouce(lineId, DataTypeEnum.DEVICE); Map deviceBuildMap = deviceStatusData.getAllDeviceMap().get(Switch.getDescriptor().getName()); - for (Builder deviceBuild : deviceBuildMap.values()) { - this.handle(deviceBuild); + if (CollectionUtils.isNotEmpty(deviceBuildMap)) { + for (Builder deviceBuild : deviceBuildMap.values()) { + this.handle(deviceBuild); + } } + } }