This commit is contained in:
Lemon
2023-09-15 15:40:38 +08:00
parent bed6b82c6e
commit 3aa4481484

View File

@@ -30,10 +30,10 @@ import java.util.concurrent.CompletableFuture;
@Slf4j @Slf4j
@Component @Component
public class LoginRequestHandler extends AbstractHandler{ public class LoginRequestHandler extends AbstractHandler {
@Autowired @Autowired
private IPileBasicInfoService pileBasicInfoService; private IPileBasicInfoService pileBasicInfoService;
@Autowired @Autowired
private YKCPushCommandService ykcPushCommandService; private YKCPushCommandService ykcPushCommandService;
@@ -44,201 +44,205 @@ public class LoginRequestHandler extends AbstractHandler{
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.LOGIN_CODE.getBytes()); private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.LOGIN_CODE.getBytes());
private List<String> newProgramVersionList = Lists.newArrayList("c6-30"); private List<String> newProgramVersionList = Lists.newArrayList("c6-30");
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
YKCOperateFactory.register(type, this); YKCOperateFactory.register(type, this);
} }
public static void main(String[] args) { public static void main(String[] args) {
String msg = "8800000000009000020f56312e323035303000898604940121c138531304"; String msg = "8800000000009000020f56312e323035303000898604940121c138531304";
byte[] msgBody = BytesUtil.str2Bcd(msg); byte[] msgBody = BytesUtil.str2Bcd(msg);
int startIndex = 0;
int length = 7;
// 桩编码
byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileSn = BytesUtil.binary(pileSnByte, 16);
// log.info("桩号:{}", pileSn);
// 桩类型 0 表示直流桩, 1 表示交流桩
startIndex += length;
length = 1;
byte[] pileTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileType = BytesUtil.bcd2Str(pileTypeByteArr);
// 充电枪数量
startIndex += length;
byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorNum = BytesUtil.bcd2Str(connectorNumByteArr);
// 通信协议版本 版本号乘 10v1.0 表示 0x0A
startIndex += length;
byte[] communicationVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
// int i = Integer.parseInt(BytesUtil.bcd2Str(communicationVersionByteArr)); // 0F --> 15
BigDecimal bigDecimal = new BigDecimal(BytesUtil.bcd2Str(communicationVersionByteArr));
BigDecimal communicationVersionTemp = bigDecimal.divide(new BigDecimal(10));
String communicationVersion = "v" + communicationVersionTemp;
// 程序版本
startIndex += length;
length = 8;
byte[] programVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String programVersion = BytesUtil.ascii2Str(programVersionByteArr);
log.info("程序版本:{} length:{}", programVersion, programVersion.length());
// 网络连接类型 0x00 SIM 卡 0x01 LAN 0x02 WAN 0x03 其他
startIndex += length;
length = 1;
byte[] internetConnectionTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String internetConnection = BytesUtil.bcd2Str(internetConnectionTypeByteArr);
// sim卡
startIndex += length;
length = 10;
byte[] simCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String iccid = BytesUtil.bin2HexStr(simCardNumByteArr);
// 运营商 0x00 移动 0x02 电信 0x03 联通 0x04 其他
startIndex += length;
length = 1;
byte[] businessTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String business = BytesUtil.bcd2Str(businessTypeByteArr);
}
@Override
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
log.info("[===执行登录逻辑===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
// 获取消息体
byte[] msgBody = ykcDataProtocol.getMsgBody();
int startIndex = 0; int startIndex = 0;
int length = 7; int length = 7;
// 桩编码 // 桩编码
byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileSn = BytesUtil.binary(pileSnByte, 16); String pileSn = BytesUtil.binary(pileSnByte, 16);
// log.info("桩号:{}", pileSn); // log.info("桩号:{}", pileSn);
// 保存时间
saveLastTime(pileSn);
// 桩类型 0 表示直流桩, 1 表示交流桩 // 桩类型 0 表示直流桩, 1 表示交流桩
startIndex += length; startIndex += length;
length = 1; length = 1;
byte[] pileTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] pileTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileType = BytesUtil.bcd2Str(pileTypeByteArr); String pileType = BytesUtil.bcd2Str(pileTypeByteArr);
// 充电枪数量 // 充电枪数量
startIndex += length; startIndex += length;
byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorNum = BytesUtil.bcd2Str(connectorNumByteArr); String connectorNum = BytesUtil.bcd2Str(connectorNumByteArr);
// 通信协议版本 版本号乘 10v1.0 表示 0x0A // 通信协议版本 版本号乘 10v1.0 表示 0x0A
startIndex += length; startIndex += length;
byte[] communicationVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] communicationVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
// int i = Integer.parseInt(BytesUtil.bcd2Str(communicationVersionByteArr)); // 0F --> 15 // int i = Integer.parseInt(BytesUtil.bcd2Str(communicationVersionByteArr)); // 0F --> 15
BigDecimal bigDecimal = new BigDecimal(BytesUtil.bcd2Str(communicationVersionByteArr)); BigDecimal bigDecimal = new BigDecimal(BytesUtil.bcd2Str(communicationVersionByteArr));
BigDecimal communicationVersionTemp = bigDecimal.divide(new BigDecimal(10)); BigDecimal communicationVersionTemp = bigDecimal.divide(new BigDecimal(10));
String communicationVersion = "v" + communicationVersionTemp; String communicationVersion = "v" + communicationVersionTemp;
// 程序版本 // 程序版本
startIndex += length; startIndex += length;
length = 8; length = 8;
byte[] programVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] programVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String programVersion = BytesUtil.ascii2Str(programVersionByteArr);
log.info("程序版本:{} length:{}", programVersion, programVersion.length());
// 网络连接类型 0x00 SIM 卡 0x01 LAN 0x02 WAN 0x03 其他
startIndex += length;
length = 1;
byte[] internetConnectionTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String internetConnection = BytesUtil.bcd2Str(internetConnectionTypeByteArr);
// sim卡
startIndex += length;
length = 10;
byte[] simCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String iccid = BytesUtil.bin2HexStr(simCardNumByteArr);
// 运营商 0x00 移动 0x02 电信 0x03 联通 0x04 其他
startIndex += length;
length = 1;
byte[] businessTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String business = BytesUtil.bcd2Str(businessTypeByteArr);
}
@Override
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
log.info("[===执行登录逻辑===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
// 获取消息体
byte[] msgBody = ykcDataProtocol.getMsgBody();
int startIndex = 0;
int length = 7;
// 桩编码
byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileSn = BytesUtil.binary(pileSnByte, 16);
// log.info("桩号:{}", pileSn);
// 保存时间
saveLastTime(pileSn);
// 桩类型 0 表示直流桩, 1 表示交流桩
startIndex += length;
length = 1;
byte[] pileTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String pileType = BytesUtil.bcd2Str(pileTypeByteArr);
// 充电枪数量
startIndex += length;
byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String connectorNum = BytesUtil.bcd2Str(connectorNumByteArr);
// 通信协议版本 版本号乘 10v1.0 表示 0x0A
startIndex += length;
byte[] communicationVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
// int i = Integer.parseInt(BytesUtil.bcd2Str(communicationVersionByteArr)); // 0F --> 15
BigDecimal bigDecimal = new BigDecimal(BytesUtil.bcd2Str(communicationVersionByteArr));
BigDecimal communicationVersionTemp = bigDecimal.divide(new BigDecimal(10));
String communicationVersion = "v" + communicationVersionTemp;
// 程序版本
startIndex += length;
length = 8;
byte[] programVersionByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String programVersion = BytesUtil.ascii2Str(programVersionByteArr); String programVersion = BytesUtil.ascii2Str(programVersionByteArr);
// log.info("程序版本:{} length:{}", programVersion, programVersion.length()); // log.info("程序版本:{} length:{}", programVersion, programVersion.length());
// 网络连接类型 0x00 SIM 卡 0x01 LAN 0x02 WAN 0x03 其他 // 网络连接类型 0x00 SIM 卡 0x01 LAN 0x02 WAN 0x03 其他
startIndex += length; startIndex += length;
length = 1; length = 1;
byte[] internetConnectionTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] internetConnectionTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String internetConnection = BytesUtil.bcd2Str(internetConnectionTypeByteArr); String internetConnection = BytesUtil.bcd2Str(internetConnectionTypeByteArr);
// sim卡 // sim卡
startIndex += length; startIndex += length;
length = 10; length = 10;
byte[] simCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] simCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String iccid = BytesUtil.bin2HexStr(simCardNumByteArr); String iccid = BytesUtil.bin2HexStr(simCardNumByteArr);
// 运营商 0x00 移动 0x02 电信 0x03 联通 0x04 其他 // 运营商 0x00 移动 0x02 电信 0x03 联通 0x04 其他
startIndex += length; startIndex += length;
length = 1; length = 1;
byte[] businessTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length); byte[] businessTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
String business = BytesUtil.bcd2Str(businessTypeByteArr); String business = BytesUtil.bcd2Str(businessTypeByteArr);
LoginRequestData loginRequestData = LoginRequestData.builder() LoginRequestData loginRequestData = LoginRequestData.builder()
.pileSn(pileSn) .pileSn(pileSn)
.pileType(pileType) .pileType(pileType)
.connectorNum(connectorNum) .connectorNum(connectorNum)
.communicationVersion(communicationVersion) .communicationVersion(communicationVersion)
.programVersion(programVersion) .programVersion(programVersion)
.internetConnection(internetConnection) .internetConnection(internetConnection)
.iccid(iccid) .iccid(iccid)
.business(business) .business(business)
.build(); .build();
// 结果(默认 0x01:登录失败) // 结果(默认 0x01:登录失败)
byte[] flag = Constants.oneByteArray; byte[] flag = Constants.oneByteArray;
// 通过桩编码SN查询数据库如果有数据则登录成功否则登录失败 // 通过桩编码SN查询数据库如果有数据则登录成功否则登录失败
PileInfoVO pileInfoVO = null; PileInfoVO pileInfoVO = null;
try { try {
pileInfoVO = pileBasicInfoService.selectPileInfoBySn(pileSn); pileInfoVO = pileBasicInfoService.selectPileInfoBySn(pileSn);
} catch (Exception e) { } catch (Exception e) {
log.error("selectPileInfoBySn发生异常", e); log.error("selectPileInfoBySn发生异常", e);
} }
if (pileInfoVO != null) { if (pileInfoVO != null) {
flag = Constants.zeroByteArray; flag = Constants.zeroByteArray;
// 登录成功保存桩号和channel的关系 // 登录成功保存桩号和channel的关系
PileChannelEntity.put(pileSn, channel); PileChannelEntity.put(pileSn, channel);
// 更改桩和该桩下的枪口状态分别为 在线、空闲 公共方法修改状态 // 更改桩和该桩下的枪口状态分别为 在线、空闲 公共方法修改状态
pileBasicInfoService.updateStatus(BytesUtil.bcd2Str(ykcDataProtocol.getFrameType()), pileSn, null, null, null); pileBasicInfoService.updateStatus(BytesUtil.bcd2Str(ykcDataProtocol.getFrameType()), pileSn, null, null, null);
// 充电桩使用的sim卡把信息存库
if (StringUtils.equals("00", internetConnection)) {
try {
pileBasicInfoService.updatePileSimInfo(pileSn, iccid);
// pileBasicInfoService.updatePileSimInfoV2(pileSn, iccid);
} catch (Exception e) {
log.error("更新充电桩sim卡信息失败", e);
}
}
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 对时 // 对时
ProofreadTimeCommand command = ProofreadTimeCommand.builder().pileSn(pileSn).build(); ProofreadTimeCommand command = ProofreadTimeCommand.builder().pileSn(pileSn).build();
ykcPushCommandService.pushProofreadTimeCommand(command); ykcPushCommandService.pushProofreadTimeCommand(command);
}); });
// log.info("下面进行下发二维码 pileSn:{}, thread:{}", pileSn, Thread.currentThread().getName()); // log.info("下面进行下发二维码 pileSn:{}, thread:{}", pileSn, Thread.currentThread().getName());
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
try { try {
Thread.sleep(600); Thread.sleep(600);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 下发二维码 // 下发二维码
IssueQRCodeCommand issueQRCodeCommand = IssueQRCodeCommand.builder().pileSn(pileSn).build(); IssueQRCodeCommand issueQRCodeCommand = IssueQRCodeCommand.builder().pileSn(pileSn).build();
ykcPushCommandService.pushIssueQRCodeCommand(issueQRCodeCommand); ykcPushCommandService.pushIssueQRCodeCommand(issueQRCodeCommand);
}); });
}
// 保存报文 TODO 没有登录认证通过还要不要保存报文? if (StringUtils.equals("00", internetConnection)) {
String jsonMsg = JSONObject.toJSONString(loginRequestData); CompletableFuture.runAsync(() -> {
pileMsgRecordService.save(pileSn, pileSn, type, jsonMsg, ykcDataProtocol.getHEXString()); // 充电桩使用的sim卡把信息存库
try {
// pileBasicInfoService.updatePileSimInfo(pileSn, iccid);
pileBasicInfoService.updatePileSimInfoV2(pileSn, iccid);
} catch (Exception e) {
log.error("更新充电桩sim卡信息失败", e);
}
});
}
// 消息体 }
byte[] messageBody = Bytes.concat(pileSnByte, flag);
return getResult(ykcDataProtocol, messageBody); // 保存报文 TODO 没有登录认证通过还要不要保存报文?
} String jsonMsg = JSONObject.toJSONString(loginRequestData);
pileMsgRecordService.save(pileSn, pileSn, type, jsonMsg, ykcDataProtocol.getHEXString());
// 消息体
byte[] messageBody = Bytes.concat(pileSnByte, flag);
return getResult(ykcDataProtocol, messageBody);
}
} }