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,139 +19,159 @@ import lombok.extern.slf4j.Slf4j; ...@@ -17,139 +19,159 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ProxyHelper { public class ProxyHelper {
/** channel绑定key */ /**
public static final AttributeKey<String> PILE_ID_KEY = AttributeKey.valueOf("pileId"); * channel绑定key
*/
/** 心跳超时时间 */ public static final AttributeKey<String> PILE_ID_KEY = AttributeKey.valueOf("pileId");
public static int HEARTBEAT_TIMEOUT_SECONDS = 30; // 心跳过期时间(10秒每次,3次没心跳则过期)
/**
// pileId -> proxyInfo * 心跳超时时间
private static Map<String, ProxyInfo> proxys = Maps.newConcurrentMap(); */
public static int HEARTBEAT_TIMEOUT_SECONDS = 30; // 心跳过期时间(10秒每次,3次没心跳则过期)
// key -> TcpCallback
// private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap(); // pileId -> proxyInfo
private static Map<String, ProxyInfo> proxys = Maps.newConcurrentMap();
// notifyClass --> data-queue
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap(); private static Map<String, TcpCallback<TcpRsp>> callbackMap = Maps.newConcurrentMap();
public static String getIp(Channel channel) { // key -> TcpCallback
String ip = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress(); // private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap();
return ip;
} // notifyClass --> data-queue
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap();
public static int getPort(Channel channel) {
int port = ((InetSocketAddress) channel.remoteAddress()).getPort(); public static String getIp(Channel channel) {
return port; String ip = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();
} return ip;
}
public static ProxyInfo registProxyInfo(String pileId, Channel channel) {
// 标记连接 public static int getPort(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY); int port = ((InetSocketAddress) channel.remoteAddress()).getPort();
if (attr.get() != null) { return port;
log.warn("【连接更换】{} -> {}", attr.get(), pileId); }
}
attr.set(pileId); public static ProxyInfo registProxyInfo(String pileId, Channel channel) {
// 标记连接
ProxyInfo proxyInfo = proxys.get(pileId); Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
if (proxyInfo == null) { if (attr.get() != null) {
proxyInfo = new ProxyInfo(channel, pileId); log.warn("【连接更换】{} -> {}", attr.get(), pileId);
proxys.put(pileId, proxyInfo); }
log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo); attr.set(pileId);
} else {
if (proxyInfo.getChannel().id() != channel.id()) { ProxyInfo proxyInfo = proxys.get(pileId);
log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(), proxyInfo.getChannel().toString()); if (proxyInfo == null) {
} proxyInfo = new ProxyInfo(channel, pileId);
} proxys.put(pileId, proxyInfo);
proxyInfo.setChannel(channel); log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo);
proxyInfo.setLastTime(new Date()); } else {
return proxyInfo; if (proxyInfo.getChannel().id() != channel.id()) {
} log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(),
proxyInfo.getChannel().toString());
// 连接断开,暂时不移除连接 }
public static void removeProxyInfo(Channel channel) { }
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY); proxyInfo.setChannel(channel);
String pileId = attr.get(); proxyInfo.setLastTime(new Date());
return proxyInfo;
removeProxyInfo(pileId); }
}
// 连接断开,暂时不移除连接
private synchronized static void removeProxyInfo(String pileId) { public static void removeProxyInfo(Channel channel) {
ProxyInfo proxyInfo = proxys.get(pileId); Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
String pileId = attr.get();
if (proxyInfo != null && proxyInfo.getChannel().isActive()) {
return; removeProxyInfo(pileId);
} }
Ocpp16Helper.resetSeq(pileId); private synchronized static void removeProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId);
log.warn("Proxy:" + pileId + "断联中。。。");
proxys.remove(pileId); // 连接断开,暂时移除连接 if (proxyInfo != null && proxyInfo.getChannel().isActive()) {
} return;
}
public static ProxyInfo getProxyInfo(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY); Ocpp16Helper.resetSeq(pileId);
String pileId = attr.get();
return getProxyInfo(pileId); log.warn("Proxy:" + pileId + "断联中。。。");
} proxys.remove(pileId); // 连接断开,暂时移除连接
}
public static ProxyInfo getProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId); public static ProxyInfo getProxyInfo(Channel channel) {
if (proxyInfo == null) { Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
throw new RuntimeException("【" + pileId + "】Proxy连接不存在!!!"); String pileId = attr.get();
} return getProxyInfo(pileId);
return proxyInfo; }
}
public static ProxyInfo getProxyInfo(String pileId) {
// TODO 心跳机制 ProxyInfo proxyInfo = proxys.get(pileId);
// TODO 错误反馈机制 if (proxyInfo == null) {
// web_req -> web_req_handler -> tcp_req -> tcp_req_handler -> dll_req -> dll_handler -> dll_rsp -> tcp_rsp -> tcp_rsp_handler -> web_rsp throw new RuntimeException("【" + pileId + "】Proxy连接不存在!!!");
}
// public static void addCallback(String reqKey, TcpCallback<? extends TcpRsp> callback) { return proxyInfo;
// callbacks.put(reqKey, callback); }
// }
// TODO 心跳机制
// @SuppressWarnings("unchecked") // TODO 错误反馈机制
// public static <Rsp extends TcpRsp> void callback(Rsp rsp) { // web_req -> web_req_handler -> tcp_req -> tcp_req_handler -> dll_req -> dll_handler -> dll_rsp -> tcp_rsp -> tcp_rsp_handler -> web_rsp
// TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// if (callback != null) { // public static void addCallback(String reqKey, TcpCallback<? extends TcpRsp> callback) {
// callback.callback(rsp); // callbacks.put(reqKey, callback);
// } // }
// }
// @SuppressWarnings("unchecked")
// public static <Req extends TcpReq, Rsp extends TcpRsp> TcpCallback<Rsp> sendAsync(ProxyInfo client, Req req, Class<Rsp> rspClass) { // public static <Rsp extends TcpRsp> void callback(Rsp rsp) {
// TcpCallback<Rsp> callback = new TcpCallback<>(rspClass); // TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// addCallback(req.getReqKey(), callback); // if (callback != null) {
// // callback.callback(rsp);
// String json = req.build(); // }
// client.getChannel().writeAndFlush(json); // }
//
// log.info("【{}】send: {}", client.getIp(), json); // public static <Req extends TcpReq, Rsp extends TcpRsp> TcpCallback<Rsp> sendAsync(ProxyInfo client, Req req, Class<Rsp> rspClass) {
// // TcpCallback<Rsp> callback = new TcpCallback<>(rspClass);
// return callback; // addCallback(req.getReqKey(), callback);
// } //
// String json = req.build();
public static void startManager() { // client.getChannel().writeAndFlush(json);
// 5秒钟执行一次 //
TaskExecutor.addTask(() -> { // log.info("【{}】send: {}", client.getIp(), json);
Map<String, ProxyInfo> map = Maps.newHashMap(); //
map.putAll(proxys); // return callback;
// }
if (map.isEmpty()) {
return; public static void startManager() {
} // 5秒钟执行一次
// TaskExecutor.addTask(() -> {
log.info("【自检】-------------------------------------------------------------------------------"); Map<String, ProxyInfo> map = Maps.newHashMap();
// log.info("【自检托管】{}", users); map.putAll(proxys);
for (ProxyInfo proxy : map.values()) {
log.info("【自检】{}", JacksonUtils.nodefaultMapper.toJson(proxy)); if (map.isEmpty()) {
// 1.移除断开连接 return;
if (!proxy.getChannel().isActive()) { }
proxy.getChannel().close(); //
ProxyHelper.removeProxyInfo(proxy.getChannel()); log.info(
continue; "【自检】-------------------------------------------------------------------------------");
} // log.info("【自检托管】{}", users);
} for (ProxyInfo proxy : map.values()) {
}, 45 * 1000, 20 * 1000); log.info("【自检】{}", JacksonUtils.nodefaultMapper.toJson(proxy));
} // 1.移除断开连接
if (!proxy.getChannel().isActive()) {
proxy.getChannel().close();
ProxyHelper.removeProxyInfo(proxy.getChannel());
continue;
}
}
}, 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,46 +24,44 @@ import cn.hutool.core.util.HexUtil; ...@@ -20,46 +24,44 @@ 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();
// 对时设置 Date date = new Date();
setTime(); String cp56Time2a = ByteUtils.cp56Time2a(date);
Thread.sleep(20000);
} TimeSettingReqData data = new TimeSettingReqData();
data.setPileId(Ocpp16Helper.strToBcd("55031412782305", data.getPileId()));
data.setTime(HexUtil.decodeHex(cp56Time2a));
System.out.println("Cp56Time2a: " + cp56Time2a);
System.out.println("Cp56Time2a原值: " + DateUtils.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
private void setTime() { TimeSettingReq req = new TimeSettingReq();
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(host); req.setData(data);
if (proxyInfo != null) {
// 对时设置
Date date = new Date();
String cp56Time2a = ByteUtils.cp56Time2a(date);
TimeSettingReqData data = new TimeSettingReqData();
data.setPileId(Ocpp16Helper.strToBcd("55031412782305", data.getPileId()));
data.setTime(HexUtil.decodeHex(cp56Time2a));
System.out.println("Cp56Time2a: " + cp56Time2a); Thread.sleep(30000);
System.out.println("Cp56Time2a原值: " + DateUtils.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
TimeSettingReq req = new TimeSettingReq(); TcpCallback<TcpRsp> callback = tcpServer.sendSync(req);
req.setData(data);
byte[] bs = Ocpp16Helper.build(req); Thread.sleep(30000);
proxyInfo.getChannel().writeAndFlush(bs);
} else { TcpRsp rsp = callback.get();
System.out.println("Client " + host + " is not connected!"); 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!