Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gass-device-report
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
耿迪迪
gass-device-report
Commits
fef6c767
Commit
fef6c767
authored
Sep 26, 2021
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
设备上报协议 gengdidi
parent
06947ed3
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
162 additions
and
40 deletions
+162
-40
NettyTCPStart.java
...java/com/zehong/gassdevicereport/netty/NettyTCPStart.java
+8
-7
NettyUDPStart.java
...java/com/zehong/gassdevicereport/netty/NettyUDPStart.java
+61
-0
PressureDecryptReportedData.java
...rt/netty/decryptreported/PressureDecryptReportedData.java
+4
-4
FlowmeterDecryReportedDataStrategy.java
...eported/flowmeter/FlowmeterDecryReportedDataStrategy.java
+3
-3
TCPServerHandler.java
...hong/gassdevicereport/netty/handler/TCPServerHandler.java
+16
-24
UDPServerHandler.java
...hong/gassdevicereport/netty/handler/UDPServerHandler.java
+65
-0
application.yml
src/main/resources/application.yml
+5
-2
No files found.
src/main/java/com/zehong/gassdevicereport/netty/NettyStart.java
→
src/main/java/com/zehong/gassdevicereport/netty/Netty
TCP
Start.java
View file @
fef6c767
package
com
.
zehong
.
gassdevicereport
.
netty
;
import
com.zehong.gassdevicereport.netty.handler.TCPServerHandler
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelInitializer
;
...
...
@@ -21,16 +22,16 @@ import javax.annotation.Resource;
* 物联网 开启检测 并写入数据库
*/
@Component
public
class
NettyStart
{
public
class
Netty
TCP
Start
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
NettyStart
.
class
);
private
Logger
logger
=
LoggerFactory
.
getLogger
(
Netty
TCP
Start
.
class
);
@Resource
private
ServerHandler
s
erverHandler
;
private
TCPServerHandler
TCPS
erverHandler
;
private
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
private
EventLoopGroup
workGroup
=
new
NioEventLoopGroup
();
@Value
(
"${
n
etty.ports}"
)
@Value
(
"${
TCPN
etty.ports}"
)
private
int
[]
ports
;
/**
* 启动netty服务
...
...
@@ -45,14 +46,14 @@ public class NettyStart {
.
childHandler
(
new
ChannelInitializer
<
SocketChannel
>()
{
@Override
protected
void
initChannel
(
SocketChannel
socketChannel
)
throws
Exception
{
socketChannel
.
pipeline
().
addLast
(
s
erverHandler
);
socketChannel
.
pipeline
().
addLast
(
TCPS
erverHandler
);
}
});
//开启需要监听 的端口
for
(
int
port
:
ports
){
ChannelFuture
future
=
b
.
bind
(
port
).
sync
();
if
(
future
.
isSuccess
())
{
logger
.
info
(
"启动 "
+
port
+
" 成功"
);
logger
.
info
(
"
TCP server
启动 "
+
port
+
" 成功"
);
}
}
}
...
...
@@ -64,6 +65,6 @@ public class NettyStart {
public
void
destroy
()
{
bossGroup
.
shutdownGracefully
().
syncUninterruptibly
();
workGroup
.
shutdownGracefully
().
syncUninterruptibly
();
logger
.
info
(
"关闭 Netty 成功"
);
logger
.
info
(
"
TCP server
关闭 Netty 成功"
);
}
}
src/main/java/com/zehong/gassdevicereport/netty/NettyUDPStart.java
0 → 100644
View file @
fef6c767
package
com
.
zehong
.
gassdevicereport
.
netty
;
import
com.zehong.gassdevicereport.netty.handler.UDPServerHandler
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioDatagramChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PreDestroy
;
import
javax.annotation.Resource
;
/**
* 物联网 开启检测 并写入数据库
*/
@Component
public
class
NettyUDPStart
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
NettyUDPStart
.
class
);
@Resource
private
UDPServerHandler
UDPServerHandler
;
private
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
@Value
(
"${UDPNetty.ports}"
)
private
int
[]
ports
;
/**
* 启动netty服务
* @throws InterruptedException
*/
@PostConstruct
public
void
start
()
throws
InterruptedException
{
Bootstrap
b
=
new
Bootstrap
();
b
.
group
(
bossGroup
)
.
channel
(
NioDatagramChannel
.
class
)
.
option
(
ChannelOption
.
SO_BACKLOG
,
128
)
.
handler
(
UDPServerHandler
);
//开启需要监听 的端口
for
(
int
port
:
ports
){
ChannelFuture
future
=
b
.
bind
(
port
).
sync
();
if
(
future
.
isSuccess
())
{
logger
.
info
(
"UDP server启动 "
+
port
+
" 成功"
);
}
}
}
/**
* 销毁
*/
@PreDestroy
public
void
destroy
()
{
bossGroup
.
shutdownGracefully
().
syncUninterruptibly
();
logger
.
info
(
"UDP server关闭 Netty 成功"
);
}
}
src/main/java/com/zehong/gassdevicereport/netty/decryptreported/PressureDecryptReportedData.java
View file @
fef6c767
...
...
@@ -64,16 +64,16 @@ public class PressureDecryptReportedData {
private
void
getDeviceType
(){
String
deviceTypeNum
=
reportedDataStr
.
substring
(
30
,
32
);
switch
(
deviceTypeNum
){
case
"
A
0"
:
case
"
a
0"
:
this
.
deviceType
=
"ZH11-NB家用报警器"
;
break
;
case
"
A
1"
:
case
"
a
1"
:
this
.
deviceType
=
"JT-ZH85N家用报警器"
;
break
;
case
"
A
2"
:
case
"
a
2"
:
this
.
deviceType
=
"燃气压力监测仪"
;
break
;
case
"
A
3"
:
case
"
a
3"
:
this
.
deviceType
=
"消防水压监测仪"
;
break
;
default
:
...
...
src/main/java/com/zehong/gassdevicereport/netty/decryptreported/flowmeter/FlowmeterDecryReportedDataStrategy.java
View file @
fef6c767
...
...
@@ -8,11 +8,11 @@ public class FlowmeterDecryReportedDataStrategy {
private
static
DecryptReportedData
decryptReportedData
(
String
hexStr
,
String
reportedDataStr
){
switch
(
hexStr
){
case
"
B
0"
:
case
"
b
0"
:
return
new
FlowmeterDecryptReportedData
(
reportedDataStr
,
"苍南EVC300体积修正仪"
);
case
"
B
1"
:
case
"
b
1"
:
return
new
FlowmeterDecryptReportedData
(
reportedDataStr
,
"天信CPU卡式体积修正仪"
);
case
"
B
2"
:
case
"
b
2"
:
return
new
Tian5cFlowmeterDecryptReportedData
(
reportedDataStr
,
"天津市第五机床厂C型体积积算仪(天五C型)"
);
default
:
return
null
;
...
...
src/main/java/com/zehong/gassdevicereport/netty/ServerHandler.java
→
src/main/java/com/zehong/gassdevicereport/netty/
handler/TCP
ServerHandler.java
View file @
fef6c767
package
com
.
zehong
.
gassdevicereport
.
netty
;
package
com
.
zehong
.
gassdevicereport
.
netty
.
handler
;
import
com.zehong.gassdevicereport.constant.Constant
;
import
com.zehong.gassdevicereport.entity.ReciveReportData
;
import
com.zehong.gassdevicereport.entity.TDeviceReportData
;
import
com.zehong.gassdevicereport.netty.decryptreported.flowmeter.DecryptReportedData
;
import
com.zehong.gassdevicereport.netty.decryptreported.flowmeter.FlowmeterDecryReportedDataStrategy
;
import
com.zehong.gassdevicereport.netty.decryptreported.PressureDecryptReportedData
;
import
com.zehong.gassdevicereport.service.ITDeviceReportDataService
;
import
com.zehong.gassdevicereport.utils.CrcUtil
;
import
com.zehong.gassdevicereport.utils.HexUtils
;
...
...
@@ -15,7 +14,6 @@ import io.netty.channel.ChannelHandler.Sharable;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.util.CharsetUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -29,9 +27,9 @@ import java.util.Date;
*/
@Component
@Sharable
public
class
ServerHandler
extends
ChannelInboundHandlerAdapter
{
public
class
TCP
ServerHandler
extends
ChannelInboundHandlerAdapter
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
ServerHandler
.
class
);
private
Logger
logger
=
LoggerFactory
.
getLogger
(
TCP
ServerHandler
.
class
);
@Autowired
private
RedisUtil
redisUtil
;
...
...
@@ -72,12 +70,14 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
//msg为接收到的客户端传递的数据 个人这边直接传的json 数据
ByteBuf
readMessage
=
(
ByteBuf
)
msg
;
String
msgStr
=
readMessage
.
toString
(
CharsetUtil
.
UTF_8
);
logger
.
info
(
"receiveMsgstr======="
+
msgStr
);
/*if(msgStr.length() != 124){
return;
}*/
//ByteBuf readMessage= (ByteBuf) msg;
//String msgStr = readMessage.toString(CharsetUtil.UTF_8);
ByteBuf
buf
=
(
ByteBuf
)
msg
;
byte
[]
bytes1
=
new
byte
[
buf
.
readableBytes
()];
//复制内容到字节数组bytes
buf
.
readBytes
(
bytes1
);
String
msgStr
=
HexUtils
.
bytesToHex
(
bytes1
);
logger
.
info
(
"receiveTCPMsgstr======="
+
msgStr
);
byte
[]
bytes
=
HexUtils
.
hexToByteArray
(
msgStr
.
substring
(
0
,
msgStr
.
length
()-
4
));
String
checkVule
=
CrcUtil
.
getCrcToHex
(
bytes
,
true
);
String
checkCode
=
msgStr
.
substring
(
msgStr
.
length
()-
4
,
msgStr
.
length
());
...
...
@@ -86,20 +86,12 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
throw
new
Exception
(
"接收数据校验失败"
);
}
SocketChannel
channel
=(
SocketChannel
)
ctx
.
channel
();
int
port
=
channel
.
localAddress
().
getPort
();
TDeviceReportData
tDeviceReportData
=
new
TDeviceReportData
();
if
(
2396
==
port
){
DecryptReportedData
decryptReportedData
=
FlowmeterDecryReportedDataStrategy
.
getDecryptReportedData
(
msgStr
);
if
(
null
==
decryptReportedData
){
logger
.
error
(
"未获取到流量计类型"
);
throw
new
Exception
(
"未获取到流量计类型"
);
}
tDeviceReportData
=
decryptReportedData
.
getReportedData
();
}
if
(
65011
==
port
){
tDeviceReportData
=
new
PressureDecryptReportedData
(
msgStr
).
getReportedData
();
DecryptReportedData
decryptReportedData
=
FlowmeterDecryReportedDataStrategy
.
getDecryptReportedData
(
msgStr
);
if
(
null
==
decryptReportedData
){
logger
.
error
(
"未获取到流量计类型"
);
throw
new
Exception
(
"未获取到流量计类型"
);
}
TDeviceReportData
tDeviceReportData
=
decryptReportedData
.
getReportedData
();
//缓存设备信息
ReciveReportData
reciveReportData
=
new
ReciveReportData
();
reciveReportData
.
setDeviceCode
(
tDeviceReportData
.
getDeviceNum
());
...
...
src/main/java/com/zehong/gassdevicereport/netty/handler/UDPServerHandler.java
0 → 100644
View file @
fef6c767
package
com
.
zehong
.
gassdevicereport
.
netty
.
handler
;
import
com.zehong.gassdevicereport.constant.Constant
;
import
com.zehong.gassdevicereport.entity.ReciveReportData
;
import
com.zehong.gassdevicereport.entity.TDeviceReportData
;
import
com.zehong.gassdevicereport.netty.decryptreported.PressureDecryptReportedData
;
import
com.zehong.gassdevicereport.service.ITDeviceReportDataService
;
import
com.zehong.gassdevicereport.utils.CrcUtil
;
import
com.zehong.gassdevicereport.utils.HexUtils
;
import
com.zehong.gassdevicereport.utils.RedisUtil
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandler.Sharable
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.channel.socket.DatagramPacket
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
@Component
@Sharable
public
class
UDPServerHandler
extends
SimpleChannelInboundHandler
<
DatagramPacket
>
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
UDPServerHandler
.
class
);
@Autowired
private
RedisUtil
redisUtil
;
@Autowired
private
ITDeviceReportDataService
itDeviceReportDataService
;
private
final
static
String
METER
=
"4"
;
private
final
static
String
METER_ALARM
=
"1"
;
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
DatagramPacket
msg
)
throws
Exception
{
//msg为接收到的客户端传递的数据 个人这边直接传的json 数据
ByteBuf
buf
=
msg
.
content
();
byte
[]
bytes1
=
new
byte
[
buf
.
readableBytes
()];
//复制内容到字节数组bytes
buf
.
readBytes
(
bytes1
);
String
msgStr
=
HexUtils
.
bytesToHex
(
bytes1
);
logger
.
info
(
"receiveUPDMsgstr======="
+
msgStr
);
byte
[]
bytes
=
HexUtils
.
hexToByteArray
(
msgStr
.
substring
(
0
,
msgStr
.
length
()-
4
));
String
checkVule
=
CrcUtil
.
getCrcToHex
(
bytes
,
true
);
String
checkCode
=
msgStr
.
substring
(
msgStr
.
length
()-
4
,
msgStr
.
length
());
if
(!
checkVule
.
equals
(
checkCode
)){
logger
.
error
(
"接收数据校验失败"
);
throw
new
Exception
(
"接收数据校验失败"
);
}
TDeviceReportData
tDeviceReportData
=
new
PressureDecryptReportedData
(
msgStr
).
getReportedData
();
//缓存设备信息
ReciveReportData
reciveReportData
=
new
ReciveReportData
();
reciveReportData
.
setDeviceCode
(
tDeviceReportData
.
getDeviceNum
());
reciveReportData
.
setDeviceType
(
METER
);
reciveReportData
.
setAlarmType
(
METER_ALARM
);
reciveReportData
.
setPressure
(
tDeviceReportData
.
getPressure
());
redisUtil
.
set
(
Constant
.
DEVICE_REDIS_KEY
+
reciveReportData
.
getDeviceCode
(),
tDeviceReportData
);
//设备上报数据入库
itDeviceReportDataService
.
insertTDeviceReportData
(
tDeviceReportData
);
}
}
src/main/resources/application.yml
View file @
fef6c767
...
...
@@ -64,5 +64,8 @@ logging:
#webSocektUrl
webSocektUrl
:
http://36.148.23.59:8901/gassafety/websocket/send
netty
:
ports
:
2396,65011
TCPNetty
:
ports
:
2396
UDPNetty
:
ports
:
65011
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