Commit 64c1d88f authored by zhushanglei's avatar zhushanglei

gps服务分开,服务端,客户端

parent dd279ad9
package server; package client;
import java.util.Date; import java.util.Date;
......
package server; package client;
public class SimplePoint { public class SimplePoint {
......
...@@ -4,11 +4,15 @@ import io.netty.bootstrap.Bootstrap; ...@@ -4,11 +4,15 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; 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.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
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.CharsetUtil; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.TcpServer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/** /**
* @author yuli * @author yuli
...@@ -16,11 +20,26 @@ import io.netty.util.CharsetUtil; ...@@ -16,11 +20,26 @@ import io.netty.util.CharsetUtil;
**/ **/
public class TcpClient { public class TcpClient {
public static String HOST = "127.0.0.1"; private static Logger logger = LoggerFactory.getLogger(TcpServer.class);
public static int PORT = 9200; public static String HOST;
public static String PORT;
static {
InputStream in = ClassLoader.getSystemResourceAsStream("config.properties");
Properties config = new Properties();
try {
config.load(in);
} catch (IOException e) {
e.printStackTrace();
}
HOST = config.getProperty("gpsServerIP");
PORT = config.getProperty("gpsServerPort");
logger.info("gps服务器地址>>" + HOST);
logger.info("gps服务器端口>>" + PORT);
}
public static Bootstrap bootstrap = getBootstrap(); public static Bootstrap bootstrap = getBootstrap();
public static Channel channel = getChannel(HOST, PORT); public static Channel channel = getChannel(HOST, Integer.parseInt(PORT));
/** /**
* 初始化Bootstrap * 初始化Bootstrap
...@@ -36,19 +55,20 @@ public class TcpClient { ...@@ -36,19 +55,20 @@ public class TcpClient {
pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("handler", new TcpClientHandler()); pipeline.addLast("handler", new TcpClientHandler());
TcpClientHandler.openComm();
} }
}); });
b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.SO_KEEPALIVE, true);
return b; return b;
} }
// 连接端口 // 连接端口
public static final Channel getChannel(String host, int port) { public static final Channel getChannel(String host, int port) {
Channel channel = null; Channel channel = null;
try { try {
channel = bootstrap.connect(host, port).sync().channel(); channel = bootstrap.connect(host, port).sync().channel();
} catch (Exception e) { } catch (Exception e) {
System.out.println("Connect Server(IP{},PORT{}) Failed"+"host:"+host+"port:"+port+"e:"+e); System.out.println("Connect Server(IP{},PORT{}) Failed" + "host:" + host + "port:" + port + "e:" + e);
return null; return null;
} }
return channel; return channel;
...@@ -56,7 +76,8 @@ public class TcpClient { ...@@ -56,7 +76,8 @@ public class TcpClient {
/** /**
* 发送信息 * 发送信息
* @param [msg] *
* @param msg
* @return void * @return void
*/ */
public static void sendMsg(String msg) throws Exception { public static void sendMsg(String msg) throws Exception {
...@@ -67,12 +88,18 @@ public class TcpClient { ...@@ -67,12 +88,18 @@ public class TcpClient {
} }
} }
public static final String client = "gpsclient";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try { try {
TcpClient.sendMsg("02"); Properties config = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("config.properties");
config.load(in);
TcpClientHandler.serverUrl = config.getProperty("serverUrl");
logger.info("服务器后端接口>>" + TcpClientHandler.serverUrl);
TcpClient.sendMsg(client);
} catch (Exception e) { } catch (Exception e) {
System.out.println("main err:"+ e); System.out.println("main err:" + e);
} }
} }
......
package client; package client;
import com.alibaba.fastjson.JSON;
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;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.TcpServer;
import java.io.IOException;
import java.util.Date;
/** /**
* @author yuli * @author yuli
...@@ -9,8 +18,150 @@ import io.netty.channel.SimpleChannelInboundHandler; ...@@ -9,8 +18,150 @@ import io.netty.channel.SimpleChannelInboundHandler;
**/ **/
public class TcpClientHandler extends SimpleChannelInboundHandler<Object> { public class TcpClientHandler extends SimpleChannelInboundHandler<Object> {
public static String serverUrl;
private static Logger logger = LoggerFactory.getLogger(TcpClientHandler.class);
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("client receive msg from server:" + msg); System.out.println("client receive msg from server:" + msg);
String writeData = msg.toString(); //要发送的字符串
byte[] bytes = writeData.getBytes();//将字符串转换为字节数组
int flag = writeComm(serialPort, bytes);
if(-2 == flag){
openComm();
}
Thread.sleep(5000);//休眠0.02秒,等待下位机传送数据到串口。如果不休眠,直接再次使用port.bytesAvailable()函数会因为下位机还没有返回数据而返回-1,并跳出循环导致数据没读完。休眠时间可以自行调试,时间越长,单次读取到的数据越多。
String result = readComm(serialPort);
if(null == result){
openComm();
}
logger.info("**COM口读出数据<<start:" + result +">>end");
sendGnss(result);
}
private void sendGnss(String msg) {
try {
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.setName(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) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
private OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8"));
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
private static SerialPort serialPort;
public static SerialPort openComm() {
SerialPort[] serialPorts = SerialPort.getCommPorts();//查找所有串口
for(SerialPort port:serialPorts){
System.out.println("PortName:"+port.getSystemPortName());//打印串口名称,如COM4
System.out.println("PortDesc:"+port.getPortDescription());//打印串口类型,如USB Serial
System.out.println("PortDescriptivePortName:"+port.getDescriptivePortName());//打印串口的完整类型,如USB-SERIAL CH340(COM4)
if(port.getSystemPortName().equalsIgnoreCase("COM1")){
serialPort = port;
}
}
serialPort.setBaudRate(115200);//设置波特率为112500
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING, 1000, 1000);//设置超时
serialPort.setRTS();//设置RTS。也可以设置DTR
serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);//设置串口的控制流,可以设置为disabled,或者CTS, RTS/CTS, DSR, DTR/DSR, Xon, Xoff, Xon/Xoff等
serialPort.setComPortParameters(115200, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);//一次性设置所有的串口参数,第一个参数为波特率,默认9600;第二个参数为每一位的大小,默认8,可以输入5到8之间的值;第三个参数为停止位大小,只接受内置常量,可以选择(ONE_STOP_BIT, ONE_POINT_FIVE_STOP_BITS, TWO_STOP_BITS);第四位为校验位,同样只接受内置常量,可以选择 NO_PARITY, EVEN_PARITY, ODD_PARITY, MARK_PARITY,SPACE_PARITY。
if(!serialPort.isOpen()){
boolean isCommOpeded = serialPort.openPort();//判断串口是否打开,如果没打开,就打开串口。打开串口的函数会返回一个boolean值,用于表明串口是否成功打开了
logger.info("COM口是否打开: "+ isCommOpeded);
}
// serialPort.closePort();//关闭串口。该函数同样会返回一个boolean值,表明串口是否成功关闭
return serialPort;
}
public static void closeComm(){
if(null != serialPort)
serialPort.closePort();
}
private int writeComm(SerialPort serialPort, byte[] data) throws InterruptedException {
// if(serialPort == null || !serialPort.isOpen()){
// logger.info("COM口未打开");
// return -2;
// }
int result = serialPort.writeBytes(data,data.length);//将字节数组全部写入串口
if(result != -1){
logger.info("COM口已成功写入数据");
}else{
logger.error("COM口写数据失败");
}
return result;
}
private String readComm(SerialPort serialPort) {
// if (serialPort == null || !serialPort.isOpen()) {
// logger.error("COM口未打开");
// return null;
// }
String readData = "";
while(serialPort.bytesAvailable()>0){ //循环读取所有的返回数据。如果可读取数据长度为0或-1,则停止读取
byte[] newData = new byte[serialPort.bytesAvailable()];//创建一个字节数组,长度为可读取的字节长度
int numRead = serialPort.readBytes(newData, newData.length);//将串口中可读取的数据读入字节数组,返回值为本次读取到的字节长度
String newDataString = new String(newData);//将新数据转为字符串
readData = readData + newDataString;//组合字符串
}
return readData;
} }
} }
...@@ -28,6 +28,7 @@ public class TcpServer { ...@@ -28,6 +28,7 @@ public class TcpServer {
// 服务器地址端口 // 服务器地址端口
// private static final String IP = "192.168.0.112"; // private static final String IP = "192.168.0.112";
// private static final String IP = "192.168.0.107"; // private static final String IP = "192.168.0.107";
public static String IP = "127.0.0.1";
private static final int PORT = 9200; private static final int PORT = 9200;
public static String deviceId ; public static String deviceId ;
...@@ -66,7 +67,6 @@ public class TcpServer { ...@@ -66,7 +67,6 @@ public class TcpServer {
pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringEncoder());
pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringDecoder());
pipeline.addLast(new TcpServerHandler()); pipeline.addLast(new TcpServerHandler());
TcpServerHandler.openComm();
} }
}); });
// 异步绑定端口 // 异步绑定端口
...@@ -88,10 +88,9 @@ public class TcpServer { ...@@ -88,10 +88,9 @@ public class TcpServer {
// logger.info("mac: "+sb.toString()); // logger.info("mac: "+sb.toString());
deviceId = hostname; deviceId = hostname;
logger.info("Local host name: "+hostname); logger.info("Local host name: "+hostname);
b.bind(addr.getHostAddress(), PORT).sync(); // b.bind(addr.getHostAddress(), PORT).sync();
// b.bind(IP, PORT).sync(); b.bind(IP, PORT).sync();
logger.info("TCP Server Started"); logger.info("TCP Server Started");
} }
// 关闭端口 // 关闭端口
protected static void shutdown() { protected static void shutdown() {
...@@ -102,11 +101,6 @@ public class TcpServer { ...@@ -102,11 +101,6 @@ public class TcpServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
logger.info("Starting TCP Server..."); logger.info("Starting TCP Server...");
Properties config = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream( "config.properties");
config.load(in);
TcpServerHandler.serverUrl = config.getProperty("serverUrl");
logger.info("后端接口>>" + TcpServerHandler.serverUrl);
TcpServer.run(); TcpServer.run();
// TcpServer.shutdown(); // TcpServer.shutdown();
} }
......
package server; package server;
import client.GpsDto;
import client.SimplePoint;
import client.TcpClient;
import client.TcpClientHandler;
import com.alibaba.fastjson.JSON; 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;
...@@ -9,7 +13,10 @@ import org.slf4j.Logger; ...@@ -9,7 +13,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author yuli * @author yuli
...@@ -20,28 +27,53 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -20,28 +27,53 @@ 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"); // public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public static String serverUrl; // public static String serverUrl;
//全局变量
Map<String, ChannelHandlerContext> gpsServerContextHashMap = new HashMap<>();
//获取全局变量信息
public ChannelHandlerContext getContext(String key){
return gpsServerContextHashMap.get(key);
}
//更新全局变量信息
public void addContext(String key,ChannelHandlerContext ctx){
gpsServerContextHashMap.put(key,ctx);
}
//清空全局变量信息
public void deleteContext(String key){
gpsServerContextHashMap.remove(key);
}
/** /**
* 打印接收到的内容,并回传 * 打印接收到的内容,并回传
// * @param [ctx, msg] // * @paractx = {DefaultChannelHandlerContext@2874} "ChannelHandlerContext(TcpServerHandler#0, [id: 0xe24a52fc, L:/127.0.0.1:9200 - R:/127.0.0.1:61801])"m [ctx, msg]
* @return void * @return void
*/ */
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("**接收到的基站数据<<start:" + msg.toString() +">>end"); // logger.info("**接收到的基站数据<<start:" + msg.toString() +">>end");
String writeData = msg.toString(); //要发送的字符串 if(TcpClient.client.equalsIgnoreCase(msg.toString())){ //移动站
byte[] bytes = writeData.getBytes();//将字符串转换为字节数组 String clinetIp = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
int flag = writeComm(serialPort, bytes); addContext(clinetIp,ctx);
// ctx.writeAndFlush("你好"); //TextWebSocketFrame
}else{ //基站
gpsServerContextHashMap.forEach((k,v)->{
v.writeAndFlush(msg.toString());
});
}
// String writeData = msg.toString(); //要发送的字符串
// byte[] bytes = writeData.getBytes();//将字符串转换为字节数组
// int flag = writeComm(serialPort, bytes);
// if(-2 == flag){ // if(-2 == flag){
// openComm(); // openComm();
// } // }
Thread.sleep(5000);//休眠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) {
...@@ -89,7 +121,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -89,7 +121,7 @@ public class TcpServerHandler extends SimpleChannelInboundHandler<Object> {
} }
} }
String jsonObject = JSON.toJSONString(gpsDto); String jsonObject = JSON.toJSONString(gpsDto);
String result = post(serverUrl,jsonObject); String result = post(TcpClientHandler.serverUrl,jsonObject);
logger.info(result); logger.info(result);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
serverUrl:http://192.168.0.111:8001/gps serverUrl:http://192.168.0.111:8001/gps
gpsServerIP:127.0.0.1
gpsServerPort:9200
\ No newline at end of file
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