Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
GnssServer
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
zhushanglei
GnssServer
Commits
0ca5bcb8
Commit
0ca5bcb8
authored
Jul 14, 2022
by
zhushanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gps数据采集修改
parent
3bc5df3b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
18 deletions
+67
-18
TcpClient.java
src/main/java/client/TcpClient.java
+22
-0
TcpClientHandler.java
src/main/java/client/TcpClientHandler.java
+45
-18
No files found.
src/main/java/client/TcpClient.java
View file @
0ca5bcb8
...
@@ -6,6 +6,10 @@ import io.netty.channel.nio.NioEventLoopGroup;
...
@@ -6,6 +6,10 @@ import io.netty.channel.nio.NioEventLoopGroup;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
io.netty.handler.codec.string.StringDecoder
;
import
io.netty.handler.codec.string.StringDecoder
;
import
io.netty.handler.codec.string.StringEncoder
;
import
io.netty.handler.codec.string.StringEncoder
;
import
io.netty.util.HashedWheelTimer
;
import
io.netty.util.Timeout
;
import
io.netty.util.Timer
;
import
io.netty.util.TimerTask
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
server.TcpServer
;
import
server.TcpServer
;
...
@@ -14,6 +18,8 @@ import java.io.IOException;
...
@@ -14,6 +18,8 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.net.InetAddress
;
import
java.net.InetAddress
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
/**
/**
* @author yuli
* @author yuli
...
@@ -105,6 +111,22 @@ public class TcpClient {
...
@@ -105,6 +111,22 @@ public class TcpClient {
TcpClientHandler
.
serverUrl
=
config
.
getProperty
(
"serverUrl"
);
TcpClientHandler
.
serverUrl
=
config
.
getProperty
(
"serverUrl"
);
logger
.
info
(
"服务器后端接口>>"
+
TcpClientHandler
.
serverUrl
);
logger
.
info
(
"服务器后端接口>>"
+
TcpClientHandler
.
serverUrl
);
TcpClient
.
sendMsg
(
client
);
TcpClient
.
sendMsg
(
client
);
//创建定时器
final
HashedWheelTimer
timer
=
new
HashedWheelTimer
(
Executors
.
defaultThreadFactory
(),
10
,
TimeUnit
.
MILLISECONDS
,
2
);
timer
.
newTimeout
(
new
TimerTask
()
{
@Override
public
void
run
(
final
Timeout
timeout
)
throws
Exception
{
logger
.
error
(
"timer:单点信号"
);
TcpClientHandler
.
positioning
();
timer
.
newTimeout
(
this
,
10
,
TimeUnit
.
SECONDS
);
//结束时候再次注册
}
},
5
,
TimeUnit
.
MILLISECONDS
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"main err:"
+
e
);
System
.
out
.
println
(
"main err:"
+
e
);
}
}
...
...
src/main/java/client/TcpClientHandler.java
View file @
0ca5bcb8
...
@@ -11,6 +11,7 @@ import server.TcpServer;
...
@@ -11,6 +11,7 @@ import server.TcpServer;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.concurrent.TimeUnit
;
/**
/**
* @author yuli
* @author yuli
...
@@ -22,8 +23,12 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -22,8 +23,12 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpClientHandler
.
class
);
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpClientHandler
.
class
);
public
static
boolean
complex
=
false
;
//是否差分定位模式
@Override
@Override
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
protected
void
channelRead0
(
ChannelHandlerContext
ctx
,
Object
msg
)
{
try
{
complex
=
true
;
System
.
out
.
println
(
"client receive msg from server:"
+
msg
);
System
.
out
.
println
(
"client receive msg from server:"
+
msg
);
String
writeData
=
msg
.
toString
();
//要发送的字符串
String
writeData
=
msg
.
toString
();
//要发送的字符串
byte
[]
bytes
=
writeData
.
getBytes
();
//将字符串转换为字节数组
byte
[]
bytes
=
writeData
.
getBytes
();
//将字符串转换为字节数组
...
@@ -39,9 +44,31 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -39,9 +44,31 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
Thread
.
sleep
(
5000
);
Thread
.
sleep
(
5000
);
logger
.
info
(
"**COM口读出数据<<start:"
+
result
+
">>end"
);
logger
.
info
(
"**COM口读出数据<<start:"
+
result
+
">>end"
);
sendGnss
(
result
);
sendGnss
(
result
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
complex
=
false
;
}
}
//单点定位数据读取
public
static
void
positioning
(){
if
(
complex
){
return
;
}
try
{
String
result
=
readComm
(
serialPort
);
if
(
null
==
result
){
openComm
();
}
logger
.
info
(
"单点定位数据读取:"
+
result
);
sendGnss
(
result
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
}
private
void
sendGnss
(
String
msg
)
{
private
static
void
sendGnss
(
String
msg
)
{
try
{
try
{
if
(
null
==
msg
||
""
.
equals
(
msg
)){
if
(
null
==
msg
||
""
.
equals
(
msg
)){
logger
.
info
(
"COM口未读出数据"
);
logger
.
info
(
"COM口未读出数据"
);
...
@@ -94,9 +121,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -94,9 +121,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
}
}
}
}
private
OkHttpClient
client
=
new
OkHttpClient
();
private
static
OkHttpClient
client
=
new
OkHttpClient
();
String
post
(
String
url
,
String
json
)
throws
IOException
{
private
static
String
post
(
String
url
,
String
json
)
throws
IOException
{
RequestBody
body
=
RequestBody
.
create
(
json
,
MediaType
.
get
(
"application/json; charset=utf-8"
));
RequestBody
body
=
RequestBody
.
create
(
json
,
MediaType
.
get
(
"application/json; charset=utf-8"
));
Request
request
=
new
Request
.
Builder
()
Request
request
=
new
Request
.
Builder
()
...
@@ -151,7 +178,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -151,7 +178,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
return
result
;
return
result
;
}
}
private
String
readComm
(
SerialPort
serialPort
)
{
private
static
String
readComm
(
SerialPort
serialPort
)
{
// if (serialPort == null || !serialPort.isOpen()) {
// if (serialPort == null || !serialPort.isOpen()) {
// logger.error("COM口未打开");
// logger.error("COM口未打开");
// return null;
// return null;
...
...
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