欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Java?Socket實現(xiàn)GPS定位數(shù)據(jù)處理

 更新時間:2024年07月12日 08:29:37   作者:肖哥彈架構(gòu)  
在許多應(yīng)用場景中,如車輛追蹤、移動設(shè)備定位等,GPS定位數(shù)據(jù)的實時獲取和處理至關(guān)重要,本文將介紹如何使用Java?Socket編程來接收GPS設(shè)備發(fā)送的數(shù)據(jù)并進(jìn)行處理,需要的朋友可以參考下

業(yè)務(wù)說明:

車輛追蹤系統(tǒng)需要實時獲取車輛的GPS定位信息。車輛上的GPS設(shè)備通過TCP連接發(fā)送其位置數(shù)據(jù)到服務(wù)器。

技術(shù)點:

  • Java Socket編程:使用Java的Socket API進(jìn)行網(wǎng)絡(luò)通信。
  • GPS定位數(shù)據(jù):理解GPS數(shù)據(jù)格式,如NMEA 0183標(biāo)準(zhǔn)。
  • 多線程處理:為每個連接創(chuàng)建線程以異步接收數(shù)據(jù)。

項目結(jié)構(gòu):

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:服務(wù)器啟動類。
  • GpsDataHandler.java:Netty通道處理器,用于處理接收到的GPS數(shù)據(jù)。
  • GpsDataParser.java:解析GPS數(shù)據(jù)的類。
  • BusinessDataService.java:業(yè)務(wù)數(shù)據(jù)處理服務(wù),負(fù)責(zé)將解析后的數(shù)據(jù)發(fā)送到MQ。
  • RabbitMqProducer.java:RabbitMQ生產(chǎn)者實現(xiàn)。
  • application.properties:應(yīng)用配置文件。
  • GpsServerTest.java:服務(wù)器的單元測試類。

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>

業(yè)務(wù)流程:

代碼:

服務(wù)器啟動類(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數(shù)據(jù)處理通道(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();
    }
}

業(yè)務(wù)數(shù)據(jù)服務(wù)(BusinessDataService.java):

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

public class BusinessDataService {

    private static final Logger logger = LoggerFactory.getLogger(BusinessDataService.class);
    // MQ客戶端或生產(chǎn)者,具體實現(xiàn)根據(jù)所使用的MQ進(jìn)行調(diào)整
    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);
        }
    }
}

業(yè)務(wù)GPS數(shù)據(jù)解析器(GpsDataParser.java):

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

    public class GpsDataParser {

        // 假設(shè)GPS數(shù)據(jù)格式為: $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、經(jīng)緯度、時間戳等信息,并創(chuàng)建BusinessGpsData對象
         * 解析NMEA 0183標(biāo)準(zhǔn)的GPGGA語句
         * @param nmeaSentence GPS數(shù)據(jù)字符串
         * @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);

                // 轉(zhuǎn)換經(jīng)緯度為統(tǒng)一格式
                latitude = latHemisphere.equals("S") ? -latitude : latitude;
                longitude = longHemisphere.equals("W") ? -longitude : longitude;

                return new BusinessGpsData(
                        latitude,
                        longitude,
                        // 此處添加其他解析字段...
                        matcher.group(2) // 時間戳
                );
            }
            return null;
        }
    }
   // 定義業(yè)務(wù)GPS數(shù)據(jù)的字段,如車輛ID、經(jīng)緯度、時間戳等
    class BusinessGpsData {
        private final double latitude;
        private final double longitude;
        // 其他業(yè)務(wù)字段...

        public BusinessGpsData(double latitude, double longitude, String timestamp) {
            this.latitude = latitude;
            this.longitude = longitude;
            // 初始化其他業(yè)務(wù)字段...
            this.timestamp = timestamp;
        }

        // Getters and Setters
    }

消息隊列生產(chǎn)者接口(MqProducer.java):

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

