Commit dec88823 by tiger

调通登录认证+心跳+对时服务

1 parent 1dfaf950
Showing 18 changed files with 320 additions and 27 deletions
...@@ -31,11 +31,13 @@ public class ClientMain { ...@@ -31,11 +31,13 @@ public class ClientMain {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// String host = "121.199.192.223"; Ocpp16Helper.scan();
// int port = 8768;
// TcpClient client = getInstance(host, port);
TcpClient client = getInstance(); String host = "121.199.192.223";
int port = 8768;
TcpClient client = getInstance(host, port);
// TcpClient client = getInstance();
Thread.sleep(3000); Thread.sleep(3000);
......
...@@ -154,6 +154,22 @@ public class Ocpp16Helper { ...@@ -154,6 +154,22 @@ public class Ocpp16Helper {
return bcdBytes; return bcdBytes;
} }
public static byte[] binToBytes(String binaryString) {
int length = binaryString.length();
if (length % 8 != 0) {
throw new IllegalArgumentException("Invalid binary string length");
}
byte[] byteArray = new byte[length / 8];
for (int i = 0; i < length; i += 8) {
String byteString = binaryString.substring(i, i + 8);
byte value = (byte) Integer.parseInt(byteString, 2);
byteArray[i / 8] = value;
}
return byteArray;
}
public static byte[] build(Object d) { public static byte[] build(Object d) {
// 创建一个 ByteBuf 对象 // 创建一个 ByteBuf 对象
ByteBuf byteBuf = Unpooled.buffer(Ocpp16Constants.TCP_MAX_LENGTH); ByteBuf byteBuf = Unpooled.buffer(Ocpp16Constants.TCP_MAX_LENGTH);
...@@ -209,11 +225,14 @@ public class Ocpp16Helper { ...@@ -209,11 +225,14 @@ public class Ocpp16Helper {
P p = null; P p = null;
try { try {
// log.info("try parse:{} - {}", type, ByteUtils.toHex(bs));
if (pair != null) {
p = (P) pair.getKey().newInstance(); p = (P) pair.getKey().newInstance();
D d = (D) pair.getValue().newInstance(); D d = (D) pair.getValue().newInstance();
p.setData(d); p.setData(d);
parse(p, bs); parse(p, bs);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -5,6 +5,8 @@ import java.util.Map; ...@@ -5,6 +5,8 @@ import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
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.LoginReqHandler;
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 lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -30,12 +32,15 @@ public final class TcpMapper { ...@@ -30,12 +32,15 @@ public final class TcpMapper {
public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", LoginRspHandler.class); public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", LoginRspHandler.class);
public static TcpMapper CMD_REQ_HEARTBEAT = regist(0x03, "充电桩心跳包", 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, "心跳包应答", null);
// public static TcpMapper CMD_RSP_HEARTBEAT = regist(0x04, "心跳包应答", HeartbeatRspHandler.class);
// TODO 补充完整所有协议 // TODO 补充完整所有协议
public static TcpMapper CMD_NOTIFY_TIME = regist(0x56, "对时设置", TimeNotifyHandler.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) {
......
...@@ -101,7 +101,8 @@ public class TcpClient { ...@@ -101,7 +101,8 @@ public class TcpClient {
sendSync(bs); sendSync(bs);
String ip = getIp(future.channel()); String ip = getIp(future.channel());
log.info("【{}】send: {}", ip, ByteUtils.toHex(bs)); log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
} }
public void sendSync(byte[] bytes) { public void sendSync(byte[] bytes) {
......
...@@ -10,6 +10,7 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper; ...@@ -10,6 +10,7 @@ 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.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.DefaultRspHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.DefaultRspHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer;
import cn.com.glsx.charging.util.ByteUtils;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -40,7 +41,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -40,7 +41,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
try { try {
TcpBase up = Ocpp16Helper.parse(bytes); TcpBase up = Ocpp16Helper.parse(bytes);
if (up == null) { if (up == null) {
log.warn("【{}】null协议:{}", ip, bytes); log.warn("【{}】null协议: 【{}】{}", ip, ByteUtils.getByte(bytes, 5), ByteUtils.toHex(bytes));
return; return;
} }
...@@ -50,6 +51,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -50,6 +51,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<byte[]> {
return; return;
} }
log.info("【{}】【{}】recv1: {}", ip, up.getClass().getSimpleName(), ByteUtils.toHex(bytes));
log.info("【{}】【{}】recv2: {}", ip, up.getClass().getSimpleName(), 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) {
handlerClass = DefaultRspHandler.class; handlerClass = DefaultRspHandler.class;
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* 用于链路状态判断,3 次未收到心跳包视为网络异常,需要重新登陆
* 68(起始标志)0D(数据长度)0001(序列号域)00(加密标志)03(类型)32010200000001
* (桩编码)0x01(枪号:1 号枪)00(枪状态:正常)6890(帧校验域)
*
* @author kome
* @date 2023年11月1日 下午6:00:39
*/
@Getter
@Setter
public class HeartbeatReqData extends TcpData {
/**
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* 枪号 BCD 码 1
*/
private byte gunNo;
/**
* 枪状态 BIN 码 1 0x00:正常 0x01:故障
*/
private byte status;
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* 用于链路状态判断
* 68(起始标志)0D(数据长度)3600(序列号域)00(加密标志)04(类型)55031412782305
* (桩编码)01(枪号) 00(心跳应答)65B2(帧校验域)
*
* @author kome
* @date 2023年11月1日 下午6:00:39
*/
@Getter
@Setter
public class HeartbeatRspData extends TcpData {
/**
* 充电桩id: BCD 码 ,不足 7 位补 0
*/
private byte[] pointId = new byte[7];
/**
* 枪号 BCD 码 1
*/
private byte gunNo;
/**
* 心跳应答 BIN 码 1 置 0
*/
private byte result;
}
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* 68(起始标志)12(数据长度)00DF(序列号域)00(加密标志)56(类型)55031412782305
* (桩编码)98B70E11100314(当前时间:2020-03-16 17:14:47)8A13(帧校验域)
*
* @author kome
* @date 2023年11月1日 下午6:00:39
*/
@Getter
@Setter
public class TimeNotifyData 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; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData;
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.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginRsp;
import io.netty.channel.Channel; import io.netty.channel.Channel;
public class DefaultRspHandler implements TcpHandler<LoginRsp, LoginRsp> { public class DefaultRspHandler implements TcpHandler<TcpRsp<TcpData>, TcpRsp<TcpData>> {
@Override @Override
public LoginRsp handle(Channel channel, LoginRsp req) { public TcpRsp<TcpData> handle(Channel channel, TcpRsp<TcpData> rsp) {
LoginRsp trsp = new LoginRsp(); log.info("【{}】{}", rsp.getClass().getSimpleName(), rsp);
return null;
LoginRspData data = new LoginRspData();
data.setPointId(null);
data.setResult((byte) 0);
// SpringContextUtil.getApplicationContext().getBean(null);
log.info("xxxx");
trsp.setData(data);
return trsp;
} }
} }
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.req;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.HeartbeatRspData;
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 io.netty.channel.Channel;
public class HeartbeatReqHandler implements TcpHandler<HeartbeatReq, HeartbeatRsp> {
@Override
public HeartbeatRsp handle(Channel channel, HeartbeatReq req) {
HeartbeatRsp rsp = new HeartbeatRsp();
HeartbeatRspData data = new HeartbeatRspData();
data.setPointId(req.getData().getPointId());
data.setGunNo(req.getData().getGunNo());
data.setResult((byte) 0);
rsp.setData(data);
return rsp;
}
}
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.protocol.login.HeartbeatRsp;
import io.netty.channel.Channel;
public class HeartbeatRspHandler implements TcpHandler<HeartbeatRsp, HeartbeatRsp> {
@Override
public HeartbeatRsp handle(Channel channel, HeartbeatRsp rsp) {
log.info("收到心跳回应:{}", rsp);
return null;
}
}
...@@ -12,12 +12,12 @@ public class LoginRspHandler implements TcpHandler<LoginRsp, LoginRsp> { ...@@ -12,12 +12,12 @@ public class LoginRspHandler implements TcpHandler<LoginRsp, LoginRsp> {
LoginRsp trsp = new LoginRsp(); LoginRsp trsp = new LoginRsp();
LoginRspData data = new LoginRspData(); LoginRspData data = new LoginRspData();
data.setPointId(null); // data.setPointId(null);
data.setResult((byte) 0); data.setResult((byte) 1);
// SpringContextUtil.getApplicationContext().getBean(null); // SpringContextUtil.getApplicationContext().getBean(null);
log.info("xxxx"); // log.info("xxxx");
trsp.setData(data); trsp.setData(data);
return trsp; return trsp;
......
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.HeartbeatReqData;
/**
* @author kome.jiang
* @date 2022年9月28日 下午2:48:32
*/
public class HeartbeatReq extends TcpReq<HeartbeatReqData> {
public HeartbeatReq() {
super(TcpMapper.CMD_REQ_HEARTBEAT.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.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.HeartbeatRspData;
/**
* 用于链路状态判断
*
* @author kome.jiang
* @date 2022年9月28日 下午2:48:32
*/
public class HeartbeatRsp extends TcpRsp<HeartbeatRspData> {
public HeartbeatRsp() {
super(TcpMapper.CMD_RSP_HEARTBEAT.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());
}
}
...@@ -63,7 +63,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -63,7 +63,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
protected void channelRead0(ChannelHandlerContext ctx, byte[] bytes) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, byte[] bytes) throws Exception {
String ip = TcpServer.getIp(ctx); String ip = TcpServer.getIp(ctx);
log.info("up:{}", ByteUtils.toHex(bytes)); log.info("【{}】up: {}", ip, ByteUtils.toHex(bytes));
// 收到的rsp或者notify // 收到的rsp或者notify
try { try {
......
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.login.HeartbeatReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.login.LoginReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.HeartbeatReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.login.LoginReq;
/**
* 模拟充电桩端,连接云快充平台的测试服务器
* 实现登录,心跳,交互
*
* @author kome
* @date 2023年11月6日 上午9:49:10
*/
public class ClientSimulateTest {
public static void main(String[] args) throws Exception {
Ocpp16Helper.scan();
String host = "121.199.192.223";
int port = 8768;
TcpClient client = ClientMain.getInstance(host, port);
Thread.sleep(3000);
LoginReq req = new LoginReq();
req.setData(new LoginReqData());
req.getData().setPointId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPointId()));
req.getData().setPointType((byte) 0);
req.getData().setGunCount((byte) 2);
req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion()));
req.getData().setProtocolVersion((byte) 10);
Ocpp16Helper.fillLen(req);
req.setCrc(Ocpp16Helper.calculateCrc(req));
client.sendSync(req);
// 定期心跳
while (true) {
HeartbeatReq heartbeat = new HeartbeatReq();
HeartbeatReqData hdata = new HeartbeatReqData();
hdata.setPointId(req.getData().getPointId());
hdata.setStatus((byte) 0);
heartbeat.setData(hdata);
Ocpp16Helper.fillLen(heartbeat);
heartbeat.setCrc(Ocpp16Helper.calculateCrc(heartbeat));
client.sendSync(heartbeat);
Thread.sleep(10000);
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!