Files
jsowell-charger-web/jsowell-netty/src/main/java/com/jsowell/netty/handler/LoginRequestHandler.java

248 lines
10 KiB
Java
Raw Normal View History

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