Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
environment_demo_down
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
shiyubin
environment_demo_down
Commits
dce6c9e0
Commit
dce6c9e0
authored
Aug 27, 2020
by
shiyubin
🌼
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http请求延迟处理
parent
ffe12602
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
69 additions
and
26 deletions
+69
-26
NettyApplication.java
src/main/java/com/gemho/netty/NettyApplication.java
+1
-1
SocketController.java
...ain/java/com/gemho/netty/controller/SocketController.java
+52
-22
nettyHandler.java
src/main/java/com/gemho/netty/controller/nettyHandler.java
+16
-3
SocketController.class
...classes/com/gemho/netty/controller/SocketController.class
+0
-0
nettyHandler.class
target/classes/com/gemho/netty/controller/nettyHandler.class
+0
-0
No files found.
src/main/java/com/gemho/netty/NettyApplication.java
View file @
dce6c9e0
...
@@ -11,7 +11,7 @@ public class NettyApplication {
...
@@ -11,7 +11,7 @@ public class NettyApplication {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
NettyApplication
.
class
,
args
);
SpringApplication
.
run
(
NettyApplication
.
class
,
args
);
new
nettyServer
(
999
9
);
new
nettyServer
(
999
7
);
}
}
}
}
src/main/java/com/gemho/netty/controller/SocketController.java
View file @
dce6c9e0
package
com
.
gemho
.
netty
.
controller
;
package
com
.
gemho
.
netty
.
controller
;
import
cn.hutool.core.util.URLUtil
;
import
cn.hutool.http.HttpUtil
;
import
com.gemho.netty.model.ResultModel
;
import
com.gemho.netty.model.ResultModel
;
import
com.gemho.netty.util.CRC16Util
;
import
com.gemho.netty.util.CRC16Util
;
import
com.gemho.netty.util.MapUtils
;
import
com.gemho.netty.util.MapUtils
;
...
@@ -10,14 +8,9 @@ import com.gemho.netty.util.StringUtil;
...
@@ -10,14 +8,9 @@ import com.gemho.netty.util.StringUtil;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
import
io.netty.buffer.Unpooled
;
import
io.netty.channel.Channel
;
import
io.netty.channel.Channel
;
import
io.netty.channel.ChannelFuture
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.net.URLEncoder
;
@RestController
@RestController
public
class
SocketController
{
public
class
SocketController
{
...
@@ -30,7 +23,9 @@ public class SocketController {
...
@@ -30,7 +23,9 @@ public class SocketController {
*/
*/
@GetMapping
(
"/clear"
)
@GetMapping
(
"/clear"
)
public
String
clearDiaplay
(
String
key
,
String
code
)
{
public
String
clearDiaplay
(
String
key
,
String
code
)
{
String
CLEAR_CODE
=
"0000000400006050"
;
// 6x6大屏: C0
// 小平指令: 60
String
CLEAR_CODE
=
"000000040000C050"
;
code
=
code
+
CLEAR_CODE
;
code
=
code
+
CLEAR_CODE
;
int
i
=
CRC16Util
.
calcCrc16
(
StringUtil
.
hexString2Bytes
(
code
));
int
i
=
CRC16Util
.
calcCrc16
(
StringUtil
.
hexString2Bytes
(
code
));
code
=
(
code
+
StringUtil
.
hightAddZero
(
StringUtil
.
encodeHEX
(
i
))).
toUpperCase
();
code
=
(
code
+
StringUtil
.
hightAddZero
(
StringUtil
.
encodeHEX
(
i
))).
toUpperCase
();
...
@@ -44,7 +39,6 @@ public class SocketController {
...
@@ -44,7 +39,6 @@ public class SocketController {
bufff
.
retain
();
bufff
.
retain
();
mapChannel
.
writeAndFlush
(
bufff
);
mapChannel
.
writeAndFlush
(
bufff
);
// bufff.retain();
// bufff.retain();
// 返回失败指令时使用
// 返回失败指令时使用
if
(!
ParamUtil
.
findMapContainerKey
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
()))
{
if
(!
ParamUtil
.
findMapContainerKey
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
()))
{
ResultModel
model
=
new
ResultModel
();
ResultModel
model
=
new
ResultModel
();
...
@@ -52,7 +46,22 @@ public class SocketController {
...
@@ -52,7 +46,22 @@ public class SocketController {
model
.
setHexCodeByte
(
StringUtil
.
hexString2Bytes
(
code
));
model
.
setHexCodeByte
(
StringUtil
.
hexString2Bytes
(
code
));
ParamUtil
.
addMap
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
(),
model
);
ParamUtil
.
addMap
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
(),
model
);
}
}
long
timeMillis
=
System
.
currentTimeMillis
();
while
(
true
)
{
String
resCode
=
nettyHandler
.
map
.
get
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
());
if
(
resCode
!=
null
)
{
if
(
"00"
.
equals
(
resCode
))
{
nettyHandler
.
map
.
remove
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
());
return
"success"
;
return
"success"
;
}
else
if
(
"01"
.
equals
(
resCode
))
{
nettyHandler
.
map
.
remove
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
());
return
"fail"
;
}
}
else
if
(
System
.
currentTimeMillis
()
-
timeMillis
>=
3000
)
{
nettyHandler
.
map
.
remove
(
"clear"
+
mapChannel
.
remoteAddress
().
toString
());
return
"未获取到返回的指令"
;
}
}
}
}
...
@@ -67,8 +76,10 @@ public class SocketController {
...
@@ -67,8 +76,10 @@ public class SocketController {
* @return String
* @return String
*/
*/
@GetMapping
(
"/display"
)
@GetMapping
(
"/display"
)
public
String
display
(
String
key
,
String
code
,
Integer
x
,
Integer
y
,
String
font
,
String
content
)
{
public
synchronized
String
display
(
String
key
,
String
code
,
Integer
x
,
Integer
y
,
String
font
,
String
content
)
{
try
{
try
{
// 获取通道
Channel
mapChannel
=
MapUtils
.
getMapChannel
(
key
);
String
DISPLAY_CODE
=
"0001"
;
String
DISPLAY_CODE
=
"0001"
;
// 指令处理
// 指令处理
String
xHex
=
Integer
.
toHexString
(
x
).
length
()
==
1
?
"0"
+
Integer
.
toHexString
(
x
)
:
Integer
.
toHexString
(
x
);
String
xHex
=
Integer
.
toHexString
(
x
).
length
()
==
1
?
"0"
+
Integer
.
toHexString
(
x
)
:
Integer
.
toHexString
(
x
);
...
@@ -83,10 +94,11 @@ public class SocketController {
...
@@ -83,10 +94,11 @@ public class SocketController {
// 数据封装
// 数据封装
ByteBuf
bufff
=
Unpooled
.
buffer
();
ByteBuf
bufff
=
Unpooled
.
buffer
();
bufff
.
writeBytes
(
StringUtil
.
hexString2Bytes
(
code
));
bufff
.
writeBytes
(
StringUtil
.
hexString2Bytes
(
code
));
// 获取通道
Channel
mapChannel
=
MapUtils
.
getMapChannel
(
key
);
mapChannel
.
writeAndFlush
(
bufff
);
System
.
out
.
println
(
"===================================是否执行"
);
mapChannel
.
writeAndFlush
(
bufff
);
// bufff.retain();
// bufff.retain();
// 返回失败指令时使用
// 返回失败指令时使用
if
(!
ParamUtil
.
findMapContainerKey
(
"display"
+
mapChannel
.
remoteAddress
().
toString
()))
{
if
(!
ParamUtil
.
findMapContainerKey
(
"display"
+
mapChannel
.
remoteAddress
().
toString
()))
{
...
@@ -95,19 +107,37 @@ public class SocketController {
...
@@ -95,19 +107,37 @@ public class SocketController {
model
.
setHexCodeByte
(
StringUtil
.
hexString2Bytes
(
code
));
model
.
setHexCodeByte
(
StringUtil
.
hexString2Bytes
(
code
));
ParamUtil
.
addMap
(
"display"
+
mapChannel
.
remoteAddress
().
toString
(),
model
);
ParamUtil
.
addMap
(
"display"
+
mapChannel
.
remoteAddress
().
toString
(),
model
);
}
}
// 获取当前时间
long
time1
=
System
.
currentTimeMillis
();
while
(
true
)
{
String
ydData
=
nettyHandler
.
map
.
get
(
"display"
+
mapChannel
.
remoteAddress
().
toString
());
if
(
ydData
!=
null
)
{
if
(
ydData
.
equals
(
"00"
))
{
nettyHandler
.
map
.
remove
(
"display"
+
mapChannel
.
remoteAddress
().
toString
());
return
"success"
;
return
"success"
;
}
catch
(
Exception
e
)
{
}
else
if
(
ydData
.
equals
(
"01"
))
{
nettyHandler
.
map
.
remove
(
"display"
+
mapChannel
.
remoteAddress
().
toString
());
return
"exefail"
;
}
}
else
if
(
System
.
currentTimeMillis
()-
time1
>=
3000
)
{
nettyHandler
.
map
.
remove
(
"display"
+
mapChannel
.
remoteAddress
().
toString
());
return
"未获取到返回的指令"
;
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
null
;
return
null
;
}
}
}
}
@GetMapping
(
"/test"
)
public
void
test
(
String
key
)
throws
Exception
{
/* @GetMapping("/test")
public void test(String key) {
System.out.println(key + "=======================");
System.out.println(key + "=======================");
String
body
=
HttpUtil
.
createGet
(
"http://localhost:9998/display?key=86433304
2029678
&code=01&x=0&y=12&font=31&content=温 度:33.5℃"
).
execute
().
body
();
String body = HttpUtil.createGet("http://localhost:9998/display?key=86433304
6020715
&code=01&x=0&y=12&font=31&content=温 度:33.5℃").execute().body();
// Thread.sleep(500);
// Thread.sleep(500);
String
body1
=
HttpUtil
.
createGet
(
"http://localhost:9998/display?key=86433304
2029678
&code=01&x=0&y=24&font=31&content=湿 度:33.5%RH"
).
execute
().
body
();
String body1 = HttpUtil.createGet("http://localhost:9998/display?key=86433304
6020715
&code=01&x=0&y=24&font=31&content=湿 度:33.5%RH").execute().body();
// Thread.sleep(500);
// Thread.sleep(500);
String
body2
=
HttpUtil
.
createGet
(
"http://localhost:9998/display?key=864333042029678&code=01&x=0&y=36&font=31&content=CO2:450ppm"
).
execute
().
body
();
String body2 = HttpUtil.createGet("http://localhost:9998/display?key=864333046020715&code=01&x=0&y=36&font=31&content=CO2:450ppm").execute().body();
}
}*/
}
}
src/main/java/com/gemho/netty/controller/nettyHandler.java
View file @
dce6c9e0
...
@@ -7,9 +7,14 @@ import io.netty.buffer.Unpooled;
...
@@ -7,9 +7,14 @@ import io.netty.buffer.Unpooled;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
public
class
nettyHandler
extends
ChannelInboundHandlerAdapter
{
public
class
nettyHandler
extends
ChannelInboundHandlerAdapter
{
static
Map
<
String
,
String
>
map
=
new
LinkedHashMap
<>();
@Override
@Override
public
void
channelActive
(
ChannelHandlerContext
ctx
)
{
public
void
channelActive
(
ChannelHandlerContext
ctx
)
{
System
.
out
.
println
(
"当前连接信息:"
+
ctx
.
channel
().
remoteAddress
());
System
.
out
.
println
(
"当前连接信息:"
+
ctx
.
channel
().
remoteAddress
());
...
@@ -41,16 +46,24 @@ public class nettyHandler extends ChannelInboundHandlerAdapter {
...
@@ -41,16 +46,24 @@ public class nettyHandler extends ChannelInboundHandlerAdapter {
byte
[]
newBys
=
new
byte
[
1
];
byte
[]
newBys
=
new
byte
[
1
];
System
.
arraycopy
(
bys
,
5
,
newBys
,
0
,
1
);
System
.
arraycopy
(
bys
,
5
,
newBys
,
0
,
1
);
System
.
out
.
println
(
"==============result:"
+
StringUtil
.
bytesToHexString
(
newBys
));
System
.
out
.
println
(
"==============result:"
+
StringUtil
.
bytesToHexString
(
newBys
));
byte
[]
typeByte
=
new
byte
[
2
];
System
.
arraycopy
(
bys
,
1
,
typeByte
,
0
,
2
);
System
.
out
.
println
(
"00:清屏异常==01:屏幕显示异常============返回异常信息指令:"
+
StringUtil
.
bytesToHexString
(
newBys
));
if
(
"00"
.
equals
(
StringUtil
.
bytesToHexString
(
newBys
)))
{
if
(
"00"
.
equals
(
StringUtil
.
bytesToHexString
(
newBys
)))
{
System
.
out
.
println
(
"正常:大屏接收到数据"
);
System
.
out
.
println
(
"正常:大屏接收到数据"
);
if
(
"0001"
.
equals
(
StringUtil
.
bytesToHexString
(
typeByte
)))
{
map
.
put
(
"display"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
"00"
);
}
else
{
}
else
{
byte
[]
typeByte
=
new
byte
[
2
];
map
.
put
(
"clear"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
"00"
);
System
.
arraycopy
(
bys
,
1
,
typeByte
,
0
,
2
);
}
System
.
out
.
println
(
"0000:清屏异常==0001:屏幕显示异常============返回异常信息指令:"
+
StringUtil
.
bytesToHexString
(
typeByte
));
}
else
{
System
.
out
.
println
(
"失败:大屏未接收到数据"
);
if
(
"0001"
.
equals
(
StringUtil
.
bytesToHexString
(
typeByte
)))
{
if
(
"0001"
.
equals
(
StringUtil
.
bytesToHexString
(
typeByte
)))
{
this
.
processResult
(
"display"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
ctx
);
this
.
processResult
(
"display"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
ctx
);
map
.
put
(
"display"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
"01"
);
}
else
{
}
else
{
this
.
processResult
(
"clear"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
ctx
);
this
.
processResult
(
"clear"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
ctx
);
map
.
put
(
"clear"
+
ctx
.
channel
().
remoteAddress
().
toString
(),
"01"
);
}
}
}
}
}
else
{
}
else
{
...
...
target/classes/com/gemho/netty/controller/SocketController.class
View file @
dce6c9e0
No preview for this file type
target/classes/com/gemho/netty/controller/nettyHandler.class
View file @
dce6c9e0
No preview for this file type
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