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
697b5cd5
Commit
697b5cd5
authored
Apr 02, 2022
by
zhushanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
ae7d5e06
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
143 additions
and
20 deletions
+143
-20
pom.xml
pom.xml
+6
-1
GpsDto.java
src/main/java/server/GpsDto.java
+44
-0
SimplePoint.java
src/main/java/server/SimplePoint.java
+24
-0
TcpServer.java
src/main/java/server/TcpServer.java
+9
-2
TcpServerHandler.java
src/main/java/server/TcpServerHandler.java
+60
-17
No files found.
pom.xml
View file @
697b5cd5
...
@@ -9,10 +9,15 @@
...
@@ -9,10 +9,15 @@
<version>
1.0
</version>
<version>
1.0
</version>
<dependencies>
<dependencies>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.80
</version>
</dependency>
<dependency>
<dependency>
<groupId>
com.squareup.okhttp3
</groupId>
<groupId>
com.squareup.okhttp3
</groupId>
<artifactId>
okhttp
</artifactId>
<artifactId>
okhttp
</artifactId>
<version>
4.9.
3
</version>
<version>
4.9.
2
</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
com.fazecast
</groupId>
<groupId>
com.fazecast
</groupId>
...
...
src/main/java/server/GpsDto.java
0 → 100644
View file @
697b5cd5
package
server
;
import
java.util.Date
;
public
class
GpsDto
{
private
Date
time
;
private
String
id
;
private
SimplePoint
location
;
private
double
gndRate
;
public
Date
getTime
()
{
return
time
;
}
public
void
setTime
(
Date
time
)
{
this
.
time
=
time
;
}
public
String
getId
()
{
return
id
;
}
public
void
setId
(
String
id
)
{
this
.
id
=
id
;
}
public
SimplePoint
getLocation
()
{
return
location
;
}
public
void
setLocation
(
SimplePoint
location
)
{
this
.
location
=
location
;
}
public
double
getGndRate
()
{
return
gndRate
;
}
public
void
setGndRate
(
double
gndRate
)
{
this
.
gndRate
=
gndRate
;
}
}
src/main/java/server/SimplePoint.java
0 → 100644
View file @
697b5cd5
package
server
;
public
class
SimplePoint
{
private
double
x
;
private
double
y
;
public
double
getX
()
{
return
x
;
}
public
void
setX
(
double
x
)
{
this
.
x
=
x
;
}
public
double
getY
()
{
return
y
;
}
public
void
setY
(
double
y
)
{
this
.
y
=
y
;
}
}
src/main/java/server/TcpServer.java
View file @
697b5cd5
...
@@ -32,6 +32,8 @@ public class TcpServer {
...
@@ -32,6 +32,8 @@ public class TcpServer {
private
static
final
String
IP
=
"192.168.0.107"
;
private
static
final
String
IP
=
"192.168.0.107"
;
private
static
final
int
PORT
=
9200
;
private
static
final
int
PORT
=
9200
;
public
static
String
deviceId
;
/** 用于分配处理业务线程的线程组个数 */
/** 用于分配处理业务线程的线程组个数 */
protected
static
final
int
BIZGROUPSIZE
=
Runtime
.
getRuntime
().
availableProcessors
()
*
2
;
protected
static
final
int
BIZGROUPSIZE
=
Runtime
.
getRuntime
().
availableProcessors
()
*
2
;
/** 业务出现线程大小 */
/** 业务出现线程大小 */
...
@@ -84,9 +86,11 @@ public class TcpServer {
...
@@ -84,9 +86,11 @@ public class TcpServer {
String
s
=
Integer
.
toHexString
(
mac
[
i
]
&
0xFF
);
String
s
=
Integer
.
toHexString
(
mac
[
i
]
&
0xFF
);
sb
.
append
(
s
.
length
()
==
1
?
0
+
s
:
s
);
sb
.
append
(
s
.
length
()
==
1
?
0
+
s
:
s
);
}
}
deviceId
=
sb
.
toString
();
logger
.
info
(
"mac: "
+
sb
.
toString
());
logger
.
info
(
"mac: "
+
sb
.
toString
());
logger
.
info
(
"Local host name: "
+
hostname
);
logger
.
info
(
"Local host name: "
+
hostname
);
b
.
bind
(
IP
,
PORT
).
sync
();
b
.
bind
(
addr
.
getHostAddress
(),
PORT
).
sync
();
// b.bind(IP, PORT).sync();
logger
.
info
(
"TCP Server Started"
);
logger
.
info
(
"TCP Server Started"
);
}
}
...
@@ -102,5 +106,8 @@ public class TcpServer {
...
@@ -102,5 +106,8 @@ public class TcpServer {
// TcpServer.shutdown();
// TcpServer.shutdown();
}
}
@Override
protected
void
finalize
()
throws
Throwable
{
super
.
finalize
();
}
}
}
src/main/java/server/TcpServerHandler.java
View file @
697b5cd5
package
server
;
package
server
;
import
com.alibaba.fastjson.JSON
;
import
com.fazecast.jSerialComm.SerialPort
;
import
com.fazecast.jSerialComm.SerialPort
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.channel.SimpleChannelInboundHandler
;
...
@@ -8,6 +9,7 @@ import org.slf4j.Logger;
...
@@ -8,6 +9,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Date
;
/**
/**
* @author yuli
* @author yuli
...
@@ -16,8 +18,9 @@ import java.io.IOException;
...
@@ -16,8 +18,9 @@ import java.io.IOException;
public
class
TcpServerHandler
extends
SimpleChannelInboundHandler
<
Object
>
{
public
class
TcpServerHandler
extends
SimpleChannelInboundHandler
<
Object
>
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpServerHandler
.
class
);
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpServerHandler
.
class
);
// public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
private
static
final
String
serverUrl
=
"http://
localhost
:8001/gps"
;
private
static
final
String
serverUrl
=
"http://
192.168.0.107
:8001/gps"
;
/**
/**
* 打印接收到的内容,并回传
* 打印接收到的内容,并回传
// * @param [ctx, msg]
// * @param [ctx, msg]
...
@@ -29,36 +32,76 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -29,36 +32,76 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
String
writeData
=
msg
.
toString
();
//要发送的字符串
String
writeData
=
msg
.
toString
();
//要发送的字符串
byte
[]
bytes
=
writeData
.
getBytes
();
//将字符串转换为字节数组
byte
[]
bytes
=
writeData
.
getBytes
();
//将字符串转换为字节数组
int
flag
=
writeComm
(
serialPort
,
bytes
);
int
flag
=
writeComm
(
serialPort
,
bytes
);
if
(-
2
==
flag
){
//
if(-2 == flag){
openComm
();
//
openComm();
}
//
}
Thread
.
sleep
(
500
);
//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
Thread
.
sleep
(
500
0
);
//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String
result
=
readComm
(
serialPort
);
String
result
=
readComm
(
serialPort
);
if
(
null
==
result
){
//
if(null == result){
openComm
();
//
openComm();
}
//
}
logger
.
info
(
"**COM口读出数据<<start:"
+
result
+
">>end"
);
logger
.
info
(
"**COM口读出数据<<start:"
+
result
+
">>end"
);
sendGnss
(
result
);
sendGnss
(
result
);
}
}
private
void
sendGnss
(
String
msg
)
{
private
void
sendGnss
(
String
msg
)
{
try
{
try
{
String
result
=
post
(
serverUrl
,
msg
);
if
(
null
==
msg
||
""
.
equals
(
msg
)){
logger
.
info
(
result
);
logger
.
info
(
"COM口未读出数据"
);
return
;
}
String
[]
gnss
=
msg
.
split
(
"\\r\\n"
);
logger
.
info
(
"gnss[0]"
+
gnss
[
0
]);
logger
.
info
(
"gnss[1]"
+
gnss
[
1
]);
SimplePoint
simplePoint
=
new
SimplePoint
();
GpsDto
gpsDto
=
new
GpsDto
();
for
(
int
i
=
0
;
i
<
gnss
.
length
;
i
++
){
if
(
"$"
.
equals
(
gnss
[
i
].
substring
(
0
,
1
))
&&
gnss
[
i
].
contains
(
"GGA"
)){
//GNSS 定位信息
String
[]
gga
=
gnss
[
i
].
split
(
","
);
logger
.
info
(
"gga"
+
gga
.
toString
());
if
(
gga
.
length
==
15
){
if
(
""
.
equals
(
gga
[
2
])){
return
;
}
double
y
=
Double
.
parseDouble
(
gga
[
2
].
substring
(
0
,
2
))
+
Double
.
parseDouble
(
gga
[
2
].
substring
(
2
,
gga
[
2
].
length
()
-
2
))
/
60
d
;
double
x
=
Double
.
parseDouble
(
gga
[
4
].
substring
(
0
,
3
))
+
Double
.
parseDouble
(
gga
[
4
].
substring
(
3
,
gga
[
4
].
length
()
-
3
))
/
60
d
;
double
elevation
=
Double
.
parseDouble
(
gga
[
9
])
+
Double
.
parseDouble
(
gga
[
11
]);
logger
.
info
(
"接收当前的经度"
+
x
+
"纬度"
+
y
+
"海拔"
+
elevation
);
simplePoint
.
setX
(
x
);
simplePoint
.
setY
(
y
);
gpsDto
.
setId
(
TcpServer
.
deviceId
);
gpsDto
.
setTime
(
new
Date
());
gpsDto
.
setLocation
(
simplePoint
);
}
}
else
if
(
"$"
.
equals
(
gnss
[
i
].
substring
(
0
,
1
))
&&
gnss
[
i
].
contains
(
"VTG"
)){
//地面速度信息
String
[]
vtg
=
gnss
[
i
].
split
(
","
);
if
(
vtg
.
length
==
10
){
if
(
""
.
equals
(
vtg
[
5
])){
return
;
}
double
sulv0
=
Double
.
parseDouble
(
vtg
[
5
].
toString
());
double
sulv1
=
Double
.
parseDouble
(
vtg
[
7
].
toString
());
gpsDto
.
setGndRate
(
sulv1
);
logger
.
info
(
"接收当前的地面速率"
+
sulv0
+
"节"
+
sulv1
+
"km/h"
);
}
}
}
String
jsonObject
=
JSON
.
toJSONString
(
gpsDto
);
String
result
=
post
(
serverUrl
,
jsonObject
);
logger
.
info
(
result
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
logger
.
error
(
e
.
getMessage
());
}
}
}
}
private
OkHttpClient
client
=
new
OkHttpClient
();
public
static
final
MediaType
JSON
=
MediaType
.
get
(
"application/json; charset=utf-8"
);
OkHttpClient
client
=
new
OkHttpClient
();
String
post
(
String
url
,
String
json
)
throws
IOException
{
String
post
(
String
url
,
String
json
)
throws
IOException
{
RequestBody
body
=
RequestBody
.
create
(
json
,
JSON
);
RequestBody
body
=
RequestBody
.
create
(
json
,
MediaType
.
get
(
"application/json; charset=utf-8"
));
Request
request
=
new
Request
.
Builder
()
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
url
(
url
)
.
post
(
body
)
.
post
(
body
)
...
...
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