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 67284dc7
authored
Nov 06, 2023
by
tiger
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
TcpBase构建时自动填充len字段和crc字段
1 parent
8629cf8c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
13 deletions
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/ClientMain.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/Ocpp16Helper.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/client/TcpClient.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/TcpServerHandler.java
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ClientSimulateTest.java
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/ClientMain.java
View file @
67284dc
...
@@ -49,9 +49,6 @@ public class ClientMain {
...
@@ -49,9 +49,6 @@ public class ClientMain {
req
.
getData
().
setSoftVersion
(
Ocpp16Helper
.
strToBcd
(
"v4.1.50"
,
req
.
getData
().
getSoftVersion
()));
req
.
getData
().
setSoftVersion
(
Ocpp16Helper
.
strToBcd
(
"v4.1.50"
,
req
.
getData
().
getSoftVersion
()));
req
.
getData
().
setProtocolVersion
((
byte
)
10
);
req
.
getData
().
setProtocolVersion
((
byte
)
10
);
Ocpp16Helper
.
fillLen
(
req
);
req
.
setCrc
(
Ocpp16Helper
.
calculateCrc
(
req
));
client
.
sendSync
(
req
);
client
.
sendSync
(
req
);
while
(
true
)
{
while
(
true
)
{
...
...
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/Ocpp16Helper.java
View file @
67284dc
...
@@ -76,12 +76,20 @@ public class Ocpp16Helper {
...
@@ -76,12 +76,20 @@ public class Ocpp16Helper {
return
(
short
)
rs
;
// 溢出自动清零
return
(
short
)
rs
;
// 溢出自动清零
}
}
public
static
<
D
extends
TcpData
,
P
extends
TcpBase
<
D
>>
void
fillLen
(
P
b
)
{
/**
* TcpBase在build时自动填充
*/
@Deprecated
private
static
<
D
extends
TcpData
,
P
extends
TcpBase
<
D
>>
void
fillLen
(
P
b
)
{
byte
[]
bs
=
build
(
b
);
byte
[]
bs
=
build
(
b
);
b
.
setLen
((
byte
)
(
bs
.
length
-
4
));
b
.
setLen
((
byte
)
(
bs
.
length
-
4
));
}
}
public
static
<
D
extends
TcpData
,
P
extends
TcpBase
<
D
>>
short
calculateCrc
(
P
b
)
{
/**
* TcpBase在build时自动填充
*/
@Deprecated
private
static
<
D
extends
TcpData
,
P
extends
TcpBase
<
D
>>
short
calculateCrc
(
P
b
)
{
byte
[]
bs
=
build
(
b
);
byte
[]
bs
=
build
(
b
);
byte
[]
data
=
ByteUtils
.
getBytes
(
bs
,
2
,
bs
.
length
-
4
);
byte
[]
data
=
ByteUtils
.
getBytes
(
bs
,
2
,
bs
.
length
-
4
);
short
crc
=
(
short
)
CrcUtils
.
calculateCRC
(
data
);
short
crc
=
(
short
)
CrcUtils
.
calculateCRC
(
data
);
...
@@ -209,6 +217,18 @@ public class Ocpp16Helper {
...
@@ -209,6 +217,18 @@ public class Ocpp16Helper {
}
}
}
}
// 自动补充len字段和crc字段
if
(
d
instanceof
TcpBase
)
{
// 补充长度字段
byte
len
=
(
byte
)
(
byteBuf
.
readableBytes
()
-
4
);
byteBuf
.
setByte
(
1
,
len
);
// 补充crc
byte
[]
data
=
byteBuf
.
slice
(
2
,
byteBuf
.
readableBytes
()
-
4
).
array
();
short
crc
=
(
short
)
CrcUtils
.
calculateCRC
(
data
);
byteBuf
.
setShort
(
byteBuf
.
readableBytes
()
-
2
,
crc
);
}
// 读取数据
// 读取数据
byte
[]
data
=
new
byte
[
byteBuf
.
readableBytes
()];
byte
[]
data
=
new
byte
[
byteBuf
.
readableBytes
()];
byteBuf
.
readBytes
(
data
);
byteBuf
.
readBytes
(
data
);
...
...
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/client/TcpClient.java
View file @
67284dc
...
@@ -3,6 +3,7 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client;
...
@@ -3,6 +3,7 @@ package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client;
import
java.net.InetSocketAddress
;
import
java.net.InetSocketAddress
;
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.tcp.base.TcpBase
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer
;
import
cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer
;
import
cn.com.glsx.charging.util.ByteUtils
;
import
cn.com.glsx.charging.util.ByteUtils
;
import
cn.com.glsx.charging.util.Task
;
import
cn.com.glsx.charging.util.Task
;
...
@@ -95,7 +96,8 @@ public class TcpClient {
...
@@ -95,7 +96,8 @@ public class TcpClient {
},
10
*
1000
);
},
10
*
1000
);
}
}
public
<
T
>
void
sendSync
(
T
data
)
{
@SuppressWarnings
({
"rawtypes"
})
public
<
T
extends
TcpBase
>
void
sendSync
(
T
data
)
{
byte
[]
bs
=
Ocpp16Helper
.
build
(
data
);
byte
[]
bs
=
Ocpp16Helper
.
build
(
data
);
sendSync
(
bs
);
sendSync
(
bs
);
...
@@ -105,7 +107,7 @@ public class TcpClient {
...
@@ -105,7 +107,7 @@ public class TcpClient {
log
.
info
(
"【{}】【{}】send2: {} "
,
ip
,
data
.
getClass
().
getSimpleName
(),
data
);
log
.
info
(
"【{}】【{}】send2: {} "
,
ip
,
data
.
getClass
().
getSimpleName
(),
data
);
}
}
p
ublic
void
sendSync
(
byte
[]
bytes
)
{
p
rivate
void
sendSync
(
byte
[]
bytes
)
{
try
{
try
{
if
(
future
==
null
||
future
.
channel
()
==
null
)
{
if
(
future
==
null
||
future
.
channel
()
==
null
)
{
log
.
info
(
"future null ..."
);
log
.
info
(
"future null ..."
);
...
...
platform-charging/src/main/java/cn/com/glsx/charging/modules/gbt/ocpp16/tcp/server/TcpServerHandler.java
View file @
67284dc
...
@@ -59,6 +59,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
...
@@ -59,6 +59,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<byte[]> {
// }
// }
// }
// }
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
@Override
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
byte
[]
bytes
)
throws
Exception
{
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
byte
[]
bytes
)
throws
Exception
{
String
ip
=
TcpServer
.
getIp
(
ctx
);
String
ip
=
TcpServer
.
getIp
(
ctx
);
...
...
platform-charging/src/test/java/cn/com/glsx/charging/gbt/ocpp16/ClientSimulateTest.java
View file @
67284dc
...
@@ -35,9 +35,6 @@ public class ClientSimulateTest {
...
@@ -35,9 +35,6 @@ public class ClientSimulateTest {
req
.
getData
().
setSoftVersion
(
Ocpp16Helper
.
strToBcd
(
"v4.1.50"
,
req
.
getData
().
getSoftVersion
()));
req
.
getData
().
setSoftVersion
(
Ocpp16Helper
.
strToBcd
(
"v4.1.50"
,
req
.
getData
().
getSoftVersion
()));
req
.
getData
().
setProtocolVersion
((
byte
)
10
);
req
.
getData
().
setProtocolVersion
((
byte
)
10
);
Ocpp16Helper
.
fillLen
(
req
);
req
.
setCrc
(
Ocpp16Helper
.
calculateCrc
(
req
));
client
.
sendSync
(
req
);
client
.
sendSync
(
req
);
// 定期心跳
// 定期心跳
...
@@ -48,9 +45,6 @@ public class ClientSimulateTest {
...
@@ -48,9 +45,6 @@ public class ClientSimulateTest {
hdata
.
setStatus
((
byte
)
0
);
hdata
.
setStatus
((
byte
)
0
);
heartbeat
.
setData
(
hdata
);
heartbeat
.
setData
(
hdata
);
Ocpp16Helper
.
fillLen
(
heartbeat
);
heartbeat
.
setCrc
(
Ocpp16Helper
.
calculateCrc
(
heartbeat
));
client
.
sendSync
(
heartbeat
);
client
.
sendSync
(
heartbeat
);
Thread
.
sleep
(
10000
);
Thread
.
sleep
(
10000
);
...
...
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