54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								using langguanApi.Model.Entity;
							 | 
						|||
| 
								 | 
							
								using MongoDB.Bson.IO;
							 | 
						|||
| 
								 | 
							
								using System.Net;
							 | 
						|||
| 
								 | 
							
								using System.Net.Sockets;
							 | 
						|||
| 
								 | 
							
								using System.Text;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace langguanApi.Service
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    public class GpsService
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        private GpsHistoryService _service = ServiceLocator.Instance.GetService<GpsHistoryService>();
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 缓冲器
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        private byte[] result = new byte[1024];
							 | 
						|||
| 
								 | 
							
								        private int port => 5002;
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 启动GPS服务
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task Start()
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            // 创建本地IP地址和TCP端口号
							 | 
						|||
| 
								 | 
							
								            var ipEndPoint = new IPEndPoint(IPAddress.Any, port);
							 | 
						|||
| 
								 | 
							
								            TcpListener listener = new(ipEndPoint);
							 | 
						|||
| 
								 | 
							
								            try
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                listener.Start();
							 | 
						|||
| 
								 | 
							
								                Console.WriteLine($"Waiting for gps connection... port:{port}");
							 | 
						|||
| 
								 | 
							
								                using TcpClient handler = await listener.AcceptTcpClientAsync();
							 | 
						|||
| 
								 | 
							
								                await using NetworkStream stream = handler.GetStream();
							 | 
						|||
| 
								 | 
							
								                int received = await stream.ReadAsync(result);
							 | 
						|||
| 
								 | 
							
								                var message = Encoding.UTF8.GetString(result, 0, received);
							 | 
						|||
| 
								 | 
							
								                Console.WriteLine($"GPS received: \"{message}\"");
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                Console.WriteLine($"GPS error:{ex.Message}");
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        /// <summary>
							 | 
						|||
| 
								 | 
							
								        /// 发送GPS数据
							 | 
						|||
| 
								 | 
							
								        /// </summary>
							 | 
						|||
| 
								 | 
							
								        /// <param name="message"></param>
							 | 
						|||
| 
								 | 
							
								        /// <returns></returns>
							 | 
						|||
| 
								 | 
							
								        public async Task Send(string message)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            var json = Newtonsoft.Json.JsonConvert.DeserializeObject<GpsHistoryDTO>(message);
							 | 
						|||
| 
								 | 
							
								            await _service.AddGpsHistory(json);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |