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 d8e74804
authored
Nov 08, 2023
by
tiger
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
代理管理
1 parent
42367b8b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
23 additions
and
139 deletions
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyHelper.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyInfo.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyUser.java
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ParamSettingTest.java
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/TimeSettingTest.java
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/UploadRealTimeDataTest.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyHelper.java
View file @
d8e7480
package
cn
.
com
.
glsx
.
charging
.
modules
.
gbt
.
ocpp16
.
tcp
.
server
;
package
cn
.
com
.
glsx
.
charging
.
modules
.
gbt
.
ocpp16
.
tcp
.
server
;
import
java.net.InetSocketAddress
;
import
java.net.InetSocketAddress
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
com.google.common.collect.Lists
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Maps
;
import
cn.com.glsx.charging.util.JacksonUtils
;
import
cn.com.glsx.charging.util.JacksonUtils
;
import
cn.com.glsx.charging.util.TaskExecutor
;
import
cn.com.glsx.charging.util.TaskExecutor
;
import
cn.hutool.core.util.StrUtil
;
import
io.netty.channel.Channel
;
import
io.netty.channel.Channel
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -22,9 +18,6 @@ public class ProxyHelper {
...
@@ -22,9 +18,6 @@ public class ProxyHelper {
// ip -> proxyInfo
// ip -> proxyInfo
private
static
Map
<
String
,
ProxyInfo
>
proxys
=
Maps
.
newConcurrentMap
();
private
static
Map
<
String
,
ProxyInfo
>
proxys
=
Maps
.
newConcurrentMap
();
// userId -> proxyUser
public
static
Map
<
String
,
ProxyUser
>
users
=
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();
...
@@ -71,13 +64,6 @@ public class ProxyHelper {
...
@@ -71,13 +64,6 @@ public class ProxyHelper {
return
;
return
;
}
}
// 所有人都踢掉线
for
(
ProxyUser
u
:
users
.
values
())
{
if
(
StrUtil
.
equals
(
u
.
getIp
(),
ip
))
{
// removeProxyUser(u.getUserId(), u, "Proxy超时-自动退出");
}
}
log
.
warn
(
"Proxy:"
+
ip
+
"断联中。。。"
);
log
.
warn
(
"Proxy:"
+
ip
+
"断联中。。。"
);
proxys
.
remove
(
ip
);
// 连接断开,暂时不移除连接
proxys
.
remove
(
ip
);
// 连接断开,暂时不移除连接
}
}
...
@@ -95,18 +81,6 @@ public class ProxyHelper {
...
@@ -95,18 +81,6 @@ public class ProxyHelper {
return
proxyInfo
;
return
proxyInfo
;
}
}
public
static
ProxyInfo
getProxyByUser
(
String
userId
,
boolean
randomIfNull
)
{
ProxyUser
proxyUser
=
getProxyUserSilence
(
userId
);
if
(
proxyUser
==
null
)
{
// TODO 随机选择一个
if
(
proxys
.
size
()
==
1
&&
randomIfNull
)
{
return
randomProxyInfo
();
}
throw
new
ProxyException
(
"ProxyUser心跳超时:"
+
userId
);
}
return
getProxyInfo
(
proxyUser
.
getIp
());
}
public
static
class
ProxyException
extends
RuntimeException
{
public
static
class
ProxyException
extends
RuntimeException
{
public
ProxyException
()
{
public
ProxyException
()
{
...
@@ -122,63 +96,6 @@ public class ProxyHelper {
...
@@ -122,63 +96,6 @@ public class ProxyHelper {
}
}
}
}
public
static
ProxyInfo
randomProxyInfo
()
{
if
(
proxys
.
isEmpty
())
{
throw
new
RuntimeException
(
"Proxy连接池为空!!!"
);
}
List
<
ProxyInfo
>
list
=
Lists
.
newArrayList
(
proxys
.
values
());
Collections
.
shuffle
(
list
);
for
(
ProxyInfo
p
:
list
)
{
if
(
p
.
getChannel
().
isActive
())
{
return
p
;
}
}
throw
new
RuntimeException
(
"Proxy连接池为空!!!"
);
}
// public static void addProxyUser(Channel channel, SelfInfoTcpData data) {
// addProxyInfo(channel);
// if (data == null || StrUtil.isEmpty(data.getUserId())) {
// return;
// }
//
// String ip = getIp(channel);
// ProxyUser proxyUser = users.get(data.getUserId());
// if (proxyUser == null) {
// proxyUser = new ProxyUser(channel, data);
// users.put(data.getUserId(), proxyUser);
// log.info("【{}】Proxy用户映射:{}", ip, data);
// }
// proxyUser.setLastTime(new Date());
// }
public
static
ProxyUser
getProxyUserSilence
(
String
userId
)
{
if
(
userId
==
null
)
{
return
null
;
}
ProxyUser
proxyUser
=
users
.
get
(
userId
);
if
(
proxyUser
==
null
||
proxyUser
.
isHeartbeatTimeout
())
{
// removeProxyUser(userId, proxyUser, "心跳超时-自动退出");
}
return
proxyUser
;
}
// private static void removeProxyUser(String userId, ProxyUser proxyUser, String reason) {
// LogoutTcpData data = new LogoutTcpData();
// data.setUserId(userId);
// data.setQuitReason(88);
// data.setQuitReasonDesc(reason);
// if (proxyUser != null && proxyUser.getData() != null) {
// data.setCorpShortName(proxyUser.getData().getCorpShortName());
// data.setCorpFullName(proxyUser.getData().getCorpFullName());
// data.setCorpId(proxyUser.getData().getCorpId());
// }
//
// LogoutTcpNotify notify = new LogoutTcpNotify();
// notify.setData(data);
// TcpHandler.notifys.add(notify);
// }
// TODO 心跳机制
// 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
// web_req -> web_req_handler -> tcp_req -> tcp_req_handler -> dll_req -> dll_handler -> dll_rsp -> tcp_rsp -> tcp_rsp_handler -> web_rsp
...
@@ -218,7 +135,7 @@ public class ProxyHelper {
...
@@ -218,7 +135,7 @@ public class ProxyHelper {
}
}
//
//
log
.
info
(
"【自检】-------------------------------------------------------------------------------"
);
log
.
info
(
"【自检】-------------------------------------------------------------------------------"
);
log
.
info
(
"【自检托管】{}"
,
users
);
//
log.info("【自检托管】{}", users);
for
(
ProxyInfo
proxy
:
map
.
values
())
{
for
(
ProxyInfo
proxy
:
map
.
values
())
{
log
.
info
(
"【自检】{}"
,
JacksonUtils
.
nodefaultMapper
.
toJson
(
proxy
));
log
.
info
(
"【自检】{}"
,
JacksonUtils
.
nodefaultMapper
.
toJson
(
proxy
));
// 1.移除断开连接
// 1.移除断开连接
...
...
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyInfo.java
View file @
d8e7480
...
@@ -20,10 +20,16 @@ public class ProxyInfo {
...
@@ -20,10 +20,16 @@ public class ProxyInfo {
private
String
ip
;
private
String
ip
;
private
int
port
;
private
int
port
;
/** Tcp连接 */
private
Channel
channel
;
private
Channel
channel
;
// 时间
/** 充电桩id */
private
String
pileId
;
/** 连接时间 */
private
Date
connectTime
=
new
Date
();
private
Date
connectTime
=
new
Date
();
/** 上次交互时间 */
private
Date
lastTime
=
new
Date
();
private
Date
lastTime
=
new
Date
();
public
ProxyInfo
(
Channel
channel
)
{
public
ProxyInfo
(
Channel
channel
)
{
...
@@ -33,11 +39,15 @@ public class ProxyInfo {
...
@@ -33,11 +39,15 @@ public class ProxyInfo {
this
.
port
=
ProxyHelper
.
getPort
(
channel
);
this
.
port
=
ProxyHelper
.
getPort
(
channel
);
}
}
private
static
int
HEARTBEAT_TIMEOUT_SECONDS
=
2
*
60
;
// 心跳过期时间(8
秒每次,3次没心跳则过期)
private
static
int
HEARTBEAT_TIMEOUT_SECONDS
=
30
;
// 心跳过期时间(10
秒每次,3次没心跳则过期)
//
四
次心跳缺失,认为断联
//
3
次心跳缺失,认为断联
public
boolean
isLastTimeout
()
{
public
boolean
isLastTimeout
()
{
return
lastTime
.
before
(
DateUtils
.
addSecond
(
new
Date
(),
-
HEARTBEAT_TIMEOUT_SECONDS
));
return
lastTime
.
before
(
DateUtils
.
addSecond
(
new
Date
(),
-
HEARTBEAT_TIMEOUT_SECONDS
));
}
}
public
boolean
isActive
()
{
return
channel
!=
null
&&
channel
.
isActive
();
}
}
}
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/ProxyUser.java
deleted
100644 → 0
View file @
42367b8
package
cn
.
com
.
glsx
.
charging
.
modules
.
gbt
.
ocpp16
.
tcp
.
server
;
import
java.util.Date
;
import
cn.com.glsx.charging.util.DateUtils
;
import
cn.com.glsx.charging.util.JacksonUtils
;
import
lombok.Getter
;
import
lombok.Setter
;
/**
* 连接上来的客户端包含的用户信息
*
* @author kome.jiang
* @date 2022年9月27日 下午7:36:02
*/
@Getter
@Setter
public
class
ProxyUser
{
private
String
ip
;
// 时间
private
Date
loginTime
=
new
Date
();
private
Date
lastTime
=
new
Date
();
// 用户基本资料
private
String
userId
;
// private SelfInfoTcpData data;
// public ProxyUser(Channel channel, SelfInfoTcpData data2) {
// this.ip = ProxyHelper.getIp(channel);
// this.userId = data2.getUserId();
// this.data = data2;
// }
private
static
final
int
HEARTBEAT_TIMEOUT_SECONDS
=
5
*
8
;
public
boolean
isHeartbeatTimeout
()
{
return
lastTime
.
before
(
DateUtils
.
addSecond
(
new
Date
(),
-
HEARTBEAT_TIMEOUT_SECONDS
));
}
@Override
public
String
toString
()
{
return
JacksonUtils
.
toJson
(
this
);
}
}
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ParamSettingTest.java
View file @
d8e7480
package
cn
.
com
.
glsx
.
charging
.
gbt
.
ocpp16
;
package
cn
.
com
.
glsx
.
charging
.
gbt
.
ocpp16
;
import
org.junit.Test
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ParamSettingReqData
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.ParamSettingReqData
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ParamSettingReq
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.ParamSettingReq
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyHelper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyHelper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo
;
import
org.junit.Test
;
/**
/**
* @Title:
* @Title:
...
@@ -37,7 +38,7 @@ public class ParamSettingTest {
...
@@ -37,7 +38,7 @@ public class ParamSettingTest {
ProxyInfo
proxyInfo
=
ProxyHelper
.
getProxyInfo
(
host
);
ProxyInfo
proxyInfo
=
ProxyHelper
.
getProxyInfo
(
host
);
if
(
proxyInfo
!=
null
)
{
if
(
proxyInfo
!=
null
)
{
ParamSettingReqData
data
=
new
ParamSettingReqData
();
ParamSettingReqData
data
=
new
ParamSettingReqData
();
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setAuthStatus
((
byte
)
0
);
data
.
setAuthStatus
((
byte
)
0
);
data
.
setMaxPower
((
byte
)
100
);
data
.
setMaxPower
((
byte
)
100
);
...
...
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/TimeSettingTest.java
View file @
d8e7480
package
cn
.
com
.
glsx
.
charging
.
gbt
.
ocpp16
;
package
cn
.
com
.
glsx
.
charging
.
gbt
.
ocpp16
;
import
java.util.Date
;
import
org.junit.Test
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TimeSettingReqData
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TimeSettingReqData
;
...
@@ -9,9 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo;
...
@@ -9,9 +13,6 @@ import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.ProxyInfo;
import
cn.com.glsx.charging.util.ByteUtils
;
import
cn.com.glsx.charging.util.ByteUtils
;
import
cn.com.glsx.charging.util.DateUtils
;
import
cn.com.glsx.charging.util.DateUtils
;
import
cn.hutool.core.util.HexUtil
;
import
cn.hutool.core.util.HexUtil
;
import
org.junit.Test
;
import
java.util.Date
;
/**
/**
* @Title:
* @Title:
...
@@ -46,7 +47,7 @@ public class TimeSettingTest {
...
@@ -46,7 +47,7 @@ public class TimeSettingTest {
String
cp56Time2a
=
ByteUtils
.
cp56Time2a
(
date
);
String
cp56Time2a
=
ByteUtils
.
cp56Time2a
(
date
);
TimeSettingReqData
data
=
new
TimeSettingReqData
();
TimeSettingReqData
data
=
new
TimeSettingReqData
();
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setTime
(
HexUtil
.
decodeHex
(
cp56Time2a
));
data
.
setTime
(
HexUtil
.
decodeHex
(
cp56Time2a
));
System
.
out
.
println
(
"Cp56Time2a: "
+
cp56Time2a
);
System
.
out
.
println
(
"Cp56Time2a: "
+
cp56Time2a
);
...
...
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/UploadRealTimeDataTest.java
View file @
d8e7480
...
@@ -26,7 +26,7 @@ public class UploadRealTimeDataTest {
...
@@ -26,7 +26,7 @@ public class UploadRealTimeDataTest {
UploadRealtimeDataReq
req
=
new
UploadRealtimeDataReq
();
UploadRealtimeDataReq
req
=
new
UploadRealtimeDataReq
();
RealTimeReqData
data
=
new
RealTimeReqData
();
RealTimeReqData
data
=
new
RealTimeReqData
();
data
.
setTransactionSerialNumber
(
Ocpp16Helper
.
strToBcd
(
"00000000000000000000000000000000"
,
data
.
getTransactionSerialNumber
()));
data
.
setTransactionSerialNumber
(
Ocpp16Helper
.
strToBcd
(
"00000000000000000000000000000000"
,
data
.
getTransactionSerialNumber
()));
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setPileId
(
Ocpp16Helper
.
strToBcd
(
"55031412782305"
,
data
.
getPileId
()));
data
.
setGunNumber
((
byte
)
1
);
data
.
setGunNumber
((
byte
)
1
);
data
.
setStatus
((
byte
)
0
);
data
.
setStatus
((
byte
)
0
);
data
.
setIfReturn
((
byte
)
1
);
data
.
setIfReturn
((
byte
)
1
);
...
...
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