Exemplo n.º 1
0
        /// <summary>
        /// Decode single AVL data
        /// </summary>
        /// <returns></returns>
        private AvlData DecodeAvlData()
        {
            var priorityAndTimestamp = _reader.ReadInt32();

            // priority
            var priorityMapIndex = 0x03 & (priorityAndTimestamp >> 30);
            var priority         = (GhAvlDataPriority)priorityMapIndex;

            // timestamp
            var timestamp = (long)(priorityAndTimestamp & 0x3FFFFFFF);
            var dateTime  = GHepoch.AddSeconds(timestamp);

            var eventId = 0;

            IoProperty?alarmProperty = null;

            if (priority == GhAvlDataPriority.Alarm)
            {
                eventId       = Constants.AlarmPropertyId;
                alarmProperty = IoProperty.Create(Constants.AlarmPropertyId, 1);
            }

            // global mask Codec7
            var mask = (GlobalMaskCodec7)_reader.ReadByte();

            var gps = GpsElement.Default;

            var gpsIo = new IoElement();


            if (mask.HasFlag(GlobalMaskCodec7.GpsElement))
            {
                var element = DecodeGpsElement();
                gps   = element.GPS;
                gpsIo = element.IO;
            }

            var ioInt8  = GetProperties(mask, GlobalMaskCodec7.IoInt8, FieldEncoding.Int8);
            var ioInt16 = GetProperties(mask, GlobalMaskCodec7.IoInt16, FieldEncoding.Int16);
            var ioInt32 = GetProperties(mask, GlobalMaskCodec7.IoInt32, FieldEncoding.Int32);

            var properties = new List <IoProperty>();

            if (alarmProperty != null)
            {
                properties.Add(alarmProperty.Value);
            }

            properties.AddRange(gpsIo.Properties);

            properties.AddRange(ioInt8 ?? Enumerable.Empty <IoProperty>());
            properties.AddRange(ioInt16 ?? Enumerable.Empty <IoProperty>());
            properties.AddRange(ioInt32 ?? Enumerable.Empty <IoProperty>());

            var ioElement = IoElement.Create(eventId, properties.Count(), properties);

            return(AvlData.Create(priority.ToString(), dateTime, gps, ioElement));
        }
Exemplo n.º 2
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));
         }
     }
 }
Exemplo n.º 4
0
        public AvlData Decode(IBitReader reader)
        {
            DateTime dateTime = reader != null?DateTimeExt.FromAvl(reader.ReadUInt32() * 1000L + reader.ReadByte() * 10) : throw new ArgumentNullException(nameof(reader));

            AvlDataPriority priority      = (AvlDataPriority)reader.ReadByte();
            GpsElementExt   gpsElementExt = FMPro3GpsElementEncoding.Instance.Decode(reader);
            IoElement       data          = FMPro3IOElementEncoding.Instance.Decode(reader);

            data.Add(gpsElementExt.IO[500001]);
            return(new AvlData(priority, dateTime, gpsElementExt.GPS, data));
        }
Exemplo n.º 5
0
        public IoElement DecodeIoElement(FieldEncoding encoding)
        {
            var count = _reader.ReadByte();

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

            for (var i = 0; i < count; i++)
            {
                properties.Add(DecodeProperty(encoding));
            }
            return(IoElement.Create(0, properties.Count(), properties));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Decode single AVL data
        /// </summary>
        /// <returns></returns>
        private AvlData DecodeAvlData()
        {
            var timestamp = _reader.ReadInt64();
            var dateTime  = AvlEpoch.AddMilliseconds(timestamp);
            var priority  = (AvlDataPriority)_reader.ReadByte();

            // GPS element decoding
            var gpsElement = DecodeGpsElement();

            // IO Element decoding
            var eventId         = _reader.ReadByte();
            var propertiesCount = _reader.ReadByte();
            // IO Element Properties decoding
            var ioProperties = DecodeIoProperties();

            var ioElement = IoElement.Create(eventId, propertiesCount, ioProperties);

            return(AvlData.Create(priority.ToString(), dateTime, gpsElement, ioElement));
        }
Exemplo 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));
        }
        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));
                }
            }
        }
Exemplo n.º 10
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));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Parses the io element.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        static IoElement ParseIoElement(string input, DateTime date)
        {
            Regex     propertyRegEx   = new Regex(@"(?<propertyName>\w+)\((?<value>\w+)/(?<value1>\w+)\)");
            Regex     propertyRegEx1  = new Regex(@"(?<propertyName>\w+)\((?<value>\w+)\)");
            var       matches         = propertyRegEx.Matches(input);
            var       matches1        = propertyRegEx1.Matches(input);
            var       ioPropertyNames = Enum.GetNames(typeof(TNIoProperty));
            IoElement ioElement       = new IoElement();

            ioElement.DataUtc = date;
            foreach (Match match in matches1)
            {
                if (!match.Success || !match.Groups["propertyName"].Success)
                {
                    continue;
                }

                if (match.Groups["propertyName"].Value.Trim() != "DataEventIO")
                {
                    continue;
                }
                ioElement.EventIoID =
                    Convert.ToByte(Enum.Parse(typeof(TNIoProperty), match.Groups["value"].Value, true));
                break;
            }
            foreach (Match match in matches)
            {
                if (!match.Success || !match.Groups["propertyName"].Success)
                {
                    continue;
                }

                var propertyName = ioPropertyNames.FirstOrDefault(p => p.Equals(match.Groups["propertyName"].Value.Trim()));
                if (propertyName == null)
                {
                    continue;
                }
                var numberOfBytes = Convert.ToInt32(match.Groups["value"].Value);
                var propertyId    = (TNIoProperty)Enum.Parse(typeof(TNIoProperty), match.Groups["propertyName"].Value, true);
                if (numberOfBytes == 1)
                {
                    var value = TypeDescriptor.GetConverter(typeof(byte)).ConvertFromString(match.Groups["value1"].Value);
                    if (value != null)
                    {
                        ioElement.OneBytesIoProperties.Add(new IoProperty <byte>(Convert.ToByte(propertyId), (byte)value));
                    }
                }
                else if (numberOfBytes == 2)
                {
                    var value = TypeDescriptor.GetConverter(typeof(ushort))
                                .ConvertFromString(match.Groups["value1"].Value);
                    if (value != null)
                    {
                        ioElement.TwoBytesIoProperties.Add(new IoProperty <ushort>(Convert.ToByte(propertyId),
                                                                                   (ushort)value));
                    }
                }
                else if (numberOfBytes == 4)
                {
                    var value = TypeDescriptor.GetConverter(typeof(uint))
                                .ConvertFromString(match.Groups["value1"].Value);
                    if (value != null)
                    {
                        ioElement.FourBytesIoProperties.Add(new IoProperty <uint>((byte)propertyId, (uint)value));
                    }
                }
                else if (numberOfBytes == 8)
                {
                    var value = TypeDescriptor.GetConverter(typeof(ulong))
                                .ConvertFromString(match.Groups["value1"].Value);
                    if (value != null)
                    {
                        ioElement.EightBytesIoProperties.Add(new IoProperty <ulong>((byte)propertyId, (ulong)value));
                    }
                }
            }
            return(ioElement);
        }