Commit 697b5cd5 authored by zhushanglei's avatar zhushanglei

init

parent ae7d5e06
...@@ -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>
......
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;
}
}
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;
}
}
...@@ -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();
}
} }
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(5000);//休眠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("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)) / 60d ;
double x = Double.parseDouble(gga[4].substring(0, 3)) + Double.parseDouble(gga[4].substring(3, gga[4].length() - 3)) / 60d;
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); 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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment