Ejemplo 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));
        }
        public AvlData Decode(IBitReader reader)
        {
            AvlData avlData = reader != null?DefaultAvlDataEncoding.Instance.Decode(reader) : throw new ArgumentNullException(nameof(reader));

            ulong num    = reader.ReadUInt64();
            byte  status = reader.ReadByte();

            avlData.Data.Add(IoProperty.Create(78, num, status));
            return(avlData);
        }
Ejemplo n.º 3
0
        public IoElement Decode(IBitReader reader)
        {
            byte num1 = reader != null?reader.ReadByte() : throw new ArgumentNullException(nameof(reader));

            int num2 = 0;
            int id   = 0;
            List <IoProperty> properties = new List <IoProperty>(num1);

            while (num2 < num1)
            {
                bool bit;
                if (reader.ReadBit(out bit) != 1)
                {
                    throw new EndOfStreamException();
                }
                if (bit)
                {
                    long bits1;
                    if (reader.ReadBits(out bits1, 3) < 3)
                    {
                        throw new EndOfStreamException();
                    }
                    if (bits1 == 0L)
                    {
                        long bits2;
                        if (reader.ReadBits(out bits2, 1) < 1)
                        {
                            throw new EndOfStreamException();
                        }
                        properties.Add(IoProperty.Create(id, (byte)bits2));
                    }
                    else
                    {
                        if (bits1 != 1L)
                        {
                            throw new Exception("Unknown type");
                        }
                        long bits2;
                        if (reader.ReadBits(out bits2, 32) < 32)
                        {
                            throw new EndOfStreamException();
                        }
                        properties.Add(IoProperty.Create(id, (int)bits2));
                    }
                    ++num2;
                }
                ++id;
            }
            return(new IoElement(0, properties));
        }
Ejemplo n.º 4
0
        private IList <IoProperty> DecodeIoProperties()
        {
            var result = new List <IoProperty>();

            // total number of I/O properties which length is 1 byte
            int ioCountInt8 = _reader.ReadByte();

            for (var i = 0; i < ioCountInt8; i++)
            {
                var propertyId = (int)_reader.ReadByte();
                var value      = _reader.ReadSByte();

                result.Add(IoProperty.Create(propertyId, value));
            }

            // total number of I/O properties which length is 2 bytes
            int ioCountInt16 = _reader.ReadByte();

            for (var i = 0; i < ioCountInt16; i++)
            {
                var propertyId = (int)_reader.ReadByte();
                var value      = _reader.ReadInt16();

                result.Add(IoProperty.Create(propertyId, value));
            }

            // total number of I/O properties which length is 4 bytes
            int ioCountInt32 = _reader.ReadByte();

            for (var i = 0; i < ioCountInt32; i++)
            {
                var propertyId = (int)_reader.ReadByte();
                var value      = _reader.ReadInt32();

                result.Add(IoProperty.Create(propertyId, value));
            }

            // total number of I/O properties which length is 8 bytes
            int ioCountInt64 = _reader.ReadByte();

            for (var i = 0; i < ioCountInt64; i++)
            {
                var propertyId = (int)_reader.ReadByte();
                var value      = _reader.ReadInt64();

                result.Add(IoProperty.Create(propertyId, value));
            }

            return(result);
        }
 private static IEnumerable <IoProperty> ReadIoProperties(
     IBinaryReader reader)
 {
     return(new IoProperty[9]
     {
         IoProperty.Create(10, reader.ReadInt16()),
         IoProperty.Create(1, reader.ReadInt16()),
         IoProperty.Create(2, reader.ReadInt16()),
         IoProperty.Create(3, reader.ReadInt16()),
         IoProperty.Create(12, reader.ReadInt16()),
         IoProperty.Create(11, reader.ReadByte()),
         IoProperty.Create(5, reader.ReadByte()),
         IoProperty.Create(4, reader.ReadByte()),
         IoProperty.Create(6, reader.ReadByte())
     });
 }
Ejemplo n.º 6
0
        public GpsElementExt Decode(IBitReader reader)
        {
            int x = reader != null?reader.ReadInt32() : throw new ArgumentNullException(nameof(reader));

            int        y          = reader.ReadInt32();
            short      altitude   = (short)Math.Round(reader.ReadInt16() / 10.0);
            short      angle      = (short)Math.Round(reader.ReadUInt16() / 100.0);
            byte       satellites = reader.ReadByte();
            short      speed      = reader.ReadInt16();
            IoProperty ioProperty = IoProperty.Create(500001, reader.ReadByte());

            return(new GpsElementExt(new GpsElement(x, y, altitude, speed, angle, satellites), new IoElement(0, new List <IoProperty>(1)
            {
                ioProperty
            })));
        }
