Esempio n. 1
0
        /// <summary>
        /// Parses the GPS element.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        static GpsElement ParseGpsElement(string input)
        {
            var   gpsElement    = new GpsElement();
            Regex propertyRegEx = new Regex(@" ([A-Z]\w+)");
            var   matches       = propertyRegEx.Matches(input);

            foreach (Match match in matches)
            {
                if (!match.Success)
                {
                    continue;
                }
                var properties   = gpsElement.GetType().GetProperties();
                var properTyName = match.Value.Trim();
                var p            = properties.FirstOrDefault(g => g.Name == properTyName);
                if (p == null)
                {
                    continue;
                }
                var startIndex    = input.IndexOf(properTyName, StringComparison.Ordinal) + 1 + properTyName.Length;
                var lengthOfValue = GetValueIndex(input.Substring(startIndex));
                var value         = input.Substring(startIndex, lengthOfValue);
                p.SetValue(gpsElement, Convert.ChangeType(value, p.PropertyType), null);
            }

            return(gpsElement);
        }
Esempio n. 2
0
        public void Encode(AvlData[] obj, IBitWriter writer)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            int length = obj.Length;

            if (length == 0)
            {
                throw new ArgumentException("0 AvlData to encode");
            }
            long num1 = Timestamp2Y2K(DateTimeExt.ToAvl(obj[0].DateTime));

            WriteBitSequence(writer, num1, 35);
            WriteBitSequence(writer, length, 5);
            long num2 = 0;
            long num3 = 0;

            for (int index = 0; index < length; ++index)
            {
                AvlData    avlData = obj[index];
                GpsElement gps     = avlData.GPS;
                bool       isValid = gps.IsValid;
                writer.WriteBit(isValid);
                if (isValid)
                {
                    long num4 = num2;
                    long num5 = num3;
                    gps  = avlData.GPS;
                    num2 = CompressGws(gps.X, -1800000000L, 1800000000L, 3600000000L, 2097151L);
                    gps  = avlData.GPS;
                    num3 = CompressGws(gps.Y, -900000000L, 900000000L, 1800000000L, 1048575L);
                    long num6 = num4 - num2;
                    long num7 = num5 - num3;
                    if (Math.Abs(num6) < 8191L && Math.Abs(num7) < 8191L)
                    {
                        writer.WriteBit(true);
                        WriteBitSequence(writer, num6 + 8191L, 14);
                        WriteBitSequence(writer, num7 + 8191L, 14);
                    }
                    else
                    {
                        writer.WriteBit(false);
                        WriteBitSequence(writer, num2, 21);
                        WriteBitSequence(writer, num3, 20);
                    }
                    IBitWriter writer1 = writer;
                    gps = avlData.GPS;
                    long speed = gps.Speed;
                    WriteBitSequence(writer1, speed, 8);
                }
            }
        }
Esempio n. 3
0
        public bool Equals(AvlData x, AvlData y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            {
                return(false);
            }
            GpsElement gps = x.GPS;
            int        num1;

            if (!gps.IsValid)
            {
                gps  = y.GPS;
                num1 = gps.IsValid ? 1 : 0;
            }
            else
            {
                num1 = 1;
            }
            if (num1 == 0)
            {
                return(true);
            }
            if (x.DateTime != y.DateTime)
            {
                return(false);
            }
            gps = x.GPS;
            int speed1 = gps.Speed;

            gps = y.GPS;
            int speed2 = gps.Speed;
            int num2;

            if (speed1 == speed2)
            {
                gps = x.GPS;
                int x1 = gps.X;
                gps = y.GPS;
                int x2 = gps.X;
                if (Math.Abs(x1 - x2) <= 1717)
                {
                    gps = x.GPS;
                    int y1 = gps.Y;
                    gps = y.GPS;
                    int y2 = gps.Y;
                    num2 = Math.Abs(y1 - y2) <= 1717 ? 1 : 0;
                    goto label_15;
                }
            }
            num2 = 0;
label_15:
            return(num2 != 0);
        }
