Commit b547e099 by 叶立庆
2 parents 89107f5c 3cdc6831
......@@ -9,8 +9,10 @@ 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.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ShutdownMachineReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.StartMachineReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TransactionRecordsReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ShutdownMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.StartMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.TransactionRecordsReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer;
import cn.com.glsx.charging.modules.model.UserBO;
......@@ -49,16 +51,18 @@ public class PileControController extends BaseController {
@Operation(summary = "开启充电桩", description = "开启充电桩")
@GetMapping("/start")
public R<Boolean> start(String pileId) {
TransactionRecordsReq req = new TransactionRecordsReq();
TransactionRecordsReqData data = new TransactionRecordsReqData();
data.setTransactionSerialNumber(
Ocpp16Helper.strToBcd("55031412782305012018061910262392",
data.getTransactionSerialNumber()));
data.setGunNumber((byte) 1);
StartMachineReq req = new StartMachineReq();
StartMachineReqData data = new StartMachineReqData();
data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("32010200000000111511161555350261",
data.getTransactionSerialNumber()));
data.setPileId(Ocpp16Helper.strToBcd(pileId, data.getPileId()));
data.setTopUnitPrice(Ocpp16Helper.strToBcd("D0FB0100", data.getTopUnitPrice()));
data.setVehicleIdentifier(
Ocpp16Helper.strToBcd("0000000000000000000000000000000001", data.getVehicleIdentifier()));
data.setGunNumber((byte) 1);
data.setLogicalCardNumber(
Ocpp16Helper.strToBcd("0000001000000573", data.getLogicalCardNumber()));
byte[] bytes = Ocpp16Helper.binToBytes(Ocpp16Helper.hexToBinStr("00000000D14B0A54"));
data.setPhysicalCardNumber(bytes);
byte[] accountBytes = Ocpp16Helper.binToBytes(Ocpp16Helper.hexToBinStr("A0860100"));
data.setAccountBalance(accountBytes);
req.setData(data);
TcpCallback<TcpRsp> tcpRspTcpCallback = ServerMain.getServer().sendSync(req);
TcpRsp tcpRsp = tcpRspTcpCallback.get();
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpBase;
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;
......@@ -10,8 +7,12 @@ import java.util.Map;
import com.google.common.collect.Maps;
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.base.TcpCallback;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.util.JacksonUtils;
import cn.com.glsx.charging.util.TaskExecutor;
import cn.hutool.core.util.StrUtil;
import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
......@@ -20,166 +21,164 @@ 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();
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);
}
public static <T extends TcpBase> String getCallbackKey(T data, Channel channel) {
if (channel == null) {
throw new RuntimeException("channel为null!!!");
}
return channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq();
}
/**
* 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 (!StrUtil.equals(attr.get(), pileId)) {
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);
}
public static <T extends TcpBase> String getCallbackKey(T data, Channel channel) {
if (channel == null) {
throw new RuntimeException("channel为null!!!");
}
return channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!