using System; using System.ComponentModel; using System.Text; namespace Plane.Net { public class PLNet : PLClass.PLNet { public PLNet(AsyncOperation asyncOperation) : base(asyncOperation) { } public event EventHandler GetUserLocation2; public event EventHandler GpsTrackEnded; public event EventHandler mynet_GPSTrackArrival2; /// /// 取轨迹。 /// /// 用户 ID。 /// 日期字符串,"20150416"。 /// 时间段,"145740-150306"。 public void GetGPSTrack2(string id, string date, string period) { SendCmd($"~{id}|{date}|{period.Replace('-', '|')}|0|1"); } public override bool ProcessData(byte[] data) { switch ((char)data[0]) { case 'G': ProcessUserLocationMessage(data); break; case '^': ProcessGpsTrackMessage(data); break; default: return base.ProcessData(data); } return true; } /// /// 查询哪天有轨迹。 /// /// 用户 ID。 /// 起始日期,"yyyyMMdd"。 /// 结束日期,"yyyyMMdd"。 public void QueryTrackDays(string id, string startDate, string endDate) { var cmd = $"DD{id}|{startDate}|{endDate}|1"; SendCmd(cmd); } /// /// 查询某天哪些时间段有轨迹。 /// /// 用户 ID。 /// 日期字符串,格式为 "yyyyMMdd"。 public void QueryTrackPeriods(string id, string date) { var cmd = $"DT{id}|{date}|1"; SendCmd(cmd); } private PositionEventArgs GetPositionEventArgs(byte[] data) { var getUserLocation2EventArgs = new PositionEventArgs(); getUserLocation2EventArgs.Ident = (char)data[1]; byte[] buffer = new byte[16]; Array.Copy(data, 2, buffer, 0, 16); getUserLocation2EventArgs.Id = Encoding.GetEncoding("GBK").GetString(buffer).TrimEnd('\0'); //buffer = new byte[4]; Array.Copy(data, 23, buffer, 0, 4); getUserLocation2EventArgs.Longitude = BitConverter.ToSingle(buffer, 0); Array.Copy(data, 27, buffer, 0, 4); getUserLocation2EventArgs.Latitude = BitConverter.ToSingle(buffer, 0); Array.Copy(data, 33, buffer, 0, 4); getUserLocation2EventArgs.Roll = BitConverter.ToSingle(buffer, 0); Array.Copy(data, 37, buffer, 0, 4); getUserLocation2EventArgs.Pitch = BitConverter.ToSingle(buffer, 0); Array.Copy(data, 41, buffer, 0, 4); getUserLocation2EventArgs.Yaw = BitConverter.ToSingle(buffer, 0); getUserLocation2EventArgs.Mode = data[45]; getUserLocation2EventArgs.Speed = data[46]; getUserLocation2EventArgs.Satellite = data[47]; getUserLocation2EventArgs.Battery = data[48]; //buffer = new byte[2]; //高度 Array.Copy(data, 31, buffer, 0, 2); getUserLocation2EventArgs.Altitude = BitConverter.ToInt16(buffer, 0); Array.Copy(data, 49, buffer, 0, 2); getUserLocation2EventArgs.Distance = BitConverter.ToUInt16(buffer, 0); Array.Copy(data, 51, buffer, 0, 2); getUserLocation2EventArgs.FlySecond = BitConverter.ToUInt16(buffer, 0); //buffer = new byte[8]; Array.Copy(data, 53, buffer, 0, 8); double r_time = BitConverter.ToDouble(buffer, 0); getUserLocation2EventArgs.RecordTime = ( new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromDays(r_time) ).ToString("yyyy-MM-dd HH:mm:ss.fff"); //new DateTime( // TimeSpan.FromMilliseconds(((r_time - 25569.0) * 86400000.0 - 28800000.0)).Ticks, // DateTimeKind.Utc // ).ToString("yyyy-MM-dd HH:mm:ss.fff"); return getUserLocation2EventArgs; } private void ProcessGpsTrackMessage(byte[] data) { int commandLength = data.Length - 1; if (commandLength < 18) { GpsTrackEnded?.Invoke(this, new GpsTrackEndedEventArgs { Id = Encoding.GetEncoding("GBK").GetString(data, 1, data.Length - 1) }); } else { var positionEventArgs = GetPositionEventArgs(data); mynet_GPSTrackArrival2?.Invoke(this, positionEventArgs); } } private void ProcessUserLocationMessage(byte[] data) { var positionEventArgs = GetPositionEventArgs(data); GetUserLocation2?.Invoke(this, positionEventArgs); } } }