Ejemplo n.º 7
0
        public IoProperty DecodeProperty(FieldEncoding encoding)
        {
            // read ID
            var propertyId = (int)_reader.ReadByte();

            switch (encoding)
            {
            case FieldEncoding.Int8:
                return(IoProperty.Create(propertyId, _reader.ReadSByte()));

            case FieldEncoding.Int16:
                return(IoProperty.Create(propertyId, _reader.ReadInt16()));

            case FieldEncoding.Int32:
                return(IoProperty.Create(propertyId, _reader.ReadInt32()));

            default:
                throw new NotSupportedException(string.Format("The field encoding \"{0}\" is not supported.", encoding));
            }
        }
        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));
        }
        private static IEnumerable <IoProperty> ReadValues(IoType type, uint values)
        {
            Dictionary <At2000Io, BinaryIoAttribute> settings = GetSettings(type);

            return(settings == null?Enumerable.Empty <IoProperty>() : GetFlags(values, settings).Select(x => IoProperty.Create(x.Key, x.Value ? (byte)1 : (byte)0)));
        }
Ejemplo n.º 10
0
        private static void Main(string[] args)
        {
            Route.RouteContext routeContext = new Route.RouteContext(new DirectionService("5b3ce3597851110001cf6248e88658f607ac495cad9088fe0cd49df3"));

            List <Models.IoPropertyType> ioPropertiesTypeList = new List <Models.IoPropertyType> (new Models.IoPropertyType[] {
                new Models.IoPropertyType()
                {
                    Id          = 1,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 2,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 3,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 4,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 5,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 6,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 7,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 8,
                    Size        = 2,
                    Description = ""
                },
                new Models.IoPropertyType()
                {
                    Id          = 9,
                    Size        = 2,
                    Description = ""
                },
            });



            List <Scenario> scenariosList = new List <Scenario>(new Scenario[] { new Scenario()
                                                                                 {
                                                                                     ObservedIoProperties = new List <Models.IoPropertyType>(new Models.IoPropertyType[] { ioPropertiesTypeList[0], ioPropertiesTypeList[1], ioPropertiesTypeList[2], }),
                                                                                     RecordConditions     = new List <RecordCondition>(new RecordCondition[] { new FixedTimeRecordCondition()
                                                                                                                                                               {
                                                                                                                                                                   Period = TimeSpan.FromSeconds(7)
                                                                                                                                                               } })
                                                                                 },
                                                                                 new Scenario()
                                                                                 {
                                                                                     ObservedIoProperties = new List <Models.IoPropertyType>(new Models.IoPropertyType[] { ioPropertiesTypeList[3], ioPropertiesTypeList[4], ioPropertiesTypeList[5], }),
                                                                                     RecordConditions     = new List <RecordCondition>(new RecordCondition[] { new FixedTimeRecordCondition()
                                                                                                                                                               {
                                                                                                                                                                   Period = TimeSpan.FromSeconds(3)
                                                                                                                                                               } })
                                                                                 }, new Scenario()
                                                                                 {
                                                                                     ObservedIoProperties = new List <Models.IoPropertyType>(new Models.IoPropertyType[] { ioPropertiesTypeList[6], ioPropertiesTypeList[7], ioPropertiesTypeList[8], }),
                                                                                     RecordConditions     = new List <RecordCondition>(new RecordCondition[] { new FixedTimeRecordCondition()
                                                                                                                                                               {
                                                                                                                                                                   Period = TimeSpan.FromSeconds(5)
                                                                                                                                                               } })
                                                                                 } });
            List <Vehicle> VehiclesList = new List <Vehicle>();

            for (int i = 0; i < 100; i++)
            {
                VehiclesList.Add(new Vehicle());
            }
            foreach (var vehicle in VehiclesList)
            {
                foreach (var ioPropertyType in ioPropertiesTypeList)
                {
                    vehicle.IoPropertiesList.Add(IoProperty.Create(ioPropertyType.Id, 0));
                }
            }
            List <Device> DevicesList = new List <Device>();

            for (int i = 0; i < 100; i++)
            {
                DevicesList.Add(new Device(
                                    new Modem()
                {
                    Imei = (ulong)i,
                    MqttBrokerAddress = MqttBrokerAddress,
                    MqttClientId      = i.ToString(),
                    MqttUserName      = "******",
                    MqttPassword      = "******"
                })
                {
                    InstalledOnVehicle = VehiclesList[i],
                    Scenarios          = new ObservableCollection <Scenario>(scenariosList),
                }
                                );
            }

            var recordingTaskManager = new RecordingTaskManager();

            recordingTaskManager.RegisterRecordingPolicies((recordCondition) => new TimeFixedRecordingPolicies(((FixedTimeRecordCondition)recordCondition).Period), typeof(FixedTimeRecordCondition));

            List <DeviceManager> deviceManagerList = new List <DeviceManager>();

            deviceManagerList.AddRange(DevicesList.Select(o => new DeviceManager(o, recordingTaskManager)));
            List <VehicleManager> vehicleManagerList = new List <VehicleManager>();

            vehicleManagerList.AddRange(VehiclesList.Select(o => {
                var vehicleManager         = new VehicleManager(o);
                vehicleManager.Route       = routeContext.GetRoute("");
                vehicleManager.IsIgnitiate = true;
                vehicleManager.Driving     = new ConstantSpeedDrivingStrategy(vehicleManager, 100);
                return(vehicleManager);
            }));
            while (true)
            {
            }
            ;
        }
Ejemplo n.º 11
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));
        }