Esempio n. 4
0
        public AvlData Decode(IBitReader reader)
        {
            DateTime dateTime = reader != null?DateTimeExt.FromAvl(reader.ReadInt64()) : throw new ArgumentNullException(nameof(reader));

            AvlDataPriority priority = (AvlDataPriority)reader.ReadByte();
            GpsElement      gps      = DefaultGpsElementEncoding.Instance.Decode(reader);
            IoElement       data     = DefaultIOElementEncoding.Instance16.Decode(reader);

            return(new AvlData(priority, dateTime, gps, data));
        }
 private static AvlData DecodeInternal(ArraySegment <byte> data)
 {
     if (data.Count != 63)
     {
         throw new Exception("Invalid packet size!");
     }
     using (MemoryStream memoryStream = new MemoryStream(data.Array, data.Offset, data.Count, false))
     {
         using (EndianBinaryReader endianBinaryReader = new EndianBinaryReader(EndianBitConverter.Little, memoryStream))
         {
             int      num1 = endianBinaryReader.ReadInt16();
             int      num2 = endianBinaryReader.ReadInt16();
             long     num3 = endianBinaryReader.ReadInt64();
             DateTime dateTime;
             if (0L <= num3 && num3 <= uint.MaxValue)
             {
                 dateTime = Common.UnixEpoch.AddSeconds(num3).ToLocalTime();
             }
             else
             {
                 if (Log.IsDebugEnabled)
                 {
                     Log.DebugFormat("Invalid date/time: {0}", (object)data.ToHexString());
                 }
                 dateTime = DateTime.MinValue;
             }
             float num4      = endianBinaryReader.ReadSingle();
             float num5      = endianBinaryReader.ReadSingle();
             short altitude  = (short)endianBinaryReader.ReadSingle();
             short speed     = (short)endianBinaryReader.ReadSingle();
             short angle     = (short)endianBinaryReader.ReadSingle();
             uint  geoEvents = endianBinaryReader.ReadUInt32();
             uint  ioEvents  = endianBinaryReader.ReadUInt32();
             uint  values    = endianBinaryReader.ReadUInt32();
             IEnumerable <IoProperty> second1 = ReadValues(IoType.Io, endianBinaryReader.ReadUInt32());
             IEnumerable <IoProperty> second2 = ReadEvents(IoType.IoEvent, ioEvents);
             IEnumerable <IoProperty> second3 = ReadValues(IoType.Geofencing, values);
             IEnumerable <IoProperty> second4 = ReadEvents(IoType.GeofencingEvent, geoEvents);
             IEnumerable <IoProperty> first   = ReadIoProperties(endianBinaryReader);
             byte              satellites     = endianBinaryReader.ReadByte();
             GpsElement        gps            = new GpsElement(num4, num5, altitude, speed, angle, satellites);
             List <IoProperty> list           = first.Union(second1).Union(second2).Union(second3).Union(second4).ToList();
             IoElement         data1          = new IoElement(GetSettings(IoType.IoEvent).Where(x => !((IEnumerable <At2000Io>)At2000EventsToIgnore).Contains(x.Key)).Where(x =>
             {
                 int num = 1 << x.Value.BitNumber;
                 return((ioEvents & num) == num);
             }).Select(x => (int)(x.Key - 34)).Union(GetSettings(IoType.GeofencingEvent).Where(x => !((IEnumerable <At2000Io>)At2000EventsToIgnore).Contains(x.Key)).Where(x =>
             {
                 int num = 1 << x.Value.BitNumber;
                 return((geoEvents & num) == num);
             }).Select(x => (int)(x.Key - 34))).ToArray(), list);
             return(new AvlData(AvlDataPriority.Low, dateTime, gps, data1));
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Decode Gps element
        /// </summary>
        /// <returns></returns>
        private GpsElement DecodeGpsElement()
        {
            var longitude  = _reader.ReadInt32();
            var latitude   = _reader.ReadInt32();
            var altitude   = _reader.ReadInt16();
            var angle      = _reader.ReadInt16();
            var satellites = _reader.ReadByte();
            var speed      = _reader.ReadInt16();

            return(GpsElement.Create(longitude, latitude, altitude, speed, angle, satellites));
        }
Esempio n. 7
0
        public AvlData Decode(IBitReader reader)
        {
            DateTime dateTime = reader != null?DateTimeExt.FromAvl(reader.ReadInt64()) : throw new ArgumentNullException(nameof(reader));

            AvlDataPriority priority = AvlDataPriority.Low;
            byte            num      = reader.ReadByte();

            if (num > 10 && num <= 20)
            {
                priority = AvlDataPriority.High;
            }
            GpsElement gps  = DefaultGpsElementEncoding.Instance.Decode(reader);
            IoElement  data = LegacyIoElementEncoding.Instance.Decode(reader);

            if (priority == AvlDataPriority.High)
            {
                data = new IoElement(-1, data.Properties);
            }
            return(new AvlData(priority, dateTime, gps, data));
        }
        public AvlData Decode(IBitReader reader)
        {
            int num1 = reader != null?reader.ReadInt32() : throw new ArgumentNullException(nameof(reader));

            int             index     = 3 & num1 >> 30;
            AvlDataPriority priority  = Priorities[index];
            long            num2      = num1 & 1073741823;
            DateTime        dateTime  = GHepoch.AddSeconds(num2);
            GhGlobalMask    mask      = (GhGlobalMask)reader.ReadByte();
            GpsElement      gps       = GpsElement.Default;
            IoElement       ioElement = new IoElement();

            if (mask.HasFlag(GhGlobalMask.GpsElement))
            {
                GpsElementExt gpsElementExt = GhGpsElementEncoding.Instance.Decode(reader);
                gps       = gpsElementExt.GPS;
                ioElement = gpsElementExt.IO;
            }
            IList <IoProperty> properties1    = GetProperties(reader, mask, GhGlobalMask.IoInt8, GhIoElementEncoding.Int8);
            IList <IoProperty> properties2    = GetProperties(reader, mask, GhGlobalMask.IoInt16, GhIoElementEncoding.Int16);
            IList <IoProperty> properties3    = GetProperties(reader, mask, GhGlobalMask.IoInt32, GhIoElementEncoding.Int32);
            List <IoProperty>  ioPropertyList = new List <IoProperty>();

            ioPropertyList.AddRange(ioElement);
            ioPropertyList.AddRange(properties1 ?? Enumerable.Empty <IoProperty>());
            ioPropertyList.AddRange(properties2 ?? Enumerable.Empty <IoProperty>());
            ioPropertyList.AddRange(properties3 ?? Enumerable.Empty <IoProperty>());
            int eventId = 0;

            if (priority == AvlDataPriority.Panic)
            {
                IoProperty ioProperty = IoProperty.Create(204, (byte)1);
                ioPropertyList.Add(ioProperty);
                eventId = ioPropertyList.SingleOrDefault(x => x.Id == 222) == IoProperty.Default ? 204 : 222;
            }
            IoElement data = new IoElement(eventId, ioPropertyList);

            return(new AvlData(priority, dateTime, gps, data));
        }
Esempio n. 9
0
        public void Encode(GpsElementExt element, IBitWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            writer.Write(element.GPS.X);
            IBitWriter bitWriter1 = writer;
            GpsElement gps        = element.GPS;
            int        y          = gps.Y;

            bitWriter1.Write(y);
            IBitWriter bitWriter2 = writer;

            gps = element.GPS;
            int num1 = (short)(gps.Altitude * 10);

            bitWriter2.Write((short)num1);
            IBitWriter bitWriter3 = writer;

            gps = element.GPS;
            int num2 = (short)(gps.Angle * 100);

            bitWriter3.Write((short)num2);
            IBitWriter bitWriter4 = writer;

            gps = element.GPS;
            int satellites = gps.Satellites;

            bitWriter4.Write((byte)satellites);
            IBitWriter bitWriter5 = writer;

            gps = element.GPS;
            int speed = gps.Speed;

            bitWriter5.Write((short)speed);
            writer.Write((byte)element.IO[500001]);
        }
        public AvlData Decode(IBitReader reader)
        {
            byte num = reader != null?reader.ReadByte() : throw new ArgumentNullException(nameof(reader));

            byte[] buffer = reader.ReadBytes(num);
            if (reader.ReadByte() != CRC.CalculateXor(buffer))
            {
                throw new Exception("AvlData Record CRC failure. Received CRC - {0}, calculated CRC - {1}.");
            }
            using (MemoryStream stream = new MemoryStream())
            {
                using (IBitReader suitableBitReader = stream.CreateSuitableBitReader())
                {
                    stream.Write(buffer, 0, num);
                    stream.Position = 0L;
                    long            timestamp = suitableBitReader.ReadUInt32() * 1000L;
                    AvlDataPriority priority  = (AvlDataPriority)suitableBitReader.ReadByte();
                    GpsElement      gps       = DefaultGpsElementEncoding.Instance.Decode(suitableBitReader);
                    IoElement       data      = DefaultIOElementEncoding.Instance.Decode(suitableBitReader);
                    DateTime        dateTime  = DateTimeExt.FromAvl(timestamp);
                    return(new AvlData(priority, dateTime, gps, data));
                }
            }
        }
Esempio n. 11
0
        public static string GetStringValue(ArraySegment <byte> arraySegment, DataType dataType)
        {
            var subArray = arraySegment.Array.Skip(arraySegment.Offset).Take(arraySegment.Count).ToArray();

            switch (dataType)
            {
            // Avl Data types
            case DataType.CodecId:
            case DataType.AvlDataCount:
            case DataType.Priority:
            case DataType.Satellites:
            case DataType.EventIoId:
            case DataType.IoCount:
            case DataType.IoCount1B:
            case DataType.IoId1B:
            case DataType.IoValue1B:
            case DataType.IoCount2B:
            case DataType.IoId2B:
            case DataType.IoCount4B:
            case DataType.IoId4B:
            case DataType.IoCount8B:
            case DataType.IoId8B:
                return(subArray[0].ToString());

            case DataType.Latitude:
            case DataType.Longitude:
                return(BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0)).ToString());

            case DataType.IoValue4B:
                return(GetSignedUnsigned32(subArray));

            case DataType.Altitude:
            case DataType.Angle:
            case DataType.Speed:
                return(BytesSwapper.Swap(BitConverter.ToInt16(subArray, 0)).ToString());

            case DataType.IoValue2B:
                return(GetSignedUnsigned16(subArray));

            case DataType.IoValue8B:
                return(GetSignedUnsigned64(subArray));

            case DataType.Timestamp:
                return(AvlEpoch.AddMilliseconds(BytesSwapper.Swap(BitConverter.ToInt64(subArray, 0))).ToString());

            // Codec7 types
            case DataType.TimestampGh:
            {
                var priorityAndTimeStamp = BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0));
                var timeStamp            = (long)(priorityAndTimeStamp & 0x3FFFFFFF);
                return(GHepoch.AddSeconds(timeStamp).ToString());
            }

            case DataType.PriorityGh:
            {
                string priorityAndTimestampBits = string.Join(" ", subArray.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
                return(((GhAvlDataPriority)Convert.ToInt16(priorityAndTimestampBits.Substring(0, 2))).ToString());
            }

            case DataType.GlobalMask:
                return(((GlobalMaskCodec7)subArray[0]).ToString());

            case DataType.GpsElementMask:
                return(((GpsElementMaskCodec7)subArray[0]).ToString());

            case DataType.LongitudeGh:
            case DataType.LatitudeGh:
            {
                var coordinate = EndianBitConverter.ToSingle(subArray, 0);
                if (!GpsElement.IsLatValid(coordinate))
                {
                    return(0.ToString());
                }
                return(coordinate.ToString());
            }

            case DataType.AngleGh:
            case DataType.SpeedGh:
                return(subArray[0].ToString());

            case DataType.CellIdAndLocalAreaGh:
            case DataType.OperatorCodeGh:
                return(BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0)).ToString());

            case DataType.SignalQualityGh:
                return(subArray[0].ToString());

            // Tcp types
            case DataType.Preamble:
            case DataType.AvlDataArrayLength:
            case DataType.Crc:
                return(GetSignedUnsigned32(subArray));

            // Udp types
            case DataType.Length:
            case DataType.PacketId:
            case DataType.ImeiLength:
                return(GetSignedUnsigned16(subArray));

            case DataType.PacketType:
            case DataType.AvlPacketId:
                return(subArray[0].ToString());

            case DataType.Imei:
                return(Encoding.UTF8.GetString(subArray));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Decode Gps element
        /// </summary>
        /// <returns></returns>
        private GpsElementExt DecodeGpsElement()
        {
            var mask = (GpsElementMaskCodec7)_reader.ReadByte();

            float x = 0;
            float y = 0;

            if (mask.HasFlag(GpsElementMaskCodec7.Coordinates))
            {
                var lat = EndianBitConverter.Int32ToSingle(_reader.ReadInt32());
                var lng = EndianBitConverter.Int32ToSingle(_reader.ReadInt32());

                if (!GpsElement.IsLatValid(lat))
                {
                    lat = 0;
                }
                if (!GpsElement.IsLngValid(lng))
                {
                    lng = 0;
                }

                y = lat;
                x = lng;
            }

            short altitude = 0;

            if (mask.HasFlag(GpsElementMaskCodec7.Altitude))
            {
                altitude = _reader.ReadInt16();
            }

            short angle = 0;

            if (mask.HasFlag(GpsElementMaskCodec7.Angle))
            {
                angle = (short)Math.Round(((double)_reader.ReadByte() * 360 / 256));
            }

            short speed = 0;

            if (mask.HasFlag(GpsElementMaskCodec7.Speed))
            {
                speed = _reader.ReadByte();
            }

            var satellites = mask.HasFlag(GpsElementMaskCodec7.Satellites) ? _reader.ReadByte() : (byte)3;

            var properties = new List <IoProperty>(3);

            if (mask.HasFlag(GpsElementMaskCodec7.CellId))
            {
                var cellId = _reader.ReadInt32();
                properties.Add(IoProperty.Create(Constants.CellIdPropertyId, cellId));
            }

            if (mask.HasFlag(GpsElementMaskCodec7.SignalQuality))
            {
                var signalQuality = _reader.ReadByte();
                properties.Add(IoProperty.Create(Constants.SignalQualityPropertyId, signalQuality));
            }

            if (mask.HasFlag(GpsElementMaskCodec7.OperatorCode))
            {
                var code = _reader.ReadInt32();
                properties.Add(IoProperty.Create(Constants.OperatorCodePropertyId, code));
            }

            // set the N/A position if it's not available
            if (x == 0 && y == 0)
            {
                speed      = GpsElement.InvalidGpsSpeed;
                satellites = 0;
            }

            var gps = GpsElement.Create(x, y, altitude, speed, angle, satellites);

            var io = IoElement.Create(0, properties.Count, properties);

            return(new GpsElementExt(gps: gps, io: io));
        }