同步获取响应数据

This commit is contained in:
Guoqs
2024-08-01 16:47:18 +08:00
parent 901310f061
commit ea1a0c81f1
2 changed files with 129 additions and 87 deletions

View File

@@ -77,72 +77,72 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
* @param frameTypeCode
* @return
*/
public boolean push(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode) {
// 通过桩编号获取channel
ChannelHandlerContext ctx = PileChannelEntity.getChannelByPileSn(pileSn);
String value = ((YKCFrameTypeCode) frameTypeCode).getValue();
if (Objects.isNull(ctx)) {
log.error("push命令[{}]失败, 桩号:{}无法获取到长连接, 请检查充电桩连接状态!", value, pileSn);
return false;
}
/*
拼接报文
*/
// 起始标志
byte[] head = new byte[]{0x68};
// public boolean push(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode) {
// // 通过桩编号获取channel
// ChannelHandlerContext ctx = PileChannelEntity.getChannelByPileSn(pileSn);
// String value = ((YKCFrameTypeCode) frameTypeCode).getValue();
// if (Objects.isNull(ctx)) {
// log.error("push命令[{}]失败, 桩号:{}无法获取到长连接, 请检查充电桩连接状态!", value, pileSn);
// return false;
// }
// /*
// 拼接报文
// */
// // 起始标志
// byte[] head = new byte[]{0x68};
//
// // 序列号域
// byte[] serialNumber = new byte[]{0x00, 0x00};
//
// // 加密标志
// byte[] encryptFlag = new byte[]{0x00};
//
// // 帧类型标志
// byte[] frameType = new byte[]{(byte) ((YKCFrameTypeCode) frameTypeCode).getCode()};
//
// // 序列号域+加密标志+帧类型标志+消息体
// byte[] temp = Bytes.concat(serialNumber, encryptFlag, frameType, msg);
//
// // 数据长度
// byte[] length = BytesUtil.intToBytes(temp.length, 1);
//
// // 帧校验域
// byte[] crc = BytesUtil.intToBytes(CRC16Util.calcCrc16(temp));
//
// // 返回报文
// byte[] writeMsg = Bytes.concat(head, length, temp, crc);
//
// // 返回完整的报文 string类型
// String wholeMsg = BytesUtil.binary(writeMsg, 16);
// ByteBuf byteBuf = ctx.channel().alloc().buffer().writeBytes(writeMsg);
// ChannelFuture channelFuture = ctx.channel().writeAndFlush(byteBuf);
// channelFuture.addListener((ChannelFutureListener) channelFutureListener -> {
// // 检查操作的状态
// if (channelFutureListener.isSuccess()) {
// log.info("【push结果===>成功】, pileSn:{}, remoteAddress:{}, channelId:{}, 帧类型:{}, 报文:{}",
// pileSn, ctx.channel().remoteAddress(), ctx.channel().id(), value, wholeMsg);
// } else {
// // 如果发生错误则访问描述原因的Throwable
// Throwable cause = channelFutureListener.cause();
// log.info("【push结果===>失败】, pileSn:{}, remoteAddress:{}, channelId:{}, 帧类型:{}, 报文:{}",
// pileSn, ctx.channel().remoteAddress(), ctx.channel().id(), value, wholeMsg);
// log.error("push发送命令失败, pileSn:{}", pileSn, cause);
// }
// });
//
// // 保存报文
// String frameTypeStr = YKCUtils.frameType2Str(((YKCFrameTypeCode) frameTypeCode).getBytes());
// if (frameTypeList.contains(frameTypeStr)) {
// pileMsgRecordService.save(pileSn, null, frameTypeStr, null, wholeMsg);
// }
// return true;
// }
// 序列号域
byte[] serialNumber = new byte[]{0x00, 0x00};
// 加密标志
byte[] encryptFlag = new byte[]{0x00};
// 帧类型标志
byte[] frameType = new byte[]{(byte) ((YKCFrameTypeCode) frameTypeCode).getCode()};
// 序列号域+加密标志+帧类型标志+消息体
byte[] temp = Bytes.concat(serialNumber, encryptFlag, frameType, msg);
// 数据长度
byte[] length = BytesUtil.intToBytes(temp.length, 1);
// 帧校验域
byte[] crc = BytesUtil.intToBytes(CRC16Util.calcCrc16(temp));
// 返回报文
byte[] writeMsg = Bytes.concat(head, length, temp, crc);
// 返回完整的报文 string类型
String wholeMsg = BytesUtil.binary(writeMsg, 16);
ByteBuf byteBuf = ctx.channel().alloc().buffer().writeBytes(writeMsg);
ChannelFuture channelFuture = ctx.channel().writeAndFlush(byteBuf);
channelFuture.addListener((ChannelFutureListener) channelFutureListener -> {
// 检查操作的状态
if (channelFutureListener.isSuccess()) {
log.info("【push结果===>成功】, pileSn:{}, remoteAddress:{}, channelId:{}, 帧类型:{}, 报文:{}",
pileSn, ctx.channel().remoteAddress(), ctx.channel().id(), value, wholeMsg);
} else {
// 如果发生错误则访问描述原因的Throwable
Throwable cause = channelFutureListener.cause();
log.info("【push结果===>失败】, pileSn:{}, remoteAddress:{}, channelId:{}, 帧类型:{}, 报文:{}",
pileSn, ctx.channel().remoteAddress(), ctx.channel().id(), value, wholeMsg);
log.error("push发送命令失败, pileSn:{}", pileSn, cause);
}
});
// 保存报文
String frameTypeStr = YKCUtils.frameType2Str(((YKCFrameTypeCode) frameTypeCode).getBytes());
if (frameTypeList.contains(frameTypeStr)) {
pileMsgRecordService.save(pileSn, null, frameTypeStr, null, wholeMsg);
}
return true;
public byte[] send(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode) throws Exception {
return this.send(msg, pileSn, frameTypeCode, 5, TimeUnit.SECONDS);
}
public byte[] pushTest(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode) throws Exception {
return this.pushTest(msg, pileSn, frameTypeCode, 5, TimeUnit.SECONDS);
}
public byte[] pushTest(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode, long timeout, TimeUnit unit) throws Exception {
public byte[] send(byte[] msg, String pileSn, Enum<YKCFrameTypeCode> frameTypeCode, long timeout, TimeUnit unit) throws Exception {
// 通过桩编号获取channel
ChannelHandlerContext ctx = PileChannelEntity.getChannelByPileSn(pileSn);
String value = ((YKCFrameTypeCode) frameTypeCode).getValue(); // 帧类型名称
@@ -275,7 +275,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
byte[] accountBalanceByteArr = YKCUtils.getPriceByte(chargeAmount.toString(), 2);
byte[] msgBody = Bytes.concat(orderIdByteArr, pileSnByteArr, connectorCodeByteArr, logicCardNumByteArr, physicsCardNumByteArr, accountBalanceByteArr);
this.push(msgBody, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_START_CHARGING_CODE);
try {
this.send(msgBody, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_START_CHARGING_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发充电指令=====】:订单id:{}, 桩号:{}, 枪口号:{}, 逻辑卡号:{}, 物理卡号:{}, 账户余额:{}",
transactionCode, pileSn, BytesUtil.bcd2Str(connectorCodeByteArr), logicCardNum, physicsCardNum, chargeAmount);
}
@@ -291,7 +295,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
String connectorCode = command.getConnectorCode();
// 远程停机
byte[] msgBody = Bytes.concat(BytesUtil.str2Bcd(pileSn), BytesUtil.str2Bcd(connectorCode));
this.push(msgBody, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_STOP_CHARGING_CODE);
try {
this.send(msgBody, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_STOP_CHARGING_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发指令=====】:远程停止充电,桩号:{},枪口号:{}", pileSn, connectorCode);
}
@@ -300,7 +308,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
String pileSn = command.getPileSn();
String connectorCode = command.getConnectorCode();
byte[] msg = BytesUtil.str2Bcd(pileSn + connectorCode);
this.push(msg, pileSn, YKCFrameTypeCode.READ_REAL_TIME_MONITOR_DATA_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.READ_REAL_TIME_MONITOR_DATA_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发指令=====】:获取充电桩:{} 的 {} 枪口实时数据信息", pileSn, connectorCode);
}
@@ -309,7 +321,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
String pileSn = command.getPileSn();
byte[] msg = BytesUtil.str2Bcd(pileSn + Constants.ZERO_ONE);
log.info("【=====平台下发指令=====】:重启充电桩:,{}", pileSn);
this.push(msg, pileSn, YKCFrameTypeCode.REMOTE_RESTART_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.REMOTE_RESTART_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@@ -351,7 +367,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
byte[] msg = Bytes.concat(pileSnByteArr, qrCodeTypeByteArr, qrCodePrefixLengthByteArr, qrCodePrefixByteArr);
// push消息
boolean result = this.push(msg, pileSn, YKCFrameTypeCode.REMOTE_ISSUE_QRCODE_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.REMOTE_ISSUE_QRCODE_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发指令=====】 pileSn:{}, 下发二维码,地址为:{}", pileSn, qrCodePrefix);
}
@@ -373,7 +393,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 拼装msg
byte[] msg = Bytes.concat(pileSnByteArr, dateBytes);
this.push(msg, pileSn, YKCFrameTypeCode.TIME_CHECK_SETTING_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.TIME_CHECK_SETTING_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("[充电桩:{}对时, 时间:{}, CP56Time2a:{}]", pileSn, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date), BytesUtil.binary(dateBytes, 16));
}
@@ -390,7 +414,7 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 拼装msg
byte[] msg = Bytes.concat(pileSnByteArr, dateBytes);
byte[] bytes = this.pushTest(msg, pileSn, YKCFrameTypeCode.TIME_CHECK_SETTING_CODE);
byte[] bytes = this.send(msg, pileSn, YKCFrameTypeCode.TIME_CHECK_SETTING_CODE);
log.info("[充电桩:{}对时, 时间:{}, CP56Time2a:{}], 响应:{}",
pileSn, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date), BytesUtil.binary(dateBytes, 16), BytesUtil.binary(bytes, 16));
}
@@ -408,18 +432,14 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
byte[] messageBody = pileBillingTemplateService.generateBillingTemplateMsgBody(pileSn, billingTemplateVO);
// 发送
if (messageBody != null) {
this.push(messageBody, pileSn, YKCFrameTypeCode.BILLING_TEMPLATE_SETTING_CODE);
try {
this.send(messageBody, pileSn, YKCFrameTypeCode.BILLING_TEMPLATE_SETTING_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
System.out.println(Constants.updateServerPort);
String numHex = Integer.toHexString(21);
byte[] bytes = BytesUtil.hexString2Bytes(numHex);
System.out.println(bytes);
}
@Override
public void pushUpdateFileCommand(UpdateFirmwareCommand command) {
List<String> pileSns = command.getPileSnList();
@@ -475,7 +495,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
byte[] msgBody = Bytes.concat(pileSnByteArr, pileModelType, ratedPowerByteArr, updateServerAddressByteArr,
updateServerPortByteArr, userNameByteArr, passwordByteArr, filePathByteArr, performTypeByteArr, overTimeByteArr);
this.push(msgBody, pileModelInfoVO.getPileSn(), YKCFrameTypeCode.REMOTE_UPDATE_CODE);
try {
this.send(msgBody, pileModelInfoVO.getPileSn(), YKCFrameTypeCode.REMOTE_UPDATE_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发指令=====】:远程更新, 桩号:{}, 类型:{}, 额定功率:{}, 服务器地址:{}, 端口号:{}, 用户名:{}, 密码:{}, 文件路径:{}",
pileModelInfoVO.getPileSn(), pileModelType, BytesUtil.bcd2Str(ratedPowerByteArr), BytesUtil.binary(updateServerAddressByteArr, 16),
BytesUtil.binary(updateServerPortByteArr, 16), BytesUtil.binary(userNameByteArr, 16), BytesUtil.binary(passwordByteArr, 16),
@@ -505,7 +529,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
byte[] msg = Bytes.concat(pileSnByteArr, workingStateByteArr, maxPowerByteArr);
this.push(msg, pileSn, YKCFrameTypeCode.CHARGING_PILE_WORKING_PARAMETER_SETTING_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.CHARGING_PILE_WORKING_PARAMETER_SETTING_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
@@ -532,7 +560,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 拼装msg信息
byte[] msg = Bytes.concat(pileSnByteArr, pileType);
this.push(msg, pileSn, YKCFrameTypeCode.QUERY_PILE_WORK_PARAMS_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.QUERY_PILE_WORK_PARAMS_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
@@ -558,7 +590,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 拼装msg信息
byte[] msg = Bytes.concat(pileSnByteArr, connectorCodeByteArr, logicByteArr, priceByte);
this.push(msg, pileSn, YKCFrameTypeCode.REMOTE_ACCOUNT_BALANCE_UPDATE_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.REMOTE_ACCOUNT_BALANCE_UPDATE_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
@@ -591,7 +627,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 拼装msg信息
byte[] msg = Bytes.concat(pileSnByteArr, connectorCodeByteArr, operateByteArr, obligateByteArr);
this.push(msg, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_GROUND_LOCK_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.REMOTE_CONTROL_GROUND_LOCK_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
@@ -656,7 +696,11 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
reservationTypeByteArr, verifyIdentityByteArr, vin1ByteArr, vin2ByteArr, vin3ByteArr,
reservedStartTimeByteArr, reservedEndTimeByteArr, amountByteArr);
this.push(msg, pileSn, YKCFrameTypeCode.RESERVATION_CHARGING_SETUP_CODE);
try {
this.send(msg, pileSn, YKCFrameTypeCode.RESERVATION_CHARGING_SETUP_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("【=====平台下发指令=====】: 预约充电指令, 交易流水号:{}, 桩编号:{}, 枪口号:{}, 操作:{}, 身份验证:{}, 开始时间:{}, 结束时间:{}, 启动金额:{}",
transactionCode, pileSn, connectorCode, operation, verifyIdentity, DateUtils.formatDateTime(reservedStartTime), DateUtils.formatDateTime(reservedEndTime), amount);