Commit c1b14ba1 by tiger

Login协议调试

1 parent a161291b
...@@ -9,17 +9,18 @@ public class ClientMain { ...@@ -9,17 +9,18 @@ public class ClientMain {
private static TcpClient instance; private static TcpClient instance;
public static TcpClient getInstance() { public static TcpClient getInstance() {
if (instance == null) { String host = "localhost";
System.setProperty("file.encoding", "UTF-8"); int port = 8888;
String host = "localhost"; host = Ocpp16Constants.getTcpHost();
int port = 8888; port = Ocpp16Constants.getTcpPort();
host = Ocpp16Constants.getTcpHost(); return getInstance(host, port);
port = Ocpp16Constants.getTcpPort(); }
host = "121.199.192.223"; public static TcpClient getInstance(String host, int port) {
port = 8768; if (instance == null) {
System.setProperty("file.encoding", "UTF-8");
TcpClient p = new TcpClient(host, port); TcpClient p = new TcpClient(host, port);
p.start(); p.start();
...@@ -30,14 +31,31 @@ public class ClientMain { ...@@ -30,14 +31,31 @@ 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";
// int port = 8768;
// TcpClient client = getInstance(host, port);
TcpClient client = getInstance(); TcpClient client = getInstance();
Thread.sleep(3000); Thread.sleep(3000);
LoginReq req = new LoginReq(); LoginReq req = new LoginReq();
req.setData(new LoginReqData()); req.setData(new LoginReqData());
byte[] bs = Ocpp16Helper.build(req); req.getData().setPointId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPointId()));
client.sendSync(bs); 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) {
Thread.sleep(300);
client.sendSync(req);
}
// 定期心跳 // 定期心跳
......
...@@ -37,7 +37,7 @@ public class Ocpp16Helper { ...@@ -37,7 +37,7 @@ public class Ocpp16Helper {
public static void scan() { public static void scan() {
try { try {
String[] path = { "cn.com.glsx.charging.modules.ocpp.ocpp16.tcp" }; String[] path = { "cn.com.glsx.charging.modules.gbt.ocpp16.tcp" };
{ {
Class pClass = TcpBase.class; Class pClass = TcpBase.class;
List<Class<?>> ps = ClassUtil.getAllAssignedClass(pClass, path); List<Class<?>> ps = ClassUtil.getAllAssignedClass(pClass, path);
...@@ -95,12 +95,63 @@ public class Ocpp16Helper { ...@@ -95,12 +95,63 @@ public class Ocpp16Helper {
// TODO // TODO
public static String bcdToStr(byte[] bs) { public static String bcdToStr(byte[] bs) {
return new String(bs); StringBuilder sb = new StringBuilder();
for (byte b : bs) {
int high = (b & 0xf0) >>> 4;
int low = b & 0x0f;
sb.append(high);
sb.append(low);
}
return sb.toString();
}
public static byte[] strToBcd(String input, byte[] bcdBytes) {
int len = input.length();
int mod = len % 2;
if (mod != 0) {
input = "0" + input; // 补齐偶数位
len = input.length();
}
for (int i = 0; i < len; i += 2) {
char highChar = input.charAt(i);
char lowChar = input.charAt(i + 1);
int high = Character.digit(highChar, 10);
int low = Character.digit(lowChar, 10);
byte bcdByte = (byte) ((high << 4) | low);
bcdBytes[i / 2] = bcdByte;
}
return bcdBytes;
} }
public static byte[] strToBcd(String s, byte[] bs) { public static byte[] strToAscii(String input, byte[] bcdBytes) {
// TODO 不足 位补 0 int len = input.length();
return bs; int mod = len % 2;
if (mod != 0) {
input = "0" + input; // 补齐偶数位
len = input.length();
}
for (int i = 0; i < len; i += 2) {
char highChar = input.charAt(i);
char lowChar = input.charAt(i + 1);
int high = Character.digit(highChar, 10);
int low = Character.digit(lowChar, 10);
byte bcdByte = (byte) ((high << 4) | low);
bcdBytes[i / 2] = bcdByte;
}
return bcdBytes;
} }
public static byte[] build(Object d) { public static byte[] build(Object d) {
...@@ -153,7 +204,7 @@ public class Ocpp16Helper { ...@@ -153,7 +204,7 @@ public class Ocpp16Helper {
} }
public static <D extends TcpData, P extends TcpBase<D>> P parse(byte[] bs) { public static <D extends TcpData, P extends TcpBase<D>> P parse(byte[] bs) {
byte type = ByteUtils.getByte(bs); byte type = ByteUtils.getByte(bs, 5);
Pair<Class<? extends TcpBase>, Class<? extends TcpData>> pair = protocols.get(type); Pair<Class<? extends TcpBase>, Class<? extends TcpData>> pair = protocols.get(type);
P p = null; P p = null;
...@@ -210,7 +261,6 @@ public class Ocpp16Helper { ...@@ -210,7 +261,6 @@ public class Ocpp16Helper {
D d = (D) parse(v, vs); D d = (D) parse(v, vs);
f.set(p, d); f.set(p, d);
} }
log.debug("build:{} field:{} value:{}", p.getClass().getSimpleName(), f.getName(), v); log.debug("build:{} field:{} value:{}", p.getClass().getSimpleName(), f.getName(), v);
} catch (Exception e) { } catch (Exception e) {
...@@ -220,7 +270,7 @@ public class Ocpp16Helper { ...@@ -220,7 +270,7 @@ public class Ocpp16Helper {
} catch (Exception e) { } catch (Exception e) {
} }
return null; return p;
} }
public static void main(String[] args) { public static void main(String[] args) {
......
package cn.com.glsx.charging.modules.gbt.ocpp16; package cn.com.glsx.charging.modules.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyHelper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer;
public class ServerMain { public class ServerMain {
...@@ -16,7 +15,7 @@ public class ServerMain { ...@@ -16,7 +15,7 @@ public class ServerMain {
// DefaultTcpNotifyHandler.startKafaka(); // DefaultTcpNotifyHandler.startKafaka();
// //
ProxyHelper.startManager(); // ProxyHelper.startManager();
synchronized (lock) { synchronized (lock) {
server = new TcpServer(host, port); server = new TcpServer(host, port);
......
...@@ -18,7 +18,7 @@ public class TcpBase<D extends TcpData> { ...@@ -18,7 +18,7 @@ public class TcpBase<D extends TcpData> {
/** /**
* 起始标识符代表一帧数据的开始,固定为 0x68 * 起始标识符代表一帧数据的开始,固定为 0x68
*/ */
protected byte type = 0x68; protected byte start = 0x68;
/** /**
* 数据域字节数,数据域长度不超过 200 字节。不加密时为原数据长度,加密时,为加密后数据长度。其值为“序列号域+加密标志+帧类型标志+消息体”字节数之和。 * 数据域字节数,数据域长度不超过 200 字节。不加密时为原数据长度,加密时,为加密后数据长度。其值为“序列号域+加密标志+帧类型标志+消息体”字节数之和。
...@@ -39,7 +39,7 @@ public class TcpBase<D extends TcpData> { ...@@ -39,7 +39,7 @@ public class TcpBase<D extends TcpData> {
/** /**
*  帧类型标志定义了上下行数据帧。 *  帧类型标志定义了上下行数据帧。
*/ */
protected byte direction = 0; protected byte type = 0;
private D data; private D data;
......
...@@ -5,6 +5,7 @@ import java.util.Map; ...@@ -5,6 +5,7 @@ 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.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;
import lombok.Getter; import lombok.Getter;
...@@ -25,9 +26,9 @@ public final class TcpMapper { ...@@ -25,9 +26,9 @@ public final class TcpMapper {
public static TcpMapper CMD_BLANK = regist(0x00, "空白协议", null); public static TcpMapper CMD_BLANK = regist(0x00, "空白协议", null);
public static TcpMapper CMD_REQ_LOGIN = regist(0x01, "充电桩登录认证", LoginRspHandler.class); public static TcpMapper CMD_REQ_LOGIN = regist(0x01, "充电桩登录认证", LoginReqHandler.class);
public static TcpMapper CMD_RSP_LOGIN = regist(0x02, "登录认证应答", null); 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, "充电桩心跳包", null);
......
...@@ -2,8 +2,9 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client; ...@@ -2,8 +2,9 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
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.JacksonUtils; import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.Task; import cn.com.glsx.charging.util.Task;
import cn.com.glsx.charging.util.TaskExecutor; import cn.com.glsx.charging.util.TaskExecutor;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
...@@ -17,10 +18,8 @@ import io.netty.channel.nio.NioEventLoopGroup; ...@@ -17,10 +18,8 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender; import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -50,10 +49,10 @@ public class TcpClient { ...@@ -50,10 +49,10 @@ public class TcpClient {
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4)); // pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 0, 4, 0, 4)); pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TcpClientHandler()); pipeline.addLast("handler", new TcpClientHandler());
} }
}); });
...@@ -98,18 +97,18 @@ public class TcpClient { ...@@ -98,18 +97,18 @@ public class TcpClient {
} }
public <T> void sendSync(T data) { public <T> void sendSync(T data) {
String json = JacksonUtils.nodefaultMapper.toJson(data); byte[] bs = Ocpp16Helper.build(data);
sendSync(json);
sendSync(bs);
// if (!(data instanceof HeartbeatDllNotify)) {
String ip = getIp(future.channel()); String ip = getIp(future.channel());
log.info("【{}】send: {}", ip, json); log.info("【{}】send: {}", ip, ByteUtils.toHex(bs));
// }
} }
public void sendSync(byte[] bytes) { public void sendSync(byte[] bytes) {
try { try {
if (future == null || future.channel() == null) { if (future == null || future.channel() == null) {
log.info("future null ...");
return; return;
} }
future.channel().writeAndFlush(bytes); future.channel().writeAndFlush(bytes);
......
...@@ -16,7 +16,6 @@ import io.netty.channel.nio.NioEventLoopGroup; ...@@ -16,7 +16,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.bytes.ByteArrayDecoder; import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder; import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.Getter; import lombok.Getter;
...@@ -57,8 +56,8 @@ public class TcpServer { ...@@ -57,8 +56,8 @@ public class TcpServer {
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4)); // pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 4, 0)); pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0));
pipeline.addLast("decoder", new ByteArrayDecoder()); pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder()); pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TcpServerHandler()); pipeline.addLast("handler", new TcpServerHandler());
......
...@@ -9,6 +9,7 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpData; ...@@ -9,6 +9,7 @@ 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.base.TcpMapper; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpMapper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.DefaultReqHandler; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.DefaultReqHandler;
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,10 +41,29 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -40,10 +41,29 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
} }
// @Override
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// // super.channelRead(ctx, msg);
// System.out.println(msg.getClass());
// if (msg instanceof ByteBuf) {
// ByteBuf byteBuf = (ByteBuf) msg;
// byte[] receivedBytes = new byte[byteBuf.readableBytes()];
// byteBuf.readBytes(receivedBytes);
//
// System.out.println("Received bytes: " + Arrays.toString(receivedBytes));
//
// // Perform decoding and processing on receivedBytes
//
// ReferenceCountUtil.release(msg);
// }
// }
@Override @Override
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));
// 收到的rsp或者notify // 收到的rsp或者notify
try { try {
TcpBase<TcpData> up = Ocpp16Helper.parse(bytes); TcpBase<TcpData> up = Ocpp16Helper.parse(bytes);
...@@ -54,6 +74,8 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -54,6 +74,8 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
return; return;
} }
log.info("up:{}", up);
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass(); Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null) { if (handlerClass == null) {
handlerClass = DefaultReqHandler.class; handlerClass = DefaultReqHandler.class;
......
...@@ -8,6 +8,8 @@ logging.file=/opt/logs/stdout.log ...@@ -8,6 +8,8 @@ logging.file=/opt/logs/stdout.log
logging.level.io.geekidea.fastspringboot=DEBUG logging.level.io.geekidea.fastspringboot=DEBUG
logging.level.org.springframework.web=INFO logging.level.org.springframework.web=INFO
logging.level.root=INFO logging.level.root=INFO
log4j.logger.io.netty.handler.codec.LengthFieldBasedFrameDecoder=DEBUG
#logging.pattern.console=%d{HH:mm:ss.SSS} [%5p] - %m \\(%F:%L\\)%n #logging.pattern.console=%d{HH:mm:ss.SSS} [%5p] - %m \\(%F:%L\\)%n
#logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] - %m (%F:%L)%n #logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] - %m (%F:%L)%n
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!