Commit 0e4373e2 by zhouhaibao

[+]发送协议提交

1 parent 465da30f
...@@ -48,5 +48,6 @@ public class ServerMain { ...@@ -48,5 +48,6 @@ public class ServerMain {
TcpCallback<TcpRsp> callback = server.sendSync(timeReq); TcpCallback<TcpRsp> callback = server.sendSync(timeReq);
TcpRsp rsp = callback.get(); TcpRsp rsp = callback.get();
} }
} }
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpCallback;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
...@@ -17,15 +19,21 @@ import lombok.extern.slf4j.Slf4j; ...@@ -17,15 +19,21 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ProxyHelper { public class ProxyHelper {
/** channel绑定key */ /**
* channel绑定key
*/
public static final AttributeKey<String> PILE_ID_KEY = AttributeKey.valueOf("pileId"); public static final AttributeKey<String> PILE_ID_KEY = AttributeKey.valueOf("pileId");
/** 心跳超时时间 */ /**
* 心跳超时时间
*/
public static int HEARTBEAT_TIMEOUT_SECONDS = 30; // 心跳过期时间(10秒每次,3次没心跳则过期) public static int HEARTBEAT_TIMEOUT_SECONDS = 30; // 心跳过期时间(10秒每次,3次没心跳则过期)
// pileId -> proxyInfo // pileId -> proxyInfo
private static Map<String, ProxyInfo> proxys = Maps.newConcurrentMap(); private static Map<String, ProxyInfo> proxys = Maps.newConcurrentMap();
private static Map<String, TcpCallback<TcpRsp>> callbackMap = Maps.newConcurrentMap();
// key -> TcpCallback // key -> TcpCallback
// private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap(); // private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap();
...@@ -57,7 +65,8 @@ public class ProxyHelper { ...@@ -57,7 +65,8 @@ public class ProxyHelper {
log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo); log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo);
} else { } else {
if (proxyInfo.getChannel().id() != channel.id()) { if (proxyInfo.getChannel().id() != channel.id()) {
log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(), proxyInfo.getChannel().toString()); log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(),
proxyInfo.getChannel().toString());
} }
} }
proxyInfo.setChannel(channel); proxyInfo.setChannel(channel);
...@@ -138,7 +147,8 @@ public class ProxyHelper { ...@@ -138,7 +147,8 @@ public class ProxyHelper {
return; return;
} }
// //
log.info("【自检】-------------------------------------------------------------------------------"); log.info(
"【自检】-------------------------------------------------------------------------------");
// log.info("【自检托管】{}", users); // log.info("【自检托管】{}", users);
for (ProxyInfo proxy : map.values()) { for (ProxyInfo proxy : map.values()) {
log.info("【自检】{}", JacksonUtils.nodefaultMapper.toJson(proxy)); log.info("【自检】{}", JacksonUtils.nodefaultMapper.toJson(proxy));
...@@ -152,4 +162,16 @@ public class ProxyHelper { ...@@ -152,4 +162,16 @@ public class ProxyHelper {
}, 45 * 1000, 20 * 1000); }, 45 * 1000, 20 * 1000);
} }
public static void addCallback(String key, TcpCallback<TcpRsp> callback) {
TcpCallback<TcpRsp> tcpRspTcpCallback = callbackMap.get(key);
if (tcpRspTcpCallback == null) {
callbackMap.put(key, callback);
} else {
callback = tcpRspTcpCallback;
}
}
public static TcpCallback<TcpRsp> getCallback(String key) {
return callbackMap.get(key);
}
} }
...@@ -113,8 +113,8 @@ public class TcpServer { ...@@ -113,8 +113,8 @@ public class TcpServer {
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> TcpCallback<TcpRsp> sendSync(T data) { public <T extends TcpBase> TcpCallback<TcpRsp> sendSync(T data) {
String ip = getIp(future.channel()); /*String ip = getIp(future.channel());*/
String ip="11";
// seq字段处理 // seq字段处理
String seqKey = ip; String seqKey = ip;
if (data.getData() != null) { if (data.getData() != null) {
...@@ -123,12 +123,13 @@ public class TcpServer { ...@@ -123,12 +123,13 @@ public class TcpServer {
data.setSeq(Ocpp16Helper.generateSeq(seqKey)); data.setSeq(Ocpp16Helper.generateSeq(seqKey));
byte[] bs = Ocpp16Helper.build(data); byte[] bs = Ocpp16Helper.build(data);
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(data.getData().getPileIdStr());
String key = future.channel().attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq(); log.info("【{}】读取proxyInfo:{}", data.getData().getPileIdStr(), proxyInfo);
String key = proxyInfo.getChannel().attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq();
TcpCallback<TcpRsp> callback = new TcpCallback<>(TcpRsp.class); TcpCallback<TcpRsp> callback = new TcpCallback<>(TcpRsp.class);
// ProxyHelper.addCallback(key, callback); ProxyHelper.addCallback(key, callback);
sendSync(bs); sendSync(bs, proxyInfo.getChannel());
log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs)); log.info("【{}】【{}】send1: {} ", ip, data.getClass().getSimpleName(), ByteUtils.toHex(bs));
log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data); log.info("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
...@@ -136,6 +137,18 @@ public class TcpServer { ...@@ -136,6 +137,18 @@ public class TcpServer {
return callback; return callback;
} }
private void sendSync(byte[] bs, Channel channel) {
try {
if (channel == null ) {
log.info("channel null ...");
return;
}
channel.writeAndFlush(bs);
} catch (Exception e) {
log.error("" + bs, e);
}
}
private void sendSync(byte[] bytes) { private void sendSync(byte[] bytes) {
try { try {
if (future == null || future.channel() == null) { if (future == null || future.channel() == null) {
......
package cn.com.glsx.charging.gbt.ocpp16; package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpCallback;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer;
import java.util.Date; import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test; import org.junit.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
...@@ -20,29 +24,22 @@ import cn.hutool.core.util.HexUtil; ...@@ -20,29 +24,22 @@ import cn.hutool.core.util.HexUtil;
* @Author xiehx * @Author xiehx
* @Date 2023/11/7 * @Date 2023/11/7
**/ **/
@Slf4j
public class TimeSettingTest { public class TimeSettingTest {
String host = "127.0.0.1"; String host = "localhost";
int port = 8888; int port = 8888;
private void serverStart() throws Exception { private TcpServer serverStart() throws Exception {
// 启动服务端 // 启动服务端
ServerMain.start(host, port); TcpServer start = ServerMain.start(host, port);
Thread.sleep(15000); Thread.sleep(15000);
return start;
} }
@Test @Test
public void test() throws Exception { public void test() throws Exception {
serverStart(); TcpServer tcpServer = serverStart();
// 对时设置
setTime();
Thread.sleep(20000);
}
private void setTime() {
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(host);
if (proxyInfo != null) {
// 对时设置
Date date = new Date(); Date date = new Date();
String cp56Time2a = ByteUtils.cp56Time2a(date); String cp56Time2a = ByteUtils.cp56Time2a(date);
...@@ -56,10 +53,15 @@ public class TimeSettingTest { ...@@ -56,10 +53,15 @@ public class TimeSettingTest {
TimeSettingReq req = new TimeSettingReq(); TimeSettingReq req = new TimeSettingReq();
req.setData(data); req.setData(data);
byte[] bs = Ocpp16Helper.build(req);
proxyInfo.getChannel().writeAndFlush(bs); Thread.sleep(30000);
} else {
System.out.println("Client " + host + " is not connected!"); TcpCallback<TcpRsp> callback = tcpServer.sendSync(req);
}
Thread.sleep(30000);
TcpRsp rsp = callback.get();
log.info("callback{}",req);
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!