Commit 710fad26 by chenxi

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/base/TcpMapper.java
2 parents 38eee007 930dc6bc
Showing 31 changed files with 476 additions and 240 deletions
......@@ -209,36 +209,30 @@ public class Ocpp16Helper {
}
}
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
// 自动补充len字段和crc字段
if (d instanceof TcpBase) {
TcpBase b = ((TcpBase) d);
// TODO seq字段处理
if (b.getSeq() == 0) {
short seq = generateSeq("Test");
b.setSeq(seq);
byteBuf.setShort(2, seq);
}
// 补充长度字段
byte len = (byte) (byteBuf.readableBytes() - 4);
byteBuf.setByte(1, len);
byte len = (byte) (data.length - 4);
b.setLen(len);
ByteUtils.putByte(data, 1, len);
// 补充crc
byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array();
short crc = CrcUtils.calculateCRC(data);
byteBuf.setShort(byteBuf.readableBytes() - 2, crc);
byte[] data1 = ByteUtils.getBytes(data, 2, data.length - 4);
short crc = CrcUtils.calculateCRC(data1);
b.setCrc(crc);
ByteUtils.putShort(data, data.length - 2, crc);
// log.info("【{}】build crc:{}", d.getClass().getSimpleName(), crc);
}
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
return data;
}
......
......@@ -2,22 +2,21 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base;
import java.util.Map;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.realtime.data.UploadRealTimeDataHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param.ParamSettingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param.ParamSettingRspHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time.TimeSettingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time.TimeSettingRspHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req.ShutdownMachineReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req.StartMachineReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp.ShutdownMachineRespHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp.StartMachineRespHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.notify.TimeNotifyHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.HeartbeatReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.LoginReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelChargingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req.ModelValReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.rsp.LoginRspHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param.ParamSettingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param.ParamSettingRspHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time.TimeSettingReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time.TimeSettingRspHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
......@@ -39,47 +38,44 @@ public final class TcpMapper {
public static TcpMapper CMD_REQ_LOGIN = regist(0x01, "充电桩登录认证", LoginReqHandler.class);
public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", LoginRspHandler.class);
public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", null);
public static TcpMapper CMD_REQ_HEARTBEAT = regist(0x03, "充电桩心跳包", HeartbeatReqHandler.class);
public static TcpMapper CMD_RSP_HEARTBEAT = regist(0x04, "心跳包应答", null);
// public static TcpMapper CMD_RSP_HEARTBEAT = regist(0x04, "心跳包应答", HeartbeatRspHandler.class);
// TODO 补充完整所有协议
public static TcpMapper CMD_REQ_MODEL_VALIDATION = regist(0x05, "计费模型验证请求", ModelValReqHandler.class);
public static TcpMapper CMD_RSP_MODEL_VALIDATION = regist(0x06, "计费模型验证应答", null);
public static TcpMapper CMD_REQ_MODEL_CHECK = regist(0x05, "计费模型验证请求", ModelValReqHandler.class);
public static TcpMapper CMD_REQ_MODEL_CHARGING = regist(0x09, "充电桩计费模型请求", ModelChargingReqHandler.class);
public static TcpMapper CMD_RSP_MODEL_CHECK = regist(0x06, "计费模型验证应答", null);
public static TcpMapper CMD_RSP_MODEL_CHARGING = regist(0x0A, "计费模型请求应答", null);
public static TcpMapper CMD_REQ_MODEL_QUERY = regist(0x09, "充电桩计费模型请求", ModelChargingReqHandler.class);
public static TcpMapper CMD_REQ_UPLOAD_REAL_TIME_DATA = regist(0x13, "读取实时检测数据", UploadRealTimeDataHandler.class);
public static TcpMapper CMD_RSP_MODEL_QUERY = regist(0x0A, "计费模型请求应答", null);
// public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.class);
// 运营交互帧类型码数据定义 0x31~0x47
public static TcpMapper FTCD_RSP_REMOTE_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class);
public static TcpMapper CMD_RSP_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class);
public static TcpMapper FTCD_REQ_REMOTE_CONTROL_STARTUP = regist(0x34, "运营平台远程控制启机", StartMachineReqHandler.class);
public static TcpMapper CMD_REQ_START_CHARGING = regist(0x34, "运营平台远程控制启机", StartMachineReqHandler.class);
public static TcpMapper FTCD_RSP_REMOTE_SHUTDOWN = regist(0x35, "远程停机命令回复", null);
public static TcpMapper CMD_RSP_STOP_CHARGING = regist(0x35, "远程停机命令回复", ShutdownMachineRespHandler.class);
public static TcpMapper FTCD_REQ_REMOTE_SHUTDOWN = regist(0x36, "运营平台远程停机", null);
public static TcpMapper CMD_REQ_STOP_CHARGING = regist(0x36, "运营平台远程停机", ShutdownMachineReqHandler.class);
public static TcpMapper FTCD_REQ_TRANSACTION_RECORDS = regist(0x3B, "交易记录", null);
public static TcpMapper CMD_REQ_TRADE_UPLOAD = regist(0x3B, "交易记录上报", null);
public static TcpMapper FTCD_RSP_TRANSACTION_CONFIRMATION = regist(0x40, "交易记录确认", null);
public static TcpMapper CMD_RSP_TRADE_UPLOAD = regist(0x40, "交易记录上报确认", null);
public static TcpMapper CMD_REQ_PARAM_SETTING = regist(0x52, "充电桩工作参数设置", ParamSettingReqHandler.class);
public static TcpMapper CMD_REQ_CFG_DOWNLOAD = regist(0x52, "充电桩工作参数设置", ParamSettingReqHandler.class);
public static TcpMapper CMD_RSP_PARAM_SETTING = regist(0x51, "充电桩工作参数设置应答", ParamSettingRspHandler.class);
public static TcpMapper CMD_RSP_CFG_DOWNLOAD = regist(0x51, "充电桩工作参数设置应答", ParamSettingRspHandler.class);
public static TcpMapper CMD_REQ_TIME_SETTING = regist(0x56, "对时设置", TimeSettingReqHandler.class);
public static TcpMapper CMD_REQ_TIME_CHECK = regist(0x56, "对时设置", TimeSettingReqHandler.class);
public static TcpMapper CMD_RSP_TIME_SETTING = regist(0x55, "对时设置应答", TimeSettingRspHandler.class);
public static TcpMapper CMD_RSP_TIME_CHECK = regist(0x55, "对时设置应答", TimeSettingRspHandler.class);
public static TcpMapper fromType(byte type) {
......
......@@ -98,11 +98,14 @@ public class TcpClient {
@SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> void sendSync(T data) {
String ip = getIp(future.channel());
// TODO seq字段处理
data.setSeq(Ocpp16Helper.generateSeq(ip));
byte[] bs = Ocpp16Helper.build(data);
sendSync(bs);
String ip = getIp(future.channel());
log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
}
......
......@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes));
log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up);
// if (!Ocpp16Helper.verifyCrc(up)) {
// log.warn("【警告】crc校验不通过:{}", up);
// }
if (!Ocpp16Helper.verifyCrc(up)) {
log.warn("【警告】crc校验不通过:{}", up);
}
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null && up instanceof TcpRsp) {
......@@ -71,8 +71,14 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) {
// seq字段处理
rsp.setSeq(up.getSeq());
byte[] bs = Ocpp16Helper.build(rsp);
ctx.channel().writeAndFlush(bs);
log.info("【{}】【{}】send1: {} ", ip, rsp.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, rsp.getClass().getSimpleName(), rsp);
}
} catch (Exception e) {
log.error("【{}】{}", ip, bytes, e);
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* @Description 运营平台远程停机
* 功能 当用户通过远程停止充电时,发送本命令,如 APP 停止充电
* 样例报文 68(起始标志)0C(数据长度)0003(序列号域)00(加密标志)36(类型)32010200000001
* (桩编码)01(枪号:1 枪)C1A9(帧校验域)
* @Author yelq
* @Date 2023/11/7-16:10
* @Version 1.0
*/
@Getter
@Setter
public class ShutdownMachineReqData extends TcpData {
/**
* 桩编号
* 数据类型: (BCD码)
* 长度(Byte):7
* 备注:不足7位补0
*/
private byte[] pileNumber = new byte[7];
/**
* 枪号
* 数据类型: (BCD码)
* 长度(Byte):1
* 备注:
*/
private byte gunNumber;
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* @Description 远程停止充电命令回复
* 功能 远程停止充电命令回复,平台发送 0x36 后即关闭订单,接收到停机指令后设备务必保证停机。
* 样例报文 68(起始标志)0E(数据长度)0003(序列号域)00(加密标志)35(类型)32010200000001
* (桩编码)01(枪号:1 枪)01(停止结果:成功)00(失败原因:00)907E(帧校验域)
* @Author yelq
* @Date 2023/11/7-16:10
* @Version 1.0
*/
@Getter
@Setter
public class ShutdownMachineRespData extends TcpData {
/**
* 桩编号
* 数据类型:byte[] (BCD码)
* 长度(Byte):7
* 备注:不足7位补0
*/
private byte[] pileNumber = new byte[7];
/**
* 枪号
* 数据类型:byte[] (BCD码)
* 长度(Byte):1
* 备注:
*/
private byte gunNumber;
/**
* 停止结果
* 数据类型:byte[] (BCD码)
* 长度(Byte):1
* 备注:0x00失败 0x01成功
*/
private byte shutdownResult;
/**
* 失败原因
* 数据类型:byte[] (BIN码)
* 长度(Byte):1
* 备注:
* 0x00 无
* 0x01 设备编号不匹配
* 0x02 枪未处于充电状态
* 0x03 其他
*/
private byte failureReason;
}
......@@ -9,7 +9,7 @@ public class DefaultRspHandler implements TcpHandler<TcpRsp<TcpData>, TcpRsp<Tcp
@Override
public TcpRsp<TcpData> handle(Channel channel, TcpRsp<TcpData> rsp) {
log.info("【{}】{}", rsp.getClass().getSimpleName(), rsp);
log.info("【{}】{} 啥也不干", rsp.getClass().getSimpleName(), rsp);
return null;
}
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.ShutdownMachineRespData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.ShutdownMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.resp.ShutdownMachineResp;
import io.netty.channel.Channel;
/**
* @Description 运营平台远程停机处理类
* @Author yelq
* @Date 2023/11/7-16:10
* @Version 1.0
*/
public class ShutdownMachineReqHandler implements TcpHandler<ShutdownMachineReq, ShutdownMachineResp> {
// * 功能 远程停止充电命令回复,平台发送 0x36 后即关闭订单,接收到停机指令后设备务必保证停机。
// * 样例报文 68(起始标志)0E(数据长度)0003(序列号域)00(加密标志)35(类型)32010200000001
// * (桩编码)01(枪号:1 枪)01(停止结果:成功)00(失败原因:00)907E(帧校验域)
@Override
public ShutdownMachineResp handle(Channel channel, ShutdownMachineReq shutdownMachineReq) {
/**请求日志打印*/
log.info("桩编号:" + Ocpp16Helper.bcdToStr(shutdownMachineReq.getData().getPileNumber()));
log.info("枪号:" + shutdownMachineReq.getData().getGunNumber());
/**请求日志打印*/
ShutdownMachineResp resp = new ShutdownMachineResp();
ShutdownMachineRespData data = new ShutdownMachineRespData();
data.setPileNumber(Ocpp16Helper.strToBcd("32010200000001", data.getPileNumber()));
data.setGunNumber((byte) 1);
data.setShutdownResult((byte) 1);
data.setFailureReason((byte) 0);
resp.setData(data);
return resp;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.ShutdownMachineRespData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.resp.ShutdownMachineResp;
import io.netty.channel.Channel;
/**
* @Description 远程停止充电命令回复处理类
* @Author yelq
* @Date 2023/11/7-16:10
* @Version 1.0
*/
public class ShutdownMachineRespHandler implements TcpHandler<ShutdownMachineResp, ShutdownMachineResp> {
@Override
public ShutdownMachineResp handle(Channel channel, ShutdownMachineResp shutdownMachineResp) {
ShutdownMachineRespData data = shutdownMachineResp.getData();
log.info("桩编号:{}", Ocpp16Helper.bcdToStr(data.getPileNumber()));
log.info("枪号:{}", data.getGunNumber());
log.info("停止结果:{}", data.getShutdownResult());
log.info("失败原因:{}", data.getFailureReason());
System.out.println("客户端已经收到消息了");
return null;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.notify;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.TimeNotify;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.DateUtils;
import io.netty.channel.Channel;
/**
* 充电桩收到服务端下发的对时命令
*
* @author kome
* @date 2023年11月6日 上午10:19:34
*/
public class TimeNotifyHandler implements TcpHandler<TimeNotify, TcpRsp> {
@Override
public TcpRsp handle(Channel channel, TimeNotify notify) {
//
log.info("【服务器时间】{}", DateUtils.datetimeToStr(ByteUtils.parseCp56Time2a(notify.getData().getTime())));
return null;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.rsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import io.netty.channel.Channel;
......@@ -9,17 +8,7 @@ public class LoginRspHandler implements TcpHandler<LoginRsp, LoginRsp> {
@Override
public LoginRsp handle(Channel channel, LoginRsp req) {
LoginRsp trsp = new LoginRsp();
LoginRspData data = new LoginRspData();
// data.setPointId(null);
data.setResult((byte) 1);
// SpringContextUtil.getApplicationContext().getBean(null);
// log.info("xxxx");
trsp.setData(data);
// 充电桩端收到登录成功后,啥也不干
return null;
}
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.ShutdownMachineReqData;
/**
* @Description todo
* @Author yelq
* @Date 2023/11/7-16:21
* @Version 1.0
*/
public class ShutdownMachineReq extends TcpReq<ShutdownMachineReqData> {
public ShutdownMachineReq() {
super(TcpMapper.FTCD_REQ_REMOTE_SHUTDOWN.getType());
}
}
......@@ -12,6 +12,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.StartMachine
*/
public class StartMachineReq extends TcpReq<StartMachineReqData> {
public StartMachineReq() {
super(TcpMapper.FTCD_REQ_REMOTE_CONTROL_STARTUP.getType());
super(TcpMapper.CMD_REQ_START_CHARGING.getType());
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.resp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.ShutdownMachineRespData;
/**
* @Description todo
* @Author yelq
* @Date 2023/11/7-16:21
* @Version 1.0
*/
public class ShutdownMachineResp extends TcpReq<ShutdownMachineRespData> {
public ShutdownMachineResp() {
super(TcpMapper.FTCD_RSP_REMOTE_SHUTDOWN.getType());
}
}
......@@ -12,6 +12,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.StartMachine
*/
public class StartMachineResp extends TcpRsp<StartMachineRspData> {
public StartMachineResp() {
super(TcpMapper.FTCD_RSP_REMOTE_START_CHARGING.getType());
super(TcpMapper.CMD_RSP_START_CHARGING.getType());
}
}
......@@ -14,6 +14,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.ModelValReqData;
public class ModelChargingReq extends TcpReq<ModelChargingReqData> {
public ModelChargingReq() {
super(TcpMapper.CMD_REQ_MODEL_CHARGING.getType());
super(TcpMapper.CMD_REQ_MODEL_QUERY.getType());
}
}
......@@ -13,6 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.ModelChargingRspDa
public class ModelChargingRsp extends TcpRsp<ModelChargingRspData> {
public ModelChargingRsp() {
super(TcpMapper.CMD_RSP_MODEL_CHARGING.getType());
super(TcpMapper.CMD_RSP_MODEL_QUERY.getType());
}
}
......@@ -13,6 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.ModelValReqData;
public class ModelValReq extends TcpReq<ModelValReqData> {
public ModelValReq() {
super(TcpMapper.CMD_REQ_MODEL_VALIDATION.getType());
super(TcpMapper.CMD_REQ_MODEL_CHECK.getType());
}
}
......@@ -14,6 +14,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.ModelValRspData;
public class ModelValRsp extends TcpRsp<ModelValRspData> {
public ModelValRsp() {
super(TcpMapper.CMD_RSP_MODEL_VALIDATION.getType());
super(TcpMapper.CMD_RSP_MODEL_CHECK.getType());
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.TimeNotifyData;
/**
* @author kome.jiang
* @date 2022年9月28日 下午2:48:32
*/
public class TimeNotify extends TcpReq<TimeNotifyData> {
public TimeNotify() {
// super(TcpMapper.CMD_NOTIFY_TIME.getType());
}
}
......@@ -13,6 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param.ParamSettingReqDat
public class ParamSettingReq extends TcpReq<ParamSettingReqData> {
public ParamSettingReq() {
super(TcpMapper.CMD_REQ_PARAM_SETTING.getType());
super(TcpMapper.CMD_REQ_CFG_DOWNLOAD.getType());
}
}
......@@ -12,7 +12,7 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param.ParamSettingRspDat
**/
public class ParamSettingRsp extends TcpRsp<ParamSettingRspData> {
public ParamSettingRsp() {
super(TcpMapper.CMD_RSP_PARAM_SETTING.getType());
super(TcpMapper.CMD_RSP_CFG_DOWNLOAD.getType());
}
}
......@@ -13,6 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time.TimeSettingReqData;
public class TimeSettingReq extends TcpReq<TimeSettingReqData> {
public TimeSettingReq() {
super(TcpMapper.CMD_REQ_TIME_SETTING.getType());
super(TcpMapper.CMD_REQ_TIME_CHECK.getType());
}
}
......@@ -12,7 +12,7 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time.TimeSettingRspData;
**/
public class TimeSettingRsp extends TcpRsp<TimeSettingRspData> {
public TimeSettingRsp() {
super(TcpMapper.CMD_RSP_TIME_SETTING.getType());
super(TcpMapper.CMD_RSP_TIME_CHECK.getType());
}
}
......@@ -90,6 +90,9 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) {
// seq字段处理
rsp.setSeq(up.getSeq());
byte[] rspBytes = Ocpp16Helper.build(rsp);
ctx.channel().writeAndFlush(rspBytes);
......
package cn.com.glsx.charging.util;
import java.util.zip.CRC32;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
......@@ -37,48 +35,64 @@ public class CrcUtils {
return (short) hashCode.asInt();
}
public static short calculateCRC2(byte[] data) {
CRC32 crc32 = new CRC32();
crc32.update(data);
return (short) crc32.getValue();
}
// public static short calculateCRC2(byte[] data) {
// CRC32 crc32 = new CRC32();
// crc32.update(data);
// return (short) crc32.getValue();
// }
public static short calculateCRC3(byte[] data) {
int polynomial = 0x180D; // CRC 多项式
public static short calculateCRC(byte[] data) {
int crc = 0xFFFF; // 初始值为 0xFFFF
for (byte b : data) {
crc ^= b & 0xFF;
for (int i = 0; i < 8; i++) {
if ((crc & 0x0001) != 0) {
crc = (crc >> 1) ^ polynomial;
} else {
crc >>= 1;
}
}
int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF;
crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF;
}
return (short) crc;
}
public static short calculateCRC(byte[] data) {
int crc = 0xFFFF; // 初始值为 0xFFFF
public static short calculateCRC2(byte[] data) {
int crc = 0xFFFF;
for (byte b : data) {
int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF;
crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF;
crc = ((crc >> 8) ^ gabyCRCHi[(crc ^ b) & 0xFF]) & 0xFFFF;
crc = ((crc >> 8) ^ gabyCRCLo[(crc & 0xFF)]) & 0xFFFF;
}
return (short) crc;
}
public static void main(String[] args) {
byte[] data = { 0x01, 0x02, 0x03 }; // Example data
int crc = calculateCRC(data);
System.out.printf("CRC: 0x%04X\n", crc);
private static final int POLYNOMIAL = 0x180D;
short c = (short) 0xDA4C;
System.out.println(c);
public static int calculateCRC3(byte[] data) {
int crc = 0xFFFF;
for (byte b : data) {
crc ^= b & 0xFF;
for (int i = 0; i < 8; i++) {
if ((crc & 0x0001) == 0x0001) {
crc = (crc >> 1) ^ POLYNOMIAL;
} else {
crc >>= 1;
}
}
}
return crc & 0xFFFF;
}
public static void main(String[] args) {
String hex = "68120003005655031412782305C832290D070B174270";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
System.out.println("expect:" + ByteUtils.getShort(bytes, bytes.length - 2));
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual:" + CrcUtils.calculateCRC(data));
System.out.println("actual1:" + CrcUtils.calculateCRC1(data));
System.out.println("actual2:" + CrcUtils.calculateCRC2(data));
System.out.println("actual3:" + CrcUtils.calculateCRC3(data));
}
......
package cn.com.glsx.charging.gbt.ocpp16;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.StartMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
/**
* 模拟充电桩端,连接云快充平台的测试服务器
......@@ -23,83 +16,6 @@ import cn.com.glsx.charging.util.CrcUtils;
* @date 2023年11月6日 上午9:49:10
*/
public class ClientSimulateTest {
@Test
public void Test() throws Exception {
// 6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "6830007C0034550314127823050120180619144446805503141278230501000000100000057300000000D14B0A54A08601004622";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
StartMachineReq rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
System.out.println("actual1: " + CrcUtils.calculateCRC1(data));
System.out.println("actual2: " + CrcUtils.calculateCRC2(data));
System.out.println("actual3: " + CrcUtils.calculateCRC3(data));
}
@Test
public void test_LoginReq_crc() throws Exception {
Ocpp16Helper.scan();
String hex = "6822000000015503141278230500020F56342E312E353000010101010101010101010104675A";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
System.out.println(protocol);
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
System.out.println("actual1: " + CrcUtils.calculateCRC1(data));
System.out.println("actual2: " + CrcUtils.calculateCRC2(data));
System.out.println("actual3: " + CrcUtils.calculateCRC3(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
}
@Test
public void testCrc() {
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "680C000000025503141278230500DA4C";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
Assert.assertEquals(rsp.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_LoginRsp_Crc() {
Ocpp16Helper.scan();
// 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
String hex = "680C000100025503141278230500DEB0";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp rsp = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + rsp.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
Assert.assertEquals(rsp.getCrc(), CrcUtils.calculateCRC(data));
}
public static void main(String[] args) throws Exception {
Ocpp16Helper.scan();
......
package cn.com.glsx.charging.gbt.ocpp16;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.TimeNotify;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.CrcUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CrcTest {
@BeforeAll
public static void test_scan() {
Ocpp16Helper.scan();
}
@Test
public void test_LoginReq_crc() throws Exception {
String hex = "6822000100015503141278230500020AFFFFFF5000000000000000000000000000000000B9BA";
log.info(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginReq protocol = Ocpp16Helper.parse(bytes);
log.info("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
log.info("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
log.info(ByteUtils.toHex(bytes));
log.info("{}", protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_LoginRsp_crc() {
String hex = "680C000100025503141278230500DEB0";
log.info(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp protocol = Ocpp16Helper.parse(bytes);
log.info("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
log.info("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
log.info(ByteUtils.toHex(bytes));
log.info("{}", protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatReq_crc() {
String hex = "680D000200035503141278230500006599";
log.info(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatReq protocol = Ocpp16Helper.parse(bytes);
log.info("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
log.info("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
log.info(ByteUtils.toHex(bytes));
log.info("{}", protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatRsp_crc() {
String hex = "680D000300045503141278230500002980";
log.info(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatRsp protocol = Ocpp16Helper.parse(bytes);
log.info("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
log.info("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
log.info(ByteUtils.toHex(bytes));
log.info("{}", protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_TimeNotify_crc() {
String hex = "68120002005655031412782305F055020E070B178676";
log.info(hex);
byte[] bytes = ByteUtils.fromHex(hex);
TimeNotify protocol = Ocpp16Helper.parse(bytes);
log.info("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
log.info("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
log.info(ByteUtils.toHex(bytes));
log.info("{}", protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
}
package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.ShutdownMachineReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.interactive.StartMachineReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.ShutdownMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.interactive.req.StartMachineReq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
/**
* @Description 运营交互帧类型码数据定义协议测试类
* @Author yelq
* @Date 2023/11/7-15:19
* @Version 1.0
*/
@RunWith(MockitoJUnitRunner.class)
public class InteractiveTest {
@Test
public void shutdownMachineTest() throws Exception {
Ocpp16Helper.scan();
TcpClient client = ClientMain.getInstance("localhost", 8888);
Thread.sleep(3000);
ShutdownMachineReq req = new ShutdownMachineReq();
ShutdownMachineReqData data = new ShutdownMachineReqData();
data.setGunNumber((byte) 1);
data.setPileNumber(Ocpp16Helper.strToBcd("32010200000001",data.getPileNumber()));
req.setData(data);
client.sendSync(req);
Thread.sleep(3000);
System.out.println("=========================");
}
@Test
public void startMachineReqTest() throws Exception {
Ocpp16Helper.scan();
TcpClient client = ClientMain.getInstance("localhost", 8888);
Thread.sleep(3000);
StartMachineReq req = new StartMachineReq();
StartMachineReqData data = new StartMachineReqData();
data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("32010200000000111511161555350261", data.getTransactionSerialNumber()));
data.setPileNumber(Ocpp16Helper.strToBcd("32010200000001",data.getPileNumber()));
data.setGunNumber((byte) 1);
data.setLogicalCardNumber(Ocpp16Helper.strToBcd("1000000573",data.getLogicalCardNumber()));
data.setPhysicalCardNumber(Ocpp16Helper.strToBcd("D14B0A54",data.getPhysicalCardNumber()));
data.setAccountBalance(Ocpp16Helper.strToBcd("00000064",data.getAccountBalance()));
req.setData(data);
client.sendSync(req);
System.out.println("=========================");
}
}
......@@ -8,8 +8,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyHelper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo;
import org.junit.Test;
import java.util.Scanner;
/**
* @Title:
* @Description:
......@@ -21,10 +19,10 @@ public class ParamSettingTest {
String host = "127.0.0.1";
int port = 8888;
public void serverStart() throws Exception{
private void serverStart() throws Exception{
// 启动服务端
ServerMain.start(host, port);
Thread.sleep(10000);
Thread.sleep(15000);
}
@Test
......@@ -32,11 +30,7 @@ public class ParamSettingTest {
serverStart();
// 充电桩工作参数设置
setParam();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter message (e.g., 'message'): ");
String message = scanner.nextLine();
}
Thread.sleep(20000);
}
private void setParam() {
......
......@@ -12,7 +12,6 @@ import cn.hutool.core.util.HexUtil;
import org.junit.Test;
import java.util.Date;
import java.util.Scanner;
/**
* @Title:
......@@ -25,10 +24,10 @@ public class TimeSettingTest {
String host = "127.0.0.1";
int port = 8888;
public void serverStart() throws Exception {
private void serverStart() throws Exception {
// 启动服务端
ServerMain.start(host, port);
Thread.sleep(10000);
Thread.sleep(15000);
}
@Test
......@@ -36,11 +35,7 @@ public class TimeSettingTest {
serverStart();
// 对时设置
setTime();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter message (e.g., 'message'): ");
String message = scanner.nextLine();
}
Thread.sleep(20000);
}
private void setTime() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!