Commit d61157ca by zhouhaibao

[+]新增callback

2 parents c734f580 5365aeb4
...@@ -53,7 +53,7 @@ public class PileControlApiController implements PileControlFeignClient { ...@@ -53,7 +53,7 @@ public class PileControlApiController implements PileControlFeignClient {
data.setPileId(Ocpp16Helper.strToBcd(startBo.getPileId(), data.getPileId())); data.setPileId(Ocpp16Helper.strToBcd(startBo.getPileId(), data.getPileId()));
data.setGunNumber(startBo.getGunNumber().byteValue()); data.setGunNumber(startBo.getGunNumber().byteValue());
data.setLogicalCardNumber( data.setLogicalCardNumber(
Ocpp16Helper.strToBcd(startBo.getTransactionSerialNumber(), data.getLogicalCardNumber())); Ocpp16Helper.strToBcd(startBo.getLogicalCardNumber(), data.getLogicalCardNumber()));
byte[] bytes = Ocpp16Helper.binToBytes( byte[] bytes = Ocpp16Helper.binToBytes(
Ocpp16Helper.hexToBinStr(startBo.getPhysicalCardNumber())); Ocpp16Helper.hexToBinStr(startBo.getPhysicalCardNumber()));
data.setPhysicalCardNumber(bytes); data.setPhysicalCardNumber(bytes);
...@@ -61,10 +61,11 @@ public class PileControlApiController implements PileControlFeignClient { ...@@ -61,10 +61,11 @@ public class PileControlApiController implements PileControlFeignClient {
BigDecimal.valueOf(startBo.getAccountBalance()), 2); BigDecimal.valueOf(startBo.getAccountBalance()), 2);
data.setAccountBalance(accountBytes); data.setAccountBalance(accountBytes);
req.setData(data); req.setData(data);
TcpCallback<TcpRsp> tcpRspTcpCallback = ServerMain.getServer().sendSync(req); /* TcpCallback<TcpRsp> tcpRspTcpCallback = ServerMain.getServer().sendSync(req);
TcpRsp tcpRsp = tcpRspTcpCallback.get(); TcpRsp tcpRsp = tcpRspTcpCallback.get();*/
return Result.success(true); return Result.success(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
return Result.failure(); return Result.failure();
} }
} }
......
...@@ -103,7 +103,7 @@ public final class TcpMapper { ...@@ -103,7 +103,7 @@ public final class TcpMapper {
public static TcpMapper CMD_REQ_MODEL_SET = regist(0x58, "计费模型设置", null); public static TcpMapper CMD_REQ_MODEL_SET = regist(0x58, "计费模型设置", null);
public static TcpMapper CMD_RSP_MODEL_SET = regist(0x57, "对时设置应答", ModelSetRspHandler.class); public static TcpMapper CMD_RSP_MODEL_SET = regist(0x57, "计费模型设置应答", ModelSetRspHandler.class);
public static TcpMapper CMD_REQ_BALANCE_UPDATE = regist(0x42, "远程账户余额更新", null); public static TcpMapper CMD_REQ_BALANCE_UPDATE = regist(0x42, "远程账户余额更新", null);
......
...@@ -2,6 +2,7 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server; ...@@ -2,6 +2,7 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
...@@ -37,6 +38,27 @@ public class ProxyHelper { ...@@ -37,6 +38,27 @@ public class ProxyHelper {
private static Map<String, TcpCallback<TcpRsp>> callbackMap = Maps.newConcurrentMap(); private static Map<String, TcpCallback<TcpRsp>> callbackMap = Maps.newConcurrentMap();
/**
* 协议帧对
*/
private static Map<String, String> typeMaps;
static {
// 初始化map
typeMaps = new HashMap<>();
typeMaps.put("33", "34");
typeMaps.put("35", "36");
typeMaps.put("41", "42");
typeMaps.put("43", "44");
typeMaps.put("45", "46");
typeMaps.put("47", "48");
typeMaps.put("51", "52");
typeMaps.put("55", "56");
typeMaps.put("57", "58");
typeMaps.put("91", "92");
typeMaps.put("93", "94");
}
// key -> TcpCallback // key -> TcpCallback
// private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap(); // private static Map<String, TcpCallback<? extends TcpRsp>> callbacks = Maps.newConcurrentMap();
...@@ -166,11 +188,10 @@ public class ProxyHelper { ...@@ -166,11 +188,10 @@ public class ProxyHelper {
} }
public static void addCallback(String key, TcpCallback<TcpRsp> callback) { public static void addCallback(String key, TcpCallback<TcpRsp> callback) {
TcpCallback<TcpRsp> tcpRspTcpCallback = callbackMap.get(key); String[] split = key.split("|");
if (tcpRspTcpCallback == null) { if (split.length > 1) {
callbackMap.put(key, callback); callbackMap.put(split[0], callback);
} else { callbackMap.put(split[1], callback);
callback = tcpRspTcpCallback;
} }
} }
...@@ -181,16 +202,17 @@ public class ProxyHelper { ...@@ -181,16 +202,17 @@ public class ProxyHelper {
/** /**
* 获取callback的key * 获取callback的key
* *
* @param data 请求体 * @param data 请求体
* @param channel 通道 * @param channel 通道
* @return
* @param <T> * @param <T>
* @return
*/ */
public static <T extends TcpBase> String getCallbackKey(T data, Channel channel) { public static <T extends TcpBase> String getCallbackKey(T data, Channel channel) {
if(channel==null){ if (channel == null) {
throw new RuntimeException("channel为null!!!"); throw new RuntimeException("channel为null!!!");
} }
return channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq(); return channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq() + "|" + channel.attr(
ProxyHelper.PILE_ID_KEY).get() + "+" + intToHex(data.getType());
} }
/** /**
...@@ -202,12 +224,33 @@ public class ProxyHelper { ...@@ -202,12 +224,33 @@ public class ProxyHelper {
*/ */
public static <T extends TcpBase> void setCallback(Channel channel, public static <T extends TcpBase> void setCallback(Channel channel,
TcpRsp rsp) { TcpRsp rsp) {
String key = getCallbackKey(rsp, channel); String key1 = channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + rsp.getSeq();
TcpCallback<TcpRsp> callback = ProxyHelper.getCallback(key); TcpCallback<TcpRsp> callback = ProxyHelper.getCallback(key1);
if (Objects.nonNull(callback)) { if (Objects.nonNull(callback)) {
callback.callback(rsp); callback.callback(rsp);
} else { } else {
log.info("key{},callback为空", key); String key2 = channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + typeMaps.get(intToHex(rsp.getType()));
TcpCallback<TcpRsp> tcpRspTcpCallback = ProxyHelper.getCallback(key2);
if (Objects.nonNull(tcpRspTcpCallback)) {
callback.callback(rsp);
} else {
log.info("key{},{},callback为空", key1, key2);
}
}
}
private static String intToHex(int n) {
//StringBuffer s = new StringBuffer();
StringBuilder sb = new StringBuilder(8);
String a;
char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(n != 0){
sb = sb.append(b[n%16]);
n = n/16;
} }
a = sb.reverse().toString();
return a;
} }
} }
...@@ -3,6 +3,7 @@ package cn.com.glsx.charging.api; ...@@ -3,6 +3,7 @@ package cn.com.glsx.charging.api;
import cn.com.glsx.Application; import cn.com.glsx.Application;
import cn.com.glsx.evbusiness.api.DataInteractionFeignClient; import cn.com.glsx.evbusiness.api.DataInteractionFeignClient;
import cn.com.glsx.evbusiness.modules.model.ocpp16.RealTimeUploadData; import cn.com.glsx.evbusiness.modules.model.ocpp16.RealTimeUploadData;
import com.glsx.plat.core.web.Result;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -24,7 +25,7 @@ public class FeignSerivceTest { ...@@ -24,7 +25,7 @@ public class FeignSerivceTest {
@Test @Test
public void Test() throws Exception { public void Test() throws Exception {
Boolean b = dataInteractionFeignClient.uploadRealTimeData(new RealTimeUploadData()); Result<Boolean> booleanResult = dataInteractionFeignClient.uploadRealTimeData(
System.out.println(b); new RealTimeUploadData());
} }
} }
...@@ -5,8 +5,13 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; ...@@ -5,8 +5,13 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ModelSetRspData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ModelSetRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ModelValReqData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ModelValReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TimeSettingReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TimeSettingRspData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ModelSetRsp; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ModelSetRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ModelValReq; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ModelValReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.TimeSettingRsp;
import cn.com.glsx.charging.util.ByteUtils;
import cn.hutool.core.util.HexUtil;
/** /**
* 计费模型设置应答 * 计费模型设置应答
...@@ -26,12 +31,18 @@ public class ModelSetTest { ...@@ -26,12 +31,18 @@ public class ModelSetTest {
Thread.sleep(3000); Thread.sleep(3000);
ModelSetRsp req = new ModelSetRsp(); /* ModelSetRsp req = new ModelSetRsp();
req.setData(new ModelSetRspData()); req.setData(new ModelSetRspData());
req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId())); req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId()));
req.getData().setSetResult((byte) 0x01); req.getData().setSetResult((byte) 0x01);*/
client.sendSync(req); TimeSettingRspData data = new TimeSettingRspData();
data.setPileId(Ocpp16Helper.strToBcd("55031412782305", data.getPileId()));
TimeSettingRsp rsp = new TimeSettingRsp();
rsp.setData(data);
client.sendSync(rsp);
} }
......
...@@ -58,8 +58,6 @@ public class TimeSettingTest { ...@@ -58,8 +58,6 @@ public class TimeSettingTest {
TcpCallback<TcpRsp> callback = tcpServer.sendSync(req); TcpCallback<TcpRsp> callback = tcpServer.sendSync(req);
Thread.sleep(10000);
TcpRsp rsp = callback.get(); TcpRsp rsp = callback.get();
log.info("callback{}",req); 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!