Commit 5d602426 by 叶立庆

Merge branch 'master' of http://192.168.3.233:8090/platform/platform-charging-server

# Conflicts:
#	platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/base/TcpMapper.java
2 parents 27dc2ac1 7d9c581a
Showing 25 changed files with 703 additions and 147 deletions
...@@ -209,36 +209,30 @@ public class Ocpp16Helper { ...@@ -209,36 +209,30 @@ public class Ocpp16Helper {
} }
} }
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
// 自动补充len字段和crc字段 // 自动补充len字段和crc字段
if (d instanceof TcpBase) { if (d instanceof TcpBase) {
TcpBase b = ((TcpBase) d); 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); byte len = (byte) (data.length - 4);
byteBuf.setByte(1, len);
b.setLen(len); b.setLen(len);
ByteUtils.putByte(data, 1, len);
// 补充crc // 补充crc
byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array(); byte[] data1 = ByteUtils.getBytes(data, 2, data.length - 4);
short crc = CrcUtils.calculateCRC(data); short crc = CrcUtils.calculateCRC(data1);
byteBuf.setShort(byteBuf.readableBytes() - 2, crc);
b.setCrc(crc); b.setCrc(crc);
ByteUtils.putShort(data, data.length - 2, crc);
// log.info("【{}】build crc:{}", d.getClass().getSimpleName(), crc); // log.info("【{}】build crc:{}", d.getClass().getSimpleName(), crc);
} }
// 读取数据
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
// 释放 ByteBuf
byteBuf.release();
return data; return data;
} }
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base;
import java.util.Map;
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.ShutdownMachineReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.resp.ShutdownMachineRespHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.interactive.req.StartMachineReqHandler; 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.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.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.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.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.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.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 com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/** /**
* 管理与server的交互 * 管理与server的交互
* *
...@@ -53,7 +55,7 @@ public final class TcpMapper { ...@@ -53,7 +55,7 @@ public final class TcpMapper {
public static TcpMapper CMD_RSP_MODEL_CHARGING = regist(0x0A, "计费模型请求应答", null); public static TcpMapper CMD_RSP_MODEL_CHARGING = regist(0x0A, "计费模型请求应答", null);
public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.class); // public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.class);
// 运营交互帧类型码数据定义 0x31~0x47 // 运营交互帧类型码数据定义 0x31~0x47
public static TcpMapper FTCD_RSP_REMOTE_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class); public static TcpMapper FTCD_RSP_REMOTE_START_CHARGING = regist(0x33, "远程启动充电命令回复", StartMachineRespHandler.class);
...@@ -68,6 +70,15 @@ public final class TcpMapper { ...@@ -68,6 +70,15 @@ public final class TcpMapper {
public static TcpMapper FTCD_RSP_TRANSACTION_CONFIRMATION = regist(0x40, "交易记录确认", null); public static TcpMapper FTCD_RSP_TRANSACTION_CONFIRMATION = regist(0x40, "交易记录确认", null);
public static TcpMapper CMD_REQ_PARAM_SETTING = regist(0x52, "充电桩工作参数设置", ParamSettingReqHandler.class);
public static TcpMapper CMD_RSP_PARAM_SETTING = regist(0x51, "充电桩工作参数设置应答", ParamSettingRspHandler.class);
public static TcpMapper CMD_REQ_TIME_SETTING = regist(0x56, "对时设置", TimeSettingReqHandler.class);
public static TcpMapper CMD_RSP_TIME_SETTING = regist(0x55, "对时设置应答", TimeSettingRspHandler.class);
public static TcpMapper fromType(byte type) { public static TcpMapper fromType(byte type) {
TcpMapper c = mappers.get(type); TcpMapper c = mappers.get(type);
if (c == null) { if (c == null) {
......
...@@ -98,11 +98,14 @@ public class TcpClient { ...@@ -98,11 +98,14 @@ public class TcpClient {
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> void sendSync(T data) { 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); byte[] bs = Ocpp16Helper.build(data);
sendSync(bs); sendSync(bs);
String ip = getIp(future.channel());
log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs)); log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data); log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
} }
......
...@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -54,9 +54,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes)); log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes));
log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up); log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), up);
// if (!Ocpp16Helper.verifyCrc(up)) { if (!Ocpp16Helper.verifyCrc(up)) {
// log.warn("【警告】crc校验不通过:{}", up); log.warn("【警告】crc校验不通过:{}", up);
// } }
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass(); Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null && up instanceof TcpRsp) { if (handlerClass == null && up instanceof TcpRsp) {
...@@ -71,9 +71,14 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -71,9 +71,14 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up); TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) { if (rsp != null) {
// String rspJson = rsp.build(); // seq字段处理
// ctx.channel().writeAndFlush(rspJson); rsp.setSeq(up.getSeq());
// log.info("【{}】send: {}", ip, StrUtil.sub(rspJson, 0, 600));
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) { } catch (Exception e) {
log.error("【{}】{}", ip, bytes, e); log.error("【{}】{}", ip, bytes, e);
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Data;
/**
* 充电桩工作参数设置
*
* 68(起始标志) 0D(数据长度) 0008(序列号域) 00(加密标志) 52(类型) 32010200000001
* (桩编码) 0x01(是否允许工作) 0x00(充电桩最大允许输出功率) 6890(帧校验域)
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
@Data
public class ParamSettingReqData extends TcpData {
/**
* 桩编号
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* 是否允许工作
* BIN 码
* 0x00 表示允许正常工作
* 0x01 表示停止使用,锁定充电桩
*/
private byte authStatus;
/**
* 充电桩最大允许输出功率
* BIN 码
* 1BIN 表示 1%,最大 100%,最小 30%
*/
private byte maxPower;
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Data;
/**
* 充电桩工作参数设置应答
*
* 68(起始标志) 0C(数据长度) 0008(序列号域) 00(加密标志) 51(类型) 32010200000001
* (桩编码) 0x01(设置结果) C1A9(帧校验域)
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
@Data
public class ParamSettingRspData extends TcpData {
/**
* 桩编号
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* 设置结果
* BIN 码
* 0x00 失败 0x01 成功
*/
private byte result;
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Data;
/**
* 对时设置
* 68(起始标志) 12(数据长度) 00DF(序列号域) 00(加密标志) 56(类型) 55031412782305
* (桩编码) 98B70E11100314(当前时间: 2020-03-16 17:14:47) 8A13(帧校验域)
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
@Data
public class TimeSettingReqData extends TcpData {
/**
* 桩编号
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* BIN码,当前时间,CP56Time2a 格式
* 参考 ByteUtils.parseCp56Time2a(ts)
* 98B70E11100314(当前时间:2020-03-16 17:14:47)
*
* 时间转十六进制字符串》十六进制字符串转byte[] (用HexUtil.decodeHex)
*/
private byte[] time = new byte[7];
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Data;
/**
* 对时设置应答
*
* 68(起始标志) 12(数据长度) A101(序列号域) 00(加密标志) 55(类型) 55031412782305
* (桩编码) 98B70E11100314(当前时间: 2020-03-16 17:14:47) 0E9B(帧校验域)
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
@Data
public class TimeSettingRspData extends TcpData {
/**
* 桩编号
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* BIN码,当前时间,CP56Time2a 格式
* 参考 ByteUtils.parseCp56Time2a(ts)
* 98B70E11100314(当前时间:2020-03-16 17:14:47)
*/
private byte[] time = new byte[7];
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.req;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler; 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.LoginReq; 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.LoginRsp;
import io.netty.channel.Channel; import io.netty.channel.Channel;
...@@ -9,7 +10,12 @@ public class LoginReqHandler implements TcpHandler<LoginReq, LoginRsp> { ...@@ -9,7 +10,12 @@ public class LoginReqHandler implements TcpHandler<LoginReq, LoginRsp> {
@Override @Override
public LoginRsp handle(Channel channel, LoginReq req) { public LoginRsp handle(Channel channel, LoginReq req) {
LoginRspData data = new LoginRspData();
data.setPointId(req.getData().getPointId());
data.setResult((byte) 0);
LoginRsp trsp = new LoginRsp(); LoginRsp trsp = new LoginRsp();
trsp.setData(data);
// trsp.setReqKey(req.getReqKey()); // trsp.setReqKey(req.getReqKey());
return trsp; return trsp;
......
...@@ -20,7 +20,7 @@ public class LoginRspHandler implements TcpHandler<LoginRsp, LoginRsp> { ...@@ -20,7 +20,7 @@ public class LoginRspHandler implements TcpHandler<LoginRsp, LoginRsp> {
// log.info("xxxx"); // log.info("xxxx");
trsp.setData(data); trsp.setData(data);
return trsp; return null;
} }
} }
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param;
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.param.ParamSettingReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param.ParamSettingRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param.ParamSettingReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param.ParamSettingRsp;
import io.netty.channel.Channel;
/**
* 充电桩工作参数设置 请求处理器
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
public class ParamSettingReqHandler implements TcpHandler<ParamSettingReq, ParamSettingRsp> {
@Override
public ParamSettingRsp handle(Channel channel, ParamSettingReq paramSettingReq) {
ParamSettingReqData data = paramSettingReq.getData();
log.info("充电桩工作参数设置 pointId {}", Ocpp16Helper.bcdToStr(data.getPointId()));
log.info("authStatus {}", data.getAuthStatus());
log.info("maxPower {}", data.getMaxPower());
ParamSettingRspData resultData = new ParamSettingRspData();
resultData.setPointId(data.getPointId());
resultData.setResult((byte) 1);
ParamSettingRsp rsp = new ParamSettingRsp();
rsp.setData(resultData);
return rsp;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.param;
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.param.ParamSettingRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param.ParamSettingRsp;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
/**
* 处理充电桩工作参数设置应答
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
@Slf4j
public class ParamSettingRspHandler implements TcpHandler<ParamSettingRsp, ParamSettingRsp> {
@Override
public ParamSettingRsp handle(Channel channel, ParamSettingRsp paramSettingRsp) {
ParamSettingRspData data = paramSettingRsp.getData();
log.info("充电桩工作参数设置应答 pointId {}", Ocpp16Helper.bcdToStr(data.getPointId()));
log.info("result {}", data.getResult());
if (data.getResult() != 1) {
log.info("充电桩 {} 工作参数设置 失败!", Ocpp16Helper.bcdToStr(data.getPointId()));
}
return null;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time;
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.time.TimeSettingReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time.TimeSettingRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time.TimeSettingReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time.TimeSettingRsp;
import cn.com.glsx.charging.util.ByteUtils;
import cn.hutool.core.util.HexUtil;
import io.netty.channel.Channel;
/**
* 对时设置 请求处理器
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/7
**/
public class TimeSettingReqHandler implements TcpHandler<TimeSettingReq, TimeSettingRsp> {
@Override
public TimeSettingRsp handle(Channel channel, TimeSettingReq timeSettingReq) {
TimeSettingReqData reqData = timeSettingReq.getData();
byte[] time = reqData.getTime();
byte[] pointId = reqData.getPointId();
TimeSettingRspData data = new TimeSettingRspData();
data.setPointId(pointId);
data.setTime(time);
String hexStr = HexUtil.encodeHexStr(time);
log.info("对时设置 pointId {}", Ocpp16Helper.bcdToStr(pointId));
log.info("hexStr {}", hexStr);
log.info("时间 {}", ByteUtils.parseCp56Time2aStr(time));
TimeSettingRsp rsp = new TimeSettingRsp();
rsp.setData(data);
return rsp;
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.time;
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.time.TimeSettingRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time.TimeSettingRsp;
import cn.com.glsx.charging.util.ByteUtils;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
/**
* 对时设置应答处理器
*
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/7
**/
@Slf4j
public class TimeSettingRspHandler implements TcpHandler<TimeSettingRsp, TimeSettingRsp> {
@Override
public TimeSettingRsp handle(Channel channel, TimeSettingRsp timeSettingRsp) {
TimeSettingRspData data = timeSettingRsp.getData();
log.info("对时设置响应 pointId {} time {}", Ocpp16Helper.bcdToStr(data.getPointId()), ByteUtils.parseCp56Time2aStr(data.getTime()) /*DateUtils.datetimeToStr(ByteUtils.parseCp56Time2a(timeSettingRsp.getData().getTime()))*/);
return null;
}
}
...@@ -11,6 +11,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.TimeNotifyData; ...@@ -11,6 +11,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.TimeNotifyData;
public class TimeNotify extends TcpReq<TimeNotifyData> { public class TimeNotify extends TcpReq<TimeNotifyData> {
public TimeNotify() { public TimeNotify() {
super(TcpMapper.CMD_NOTIFY_TIME.getType()); // super(TcpMapper.CMD_NOTIFY_TIME.getType());
} }
} }
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param;
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.param.ParamSettingReqData;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
public class ParamSettingReq extends TcpReq<ParamSettingReqData> {
public ParamSettingReq() {
super(TcpMapper.CMD_REQ_PARAM_SETTING.getType());
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param.ParamSettingRspData;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
public class ParamSettingRsp extends TcpRsp<ParamSettingRspData> {
public ParamSettingRsp() {
super(TcpMapper.CMD_RSP_PARAM_SETTING.getType());
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time;
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.time.TimeSettingReqData;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
public class TimeSettingReq extends TcpReq<TimeSettingReqData> {
public TimeSettingReq() {
super(TcpMapper.CMD_REQ_TIME_SETTING.getType());
}
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time.TimeSettingRspData;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/6
**/
public class TimeSettingRsp extends TcpRsp<TimeSettingRspData> {
public TimeSettingRsp() {
super(TcpMapper.CMD_RSP_TIME_SETTING.getType());
}
}
...@@ -90,6 +90,9 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -90,6 +90,9 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up); TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) { if (rsp != null) {
// seq字段处理
rsp.setSeq(up.getSeq());
byte[] rspBytes = Ocpp16Helper.build(rsp); byte[] rspBytes = Ocpp16Helper.build(rsp);
ctx.channel().writeAndFlush(rspBytes); ctx.channel().writeAndFlush(rspBytes);
......
package cn.com.glsx.charging.util; package cn.com.glsx.charging.util;
import java.util.zip.CRC32;
import com.google.common.hash.HashCode; import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
...@@ -37,48 +35,64 @@ public class CrcUtils { ...@@ -37,48 +35,64 @@ public class CrcUtils {
return (short) hashCode.asInt(); return (short) hashCode.asInt();
} }
public static short calculateCRC2(byte[] data) { // public static short calculateCRC2(byte[] data) {
CRC32 crc32 = new CRC32(); // CRC32 crc32 = new CRC32();
crc32.update(data); // crc32.update(data);
return (short) crc32.getValue(); // return (short) crc32.getValue();
} // }
public static short calculateCRC3(byte[] data) { public static short calculateCRC(byte[] data) {
int polynomial = 0x180D; // CRC 多项式
int crc = 0xFFFF; // 初始值为 0xFFFF int crc = 0xFFFF; // 初始值为 0xFFFF
for (byte b : data) { for (byte b : data) {
crc ^= b & 0xFF; int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF;
for (int i = 0; i < 8; i++) { crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF;
if ((crc & 0x0001) != 0) {
crc = (crc >> 1) ^ polynomial;
} else {
crc >>= 1;
}
}
} }
return (short) crc; return (short) crc;
} }
public static short calculateCRC(byte[] data) { public static short calculateCRC2(byte[] data) {
int crc = 0xFFFF; // 初始值为 0xFFFF int crc = 0xFFFF;
for (byte b : data) { for (byte b : data) {
int index = ((crc >> 8) ^ (b & 0xFF)) & 0xFF; crc = ((crc >> 8) ^ gabyCRCHi[(crc ^ b) & 0xFF]) & 0xFFFF;
crc = ((crc << 8) ^ (gabyCRCHi[index] << 8) ^ (gabyCRCLo[index] & 0xFF)) & 0xFFFF; crc = ((crc >> 8) ^ gabyCRCLo[(crc & 0xFF)]) & 0xFFFF;
} }
return (short) crc; return (short) crc;
} }
public static void main(String[] args) { private static final int POLYNOMIAL = 0x180D;
byte[] data = { 0x01, 0x02, 0x03 }; // Example data
int crc = calculateCRC(data);
System.out.printf("CRC: 0x%04X\n", crc);
short c = (short) 0xDA4C; public static int calculateCRC3(byte[] data) {
System.out.println(c); 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; 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.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; 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.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.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData; 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.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq; 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; ...@@ -23,83 +16,6 @@ import cn.com.glsx.charging.util.CrcUtils;
* @date 2023年11月6日 上午9:49:10 * @date 2023年11月6日 上午9:49:10
*/ */
public class ClientSimulateTest { 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 { public static void main(String[] args) throws Exception {
Ocpp16Helper.scan(); 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;
public class CrcTest {
@BeforeAll
public static void test_scan() {
Ocpp16Helper.scan();
}
@Test
public void test_LoginReq_crc() throws Exception {
String hex = "6822000100015503141278230500020AFFFFFF5000000000000000000000000000000000B9BA";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_LoginRsp_crc() {
String hex = "680C000100025503141278230500DEB0";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
LoginRsp protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatReq_crc() {
String hex = "680D000200035503141278230500006599";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatReq protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_HeartbeatRsp_crc() {
String hex = "680D000300045503141278230500002980";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
HeartbeatRsp protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
@Test
public void test_TimeNotify_crc() {
String hex = "68120002005655031412782305F055020E070B178676";
System.out.println(hex);
byte[] bytes = ByteUtils.fromHex(hex);
TimeNotify protocol = Ocpp16Helper.parse(bytes);
System.out.println("expect: " + protocol.getCrc());
byte[] data = ByteUtils.getBytes(bytes, 2, bytes.length - 4);
System.out.println("actual: " + CrcUtils.calculateCRC(data));
bytes = Ocpp16Helper.build(protocol);
System.out.println(ByteUtils.toHex(bytes));
System.out.println(protocol);
Assert.assertEquals(protocol.getCrc(), CrcUtils.calculateCRC(data));
}
}
package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.param.ParamSettingReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.param.ParamSettingReq;
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:
* @Author xiehx
* @Date 2023/11/7
**/
public class ParamSettingTest {
String host = "127.0.0.1";
int port = 8888;
public void serverStart() throws Exception{
// 启动服务端
ServerMain.start(host, port);
Thread.sleep(10000);
}
@Test
public void test() throws Exception {
serverStart();
// 充电桩工作参数设置
setParam();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter message (e.g., 'message'): ");
String message = scanner.nextLine();
}
}
private void setParam() {
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(host);
if (proxyInfo != null) {
ParamSettingReqData data = new ParamSettingReqData();
data.setPointId(Ocpp16Helper.strToBcd("55031412782305", data.getPointId()));
data.setAuthStatus((byte) 0);
data.setMaxPower((byte) 100);
ParamSettingReq req = new ParamSettingReq();
req.setData(data);
byte[] bs = Ocpp16Helper.build(req);
proxyInfo.getChannel().writeAndFlush(bs);
} else {
System.out.println("Client " + host + " is not connected!");
}
}
}
package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.time.TimeSettingReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.time.TimeSettingReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyHelper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.DateUtils;
import cn.hutool.core.util.HexUtil;
import org.junit.Test;
import java.util.Date;
import java.util.Scanner;
/**
* @Title:
* @Description:
* @Author xiehx
* @Date 2023/11/7
**/
public class TimeSettingTest {
String host = "127.0.0.1";
int port = 8888;
public void serverStart() throws Exception {
// 启动服务端
ServerMain.start(host, port);
Thread.sleep(10000);
}
@Test
public void test() throws Exception {
serverStart();
// 对时设置
setTime();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter message (e.g., 'message'): ");
String message = scanner.nextLine();
}
}
private void setTime() {
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(host);
if (proxyInfo != null) {
// 对时设置
Date date = new Date();
String cp56Time2a = ByteUtils.cp56Time2a(date);
TimeSettingReqData data = new TimeSettingReqData();
data.setPointId(Ocpp16Helper.strToBcd("55031412782305", data.getPointId()));
data.setTime(HexUtil.decodeHex(cp56Time2a));
System.out.println("Cp56Time2a: " + cp56Time2a);
System.out.println("Cp56Time2a原值: " + DateUtils.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
TimeSettingReq req = new TimeSettingReq();
req.setData(data);
byte[] bs = Ocpp16Helper.build(req);
proxyInfo.getChannel().writeAndFlush(bs);
} else {
System.out.println("Client " + host + " is not connected!");
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!