Commit 28fd649c by tiger

LoginReq

1 parent 81f2091a
Showing 13 changed files with 127 additions and 168 deletions
package cn.com.glsx.charging.modules.ocpp.ocpp16;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.TcpHandlerHelper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.data.LoginReqData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginReq;
public class ClientMain {
......@@ -11,14 +12,15 @@ public class ClientMain {
if (instance == null) {
System.setProperty("file.encoding", "UTF-8");
TcpHandlerHelper.scan();
String host = "localhost";
int port = 8888;
host = Ocpp16Constants.getTcpHost();
port = Ocpp16Constants.getTcpPort();
host = "121.199.192.223";
port = 8768;
TcpClient p = new TcpClient(host, port);
p.start();
......@@ -27,7 +29,17 @@ public class ClientMain {
return instance;
}
public static void main(String[] args) {
getInstance();
public static void main(String[] args) throws Exception {
TcpClient client = getInstance();
Thread.sleep(3000);
LoginReq req = new LoginReq();
req.setData(new LoginReqData());
byte[] bs = Ocpp16Helper.build(req);
client.sendSync(bs);
// 定期心跳
}
}
......@@ -12,7 +12,7 @@ import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.data.LoginReqData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginReq;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.ClassUtil;
import cn.com.glsx.charging.util.ClassUtils;
......@@ -66,11 +66,11 @@ public class Ocpp16Helper {
}
}
public static short generateSeq(String channelId) {
AtomicInteger ai = seqFactorys.get(channelId);
if (ai == null) {
ai = new AtomicInteger();
seqFactorys.put(channelId, ai);
}
int rs = ai.incrementAndGet();
return (short) rs; // 溢出自动清零
......@@ -93,6 +93,16 @@ public class Ocpp16Helper {
return crc == b.getCrc();
}
// TODO
public static String bcdToStr(byte[] bs) {
return new String(bs);
}
public static byte[] strToBcd(String s, byte[] bs) {
// TODO 不足 位补 0
return bs;
}
public static byte[] build(Object d) {
// 创建一个 ByteBuf 对象
ByteBuf byteBuf = Unpooled.buffer(Ocpp16Constants.TCP_MAX_LENGTH);
......@@ -108,19 +118,25 @@ public class Ocpp16Helper {
Object v = f.get(d);
if (v instanceof byte[]) {
byteBuf.writeBytes((byte[]) v);
} else if (v instanceof Byte) {
byteBuf.writeByte((byte) v);
} else if (v instanceof Short) {
byteBuf.writeByte((short) v);
byteBuf.writeShort((short) v);
} else if (v instanceof Integer) {
byteBuf.writeByte((int) v);
byteBuf.writeInt((int) v);
} else if (v instanceof String) {
// byteBuf.writeCharSequence((CharSequence) v, Charset.forName("UTF-8")); //TODO 字符串暂时按byte[]来定义,以方便确定长度
} else if (v instanceof TcpData) {
byte[] bs = build(v);
byteBuf.writeBytes(bs);
}
log.debug("build:{} field:{} value:{}", d.getClass().getSimpleName(), f.getName(), v);
// log.info("build:{} field:{} value:{} -- {}", d.getClass().getSimpleName(), f.getName(), v, byteBuf.readableBytes());
} catch (Exception e) {
e.printStackTrace();
}
......@@ -140,11 +156,23 @@ public class Ocpp16Helper {
byte type = ByteUtils.getByte(bs);
Pair<Class<? extends TcpBase>, Class<? extends TcpData>> pair = protocols.get(type);
P p = null;
try {
P p = (P) pair.getKey().newInstance();
p = (P) pair.getKey().newInstance();
D d = (D) pair.getValue().newInstance();
p.setData(d);
parse(p, bs);
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
public static <D> Object parse(Object p, byte[] bs) {
try {
int offset = 0;
List<Field> fields = ClassUtils.getAllFields(pair.getKey());
List<Field> fields = ClassUtils.getAllFields(p.getClass());
for (Field f : fields) {
try {
if (f.getAnnotation(JsonIgnore.class) != null) {
......@@ -153,6 +181,8 @@ public class Ocpp16Helper {
f.setAccessible(true);
Object v = f.get(p);
// System.err.println(p.getClass() + " -- " + f.getName() + " -- " + v + " total:" + bs.length + " offset:" + offset);
if (v instanceof byte[]) {
byte[] vs = ByteUtils.getBytes(bs, offset, ((byte[]) v).length);
offset += vs.length;
......@@ -174,9 +204,12 @@ public class Ocpp16Helper {
f.set(p, vs);
} else if (v instanceof TcpData) {
byte vs = ByteUtils.getByte(bs, offset);
offset += 1;
f.set(p, vs);
byte[] build = build(v);
byte[] vs = ByteUtils.getBytes(bs, offset, build.length);
offset += vs.length;
D d = (D) parse(v, vs);
f.set(p, d);
}
log.debug("build:{} field:{} value:{}", p.getClass().getSimpleName(), f.getName(), v);
......@@ -192,29 +225,19 @@ public class Ocpp16Helper {
public static void main(String[] args) {
LoginReq req = new LoginReq();
List<Field> fields = ClassUtils.getAllFields(req.getClass());
for (Field f : fields) {
if (f.getName().equals("data")) {
try {
f.setAccessible(true);
Object v = f.get(req);
System.out.println(v);
System.out.println(v instanceof TcpData);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
req.setData(new LoginReqData());
fillLen(req);
req.setCrc(calculateCrc(req));
verifyCrc(req);
System.out.println(req);
System.out.println(build(req).length);
scan();
byte[] bs = build(req);
TcpBase<TcpData> base = parse(bs);
System.out.println(base);
}
}
\ No newline at end of file
package cn.com.glsx.charging.modules.ocpp.ocpp16;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.TcpHandlerHelper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.ProxyHelper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.TcpServer;
......@@ -13,8 +12,10 @@ public class ServerMain {
System.setProperty("file.encoding", "UTF-8");
if (server == null && !lock) {
TcpHandlerHelper.scan();
Ocpp16Helper.scan();
// DefaultTcpNotifyHandler.startKafaka();
//
ProxyHelper.startManager();
synchronized (lock) {
......
......@@ -5,6 +5,7 @@ import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.handler.LoginRspHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
......@@ -24,7 +25,7 @@ public final class TcpMapper {
public static TcpMapper CMD_BLANK = regist(0x00, "空白协议", null);
public static TcpMapper CMD_REQ_LOGIN = regist(0x01, "充电桩登录认证", null);
public static TcpMapper CMD_REQ_LOGIN = regist(0x01, "充电桩登录认证", LoginRspHandler.class);
public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", null);
......
......@@ -102,19 +102,19 @@ public class TcpClient {
sendSync(json);
// if (!(data instanceof HeartbeatDllNotify)) {
String ip = getIp(future.channel());
log.info("【{}】send: {}", ip, json);
// }
String ip = getIp(future.channel());
log.info("【{}】send: {}", ip, json);
// }
}
public void sendSync(String json) {
public void sendSync(byte[] bytes) {
try {
if (future == null || future.channel() == null) {
return;
}
future.channel().writeAndFlush(json);
future.channel().writeAndFlush(bytes);
} catch (Exception e) {
log.error(json, e);
log.error("" + bytes, e);
}
}
......
......@@ -8,7 +8,7 @@ import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.TcpServer;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.handler.DefaultTcpRspHandler;
import cn.com.glsx.charging.util.ClassUtils;
......@@ -98,7 +98,6 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
}
public static void main(String[] args) {
TcpHandlerHelper.scan();
int type = new LoginReq().getType();
Class<? extends TcpReq> reqClass = (Class<? extends TcpReq>) TcpHandlerHelper.getTcpProtocolClass(type);
......
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.handler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.data.LoginRspData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginRsp;
import io.netty.channel.Channel;
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) 0);
// SpringContextUtil.getApplicationContext().getBean(null);
log.info("xxxx");
trsp.setData(data);
return trsp;
}
}
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.data;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpData;
import lombok.Getter;
import lombok.Setter;
/**
* 68(起始标志)22(数据长度)0000(序列号域)00(加密标志)01(类型)55031412782305
* (桩编码)00(桩类型)02(充电枪数量)0F(通信协议版本:V1.5)56342E312E353000(程
* 序版本:v4.1.50)01(网络链接类型)01010101010101010101(sim 卡)04(运营商)675A(帧
* 校验域
* 68(起始标志)0C(数据长度)0000(序列号域)00(加密标志)02(类型)55 03 14 12 78 23
* 05(桩编码)00(登陆结果:成功)DA 4C(帧校验域)
*
* @author kome
* @date 2023年11月1日 下午6:00:39
......@@ -25,47 +21,8 @@ public class LoginRspData extends TcpData {
private byte[] pointId = new byte[7];
/**
* 0 表示直流桩,1 表示交流桩
*/
private byte pointType;
/**
* 充电枪数量
*/
private byte gunCount;
/**
* 通信协议版本 BIN 码 1 版本号乘 10,v1.0 表示 0x0A
*/
private byte protocolVersion;
/**
* 程序版本 ASCII 码 8 不足 8 位补零
*/
@JsonSerialize
private byte[] softVersion = new byte[8];
/**
* 网络链接类型
* 0x00 SIM 卡
* 0x01 LAN
* 0x02 WAN
* 0x03 其他
*/
private byte network;
/**
* Sim 卡 BCD 码 10 不足 10 位补零,取不到置零
*/
private byte[] sim = new byte[10];
/**
* 运营商 BIN 码
* 0x00 移动
* 0x02 电信
* 0x03 联通
* 0x04 其他
* 登陆结果: 0x00:登陆成功 0x01:登陆失败
*/
private byte operator;
private byte result;
}
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl;
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpMapper;
......
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl;
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpRsp;
......
......@@ -3,17 +3,12 @@ package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import cn.com.glsx.charging.modules.ocpp.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.TcpHandlerHelper;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.notify.HeartbeatTcpNotify;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.handler.DefaultTcpRspHandler;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.handler.DefaultReqHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
......@@ -41,6 +36,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
log.info("##############################################################");
ProxyHelper.removeProxyInfo(ctx.channel());
// rest sequence
}
......@@ -50,24 +46,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
// 收到的rsp或者notify
try {
JSON parse = JSONUtil.parse(json);
int type = Integer.valueOf(ObjectUtil.defaultIfNull(parse.getByPath("type"), "0").toString());
int subtype = Integer.valueOf(ObjectUtil.defaultIfNull(parse.getByPath("data.msgtype"), "-1").toString());
TcpBase up = TcpHandlerHelper.parse(type, subtype, json);
if (up == null) {
log.warn("【{}】空Json:{}", ip, json);
return;
}
if (up instanceof HeartbeatTcpNotify) {
log.debug("【{}】recv: {} | {}", ip, StrUtil.sub(json, 0, 600), up.getClass().getSimpleName());
} else {
log.info("-------------------------------------");
log.info("【{}】recv: {} | {}", ip, StrUtil.sub(json, 0, 600), up.getClass().getSimpleName());
log.debug("【{}】解析: {} {}:{} ", ip, StrUtil.sub(up.build(), 0, 600), up.getClass().getSimpleName(),
up.getData() == null ? "" : up.getData().getClass().getSimpleName());
}
TcpBase<TcpData> up = Ocpp16Helper.parse(bytes);
TcpMapper mapper = TcpMapper.fromType(up.getType());
if (mapper == null) {
......@@ -76,8 +55,8 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
}
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null && up instanceof TcpRsp) {
handlerClass = DefaultTcpRspHandler.class;
if (handlerClass == null) {
handlerClass = DefaultReqHandler.class;
}
if (handlerClass == null) {
log.info("【{}】TcpMapper没有配置handler: {} - {}", ip, mapper.getType(), mapper.getRemark());
......@@ -88,12 +67,12 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
TcpBase rsp = handler.handle(ctx.channel(), up);
if (rsp != null) {
String rspJson = rsp.build();
ctx.channel().writeAndFlush(rspJson);
log.info("【{}】send: {}", ip, StrUtil.sub(json, 0, 600));
byte[] rspBytes = Ocpp16Helper.build(rsp);
ctx.channel().writeAndFlush(rspBytes);
log.info("【{}】send: {}", ip, rsp);
}
} catch (Exception e) {
log.error("【{}】{}", ip, json, e);
log.error("【{}】{}", ip, e);
} finally {
ProxyHelper.addProxyInfo(ctx.channel());
}
......
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.client.handler;
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.handler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpData;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginRsp;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginRsp;
import cn.hutool.core.codec.Base64;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LoginReqHandler implements TcpHandler<LoginReq, LoginRsp> {
public class DefaultReqHandler implements TcpHandler<TcpBase<TcpData>, LoginRsp> {
@Override
public LoginRsp handle(Channel channel, LoginReq req) {
public LoginRsp handle(Channel channel, TcpBase<TcpData> req) {
LoginRsp trsp = new LoginRsp();
// trsp.setReqKey(req.getReqKey());
......
package cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.server.handler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.base.TcpHandler;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.impl.LoginRsp;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginReq;
import cn.com.glsx.charging.modules.ocpp.ocpp16.tcp.protocol.LoginRsp;
import cn.hutool.core.codec.Base64;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
......@@ -15,46 +15,6 @@ public class LoginReqHandler implements TcpHandler<LoginReq, LoginRsp> {
LoginRsp trsp = new LoginRsp();
// trsp.setReqKey(req.getReqKey());
// LoginQrcodeTcpData tdata = new LoginQrcodeTcpData();
// trsp.setData(tdata);
//
// try {
// DllClient client = DllFacade.openNewWxwork(req.getData().getUserId(), 0, "TcpReq");
//
// if (client == null) {
// trsp.setError(TcpErrorEnum.ERROR_OPERATION_FAILED.getError());
// return trsp;
// }
//
// SelfInfoDllData userInfo = client.getUserInfo();
// if (userInfo == null || userInfo.getUserId() == null) {
// int i = 50;
// while (client.getQrcodeData() == null && i-- > 0) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// LoginQrcodeDllData data = client.getQrcodeData();
// if (data == null || data.getBase64EncodePath() == null) {
// trsp.setError(TcpErrorEnum.ERROR_OPERATION_FAILED.getError());
// return trsp;
// }
// String imagePath = data.getBase64EncodePath();
// log.info("loginQrcodePath:{}", imagePath);
// byte[] bytes = FileUtil.readBytes(new File(imagePath));
// tdata.setBase64EncodeQrcode(Base64.encode(bytes));
// tdata.setLoginQrcodeKey(data.getLoginQrcodeKey());
// } else {
// BeanUtils.copyProperties(userInfo, tdata);
// }
// } catch (Exception e) {
// e.printStackTrace();
// //
// trsp.setError(TcpErrorEnum.ERROR_USERID_NOT_LOGIN.getError());
// }
return trsp;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!