Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
supplyheatdevicedatacollection
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
supplyheatdevicedatacollection
Commits
9fe17817
Commit
9fe17817
authored
May 17, 2022
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
消息恢复修改 gengdidi
parent
86fbb808
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
63 deletions
+77
-63
InformationDecode.java
src/main/java/com/zehong/netty/decode/InformationDecode.java
+6
-1
PackageInformationDecode.java
...ava/com/zehong/netty/decode/PackageInformationDecode.java
+1
-56
TcpServerHandler.java
src/main/java/com/zehong/netty/handler/TcpServerHandler.java
+70
-6
No files found.
src/main/java/com/zehong/netty/decode/InformationDecode.java
View file @
9fe17817
...
...
@@ -11,6 +11,11 @@ public class InformationDecode {
*/
private
MsgDecode
decode
;
/**
* 总分包数
*/
public
String
totalPackageCount
;
public
InformationDecode
(
MsgDecode
decode
){
this
.
decode
=
decode
;
...
...
@@ -23,6 +28,7 @@ public class InformationDecode {
new
DateTimeDecode
(
decode
).
decodeDateTime
();
//分包信息
PackageInformationDecode
packageInformationDecode
=
new
PackageInformationDecode
(
decode
);
this
.
totalPackageCount
=
packageInformationDecode
.
totalPackageCount
;
packageInformationDecode
.
decodePakageInformation
();
if
(
packageInformationDecode
.
isNotSaveCurrentIndex
){
//热量表数据列表
...
...
@@ -37,5 +43,4 @@ public class InformationDecode {
new
ErrorDataEquipmentIdListsDecode
(
decode
,
heatMeterDataListLength
+
valueDataListsLength
).
decodeErrorDataEquipmentIdLists
();
}
}
}
src/main/java/com/zehong/netty/decode/PackageInformationDecode.java
View file @
9fe17817
package
com
.
zehong
.
netty
.
decode
;
import
cn.hutool.core.date.DateUtil
;
import
com.zehong.config.NettyClientConfig
;
import
com.zehong.utils.HexUtils
;
import
com.zehong.utils.NettyClient
;
import
com.zehong.utils.RedisUtil
;
import
com.zehong.utils.SpringUtil
;
import
org.slf4j.Logger
;
...
...
@@ -11,7 +7,6 @@ import org.slf4j.LoggerFactory;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.zip.CRC32
;
/**
* @author gengd
...
...
@@ -34,7 +29,7 @@ public class PackageInformationDecode {
/**
* 总包数
*/
p
rivate
String
totalPackageCount
;
p
ublic
String
totalPackageCount
;
/**
* 当前包数
...
...
@@ -78,14 +73,6 @@ public class PackageInformationDecode {
}
cashInfo
.
add
(
currentPackageIndex
);
isNotSaveCurrentIndex
=
true
;
//如接受到所有分包信息,移除分包缓存
if
(
Integer
.
parseInt
(
totalPackageCount
,
16
)
==
cashInfo
.
size
()){
//确认消息
confirmInformation
();
//移除分包缓存
redisUtil
.
del
(
decode
.
getTransmissionId
());
return
;
}
// 缓存分包信息
redisUtil
.
set
(
decode
.
getTransmissionId
(),
cashInfo
,
60
*
60
*
6
);
return
;
...
...
@@ -95,46 +82,4 @@ public class PackageInformationDecode {
packageInfoCash
.
add
(
currentPackageIndex
);
redisUtil
.
set
(
decode
.
getTransmissionId
(),
packageInfoCash
,
60
*
60
*
6
);
}
/**
* 确认成功接收消息
*/
public
void
confirmInformation
(){
String
head
=
decode
.
getMsgHead
();
String
tail
=
decode
.
getMsgTail
();
String
dateTime
=
getDateTimeHex
();
CRC32
crc32
=
new
CRC32
();
crc32
.
update
((
head
.
substring
(
8
,
head
.
length
())+
dateTime
).
getBytes
());
String
checkCode
=
Long
.
toHexString
(
crc32
.
getValue
());
StringBuilder
reqMsg
=
new
StringBuilder
(
head
);
reqMsg
.
append
(
dateTime
)
.
append
(
checkCode
)
.
append
(
tail
.
substring
(
8
,
12
));
NettyClientConfig
config
=
SpringUtil
.
getBean
(
NettyClientConfig
.
class
);
String
msg
=
NettyClient
.
nettyConnection
(
reqMsg
.
toString
(),
config
.
getHost
(),
config
.
getPort
());
logger
.
info
(
"transmissionId:"
+
decode
.
getTransmissionId
()
+
"消息确认服务器回复消息:"
+
msg
);
}
/**
* 拼接信息单元
* @return String
*/
private
static
String
getDateTimeHex
(){
String
now
=
DateUtil
.
now
();
String
[]
date
=
now
.
split
(
" "
);
//年月日
String
[]
yearDate
=
date
[
0
].
split
(
"-"
);
//时分秒
String
[]
time
=
date
[
1
].
split
(
":"
);
System
.
out
.
println
(
"年:"
+
HexUtils
.
bytesToHex
(
yearDate
[
0
].
substring
(
0
,
2
).
getBytes
())
+
" 时:"
+
time
.
toString
());
StringBuilder
dateTime
=
new
StringBuilder
(
""
);
dateTime
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
0
].
substring
(
0
,
2
))))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
0
].
substring
(
2
,
4
))))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
1
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
2
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
0
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
1
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
2
])));
return
dateTime
.
toString
();
}
}
src/main/java/com/zehong/netty/handler/TcpServerHandler.java
View file @
9fe17817
package
com
.
zehong
.
netty
.
handler
;
import
cn.hutool.core.date.DateUtil
;
import
com.zehong.enums.MessageTypeEnum
;
import
com.zehong.netty.decode.InformationDecode
;
import
com.zehong.netty.decode.MsgDecode
;
import
com.zehong.utils.HexUtils
;
import
com.zehong.utils.RedisUtil
;
import
com.zehong.utils.SpringUtil
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandler.Sharable
;
import
io.netty.channel.ChannelHandlerContext
;
...
...
@@ -12,11 +14,11 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import
io.netty.channel.socket.SocketChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.io.UnsupportedEncodingException
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.zip.CRC32
;
/**
* @author gengd
...
...
@@ -28,9 +30,6 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter {
private
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpServerHandler
.
class
);
@Autowired
private
RedisUtil
redisUtil
;
@Override
public
void
channelActive
(
ChannelHandlerContext
ctx
)
{
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
...
...
@@ -76,7 +75,72 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter {
return
;
}
//解析信息单元
new
InformationDecode
(
decode
).
infoMsgDecode
();
InformationDecode
info
=
new
InformationDecode
(
decode
);
info
.
infoMsgDecode
();
if
(
judgeIsLastPackage
(
decode
,
info
.
totalPackageCount
)){
ctx
.
writeAndFlush
(
confirmInformation
(
decode
));
}
}
/**
* 判断是否为最后一次分包信息
* @param decode 上传数据
* @param totalPackageCount 分包总数
* @return boolean
*/
private
boolean
judgeIsLastPackage
(
MsgDecode
decode
,
String
totalPackageCount
){
RedisUtil
redisUtil
=
SpringUtil
.
getBean
(
RedisUtil
.
class
);
List
<
String
>
cashInfo
=
(
List
<
String
>)
redisUtil
.
get
(
decode
.
getTransmissionId
());
//如接受到所有分包信息,移除分包缓存
if
(
Integer
.
parseInt
(
totalPackageCount
,
16
)
==
cashInfo
.
size
()){
//确认消息
//confirmInformation();
//移除分包缓存
redisUtil
.
del
(
decode
.
getTransmissionId
());
return
true
;
}
return
false
;
}
/**
* 确认成功接收消息
*/
private
String
confirmInformation
(
MsgDecode
decode
){
String
head
=
decode
.
getMsgHead
();
String
tail
=
decode
.
getMsgTail
();
String
dateTime
=
getDateTimeHex
();
CRC32
crc32
=
new
CRC32
();
crc32
.
update
((
head
.
substring
(
8
,
head
.
length
())+
dateTime
).
getBytes
());
String
checkCode
=
Long
.
toHexString
(
crc32
.
getValue
());
StringBuilder
reqMsg
=
new
StringBuilder
(
head
);
reqMsg
.
append
(
dateTime
)
.
append
(
checkCode
)
.
append
(
tail
.
substring
(
8
,
12
));
logger
.
info
(
"transmissionId:"
+
decode
.
getTransmissionId
()
+
"消息确认服务器回复消息:"
+
reqMsg
.
toString
());
return
reqMsg
.
toString
();
}
/**
* 拼接信息单元
* @return String
*/
private
static
String
getDateTimeHex
(){
String
now
=
DateUtil
.
now
();
String
[]
date
=
now
.
split
(
" "
);
//年月日
String
[]
yearDate
=
date
[
0
].
split
(
"-"
);
//时分秒
String
[]
time
=
date
[
1
].
split
(
":"
);
System
.
out
.
println
(
"年:"
+
HexUtils
.
bytesToHex
(
yearDate
[
0
].
substring
(
0
,
2
).
getBytes
())
+
" 时:"
+
time
.
toString
());
StringBuilder
dateTime
=
new
StringBuilder
(
""
);
dateTime
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
0
].
substring
(
0
,
2
))))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
0
].
substring
(
2
,
4
))))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
1
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
yearDate
[
2
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
0
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
1
])))
.
append
(
String
.
format
(
"%02x"
,
Integer
.
parseInt
(
time
[
2
])));
return
dateTime
.
toString
();
}
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment