Skip to content

Commit

Permalink
feat(AttachmentServer): 完善附件服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
hylexus committed Jan 28, 2024
1 parent f932913 commit f6df40e
Show file tree
Hide file tree
Showing 50 changed files with 1,274 additions and 522 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 2.1.4-rc.2(2024-01-28)

### ⭐ New Features

- 优化苏标附件服务器的支持
- 支持位置附加项列表注解 `@RequestFieldAlias.LocationMsgExtraItemMapping()`
- 新增 `@RequestField#conditionalOn()` 属性
- 内置几个和苏标相关的实体类

## 2.1.4-beta1(2024-01-14)

### ⭐ New Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.github.hylexus.jt.jt808.spec.session.Jt808Session;
import io.github.hylexus.jt.jt808.support.annotation.handler.Jt808RequestHandler;
import io.github.hylexus.jt.jt808.support.annotation.handler.Jt808RequestHandlerMapping;
import io.github.hylexus.jt.jt808.support.extension.attachment.AttachmentJt808SessionManager;
import io.github.hylexus.jt.jt808.support.extension.attachment.impl.SimpleAttachmentJt808RequestProcessor;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -39,14 +38,8 @@ public class AttachmentFileHandler {
.expireAfterWrite(Duration.ofMinutes(5))
.build();

// !!!如果需要用 session 的话,附件相关的几个 808 指令对应的 session 应该从 AttachmentJt808SessionManager 中获取
// !!!而不是从普通的 Jt808SessionManager 中获取
// !!!因为 附件上传 和 普通指令 是不同的 TCP 连接
private final AttachmentJt808SessionManager sessionManager;

public AttachmentFileHandler(AttachmentFileService attachmentFileService, AttachmentJt808SessionManager sessionManager) {
public AttachmentFileHandler(AttachmentFileService attachmentFileService) {
this.attachmentFileService = attachmentFileService;
this.sessionManager = sessionManager;
}

@Jt808RequestHandlerMapping(msgType = 0x1210, versions = Jt808ProtocolVersion.AUTO_DETECTION)
Expand Down Expand Up @@ -124,9 +117,7 @@ private void warnLogIfNecessary(Jt808Request request, String msg) {
if (request.session() == null) {
return;
}
if (sessionManager.findByTerminalId(request.terminalId()).orElseThrow() != request.session()) {
log.error("session invalid");
}

if (request.session().role() == Jt808Session.Role.INSTRUCTION) {
log.warn(msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.hylexus.jt.demos.jt808.handler;

import io.github.hylexus.jt.demos.jt808.msg.req.LocationBatchUploadMsgV2019;
import io.github.hylexus.jt.demos.jt808.msg.req.LocationUploadMsgV2019;
import io.github.hylexus.jt.demos.jt808.service.LocationMsgService;
import io.github.hylexus.jt.jt808.spec.Jt808RequestEntity;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2013Alias;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2013AliasV2;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2019AliasV2;
import io.github.hylexus.jt.jt808.spec.builtin.msg.resp.BuiltinServerCommonReplyMsg;
import io.github.hylexus.jt.jt808.spec.session.Jt808Session;
import io.github.hylexus.jt.jt808.support.annotation.handler.Jt808RequestHandler;
Expand Down Expand Up @@ -43,8 +43,8 @@ public BuiltinServerCommonReplyMsg processLocationBatchUploadMsgV2019(Jt808Reque
}

@Jt808RequestHandlerMapping(msgType = 0x0200, versions = VERSION_2019)
public BuiltinServerCommonReplyMsg processLocationUploadMsgV2019(Jt808RequestEntity<LocationUploadMsgV2019> request, Jt808Session session) {
final LocationUploadMsgV2019 body = request.body();
public BuiltinServerCommonReplyMsg processLocationUploadMsgV2019(Jt808RequestEntity<BuiltinMsg0200V2019AliasV2> request, Jt808Session session) {
final BuiltinMsg0200V2019AliasV2 body = request.body();
log.info("LocationUpload -- V2019 -- {}", body);

locationMsgService.processLocationMsg(request.session(), body);
Expand All @@ -53,11 +53,11 @@ public BuiltinServerCommonReplyMsg processLocationUploadMsgV2019(Jt808RequestEnt
}

@Jt808RequestHandlerMapping(msgType = 0x0200, versions = VERSION_2013)
public BuiltinServerCommonReplyMsg processLocationUploadMsgV2013(Jt808RequestEntity<BuiltinMsg0200V2013Alias> request, Jt808Session session) {
final BuiltinMsg0200V2013Alias body = request.body();
public BuiltinServerCommonReplyMsg processLocationUploadMsgV2013(Jt808RequestEntity<BuiltinMsg0200V2013AliasV2> request, Jt808Session session) {
final BuiltinMsg0200V2013AliasV2 body = request.body();
log.info("LocationUpload -- V2013 -- {}", body);

locationMsgService.processLocationMsg(body, request.session());
locationMsgService.processLocationMsg(request.session(), body);

return BuiltinServerCommonReplyMsg.success(request.msgId(), request.flowId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.hylexus.jt.demos.jt808.service;

import io.github.hylexus.jt.demos.jt808.configuration.pros.Jt808AppProps;
import io.github.hylexus.jt.jt808.spec.builtin.msg.extension.location.AlarmIdentifierAlias;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg1210Alias;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg30316364Alias;
import io.github.hylexus.jt.jt808.spec.session.Jt808Session;
Expand All @@ -9,8 +10,8 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AttachmentFileService {
Expand All @@ -22,12 +23,15 @@ public AttachmentFileService(Jt808AppProps appProps) {
}

public void writeDataFragment(Jt808Session session, BuiltinMsg30316364Alias body, BuiltinMsg1210Alias group) {
final AlarmIdentifierAlias alarmIdentifier = group.getAlarmIdentifier();
final LocalDateTime localDateTime = alarmIdentifier.getTime();
// 这里就瞎写了一个路径 看你需求随便改
final String filePath = appProps.getAttachment().getTemporaryPath() + File.separator
+ DateTimeFormatter.ofPattern("yyyyMMddHH").format(localDateTime) + File.separator
+ session.terminalId() + File.separator
+ new SimpleDateFormat("yyyy-MM-dd HH").format(new Date()) + File.separator
+ group.getMessageType() + File.separator
+ group.getAlarmNo() + File.separator
// + group.getAlarmNo() + File.separator
+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(localDateTime) + "-" + group.getAlarmNo() + File.separator
+ body.getFileName().trim();

final File tempFile = new File(filePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.github.hylexus.jt.demos.jt808.service;

import io.github.hylexus.jt.demos.jt808.configuration.pros.Jt808AppProps;
import io.github.hylexus.jt.demos.jt808.msg.req.LocationUploadMsgV2019;
import io.github.hylexus.jt.jt808.spec.Jt808CommandKey;
import io.github.hylexus.jt.jt808.spec.Jt808CommandSender;
import io.github.hylexus.jt.jt808.spec.builtin.msg.extension.BuiltinMsg64;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2013Alias;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.extra.ExtraItemSupport;
import io.github.hylexus.jt.jt808.spec.builtin.msg.extension.location.*;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2013AliasV2;
import io.github.hylexus.jt.jt808.spec.builtin.msg.req.BuiltinMsg0200V2019AliasV2;
import io.github.hylexus.jt.jt808.spec.builtin.msg.resp.BuiltinMsg9208Alias;
import io.github.hylexus.jt.jt808.spec.impl.BuiltinJt808MsgType;
import io.github.hylexus.jt.jt808.spec.session.Jt808Session;
Expand All @@ -18,9 +17,9 @@
import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand All @@ -29,54 +28,104 @@ public class LocationMsgService {
private final Jt808CommandSender commandSender;
private final Jt808AppProps appProps;
private final int attachmentServerPortTcp;
private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(4);

public LocationMsgService(Jt808CommandSender commandSender, Jt808AppProps appProps, @Value("${jt808.attachment-server.port}") int portTcp) {
this.commandSender = commandSender;
this.appProps = appProps;
this.attachmentServerPortTcp = portTcp;
}

public void processLocationMsg(Jt808Session session, LocationUploadMsgV2019 body) {
if (!CollectionUtils.isEmpty(body.getExtraItemList())) {
final Map<Integer, ExtraItemSupport> groupById = body.getExtraItemList().stream().collect(Collectors.toMap(LocationUploadMsgV2019.ExtraItem::getId, Function.identity()));
this.processExtraItemList(session, groupById);
public void processLocationMsg(Jt808Session session, BuiltinMsg0200V2019AliasV2 body) {
final Map<Integer, Object> extraItemMap = body.getExtraItemMap();
// 处理附加项列表
if (!CollectionUtils.isEmpty(extraItemMap)) {
this.processExtraItemList(session, extraItemMap);
}
// 处理其他消息属性...
}

public void processLocationMsg(BuiltinMsg0200V2013Alias body, Jt808Session session) {
if (!CollectionUtils.isEmpty(body.getExtraItemList())) {
final Map<Integer, ExtraItemSupport> groupById = body.getExtraItemList().stream().collect(Collectors.toMap(BuiltinMsg0200V2013Alias.ExtraItem::getId, Function.identity()));
this.processExtraItemList(session, groupById);
public void processLocationMsg(Jt808Session session, BuiltinMsg0200V2013AliasV2 body) {
final Map<Integer, Object> extraItemMap = body.getExtraItemMap();
// 处理附加项列表
if (!CollectionUtils.isEmpty(extraItemMap)) {
this.processExtraItemList(session, extraItemMap);
}
// 处理其他消息属性...
}

private void processExtraItemList(Jt808Session session, Map<Integer, ExtraItemSupport> groupById) {
private void processExtraItemList(Jt808Session session, Map<Integer, Object> groupById) {
// 苏标: 高级驾驶辅助系统报警信息,定义见表 4-15
this.process0x64IfNecessary(session, groupById.get(0x64));
// ADAS模块视频通道
this.process0x64IfNecessary(session, (BuiltinMsg64Alias) groupById.get(0x64));

// 苏标: 驾驶员状态监测系统报警信息,定义见表 4-17
// DSM模块视频通道
this.process0x65IfNecessary(session, (BuiltinMsg65Alias) groupById.get(0x65));

// 苏标: 胎压监测系统报警信息,定义见表 4-18
this.process0x66IfNecessary(session, (BuiltinMsg66Alias) groupById.get(0x66));

// 苏标: 盲区监测系统报警信息,定义见表 4-20
this.process0x67IfNecessary(session, (BuiltinMsg67Alias) groupById.get(0x67));
}

private void process0x64IfNecessary(Jt808Session session, ExtraItemSupport extraItem) {
if (extraItem == null) {
private void process0x67IfNecessary(Jt808Session session, BuiltinMsg67Alias msg67) {
if (msg67 == null) {
return;
}

final BuiltinMsg64 msg64 = new BuiltinMsg64(extraItem.getContent());
log.info("ExtraItem-67==>AlarmIdentifier: {}", msg67.getAlarmIdentifier());
this.doSendMsg9208(session, msg67.getAlarmIdentifier());
}

private void process0x66IfNecessary(Jt808Session session, BuiltinMsg66Alias msg66) {
if (msg66 == null) {
return;
}

log.info("ExtraItem-66==>AlarmIdentifier: {}", msg66.getAlarmIdentifier());
this.doSendMsg9208(session, msg66.getAlarmIdentifier());
}

private void process0x65IfNecessary(Jt808Session session, BuiltinMsg65Alias msg65) {
if (msg65 == null) {
return;
}

log.info("ExtraItem-65==>AlarmIdentifier: {}", msg65.getAlarmIdentifier());
this.doSendMsg9208(session, msg65.getAlarmIdentifier());
}

private void process0x64IfNecessary(Jt808Session session, BuiltinMsg64Alias msg64) {
if (msg64 == null) {
return;
}

log.info("ExtraItem-64==>AlarmIdentifier: {}", msg64.getAlarmIdentifier());
this.doSendMsg9208(session, msg64.getAlarmIdentifier());
}

private void doSendMsg9208(Jt808Session session, AlarmIdentifierAlias alarmIdentifier) {
final BuiltinMsg9208Alias msg9208 = new BuiltinMsg9208Alias();
msg9208.setAttachmentServerIp(appProps.getServerIp());
msg9208.setAttachmentServerIpLength((short) appProps.getServerIp().length());
msg9208.setAttachmentServerPortTcp(attachmentServerPortTcp);
msg9208.setAttachmentServerPortUdp(0);
msg9208.setAlarmIdentifier(msg64.getAlarmIdentifiers());
msg9208.setAlarmIdentifier(alarmIdentifier);
msg9208.setAlarmNo(Randoms.randomString(32));
msg9208.setReservedByte16("");
final Jt808CommandKey commandKey = Jt808CommandKey.of(session.terminalId(), BuiltinJt808MsgType.SERVER_MSG_9208, session.nextFlowId());
try {
final Object resp = this.commandSender.sendCommandAndWaitingForReply(commandKey, msg9208, 20L, TimeUnit.SECONDS);
log.info("RESP <-- 0x9208: {}", resp);
} catch (Throwable e) {
log.error("下发 0x9208 异常", e);
throw new RuntimeException(e);
}

EXECUTOR.submit(() -> {
final Jt808CommandKey commandKey = Jt808CommandKey.of(session.terminalId(), BuiltinJt808MsgType.CLIENT_COMMON_REPLY, session.nextFlowId());
try {
log.info("Waiting for <{}>", commandKey);
final Object resp = this.commandSender.sendCommandAndWaitingForReply(commandKey, msg9208, 20L, TimeUnit.SECONDS);
log.info("RESP <-- 0x9208: {}", resp);
} catch (Throwable e) {
log.error("下发 0x9208 异常", e);
throw new RuntimeException(e);
}
});
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectGroup=io.github.hylexus.jt
projectVersion=2.1.4-beta1
projectVersion=2.1.4-rc.2
# scm
projectScmUrl=https://github.com/hylexus/jt-framework
projectScmConnection=scm:git:git@github.com:hylexus/jt-framework.git
Expand Down
Loading

0 comments on commit f6df40e

Please sign in to comment.