111 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
using System.Text;
 | 
						|
 | 
						|
namespace langguanApi.Common.Gps
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// gps 协议解析器
 | 
						|
    /// </summary>
 | 
						|
    public class ProtocolParser
 | 
						|
    {
 | 
						|
        /// <summary>
 | 
						|
        /// 解析消息头
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="data"></param>
 | 
						|
        /// <param name="offset"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public static MessageHeader ParseHeader(byte[] data, ref int offset)
 | 
						|
        {
 | 
						|
            var header = new MessageHeader
 | 
						|
            {
 | 
						|
                MessageId = BitConverter.ToUInt16(data, offset),
 | 
						|
                MessageBodyProperty = BitConverter.ToUInt16(data, offset + 2),
 | 
						|
                TerminalId = Encoding.ASCII.GetString(data, offset + 4, 6),
 | 
						|
                MessageSerialNumber = BitConverter.ToUInt16(data, offset + 10)
 | 
						|
            };
 | 
						|
 | 
						|
            offset += 12;
 | 
						|
            return header;
 | 
						|
        }
 | 
						|
        public struct Position
 | 
						|
        {
 | 
						|
            public double Latitude;
 | 
						|
            public double Longitude;
 | 
						|
            public double Altitude;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 解析位置信息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="data"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public static Position ParsePosition(byte[] data)
 | 
						|
        {
 | 
						|
            // 根据协议中的位置信息的偏移量和长度进行解析
 | 
						|
            int positionOffset = 21; // 位置信息在数据中的偏移量
 | 
						|
            int latitudeOffset = positionOffset + 0;
 | 
						|
            int longitudeOffset = positionOffset + 8;
 | 
						|
            int altitudeOffset = positionOffset + 16;
 | 
						|
 | 
						|
            double latitude = BitConverter.ToDouble(data, latitudeOffset);
 | 
						|
            double longitude = BitConverter.ToDouble(data, longitudeOffset);
 | 
						|
            double altitude = BitConverter.ToDouble(data, altitudeOffset);
 | 
						|
 | 
						|
            return new Position
 | 
						|
            {
 | 
						|
                Latitude = latitude,
 | 
						|
                Longitude = longitude,
 | 
						|
                Altitude = altitude
 | 
						|
            };
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 解析终端注册消息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="data"></param>
 | 
						|
        /// <param name="offset"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public static TerminalRegisterMessage ParseTerminalRegisterMessage(byte[] data, ref int offset)
 | 
						|
        {
 | 
						|
            var message = new TerminalRegisterMessage
 | 
						|
            {
 | 
						|
                ProvinceId = BitConverter.ToUInt16(data, offset),
 | 
						|
                CityId = BitConverter.ToUInt16(data, offset + 2),
 | 
						|
                ManufacturerId = data.Skip(offset + 4).Take(5).ToArray(),
 | 
						|
                TerminalType = data.Skip(offset + 9).Take(8).ToArray(),
 | 
						|
                TerminalId = data.Skip(offset + 17).Take(7).ToArray(),
 | 
						|
                LicensePlateColor = data[offset + 24],
 | 
						|
                LicensePlate = Encoding.ASCII.GetString(data, offset + 25, data.Length - offset - 25)
 | 
						|
            };
 | 
						|
 | 
						|
            offset += 25 + message.LicensePlate.Length;
 | 
						|
            return message;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 解析位置上报消息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="data"></param>
 | 
						|
        /// <param name="offset"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public static LocationReportMessage ParseLocationReportMessage(byte[] data, ref int offset)
 | 
						|
        {
 | 
						|
            var message = new LocationReportMessage
 | 
						|
            {
 | 
						|
                AlarmFlag = BitConverter.ToUInt32(data, offset),
 | 
						|
                Status = BitConverter.ToUInt32(data, offset + 4),
 | 
						|
                Latitude = BitConverter.ToUInt32(data, offset + 8),
 | 
						|
                Longitude = BitConverter.ToUInt32(data, offset + 12),
 | 
						|
                Altitude = BitConverter.ToUInt16(data, offset + 16),
 | 
						|
                Speed = BitConverter.ToUInt16(data, offset + 18),
 | 
						|
                Direction = BitConverter.ToUInt16(data, offset + 20),
 | 
						|
                Time = DateTime.ParseExact(
 | 
						|
                    Encoding.ASCII.GetString(data, offset + 22, 6),
 | 
						|
                    "yyMMddHHmmss",
 | 
						|
                    null
 | 
						|
                )
 | 
						|
            };
 | 
						|
 | 
						|
            offset += 28;
 | 
						|
            return message;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |