From e8ba67dae76d0d77e78f7eaa10696f79ef0bcb56 Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Thu, 29 Jun 2023 13:18:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E6=95=B0=E6=8D=AE=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../ats/message/OccMessageManage.java | 4 +- .../ats/message/OccTcpClientConnection.java | 3 +- .../ats/message/XianOccMessagingClient.java | 9 +- .../ats/message/line3/DateTimeUtil.java | 25 +++ .../message/line3/req/ReportAskRequest.java | 184 ++++++++++-------- .../SpringSecurityConfiguration.java | 2 +- .../mock/message/MockDeviceController.java | 66 ++++++- src/main/resources/application.yml | 4 +- 9 files changed, 195 insertions(+), 104 deletions(-) diff --git a/pom.xml b/pom.xml index d0663cd..4089971 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ org.springdoc springdoc-openapi-starter-webmvc-ui - 2.1.0 + 2.0.2 diff --git a/src/main/java/club/joylink/xiannccda/ats/message/OccMessageManage.java b/src/main/java/club/joylink/xiannccda/ats/message/OccMessageManage.java index f64acc8..b3a806d 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/OccMessageManage.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/OccMessageManage.java @@ -15,12 +15,12 @@ public class OccMessageManage implements ApplicationRunner { Map clientMap = new HashMap<>(); - public void sendMsg(Integer lineId, MessageData md) { + public void sendMsg(Integer lineId, MessageData md, boolean isRealTime) { XianOccMessagingClient client = this.clientMap.get(lineId); if (Objects.isNull(client)) { throw new RuntimeException("未获取到对应的occ客户端"); } - client.send(md); + client.send(md, isRealTime); } public void registerClient(XianOccMessagingClient client) { diff --git a/src/main/java/club/joylink/xiannccda/ats/message/OccTcpClientConnection.java b/src/main/java/club/joylink/xiannccda/ats/message/OccTcpClientConnection.java index e969022..525f3eb 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/OccTcpClientConnection.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/OccTcpClientConnection.java @@ -7,6 +7,7 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; +import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -39,7 +40,7 @@ public class OccTcpClientConnection { volatile long lastReceiveMessageTime; public void write(MessageData messageData) { - this.channel.write(messageData); + this.channel.writeAndFlush(List.of(messageData)); } 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 f3ed866..0fb0c67 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/XianOccMessagingClient.java @@ -22,14 +22,17 @@ public class XianOccMessagingClient { */ private final OccTcpClientConnection nrtConnection; - public void send(MessageData md) { - if (rtConnection.connected) { - this.rtConnection.write(md); + public void send(MessageData md, boolean isRealTime) { + OccTcpClientConnection conn = isRealTime ? rtConnection : nrtConnection; + + if (conn.connected) { + conn.write(md); } else { throw new RuntimeException("未连接occ"); } } + public XianOccMessagingClient(int lineId, String host) { this.host = host; this.lineId = lineId; diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/DateTimeUtil.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/DateTimeUtil.java index 79a2cdb..80f2d74 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/DateTimeUtil.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/DateTimeUtil.java @@ -4,6 +4,8 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import org.apache.commons.lang3.StringUtils; public class DateTimeUtil { @@ -27,6 +29,29 @@ public class DateTimeUtil { buf.writeByte(from.getSecond()); } + public static LocalDateTime parse(String dateTimeStr, DateTimeFormatter formatter) { + if (StringUtils.isEmpty(dateTimeStr)) { + return null; + } + try { + + return LocalDateTime.parse(dateTimeStr, formatter); + } catch (Exception e) { + throw new RuntimeException("解析时间错误", e); + } + } + + public static byte[] convert(String dateTimeStr, DateTimeFormatter formatter) { + + try { + LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter); + return convert(dateTime); + + } catch (Exception e) { + throw new RuntimeException("解析时间错误", e); + } + } + /** * 例如,传送2001年9月21日15时29分30秒,则年、月、日、时、分、秒各单元的值分别为:20、01、9、21、15、29、30 */ diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/req/ReportAskRequest.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/req/ReportAskRequest.java index 473bc4b..8a67701 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/req/ReportAskRequest.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/req/ReportAskRequest.java @@ -7,99 +7,113 @@ import io.netty.buffer.ByteBuf; import java.time.LocalDateTime; import java.util.Arrays; +import java.util.Objects; +import org.apache.commons.lang3.ArrayUtils; +import org.springframework.util.CollectionUtils; /** * 2.8.1 统计信息查询消息 */ public class ReportAskRequest extends MessageRequest { - /** - * 线路号(2) - */ - private Short lineId; - /** - * 查询标识号(2)
- * 同一时间多个查询的report_id不允许重复,答复消息中的report_id值跟查询消息中的相同 - */ - private Short reportId; - /** - * 消息种类(2)
- * 1:车组运行里程报告
- * 2:司机驾驶里程报告
- * 3:调度日志报告
- * 4:存备车报告
- * 5:列车整备状态报告
- */ - private Short msgFlag; - /** - * 消息内容(7)
- * 当消息种类为2或3时,消息内容为调度日期格式 - */ - private byte[] msgContent; - /** - * 预留(7) - */ - private byte[] reserve; - public ReportAskRequest(final short lineId,final short reportId,final MsgFlagEnum msgFlag){ - super(MessageId.REPORT_ASK,2+2+2+7+7); - this.lineId=lineId; - this.reportId=reportId; - this.msgFlag=msgFlag.flag; - this.reserve=new byte[7]; - Arrays.fill(this.reserve, (byte) 0); - this.msgContent=new byte[7]; - Arrays.fill(this.msgContent, (byte) 0); - } - /** - * 填充消息内容(7)
- * 当消息种类为2或3时,消息内容为调度日期格式
- * 例如,传送2001年9月21日15时29分30秒,则年、月、日、时、分、秒各单元的值分别为:20、01、9、21、15、29、30 - */ - public ReportAskRequest fillMsgContentOfDate(final LocalDateTime msgContent){ - DateTimeUtil.convert(msgContent,this.msgContent); - return this; - } - @Override - public void encode2(ByteBuf buf) { - buf.writeShort(this.lineId); - buf.writeShort(this.reportId); - buf.writeShort(this.msgFlag); - buf.writeBytes(this.msgContent); - buf.writeBytes(this.reserve); + /** + * 线路号(2) + */ + private Short lineId; + /** + * 查询标识号(2)
同一时间多个查询的report_id不允许重复,答复消息中的report_id值跟查询消息中的相同 + */ + private Short reportId; + /** + * 消息种类(2)
1:车组运行里程报告
2:司机驾驶里程报告
3:调度日志报告
4:存备车报告
5:列车整备状态报告
+ */ + private Short msgFlag; + /** + * 消息内容(7)
当消息种类为2或3时,消息内容为调度日期格式 + */ + private byte[] msgContent; + /** + * 预留(7) + */ + private byte[] reserve; + + public ReportAskRequest(final short lineId, final short reportId, final MsgFlagEnum msgFlag, LocalDateTime localDateTime) { + super(MessageId.REPORT_ASK, 2 + 2 + 2 + 7 + 7); + this.lineId = lineId; + this.reportId = reportId; + this.msgFlag = msgFlag.flag; + this.reserve = new byte[7]; + Arrays.fill(this.reserve, (byte) 0); + if ((this.msgFlag == MsgFlagEnum.DRIVER_DISTANCE_REPORT.flag || this.msgFlag == MsgFlagEnum.DISPATCHER_REPORT.flag)) { + if (Objects.isNull(localDateTime)) { + throw new RuntimeException("查询 司机驾驶里程报告 或 调度日志报告 时间不能为空"); + } else { + this.msgContent = DateTimeUtil.convert(localDateTime); + } + } else { + this.msgContent = new byte[7]; + Arrays.fill(this.msgContent, (byte) 0); } + } + + + public ReportAskRequest(final short lineId, final short reportId, final MsgFlagEnum msgFlag) { + super(MessageId.REPORT_ASK, 2 + 2 + 2 + 7 + 7); + this.lineId = lineId; + this.reportId = reportId; + this.msgFlag = msgFlag.flag; + this.reserve = new byte[7]; + Arrays.fill(this.reserve, (byte) 0); + this.msgContent = new byte[7]; + Arrays.fill(this.msgContent, (byte) 0); + } + + /** + * 填充消息内容(7)
当消息种类为2或3时,消息内容为调度日期格式
例如,传送2001年9月21日15时29分30秒,则年、月、日、时、分、秒各单元的值分别为:20、01、9、21、15、29、30 + */ + public ReportAskRequest fillMsgContentOfDate(final LocalDateTime msgContent) { + DateTimeUtil.convert(msgContent, this.msgContent); + return this; + } + + @Override + public void encode2(ByteBuf buf) { + buf.writeShort(this.lineId); + buf.writeShort(this.reportId); + buf.writeShort(this.msgFlag); + buf.writeBytes(this.msgContent); + buf.writeBytes(this.reserve); + } + + /** + * 消息种类(2)
1:车组运行里程报告
2:司机驾驶里程报告
3:调度日志报告
4:存备车报告
5:列车整备状态报告
+ */ + public static enum MsgFlagEnum { /** - * 消息种类(2)
- * 1:车组运行里程报告
- * 2:司机驾驶里程报告
- * 3:调度日志报告
- * 4:存备车报告
- * 5:列车整备状态报告
+ * 1:车组运行里程报告 */ - public static enum MsgFlagEnum{ - /** - * 1:车组运行里程报告 - */ - GROUP_RUNNING_REPORT(1), - /** - * 2:司机驾驶里程报告 - */ - DRIVER_DISTANCE_REPORT(2), - /** - * 3:调度日志报告 - */ - DISPATCHER_REPORT(3), - /** - * 4:存备车报告 - */ - GROUP_BAK_REPORT(4), - /** - * 5:列车整备状态报告 - */ - GROUP_STATUS_REPORT(5), - ; - private short flag; - private MsgFlagEnum(int flag){ - this.flag = (short) flag; - } + GROUP_RUNNING_REPORT(1), + /** + * 2:司机驾驶里程报告 + */ + DRIVER_DISTANCE_REPORT(2), + /** + * 3:调度日志报告 + */ + DISPATCHER_REPORT(3), + /** + * 4:存备车报告 + */ + GROUP_BAK_REPORT(4), + /** + * 5:列车整备状态报告 + */ + GROUP_STATUS_REPORT(5), + ; + private short flag; + + private MsgFlagEnum(int flag) { + this.flag = (short) flag; } + } } diff --git a/src/main/java/club/joylink/xiannccda/configuration/SpringSecurityConfiguration.java b/src/main/java/club/joylink/xiannccda/configuration/SpringSecurityConfiguration.java index 4e487b1..54b852b 100644 --- a/src/main/java/club/joylink/xiannccda/configuration/SpringSecurityConfiguration.java +++ b/src/main/java/club/joylink/xiannccda/configuration/SpringSecurityConfiguration.java @@ -58,7 +58,7 @@ public class SpringSecurityConfiguration { static { AuthWhiteList.add(AntPathRequestMatcher.antMatcher(HttpMethod.POST, "/api/user/register")); AuthWhiteList.add(AntPathRequestMatcher.antMatcher(HttpMethod.POST, "/api/user/login")); - AuthWhiteList.add(AntPathRequestMatcher.antMatcher(HttpMethod.GET, "/mock/**/*"));//测试使用,过后删除或注释 + AuthWhiteList.add(AntPathRequestMatcher.antMatcher("/mock/**/*"));//测试使用,过后删除或注释 AuthWhiteList.add(AntPathRequestMatcher.antMatcher("/ws-default")); AuthWhiteList.add(AntPathRequestMatcher.antMatcher("/swagger-ui/**")); AuthWhiteList.add(AntPathRequestMatcher.antMatcher("/api-docs/**")); diff --git a/src/main/java/club/joylink/xiannccda/mock/message/MockDeviceController.java b/src/main/java/club/joylink/xiannccda/mock/message/MockDeviceController.java index 495bdb7..e6c8a96 100644 --- a/src/main/java/club/joylink/xiannccda/mock/message/MockDeviceController.java +++ b/src/main/java/club/joylink/xiannccda/mock/message/MockDeviceController.java @@ -1,37 +1,83 @@ package club.joylink.xiannccda.mock.message; import club.joylink.xiannccda.ats.message.OccMessageManage; +import club.joylink.xiannccda.ats.message.line3.DateTimeUtil; +import club.joylink.xiannccda.ats.message.line3.req.AlarmAckRequest; +import club.joylink.xiannccda.ats.message.line3.req.AlarmAckRequest.MsgTypeEnum; import club.joylink.xiannccda.ats.message.line3.req.LoadDeviceStatusRequest; +import club.joylink.xiannccda.ats.message.line3.req.LoadHistoryTGDataRequest; +import club.joylink.xiannccda.ats.message.line3.req.LoadHistoryTGDataRequest.ApplyTypeEnum; +import club.joylink.xiannccda.ats.message.line3.req.ReportAskRequest; +import club.joylink.xiannccda.ats.message.line3.req.ReportAskRequest.MsgFlagEnum; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import lombok.RequiredArgsConstructor; +import io.swagger.v3.oas.annotations.tags.Tag; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/mock") -@RequiredArgsConstructor +@Tag(name = "mock") + public class MockDeviceController { - private final NccMockDataService mockDataService; - - private final OccMessageManage occMessageManage; + @Autowired + private NccMockDataService mockDataService; + @Autowired + private OccMessageManage occMessageManage; @GetMapping("reset") @Operation(summary = "重置设备状态") - @ApiResponse(description = "重置设备状态") public void reset() { this.mockDataService.reset(); } - @GetMapping("ats设备信息") + @GetMapping("load/device") @Operation(summary = "ats设备信息") - @ApiResponse(description = "ats设备信息") public void atsRequest(@RequestParam("lineId") Integer lineId) { LoadDeviceStatusRequest deviceStatusRequest = new LoadDeviceStatusRequest(lineId.shortValue()); - this.occMessageManage.sendMsg(lineId, deviceStatusRequest); + this.occMessageManage.sendMsg(lineId, deviceStatusRequest, true); + } + + @PostMapping("load/reportAsk") + @Operation(summary = "统计信息查询") + public void reportAskRequest(@RequestParam("lineId") Integer lineId, + @RequestParam("reportId") Short reportId, + @RequestParam("msgFlag") MsgFlagEnum msgFlag, @RequestParam(value = "content", required = false) String content) { + LocalDateTime dateTime = DateTimeUtil.parse(content, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + ReportAskRequest askRequest = new ReportAskRequest(lineId.shortValue(), reportId, msgFlag, dateTime); + this.occMessageManage.sendMsg(lineId, askRequest, false); + } + + + @GetMapping("load/alarm") + @Operation(summary = "事件及告警信息请求") + public void alarmAckRequest(@RequestParam("lineId") Integer lineId, + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = DateTimeFormat.ISO.DATE_TIME) + @RequestParam(value = "startDateTime", defaultValue = "2023-07-07 11:11:11") LocalDateTime startDateTime, + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = DateTimeFormat.ISO.DATE_TIME) + @RequestParam(value = "endDateTime", defaultValue = "2023-07-07 11:11:11") LocalDateTime endDateTime, + @RequestParam("msgType") MsgTypeEnum typeEnum, + @RequestParam("reportId") Short reportId) { + AlarmAckRequest aar = new AlarmAckRequest(lineId.shortValue(), reportId, typeEnum, startDateTime, endDateTime); + + this.occMessageManage.sendMsg(lineId, aar, false); + } + + @GetMapping("load/history") + @Operation(summary = "历史运行图申请消息") + public void loadHistoryTgRequest(@RequestParam("lineId") Integer lineId, + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = DateTimeFormat.ISO.DATE_TIME) + @RequestParam(value = "localDateTime", defaultValue = "2023-07-07 11:11:11") LocalDateTime localDateTime, + @RequestParam("applyType") ApplyTypeEnum typeEnum) { + LoadHistoryTGDataRequest tgDataRequest = new LoadHistoryTGDataRequest(lineId.shortValue(), localDateTime, typeEnum); + this.occMessageManage.sendMsg(lineId, tgDataRequest, false); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3547fb9..011d8a7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,7 +19,9 @@ key: public: classpath:app.pub private: classpath:app.key spring-doc: - packages-to-scan: club.joylink.xiannccda.controller + packages-to-scan: + - club.joylink.xiannccda.controller + - club.joylink.xiannccda.mock.message swagger-ui: enabled: true api-docs: