使用Java Socket实现GPS定位数据处理

48 篇文章 0 订阅
35 篇文章 0 订阅

在许多应用场景中,如车辆追踪、移动设备定位等,GPS定位数据的实时获取和处理至关重要。本文将介绍如何使用Java Socket编程来接收GPS设备发送的数据并进行处理。

肖哥弹架构 跟大家“弹弹” 车辆GPS数据收集,需要代码关注

欢迎 点赞,点赞,点赞。

关注公号Solomon肖哥弹架构获取更多精彩内容

历史热点文章

业务说明:

车辆追踪系统需要实时获取车辆的GPS定位信息。车辆上的GPS设备通过TCP连接发送其位置数据到服务器。

技术点:

  1. Java Socket编程:使用Java的Socket API进行网络通信。
  2. GPS定位数据:理解GPS数据格式,如NMEA 0183标准。
  3. 多线程处理:为每个连接创建线程以异步接收数据。

项目结构:

gps-tracking-system/
|-- src/
|   |-- main/
|   |   |-- java/
|   |   |   `-- com/
|   |   |       `-- example/
|   |   |           |-- GpsServer.java
|   |   |           |-- GpsDataHandler.java
|   |   |           |-- GpsDataParser.java
|   |   |           |-- BusinessDataService.java
|   |   |           `-- RabbitMqProducer.java
|   |   `-- resources/
|   |       `-- application.properties
|   `-- test/
|       `-- java/
|           `-- com/
|               `-- example/
|                   `-- GpsServerTest.java
`-- pom.xml
  • GpsServer.java:服务器启动类。
  • GpsDataHandler.java:Netty通道处理器,用于处理接收到的GPS数据。
  • GpsDataParser.java:解析GPS数据的类。
  • BusinessDataService.java:业务数据处理服务,负责将解析后的数据发送到MQ。
  • RabbitMqProducer.java:RabbitMQ生产者实现。
  • application.properties:应用配置文件。
  • GpsServerTest.java:服务器的单元测试类。

Maven依赖:

pom.xml文件中,您需要添加以下依赖:

<dependencies>
    <!-- Netty -->
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.68.Final</version>
    </dependency>
    
    <!-- SLF4J API -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>
    
    <!-- Logback Classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    
    <!-- RabbitMQ Java Client -->
    <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>5.10.0</version>
    </dependency>
    
    <!-- JUnit for testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

业务流程:

在这里插入图片描述

代码:

服务器启动类(GpsServer.java):
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.io.IOException;
import java.util.Properties;

public class GpsServer {

    private final int port;

    public GpsServer() throws IOException {
        Properties props = new Properties();
        props.load(getClass().getClassLoader().getResourceAsStream("application.properties"));
        this.port = Integer.parseInt(props.getProperty("gps.server.port"));
    }

    public void run() throws InterruptedException {
    
        MqProducer mqProducer = new RabbitMqProducer("mq-broker1:5672"); 
        BusinessDataService businessDataService = new BusinessDataService(mqProducer);
    
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(SocketChannel ch) {
                     ch.pipeline().addLast(new GpsDataHandler(businessDataService));
                 }
             });

            b.bind(port).sync().channel().closeFuture().await();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new GpsServer().run();
    }
}
GPS数据处理通道(GpsDataHandler.java):
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GpsDataHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(GpsDataHandler.class);
    private final BusinessDataService businessDataService;

    public GpsDataHandler(BusinessDataService businessDataService) {
        this.businessDataService = businessDataService;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        try {
            byte[] data = new byte[in.readableBytes()];
            in.readBytes(data);
            String gpsData = new String(data, StandardCharsets.UTF_8);
            businessDataService.processBusinessData(gpsData);
        } catch (Exception e) {
            logger.error("Error processing GPS data", e);
        } finally {
            in.release();
            ctx.close(); // Close the connection after processing
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.error("Exception caught in GPS data handler", cause);
        ctx.close();
    }
}

业务数据服务(BusinessDataService.java):

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BusinessDataService {

    private static final Logger logger = LoggerFactory.getLogger(BusinessDataService.class);
    // MQ客户端或生产者,具体实现根据所使用的MQ进行调整
    private final MqProducer mqProducer;

    public BusinessDataService(MqProducer mqProducer) {
        this.mqProducer = mqProducer;
    }

    public void processBusinessData(String gpsData) {
        try {
            BusinessGpsData businessData = GpsDataParser.parse(gpsData);
            if (businessData != null) {
                mqProducer.send(businessData);
                logger.info("Business GPS data sent to message queue: {}", businessData);
            }
        } catch (Exception e) {
            logger.error("Error processing business GPS data", e);
        }
    }
}

业务GPS数据解析器(GpsDataParser.java):


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class GpsDataParser {

        // 假设GPS数据格式为: $GPGGA,xxx,xxx,xxx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx*
        private static final Pattern GPGGA_PATTERN = Pattern.compile("\\$GPGGA,\\d{2},\\d{2},(\\d{2}\\.\\d{7}),(\\d{3}),(\\d{2}\\.\\d{7}),([NS]),(\\d{3}\\.\\d{7}),([EW]),\\d{2}\\.\\d,\\d{2}\\.\\d,\\d{2},M,\\d{2},M,\\d{2},\\d{2}\\*[0-9A-F]{2}");

        /**
         * 例如,提取车辆ID、经纬度、时间戳等信息,并创建BusinessGpsData对象
         * 解析NMEA 0183标准的GPGGA语句
         * @param nmeaSentence GPS数据字符串
         * @return 解析后的BusinessGpsData对象
         */
         
        public static BusinessGpsData parse(String nmeaSentence) {
            Matcher matcher = GPGGA_PATTERN.matcher(nmeaSentence);
            if (matcher.find()) {
                double latitude = Double.parseDouble(matcher.group(1));
                String latHemisphere = matcher.group(3);
                double longitude = Double.parseDouble(matcher.group(4));
                String longHemisphere = matcher.group(6);

                // 转换经纬度为统一格式
                latitude = latHemisphere.equals("S") ? -latitude : latitude;
                longitude = longHemisphere.equals("W") ? -longitude : longitude;

                return new BusinessGpsData(
                        latitude,
                        longitude,
                        // 此处添加其他解析字段...
                        matcher.group(2) // 时间戳
                );
            }
            return null;
        }
    }
   // 定义业务GPS数据的字段,如车辆ID、经纬度、时间戳等
    class BusinessGpsData {
        private final double latitude;
        private final double longitude;
        // 其他业务字段...

        public BusinessGpsData(double latitude, double longitude, String timestamp) {
            this.latitude = latitude;
            this.longitude = longitude;
            // 初始化其他业务字段...
            this.timestamp = timestamp;
        }

        // Getters and Setters
    }

消息队列生产者接口(MqProducer.java):

public interface MqProducer {
    void send(BusinessGpsData businessData);
}

消息队列生产者实现(RabbitMqProducer.java):

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMqProducer implements MqProducer {

    private final String queueName;
    private final ConnectionFactory factory;
    private Connection connection;
    private Channel channel;

    public RabbitMqProducer(String host) {
        this.queueName = "gps_data_queue";
        this.factory = new ConnectionFactory();
        this.factory.setHost(host);
        try {
            this.connection = factory.newConnection();
            this.channel = connection.createChannel();
            this.channel.queueDeclare(queueName, true, false, false, null);
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
            // Handle exception
        }
    }

    @Override
    public void send(BusinessGpsData businessData) {
        String message = "BusinessGpsData [" +
                "latitude=" + businessData.getLatitude() +
                ", longitude=" + businessData.getLongitude() +
                // 其他字段...
                ", timestamp=" + businessData.getTimestamp() +
                "]";
        try {
            channel.basicPublish("", queueName, null, message.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            // Handle exception
        }
    }

    // Ensure resources are cleaned up
    public void close() {
        if (channel != null) {
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

配置文件(resources/application.properties):

gps.server.port=6789
mq.broker.list=mq-broker1:5672

结论:

使用Java Socket编程实现GPS定位数据的接收与处理是一个强大且灵活的解决方案。通过创建一个监听特定端口的服务器,可以接收来自GPS设备的实时数据流。每个连接都在单独的线程中处理,确保了应用程序能够响应多个设备。解析GPS数据后,可以执行各种业务逻辑,如更新车辆位置、计算路线等。这种方法适用于需要实时位置跟踪和数据处理的多种应用场景。

  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Solomon_肖哥弹架构

你的欣赏就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值