消息隊列生產(chǎn)者實現(xiàn)(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

結(jié)論:

使用Java Socket編程實現(xiàn)GPS定位數(shù)據(jù)的接收與處理是一個強大且靈活的解決方案。通過創(chuàng)建一個監(jiān)聽特定端口的服務(wù)器,可以接收來自GPS設(shè)備的實時數(shù)據(jù)流。每個連接都在單獨的線程中處理,確保了應(yīng)用程序能夠響應(yīng)多個設(shè)備。解析GPS數(shù)據(jù)后,可以執(zhí)行各種業(yè)務(wù)邏輯,如更新車輛位置、計算路線等。這種方法適用于需要實時位置跟蹤和數(shù)據(jù)處理的多種應(yīng)用場景。

以上就是使用Java Socket實現(xiàn)GPS定位數(shù)據(jù)處理的詳細(xì)內(nèi)容,更多關(guān)于Java Socket實現(xiàn)GPS定位的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java使用hadoop實現(xiàn)關(guān)聯(lián)商品統(tǒng)計

    java使用hadoop實現(xiàn)關(guān)聯(lián)商品統(tǒng)計

    本篇文章java使用hadoop實現(xiàn)關(guān)聯(lián)商品統(tǒng)計,可以實現(xiàn)商品的關(guān)聯(lián)統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • IDEA中try catch拋異??旖萱I分享

    IDEA中try catch拋異??旖萱I分享

    在編寫Java代碼時,使用IDEA的快捷鍵CTRL+ALT+t可以快速生成try..catch語句塊,有效提高編碼效率,首先選擇需要處理的代碼片段,然后按下快捷鍵,選擇try/catch選項,即可自動包圍選中代碼,這一快捷操作簡化了異常處理步驟,減少了手動編寫的時間,是編程中的實用技巧
    2024-10-10
  • SpringBoot中實現(xiàn)接收文件和對象

    SpringBoot中實現(xiàn)接收文件和對象

    這篇文章主要介紹了SpringBoot實現(xiàn)接收文件和對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java并發(fā)中線程封閉知識點詳解

    Java并發(fā)中線程封閉知識點詳解

    在本篇文章里我們給大家整理了關(guān)于Java并發(fā)中線程封閉的知識點總結(jié)內(nèi)容,需要的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • Java編程實現(xiàn)遍歷兩個MAC地址之間所有MAC的方法

    Java編程實現(xiàn)遍歷兩個MAC地址之間所有MAC的方法

    這篇文章主要介紹了Java編程實現(xiàn)遍歷兩個MAC地址之間所有MAC的方法,涉及Java針對MAC的遍歷獲取與字符串轉(zhuǎn)換相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Eclipse如何導(dǎo)入Maven項目詳解(新手初學(xué))

    Eclipse如何導(dǎo)入Maven項目詳解(新手初學(xué))

    這篇文章主要介紹了Eclipse如何導(dǎo)入Maven項目詳解(新手初學(xué)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Postman實現(xiàn)傳List<String>集合

    Postman實現(xiàn)傳List<String>集合

    這篇文章主要介紹了Postman實現(xiàn)傳List<String>集合方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java和matlab畫多邊形閉合折線圖示例講解

    java和matlab畫多邊形閉合折線圖示例講解

    由于要將“哈密頓回路問題(TSP)”的求解中間結(jié)果表示出來,查了一下使用程序畫多邊形圖形?,F(xiàn)在在總結(jié)一下,這個圖是“由給定節(jié)點首尾相連的”閉合多邊形
    2014-02-02
  • Java 超詳細(xì)圖解集合框架的數(shù)據(jù)結(jié)構(gòu)

    Java 超詳細(xì)圖解集合框架的數(shù)據(jù)結(jié)構(gòu)

    什么是集合框架呢?集合框架是為表示和操作集合而規(guī)定的一種統(tǒng)一的標(biāo)準(zhǔn)的體系結(jié)構(gòu)。最簡單的集合如數(shù)組、列表和隊列等,任何集合框架一般包含:對外的接口、接口的實現(xiàn)和對集合運算的算法
    2022-04-04
  • SpringBoot內(nèi)部外部配置文件加載順序解析

    SpringBoot內(nèi)部外部配置文件加載順序解析

    這篇文章主要介紹了SpringBoot內(nèi)部外部配置文件加載順序解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07

最新評論