Commit c1b14ba1 by tiger

Login协议调试

1 parent a161291b
......@@ -9,17 +9,18 @@ public class ClientMain {
private static TcpClient instance;
public static TcpClient getInstance() {
if (instance == null) {
System.setProperty("file.encoding", "UTF-8");
String host = "localhost";
int port = 8888;
String host = "localhost";
int port = 8888;
host = Ocpp16Constants.getTcpHost();
port = Ocpp16Constants.getTcpPort();
host = Ocpp16Constants.getTcpHost();
port = Ocpp16Constants.getTcpPort();
return getInstance(host, port);
}
host = "121.199.192.223";
port = 8768;
public static TcpClient getInstance(String host, int port) {
if (instance == null) {
System.setProperty("file.encoding", "UTF-8");
TcpClient p = new TcpClient(host, port);
p.start();
......@@ -30,14 +31,31 @@ public class ClientMain {
}
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();
Thread.sleep(3000);
LoginReq req = new LoginReq();
req.setData(new LoginReqData());
byte[] bs = Ocpp16Helper.build(req);
client.sendSync(bs);
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) {
Thread.sleep(300);
client.sendSync(req);
}
// 定期心跳
......
......@@ -37,7 +37,7 @@ public class Ocpp16Helper {
public static void scan() {
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;
List<Class<?>> ps = ClassUtil.getAllAssignedClass(pClass, path);
......@@ -95,12 +95,63 @@ public class Ocpp16Helper {
// TODO
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) {
// TODO 不足 位补 0
return bs;
public static byte[] strToAscii(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[] build(Object d) {
......@@ -153,7 +204,7 @@ public class Ocpp16Helper {
}
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);
P p = null;
......@@ -210,7 +261,6 @@ public class Ocpp16Helper {
D d = (D) parse(v, vs);
f.set(p, d);
}
log.debug("build:{} field:{} value:{}", p.getClass().getSimpleName(), f.getName(), v);
} catch (Exception e) {
......@@ -220,7 +270,7 @@ public class Ocpp16Helper {
} catch (Exception e) {
}
return null;
return p;
}
public static void main(String[] args) {
......
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;
public class ServerMain {
......@@ -16,7 +15,7 @@ public class ServerMain {
// DefaultTcpNotifyHandler.startKafaka();
//
ProxyHelper.startManager();
// ProxyHelper.startManager();
synchronized (lock) {
server = new TcpServer(host, port);
......
......@@ -18,7 +18,7 @@ public class TcpBase<D extends TcpData> {
/**
* 起始标识符代表一帧数据的开始,固定为 0x68
*/
protected byte type = 0x68;
protected byte start = 0x68;
/**
* 数据域字节数,数据域长度不超过 200 字节。不加密时为原数据长度,加密时,为加密后数据长度。其值为“序列号域+加密标志+帧类型标志+消息体”字节数之和。
......@@ -39,7 +39,7 @@ public class TcpBase<D extends TcpData> {
/**
*  帧类型标志定义了上下行数据帧。
*/
protected byte direction = 0;
protected byte type = 0;
private D data;
......
......@@ -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.gbt.ocpp16.tcp.handler.login.req.LoginReqHandler;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.handler.login.rsp.LoginRspHandler;
import lombok.AllArgsConstructor;
import lombok.Getter;
......@@ -25,9 +26,9 @@ public final class TcpMapper {
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);
......
......@@ -2,8 +2,9 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client;
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.util.JacksonUtils;
import cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.Task;
import cn.com.glsx.charging.util.TaskExecutor;
import io.netty.bootstrap.Bootstrap;
......@@ -17,10 +18,8 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
......@@ -50,10 +49,10 @@ public class TcpClient {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 0, 4, 0, 4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
// pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0));
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TcpClientHandler());
}
});
......@@ -98,18 +97,18 @@ public class TcpClient {
}
public <T> void sendSync(T data) {
String json = JacksonUtils.nodefaultMapper.toJson(data);
sendSync(json);
byte[] bs = Ocpp16Helper.build(data);
sendSync(bs);
// if (!(data instanceof HeartbeatDllNotify)) {
String ip = getIp(future.channel());
log.info("【{}】send: {}", ip, json);
// }
log.info("【{}】send: {}", ip, ByteUtils.toHex(bs));
}
public void sendSync(byte[] bytes) {
try {
if (future == null || future.channel() == null) {
log.info("future null ...");
return;
}
future.channel().writeAndFlush(bytes);
......
......@@ -16,7 +16,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
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.ByteArrayEncoder;
import lombok.Getter;
......@@ -57,8 +56,8 @@ public class TcpServer {
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 4, 0));
// pipeline.addLast("lengthEncoder", new LengthFieldPrepender(4));
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0));
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TcpServerHandler());
......
......@@ -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.TcpMapper;
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.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
......@@ -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
protected void channelRead0(ChannelHandlerContext ctx, byte[] bytes) throws Exception {
String ip = TcpServer.getIp(ctx);
log.info("up:{}", ByteUtils.toHex(bytes));
// 收到的rsp或者notify
try {
TcpBase<TcpData> up = Ocpp16Helper.parse(bytes);
......@@ -54,6 +74,8 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
return;
}
log.info("up:{}", up);
Class<? extends TcpHandler> handlerClass = mapper.getHandlerClass();
if (handlerClass == null) {
handlerClass = DefaultReqHandler.class;
......
......@@ -8,6 +8,8 @@ logging.file=/opt/logs/stdout.log
logging.level.io.geekidea.fastspringboot=DEBUG
logging.level.org.springframework.web=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.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!