Commit 0e4373e2 by zhouhaibao

[+]发送协议提交

1 parent 465da30f
......@@ -48,5 +48,6 @@ public class ServerMain {
TcpCallback<TcpRsp> callback = server.sendSync(timeReq);
TcpRsp rsp = callback.get();
}
}
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.util.Date;
import java.util.Map;
......@@ -17,139 +19,159 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ProxyHelper {
/** 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();
// key -> TcpCallback
// private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap();
// notifyClass --> data-queue
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap();
public static String getIp(Channel channel) {
String ip = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();
return ip;
}
public static int getPort(Channel channel) {
int port = ((InetSocketAddress) channel.remoteAddress()).getPort();
return port;
}
public static ProxyInfo registProxyInfo(String pileId, Channel channel) {
// 标记连接
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
if (attr.get() != null) {
log.warn("【连接更换】{} -> {}", attr.get(), pileId);
}
attr.set(pileId);
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo == null) {
proxyInfo = new ProxyInfo(channel, pileId);
proxys.put(pileId, proxyInfo);
log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo);
} else {
if (proxyInfo.getChannel().id() != channel.id()) {
log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(), proxyInfo.getChannel().toString());
}
}
proxyInfo.setChannel(channel);
proxyInfo.setLastTime(new Date());
return proxyInfo;
}
// 连接断开,暂时不移除连接
public static void removeProxyInfo(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
String pileId = attr.get();
removeProxyInfo(pileId);
}
private synchronized static void removeProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo != null && proxyInfo.getChannel().isActive()) {
return;
}
Ocpp16Helper.resetSeq(pileId);
log.warn("Proxy:" + pileId + "断联中。。。");
proxys.remove(pileId); // 连接断开,暂时移除连接
}
public static ProxyInfo getProxyInfo(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
String pileId = attr.get();
return getProxyInfo(pileId);
}
public static ProxyInfo getProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo == null) {
throw new RuntimeException("【" + pileId + "】Proxy连接不存在!!!");
}
return proxyInfo;
}
// TODO 心跳机制
// TODO 错误反馈机制
// web_req -> web_req_handler -> tcp_req -> tcp_req_handler -> dll_req -> dll_handler -> dll_rsp -> tcp_rsp -> tcp_rsp_handler -> web_rsp
// public static void addCallback(String reqKey, TcpCallback<? extends TcpRsp> callback) {
// callbacks.put(reqKey, callback);
// }
// @SuppressWarnings("unchecked")
// public static <Rsp extends TcpRsp> void callback(Rsp rsp) {
// TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// if (callback != null) {
// callback.callback(rsp);
// }
// }
// public static <Req extends TcpReq, Rsp extends TcpRsp> TcpCallback<Rsp> sendAsync(ProxyInfo client, Req req, Class<Rsp> rspClass) {
// TcpCallback<Rsp> callback = new TcpCallback<>(rspClass);
// addCallback(req.getReqKey(), callback);
//
// String json = req.build();
// client.getChannel().writeAndFlush(json);
//
// log.info("【{}】send: {}", client.getIp(), json);
//
// return callback;
// }
public static void startManager() {
// 5秒钟执行一次
TaskExecutor.addTask(() -> {
Map<String, ProxyInfo> map = Maps.newHashMap();
map.putAll(proxys);
if (map.isEmpty()) {
return;
}
//
log.info("【自检】-------------------------------------------------------------------------------");
// log.info("【自检托管】{}", users);
for (ProxyInfo proxy : map.values()) {
log.info("【自检】{}", JacksonUtils.nodefaultMapper.toJson(proxy));
// 1.移除断开连接
if (!proxy.getChannel().isActive()) {
proxy.getChannel().close();
ProxyHelper.removeProxyInfo(proxy.getChannel());
continue;
}
}
}, 45 * 1000, 20 * 1000);
}
/**
* 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();
private static Map<String, TcpCallback<TcpRsp>> callbackMap = Maps.newConcurrentMap();
// key -> TcpCallback
// private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap();
// notifyClass --> data-queue
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap();
public static String getIp(Channel channel) {
String ip = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();
return ip;
}
public static int getPort(Channel channel) {
int port = ((InetSocketAddress) channel.remoteAddress()).getPort();
return port;
}
public static ProxyInfo registProxyInfo(String pileId, Channel channel) {
// 标记连接
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
if (attr.get() != null) {
log.warn("【连接更换】{} -> {}", attr.get(), pileId);
}
attr.set(pileId);
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo == null) {
proxyInfo = new ProxyInfo(channel, pileId);
proxys.put(pileId, proxyInfo);
log.info("【{}】Proxy连接建立:{}", pileId, proxyInfo);
} else {
if (proxyInfo.getChannel().id() != channel.id()) {
log.warn("【警告】pileId:{} 包含两个连接,新:{} 会覆盖旧:{} ...", pileId, channel.toString(),
proxyInfo.getChannel().toString());
}
}
proxyInfo.setChannel(channel);
proxyInfo.setLastTime(new Date());
return proxyInfo;
}
// 连接断开,暂时不移除连接
public static void removeProxyInfo(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
String pileId = attr.get();
removeProxyInfo(pileId);
}
private synchronized static void removeProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo != null && proxyInfo.getChannel().isActive()) {
return;
}
Ocpp16Helper.resetSeq(pileId);
log.warn("Proxy:" + pileId + "断联中。。。");
proxys.remove(pileId); // 连接断开,暂时移除连接
}
public static ProxyInfo getProxyInfo(Channel channel) {
Attribute<String> attr = channel.attr(ProxyHelper.PILE_ID_KEY);
String pileId = attr.get();
return getProxyInfo(pileId);
}
public static ProxyInfo getProxyInfo(String pileId) {
ProxyInfo proxyInfo = proxys.get(pileId);
if (proxyInfo == null) {
throw new RuntimeException("【" + pileId + "】Proxy连接不存在!!!");
}
return proxyInfo;
}
// TODO 心跳机制
// TODO 错误反馈机制
// web_req -> web_req_handler -> tcp_req -> tcp_req_handler -> dll_req -> dll_handler -> dll_rsp -> tcp_rsp -> tcp_rsp_handler -> web_rsp
// public static void addCallback(String reqKey, TcpCallback<? extends TcpRsp> callback) {
// callbacks.put(reqKey, callback);
// }
// @SuppressWarnings("unchecked")
// public static <Rsp extends TcpRsp> void callback(Rsp rsp) {
// TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// if (callback != null) {
// callback.callback(rsp);
// }
// }
// public static <Req extends TcpReq, Rsp extends TcpRsp> TcpCallback<Rsp> sendAsync(ProxyInfo client, Req req, Class<Rsp> rspClass) {
// TcpCallback<Rsp> callback = new TcpCallback<>(rspClass);
// addCallback(req.getReqKey(), callback);
//
// String json = req.build();
// client.getChannel().writeAndFlush(json);
//
// log.info("【{}】send: {}", client.getIp(), json);
//
// return callback;
// }
public static void startManager() {
// 5秒钟执行一次
TaskExecutor.addTask(() -> {
Map<String, ProxyInfo> map = Maps.newHashMap();
map.putAll(proxys);
if (map.isEmpty()) {
return;
}
//
log.info(
"【自检】-------------------------------------------------------------------------------");
// log.info("【自检托管】{}", users);
for (ProxyInfo proxy : map.values()) {
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 {
@SuppressWarnings({ "rawtypes" })
public <T extends TcpBase> TcpCallback<TcpRsp> sendSync(T data) {
String ip = getIp(future.channel());
/*String ip = getIp(future.channel());*/
String ip="11";
// seq字段处理
String seqKey = ip;
if (data.getData() != null) {
......@@ -123,12 +123,13 @@ public class TcpServer {
data.setSeq(Ocpp16Helper.generateSeq(seqKey));
byte[] bs = Ocpp16Helper.build(data);
String key = future.channel().attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq();
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(data.getData().getPileIdStr());
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);
// 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("【{}】【{}】send2: {} ", ip, data.getClass().getSimpleName(), data);
......@@ -136,6 +137,18 @@ public class TcpServer {
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) {
try {
if (future == null || future.channel() == null) {
......
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 lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
......@@ -20,46 +24,44 @@ import cn.hutool.core.util.HexUtil;
* @Author xiehx
* @Date 2023/11/7
**/
@Slf4j
public class TimeSettingTest {
String host = "127.0.0.1";
String host = "localhost";
int port = 8888;
private void serverStart() throws Exception {
private TcpServer serverStart() throws Exception {
// 启动服务端
ServerMain.start(host, port);
Thread.sleep(15000);
TcpServer start = ServerMain.start(host, port);
Thread.sleep(15000);
return start;
}
@Test
public void test() throws Exception {
serverStart();
// 对时设置
setTime();
Thread.sleep(20000);
}
TcpServer tcpServer = serverStart();
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);
System.out.println("Cp56Time2a原值: " + DateUtils.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
private void setTime() {
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(host);
if (proxyInfo != null) {
// 对时设置
Date date = new Date();
String cp56Time2a = ByteUtils.cp56Time2a(date);
TimeSettingReq req = new TimeSettingReq();
req.setData(data);
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"));
Thread.sleep(30000);
TimeSettingReq req = new TimeSettingReq();
req.setData(data);
TcpCallback<TcpRsp> callback = tcpServer.sendSync(req);
byte[] bs = Ocpp16Helper.build(req);
proxyInfo.getChannel().writeAndFlush(bs);
} else {
System.out.println("Client " + host + " is not connected!");
}
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!