89 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
using langguanApi.Model.Entity;
 | 
						|
using MongoDB.Bson.IO;
 | 
						|
using Org.BouncyCastle.Utilities.Net;
 | 
						|
using System.Net;
 | 
						|
using System.Net.Sockets;
 | 
						|
using System.Text;
 | 
						|
using IPAddress = System.Net.IPAddress;
 | 
						|
 | 
						|
namespace langguanApi.Service
 | 
						|
{
 | 
						|
 | 
						|
    public class GpsService
 | 
						|
    {
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 缓冲器
 | 
						|
        /// </summary>
 | 
						|
        private byte[] result = new byte[1024];
 | 
						|
        private int port => 5002;
 | 
						|
        /// <summary>
 | 
						|
        /// 启动GPS服务
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task Start()
 | 
						|
        {
 | 
						|
            // 创建本地IP地址和TCP端口号
 | 
						|
            // 定义监听地址和端口
 | 
						|
            try
 | 
						|
            {
 | 
						|
                // 定义监听地址和端口
 | 
						|
                IPAddress ipAddress = IPAddress.Any; // 使用本地所有可用的 IP 地址
 | 
						|
 | 
						|
                // 创建 TcpListener 实例
 | 
						|
                TcpListener listener = new TcpListener(ipAddress, port);
 | 
						|
                // 开始监听
 | 
						|
                listener.Start();
 | 
						|
                Console.WriteLine($" gps Listening on {ipAddress}:{port}...");
 | 
						|
                while (true)
 | 
						|
                {
 | 
						|
                    // 接受客户端连接
 | 
						|
                    TcpClient client = await listener.AcceptTcpClientAsync();
 | 
						|
                    // 处理客户端连接
 | 
						|
                    _ = Task.Run(() => HandleClientAsync(client));
 | 
						|
                }
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                Console.WriteLine($"GPS error:{ex.Message}");
 | 
						|
            }
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 发送GPS数据
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="message"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task HandleClientAsync(TcpClient client)
 | 
						|
        {
 | 
						|
 | 
						|
            string receivedData = string.Empty;
 | 
						|
            using (NetworkStream stream = client.GetStream())
 | 
						|
            {
 | 
						|
                byte[] buffer = new byte[1024];
 | 
						|
                int bytesRead;
 | 
						|
 | 
						|
                // 持续读取数据直到客户端断开连接
 | 
						|
                while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
 | 
						|
                {
 | 
						|
                    // 将字节数据转换为字符串
 | 
						|
                    receivedData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
 | 
						|
                    Console.WriteLine($" gps Received: {receivedData}");
 | 
						|
 | 
						|
 | 
						|
                    var json = Newtonsoft.Json.JsonConvert.DeserializeObject<GpsHistoryDTO>(receivedData);
 | 
						|
                    var _service = ServiceLocator.Instance.GetService<GpsHistoryService>();
 | 
						|
                    await _service.AddGpsHistory(json);
 | 
						|
                    // 发送响应给客户端
 | 
						|
                    string response = "Data received";
 | 
						|
                    byte[] responseData = Encoding.UTF8.GetBytes(response);
 | 
						|
                    await stream.WriteAsync(responseData, 0, responseData.Length);
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
          //  client.Close();
 | 
						|
            Console.WriteLine("Client disconnected.");
 | 
						|
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |