Commit 67284dc7 by tiger

TcpBase构建时自动填充len字段和crc字段

1 parent 8629cf8c
...@@ -49,9 +49,6 @@ public class ClientMain { ...@@ -49,9 +49,6 @@ public class ClientMain {
req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion())); req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion()));
req.getData().setProtocolVersion((byte) 10); req.getData().setProtocolVersion((byte) 10);
Ocpp16Helper.fillLen(req);
req.setCrc(Ocpp16Helper.calculateCrc(req));
client.sendSync(req); client.sendSync(req);
while (true) { while (true) {
......
...@@ -76,12 +76,20 @@ public class Ocpp16Helper { ...@@ -76,12 +76,20 @@ public class Ocpp16Helper {
return (short) rs; // 溢出自动清零 return (short) rs; // 溢出自动清零
} }
public static <D extends TcpData, P extends TcpBase<D>> void fillLen(P b) { /**
* TcpBase在build时自动填充
*/
@Deprecated
private static <D extends TcpData, P extends TcpBase<D>> void fillLen(P b) {
byte[] bs = build(b); byte[] bs = build(b);
b.setLen((byte) (bs.length - 4)); b.setLen((byte) (bs.length - 4));
} }
public static <D extends TcpData, P extends TcpBase<D>> short calculateCrc(P b) { /**
* TcpBase在build时自动填充
*/
@Deprecated
private static <D extends TcpData, P extends TcpBase<D>> short calculateCrc(P b) {
byte[] bs = build(b); byte[] bs = build(b);
byte[] data = ByteUtils.getBytes(bs, 2, bs.length - 4); byte[] data = ByteUtils.getBytes(bs, 2, bs.length - 4);
short crc = (short) CrcUtils.calculateCRC(data); short crc = (short) CrcUtils.calculateCRC(data);
...@@ -209,6 +217,18 @@ public class Ocpp16Helper { ...@@ -209,6 +217,18 @@ public class Ocpp16Helper {
} }
} }
// 自动补充len字段和crc字段
if (d instanceof TcpBase) {
// 补充长度字段
byte len = (byte) (byteBuf.readableBytes() - 4);
byteBuf.setByte(1, len);
// 补充crc
byte[] data = byteBuf.slice(2, byteBuf.readableBytes() - 4).array();
short crc = (short) CrcUtils.calculateCRC(data);
byteBuf.setShort(byteBuf.readableBytes() - 2, crc);
}
// 读取数据 // 读取数据
byte[] data = new byte[byteBuf.readableBytes()]; byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data); byteBuf.readBytes(data);
......
...@@ -3,6 +3,7 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client; ...@@ -3,6 +3,7 @@ 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.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpBase;
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 cn.com.glsx.charging.util.ByteUtils;
import cn.com.glsx.charging.util.Task; import cn.com.glsx.charging.util.Task;
...@@ -95,7 +96,8 @@ public class TcpClient { ...@@ -95,7 +96,8 @@ public class TcpClient {
}, 10 * 1000); }, 10 * 1000);
} }
public <T> void sendSync(T data) { @SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> void sendSync(T data) {
byte[] bs = Ocpp16Helper.build(data); byte[] bs = Ocpp16Helper.build(data);
sendSync(bs); sendSync(bs);
...@@ -105,7 +107,7 @@ public class TcpClient { ...@@ -105,7 +107,7 @@ public class TcpClient {
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data); log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
} }
public void sendSync(byte[] bytes) { private void sendSync(byte[] bytes) {
try { try {
if (future == null || future.channel() == null) { if (future == null || future.channel() == null) {
log.info("future null ..."); log.info("future null ...");
......
...@@ -59,6 +59,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> { ...@@ -59,6 +59,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
// } // }
// } // }
@SuppressWarnings({ "unchecked", "rawtypes" })
@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);
......
...@@ -35,9 +35,6 @@ public class ClientSimulateTest { ...@@ -35,9 +35,6 @@ public class ClientSimulateTest {
req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion())); req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion()));
req.getData().setProtocolVersion((byte) 10); req.getData().setProtocolVersion((byte) 10);
Ocpp16Helper.fillLen(req);
req.setCrc(Ocpp16Helper.calculateCrc(req));
client.sendSync(req); client.sendSync(req);
// 定期心跳 // 定期心跳
...@@ -48,9 +45,6 @@ public class ClientSimulateTest { ...@@ -48,9 +45,6 @@ public class ClientSimulateTest {
hdata.setStatus((byte) 0); hdata.setStatus((byte) 0);
heartbeat.setData(hdata); heartbeat.setData(hdata);
Ocpp16Helper.fillLen(heartbeat);
heartbeat.setCrc(Ocpp16Helper.calculateCrc(heartbeat));
client.sendSync(heartbeat); client.sendSync(heartbeat);
Thread.sleep(10000); Thread.sleep(10000);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!