Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-charging-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 1d2a73a4
authored
Nov 09, 2023
by
zhouhaibao
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]callback统一处理
1 parent
35e05da1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
195 additions
and
175 deletions
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/handler/ParamSettingRspHandler.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/handler/TimeSettingRspHandler.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyHelper.java
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ModelValTest.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/handler/ParamSettingRspHandler.java
View file @
1d2a73a
...
@@ -35,13 +35,7 @@ public class ParamSettingRspHandler implements TcpHandler<ParamSettingRsp, Param
...
@@ -35,13 +35,7 @@ public class ParamSettingRspHandler implements TcpHandler<ParamSettingRsp, Param
}
}
// 结果响应给业务侧
// 结果响应给业务侧
String
key
=
ProxyHelper
.
getCallbackKey
(
rsp
,
channel
);
ProxyHelper
.
getCallbackKey
(
rsp
,
channel
,
rsp
);
TcpCallback
<
TcpRsp
>
callback
=
ProxyHelper
.
getCallback
(
key
);
if
(
Objects
.
nonNull
(
callback
))
{
callback
.
callback
(
rsp
);
}
else
{
log
.
info
(
"callback为空"
);
}
return
null
;
return
null
;
}
}
}
}
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/handler/TimeSettingRspHandler.java
View file @
1d2a73a
...
@@ -30,13 +30,8 @@ public class TimeSettingRspHandler implements TcpHandler<TimeSettingRsp, TimeSet
...
@@ -30,13 +30,8 @@ public class TimeSettingRspHandler implements TcpHandler<TimeSettingRsp, TimeSet
log
.
info
(
"对时设置响应 pointId {} time {}"
,
Ocpp16Helper
.
bcdToStr
(
data
.
getPileId
()),
ByteUtils
.
parseCp56Time2aStr
(
data
.
getTime
()));
log
.
info
(
"对时设置响应 pointId {} time {}"
,
Ocpp16Helper
.
bcdToStr
(
data
.
getPileId
()),
ByteUtils
.
parseCp56Time2aStr
(
data
.
getTime
()));
// TODO
// TODO
String
key
=
ProxyHelper
.
getCallbackKey
(
rsp
,
channel
);
ProxyHelper
.
getCallbackKey
(
rsp
,
channel
,
rsp
);
TcpCallback
<
TcpRsp
>
callback
=
ProxyHelper
.
getCallback
(
key
);
if
(
Objects
.
nonNull
(
callback
))
{
callback
.
callback
(
rsp
);
}
else
{
log
.
info
(
"callback为空"
);
}
return
null
;
return
null
;
}
}
...
...
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyHelper.java
View file @
1d2a73a
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.protocol.TimeSettingRsp
;
import
java.net.InetSocketAddress
;
import
java.net.InetSocketAddress
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -16,169 +17,199 @@ import cn.hutool.core.util.StrUtil;
...
@@ -16,169 +17,199 @@ import cn.hutool.core.util.StrUtil;
import
io.netty.channel.Channel
;
import
io.netty.channel.Channel
;
import
io.netty.util.Attribute
;
import
io.netty.util.Attribute
;
import
io.netty.util.AttributeKey
;
import
io.netty.util.AttributeKey
;
import
java.util.Objects
;
import
lombok.extern.slf4j.Slf4j
;
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
();
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();
// notifyClass --> data-queue
// notifyClass --> data-queue
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap();
// private static final Map<String, Queue<TcpNotify>> notifys = Maps.newHashMap();
public
static
String
getIp
(
Channel
channel
)
{
public
static
String
getIp
(
Channel
channel
)
{
String
ip
=
((
InetSocketAddress
)
channel
.
remoteAddress
()).
getAddress
().
getHostAddress
();
String
ip
=
((
InetSocketAddress
)
channel
.
remoteAddress
()).
getAddress
().
getHostAddress
();
return
ip
;
return
ip
;
}
}
public
static
int
getPort
(
Channel
channel
)
{
public
static
int
getPort
(
Channel
channel
)
{
int
port
=
((
InetSocketAddress
)
channel
.
remoteAddress
()).
getPort
();
int
port
=
((
InetSocketAddress
)
channel
.
remoteAddress
()).
getPort
();
return
port
;
return
port
;
}
}
public
static
ProxyInfo
registProxyInfo
(
String
pileId
,
Channel
channel
)
{
public
static
ProxyInfo
registProxyInfo
(
String
pileId
,
Channel
channel
)
{
// 标记连接
// 标记连接
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
if
(!
StrUtil
.
equals
(
attr
.
get
(),
pileId
))
{
if
(!
StrUtil
.
equals
(
attr
.
get
(),
pileId
))
{
log
.
warn
(
"【连接更换】{} -> {}"
,
attr
.
get
(),
pileId
);
log
.
warn
(
"【连接更换】{} -> {}"
,
attr
.
get
(),
pileId
);
}
}
attr
.
set
(
pileId
);
attr
.
set
(
pileId
);
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
if
(
proxyInfo
==
null
)
{
if
(
proxyInfo
==
null
)
{
proxyInfo
=
new
ProxyInfo
(
channel
,
pileId
);
proxyInfo
=
new
ProxyInfo
(
channel
,
pileId
);
proxys
.
put
(
pileId
,
proxyInfo
);
proxys
.
put
(
pileId
,
proxyInfo
);
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
.
setLastTime
(
new
Date
());
proxyInfo
.
setChannel
(
channel
);
return
proxyInfo
;
proxyInfo
.
setLastTime
(
new
Date
());
}
return
proxyInfo
;
}
// 连接断开,暂时不移除连接
public
static
void
removeProxyInfo
(
Channel
channel
)
{
// 连接断开,暂时不移除连接
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
public
static
void
removeProxyInfo
(
Channel
channel
)
{
String
pileId
=
attr
.
get
();
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
String
pileId
=
attr
.
get
();
removeProxyInfo
(
pileId
);
}
removeProxyInfo
(
pileId
);
}
private
synchronized
static
void
removeProxyInfo
(
String
pileId
)
{
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
private
synchronized
static
void
removeProxyInfo
(
String
pileId
)
{
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
if
(
proxyInfo
!=
null
&&
proxyInfo
.
getChannel
().
isActive
())
{
return
;
if
(
proxyInfo
!=
null
&&
proxyInfo
.
getChannel
().
isActive
())
{
}
return
;
}
Ocpp16Helper
.
resetSeq
(
pileId
);
Ocpp16Helper
.
resetSeq
(
pileId
);
log
.
warn
(
"Proxy:"
+
pileId
+
"断联中。。。"
);
proxys
.
remove
(
pileId
);
// 连接断开,暂时移除连接
log
.
warn
(
"Proxy:"
+
pileId
+
"断联中。。。"
);
}
proxys
.
remove
(
pileId
);
// 连接断开,暂时移除连接
}
public
static
ProxyInfo
getProxyInfo
(
Channel
channel
)
{
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
public
static
ProxyInfo
getProxyInfo
(
Channel
channel
)
{
String
pileId
=
attr
.
get
();
Attribute
<
String
>
attr
=
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
);
return
getProxyInfo
(
pileId
);
String
pileId
=
attr
.
get
();
}
return
getProxyInfo
(
pileId
);
}
public
static
ProxyInfo
getProxyInfo
(
String
pileId
)
{
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
public
static
ProxyInfo
getProxyInfo
(
String
pileId
)
{
if
(
proxyInfo
==
null
)
{
ProxyInfo
proxyInfo
=
proxys
.
get
(
pileId
);
throw
new
RuntimeException
(
"【"
+
pileId
+
"】Proxy连接不存在!!!"
);
if
(
proxyInfo
==
null
)
{
}
throw
new
RuntimeException
(
"【"
+
pileId
+
"】Proxy连接不存在!!!"
);
return
proxyInfo
;
}
}
return
proxyInfo
;
}
// TODO 心跳机制
// 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
// 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);
// 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) {
// @SuppressWarnings("unchecked")
// TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// public static <Rsp extends TcpRsp> void callback(Rsp rsp) {
// if (callback != null) {
// TcpCallback<Rsp> callback = (TcpCallback<Rsp>) callbacks.get(rsp.getReqKey());
// callback.callback(rsp);
// 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);
// public static <Req extends TcpReq, Rsp extends TcpRsp> TcpCallback<Rsp> sendAsync(ProxyInfo client, Req req, Class<Rsp> rspClass) {
// addCallback(req.getReqKey(), callback);
// TcpCallback<Rsp> callback = new TcpCallback<>(rspClass);
//
// addCallback(req.getReqKey(), callback);
// String json = req.build();
//
// client.getChannel().writeAndFlush(json);
// String json = req.build();
//
// client.getChannel().writeAndFlush(json);
// log.info("【{}】send: {}", client.getIp(), json);
//
//
// log.info("【{}】send: {}", client.getIp(), json);
// return callback;
//
// }
// return callback;
// }
public
static
void
startManager
()
{
// 5秒钟执行一次
public
static
void
startManager
()
{
TaskExecutor
.
addTask
(()
->
{
// 5秒钟执行一次
Map
<
String
,
ProxyInfo
>
map
=
Maps
.
newHashMap
();
TaskExecutor
.
addTask
(()
->
{
map
.
putAll
(
proxys
);
Map
<
String
,
ProxyInfo
>
map
=
Maps
.
newHashMap
();
map
.
putAll
(
proxys
);
if
(
map
.
isEmpty
())
{
return
;
if
(
map
.
isEmpty
())
{
}
return
;
//
}
log
.
info
(
"【自检】-------------------------------------------------------------------------------"
);
//
// log.info("【自检托管】{}", users);
log
.
info
(
for
(
ProxyInfo
proxy
:
map
.
values
())
{
"【自检】-------------------------------------------------------------------------------"
);
log
.
info
(
"【自检】{}"
,
JacksonUtils
.
nodefaultMapper
.
toJson
(
proxy
));
// log.info("【自检托管】{}", users);
// 1.移除断开连接
for
(
ProxyInfo
proxy
:
map
.
values
())
{
if
(!
proxy
.
getChannel
().
isActive
())
{
log
.
info
(
"【自检】{}"
,
JacksonUtils
.
nodefaultMapper
.
toJson
(
proxy
));
proxy
.
getChannel
().
close
();
// 1.移除断开连接
ProxyHelper
.
removeProxyInfo
(
proxy
.
getChannel
());
if
(!
proxy
.
getChannel
().
isActive
())
{
continue
;
proxy
.
getChannel
().
close
();
}
ProxyHelper
.
removeProxyInfo
(
proxy
.
getChannel
());
}
continue
;
},
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
)
{
public
static
void
addCallback
(
String
key
,
TcpCallback
<
TcpRsp
>
callback
)
{
callbackMap
.
put
(
key
,
callback
);
TcpCallback
<
TcpRsp
>
tcpRspTcpCallback
=
callbackMap
.
get
(
key
);
}
else
{
if
(
tcpRspTcpCallback
==
null
)
{
callback
=
tcpRspTcpCallback
;
callbackMap
.
put
(
key
,
callback
);
}
}
else
{
}
callback
=
tcpRspTcpCallback
;
}
public
static
TcpCallback
<
TcpRsp
>
getCallback
(
String
key
)
{
}
return
callbackMap
.
get
(
key
);
}
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!!!"
);
/**
}
* 获取callback的key
return
channel
.
attr
(
ProxyHelper
.
PILE_ID_KEY
).
get
()
+
"+"
+
data
.
getSeq
();
*
}
* @param data 请求体
* @param channel 通道
* @return
* @param <T>
*/
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
();
}
/**
* 获取callback的key,并设置值
*
* @param data 请求体
* @param channel 通道
* @param rsp 返回值
* @param <T>
*/
public
static
<
T
extends
TcpBase
>
void
getCallbackKey
(
T
data
,
Channel
channel
,
TcpRsp
rsp
)
{
String
key
=
getCallbackKey
(
data
,
channel
);
TcpCallback
<
TcpRsp
>
callback
=
ProxyHelper
.
getCallback
(
key
);
if
(
Objects
.
nonNull
(
callback
))
{
callback
.
callback
(
rsp
);
}
else
{
log
.
info
(
"key{},callback为空"
,
key
);
}
}
}
}
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ModelValTest.java
View file @
1d2a73a
...
@@ -26,7 +26,7 @@ public class ModelValTest {
...
@@ -26,7 +26,7 @@ public class ModelValTest {
ModelValReq
req
=
new
ModelValReq
();
ModelValReq
req
=
new
ModelValReq
();
req
.
setData
(
new
ModelValReqData
());
req
.
setData
(
new
ModelValReqData
());
req
.
getData
().
setPileId
(
Ocpp16Helper
.
strToBcd
(
"
00000002100010
"
,
req
.
getData
().
getPileId
()));
req
.
getData
().
setPileId
(
Ocpp16Helper
.
strToBcd
(
"
32010200000001
"
,
req
.
getData
().
getPileId
()));
req
.
getData
().
setModelCode
(
Ocpp16Helper
.
strToBcd
(
"0000"
,
req
.
getData
().
getModelCode
()));
req
.
getData
().
setModelCode
(
Ocpp16Helper
.
strToBcd
(
"0000"
,
req
.
getData
().
getModelCode
()));
client
.
sendSync
(
req
);
client
.
sendSync
(
req
);
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment