Ejemplo n.º 1
0
        public SpatialRecord Map(ISOSpatialRow isoSpatialRow, List <WorkingData> meters)
        {
            var spatialRecord = new SpatialRecord();

            foreach (var meter in meters.OfType <NumericWorkingData>())
            {
                SetNumericMeterValue(isoSpatialRow, meter, spatialRecord);
            }

            foreach (var meter in meters.OfType <EnumeratedWorkingData>())
            {
                SetEnumeratedMeterValue(isoSpatialRow, meter, spatialRecord);
            }

            spatialRecord.Geometry = new ApplicationDataModel.Shapes.Point
            {
                X = Convert.ToDouble(isoSpatialRow.EastPosition * CoordinateMultiplier),
                Y = Convert.ToDouble(isoSpatialRow.NorthPosition * CoordinateMultiplier),
                Z = isoSpatialRow.Elevation
            };

            spatialRecord.Timestamp = isoSpatialRow.TimeStart;

            return(spatialRecord);
        }
        public void GivenCondensedWorkStateWithSomeStatesNotInstalledThenOnlyCreatesInstalledSections()
        {
            var        spatialRow   = new ISOSpatialRow();
            const long notInstalled = 3;
            long       value        = 0;

            for (int i = 15; i > 11; --i)
            {
                value |= (notInstalled << i * 2);
            }
            var spatialValue = new SpatialValue
            {
                Dlv = new DLV
                {
                    A = "A1"   // need to set A specified true?
                },
                Value = value
            };

            spatialRow.SpatialValues = new List <SpatialValue> {
                spatialValue
            };
            _isoSpatialRows = new List <ISOSpatialRow> {
                spatialRow
            };
            var result = new CondensedWorkStateMeterCreator(161, 161).CreateMeters(_isoSpatialRows);

            Assert.AreEqual(12, result.Count);
        }
Ejemplo n.º 3
0
        public static void AreEqual(ISOSpatialRow isoSpatialRow, SpatialRecord adaptSpatialRecord, IEnumerable <WorkingData> meters)
        {
            foreach (var meter in meters)
            {
                var isoValue = isoSpatialRow.SpatialValues.SingleOrDefault(v => v.Id == meter.Id.FindIntIsoId());

                if (isoValue == null)
                {
                    continue;
                }

                if (meter is NumericWorkingData)
                {
                    var numericMeter = meter as NumericWorkingData;

                    var numericRepresentationValue = (NumericRepresentationValue)adaptSpatialRecord.GetMeterValue(numericMeter);
                    if (isoValue != null)
                    {
                        Assert.AreEqual(isoValue.Value, numericRepresentationValue.Value.Value);
                    }
                }
                if (meter is EnumeratedWorkingData)
                {
                    var isoEnumeratedMeter = meter as ISOEnumeratedMeter;
                    var enumeratedValue    = isoEnumeratedMeter.GetEnumeratedValue(isoValue, isoEnumeratedMeter);
                    Assert.AreEqual(enumeratedValue.Representation.Description, meter.Representation.Description);
                }
            }
        }
Ejemplo n.º 4
0
        public IEnumerable <SpatialRecord> Map(IEnumerable <ISOSpatialRow> isoSpatialRows, List <WorkingData> meters, Dictionary <string, List <ISOProductAllocation> > productAllocations)
        {
            //Compare the first spatial record to product allocations in case there is a mismatch between local time and UTC in comparing dates
            //This value will reflect an offset between the processing computer's timezone settings vs. the actual offset of the data itself,
            //and serves to provide a correction if PANs were reported as UTC.
            //This code will require an exact minute/second match between the first record and one of the PANs to take effect
            ISOSpatialRow firstSpatialRow = isoSpatialRows.First();

            if (productAllocations.Any())
            {
                foreach (ISOProductAllocation pan in productAllocations.First().Value)
                {
                    if (pan.AllocationStamp?.Start != null &&
                        pan.AllocationStamp.Start.Value.Minute == firstSpatialRow.TimeStart.Minute &&
                        pan.AllocationStamp.Start.Value.Second == firstSpatialRow.TimeStart.Second)
                    {
                        _effectiveTimeZoneOffset = (firstSpatialRow.TimeStart - pan.AllocationStamp.Start.Value).TotalHours;
                    }
                }
            }

            _representationValueInterpolator.Clear();

            return(isoSpatialRows.Select(r => Map(r, meters, productAllocations)));
        }
Ejemplo n.º 5
0
        private void SetGpsUtcDateTime(PTN ptn, ISOSpatialRow record, System.IO.BinaryReader binaryReader)
        {
            if (ptn.HSpecified)
            {
                if (ptn.H.HasValue)
                {
                    record.GpsUtcTime = Convert.ToInt32(ptn.H.Value);
                }
                else
                {
                    record.GpsUtcTime = binaryReader.ReadInt32();
                }
            }

            if (ptn.ISpecified)
            {
                if (ptn.I.HasValue)
                {
                    record.GpsUtcDate = (short)ptn.I.Value;
                }
                else
                {
                    record.GpsUtcDate = binaryReader.ReadInt16();
                }
            }

            if (record.GpsUtcDate != null && record.GpsUtcTime != null)
            {
                record.GpsUtcDateTime = _firstDayOf1980.AddDays((double)record.GpsUtcDate).AddMilliseconds((double)record.GpsUtcTime);
            }
        }
Ejemplo n.º 6
0
        public SpatialRecord Map(ISOSpatialRow isoSpatialRow, List <WorkingData> meters, Dictionary <string, List <ISOProductAllocation> > productAllocations)
        {
            var spatialRecord = new SpatialRecord();

            spatialRecord.Geometry = new ApplicationDataModel.Shapes.Point
            {
                X = Convert.ToDouble(isoSpatialRow.EastPosition * CoordinateMultiplier),
                Y = Convert.ToDouble(isoSpatialRow.NorthPosition * CoordinateMultiplier),
                Z = Convert.ToDouble(isoSpatialRow.Elevation.GetValueOrDefault() * ZMultiplier)
            };

            spatialRecord.Timestamp = isoSpatialRow.TimeStart;

            foreach (var meter in meters.OfType <NumericWorkingData>())
            {
                SetNumericMeterValue(isoSpatialRow, meter, spatialRecord, productAllocations);
            }

            foreach (var meter in meters.OfType <EnumeratedWorkingData>())
            {
                SetEnumeratedMeterValue(isoSpatialRow, meter, spatialRecord);
            }

            return(spatialRecord);
        }
Ejemplo n.º 7
0
        public void GivenIsoSpatialRowsWithoutEnumeratedMeterOnSecondWhenMapThenSecondValueIsInterpolator()
        {
            var spatialValue = new SpatialValue
            {
                Id    = 0,
                Value = 1
            };

            _isoSpatialRow.SpatialValues = new List <SpatialValue> {
                spatialValue
            };
            var isoSpatialRow2 = new ISOSpatialRow {
                SpatialValues = new List <SpatialValue>()
            };

            _isoSpatialRows = new List <ISOSpatialRow> {
                _isoSpatialRow, isoSpatialRow2
            };

            var meter = new ISOEnumeratedMeter
            {
                Representation = RepresentationInstanceList.dtSectionControlMasterState.ToModelRepresentation(),
                ValueCodes     = new List <int> {
                    1, 2, 3
                },
                DeviceElementUseId = 1,
                GetEnumeratedValue = (sv, im) => new EnumeratedValue {
                    Value = new AgGateway.ADAPT.ApplicationDataModel.Representations.EnumerationMember {
                        Code = 3
                    }
                }
            };

            var uniqueId = new UniqueId
            {
                IdType = IdTypeEnum.String,
                Id     = "DLV0"
            };

            meter.Id.UniqueIds.Add(uniqueId);
            _meters.Add(meter);


            var enumeratedRepresentation = new EnumeratedValue {
                Value = new AgGateway.ADAPT.ApplicationDataModel.Representations.EnumerationMember {
                    Code = 3
                }
            };

            _spatialValueInterpolator.Setup(s => s.Interpolate(meter)).Returns(enumeratedRepresentation);

            var result = Map().ToList();

            Assert.AreEqual(enumeratedRepresentation, result[1].GetMeterValue(meter) as EnumeratedValue);
        }
Ejemplo n.º 8
0
        public void Setup()
        {
            _isoSpatialRow  = new ISOSpatialRow();
            _isoSpatialRows = new List <ISOSpatialRow> {
                _isoSpatialRow
            };
            _meters = new List <WorkingData>();

            _spatialValueInterpolator = new Mock <IRepresentationValueInterpolator>();
            _spatialRecordMapper      = new SpatialRecordMapper(_spatialValueInterpolator.Object);
        }
Ejemplo n.º 9
0
        private static void AreEqual(ISOSpatialRow isoSpatialRow, SpatialRecord adaptSpatialRecord, IEnumerable <WorkingData> meters)
        {
            Assert.AreEqual(isoSpatialRow.TimeStart, adaptSpatialRecord.Timestamp);

            var point = adaptSpatialRecord.Geometry as Point;

            Assert.AreEqual(isoSpatialRow.EastPosition * CoordinateMultiplier, point.X, 0.000001);
            Assert.AreEqual(isoSpatialRow.NorthPosition * CoordinateMultiplier, point.Y, 0.000001);
            Assert.AreEqual(isoSpatialRow.Elevation, point.Z);

            SpatialValueAssert.AreEqual(isoSpatialRow, adaptSpatialRecord, meters);
        }
Ejemplo n.º 10
0
        private static void AreEqual(SpatialRecord adaptSpatialRecord, ISOSpatialRow isoSpatialRow, List <WorkingData> meters)
        {
            Assert.AreEqual(adaptSpatialRecord.Timestamp, isoSpatialRow.TimeStart);

            var point = (Point)adaptSpatialRecord.Geometry;

            Assert.AreEqual((int)(point.X / CoordinateMultiplier), isoSpatialRow.EastPosition, CoordinateMultiplier);
            Assert.AreEqual((int)(point.Y / CoordinateMultiplier), isoSpatialRow.NorthPosition, CoordinateMultiplier);
            Assert.AreEqual(point.Z, isoSpatialRow.Elevation);

            SpatialValueAssert.AreEqual(isoSpatialRow, adaptSpatialRecord, meters);
        }
Ejemplo n.º 11
0
        private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingData meter, SpatialRecord spatialRecord, Dictionary <string, List <ISOProductAllocation> > productAllocations)
        {
            var isoValue = isoSpatialRow.SpatialValues.FirstOrDefault(v =>
                                                                      v.DataLogValue.ProcessDataDDI != "DFFE" &&
                                                                      _workingDataMapper.DataLogValuesByWorkingDataID.ContainsKey(meter.Id.ReferenceId) &&
                                                                      v.DataLogValue.DeviceElementIdRef == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].DeviceElementIdRef &&
                                                                      v.DataLogValue.ProcessDataDDI == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].ProcessDataDDI);


            if (isoValue != null)
            {
                var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                spatialRecord.SetMeterValue(meter, value);

                var other = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                _representationValueInterpolator.SetMostRecentMeterValue(meter, other);
            }
            else if (meter.Representation.Code == "vrProductIndex")
            {
                string detID = _workingDataMapper.ISODeviceElementIDsByWorkingDataID[meter.Id.ReferenceId];
                if (productAllocations.ContainsKey(detID)) //The DeviceElement for this meter exists in the list of allocations
                {
                    double numericValue = 0d;
                    if (productAllocations[detID].Count == 1 || TimeLogMapper.GetDistinctProductIDs(_taskDataMapper, productAllocations).Count == 1)
                    {
                        //This product is consistent throughout the task on this device element
                        int?adaptProductID = _taskDataMapper.InstanceIDMap.GetADAPTID(productAllocations[detID].Single().ProductIdRef);
                        numericValue = adaptProductID.HasValue ? adaptProductID.Value : 0d;
                    }
                    else if (productAllocations[detID].Count > 1)
                    {
                        //There are multiple product allocations for the device element
                        //Find the product allocation that governs this timestamp
                        ISOProductAllocation relevantPan = productAllocations[detID].FirstOrDefault(p => Offset(p.AllocationStamp.Start) <= spatialRecord.Timestamp &&
                                                                                                    (p.AllocationStamp.Stop == null ||
                                                                                                     Offset(p.AllocationStamp.Stop) >= spatialRecord.Timestamp));
                        if (relevantPan != null)
                        {
                            int?adaptProductID = _taskDataMapper.InstanceIDMap.GetADAPTID(relevantPan.ProductIdRef);
                            numericValue = adaptProductID.HasValue ? adaptProductID.Value : 0d;
                        }
                    }
                    var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, numericValue));
                    spatialRecord.SetMeterValue(meter, value);
                }
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as NumericRepresentationValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 12
0
        private ISOSpatialRow MergeRecords(List <BinaryReaderHelper> candidates)
        {
            // Merge data from all candidates into first record
            ISOSpatialRow result = null;

            foreach (var candidate in candidates)
            {
                result = result == null ? candidate.CurrentRecord : result.Merge(candidate.CurrentRecord);
                // Clear current record to force reading next one
                candidate.CurrentRecord = null;
            }
            return(result);
        }
Ejemplo n.º 13
0
        private static void SetSpatialValues(TIM tim, ISOSpatialRow record, System.IO.BinaryReader binaryReader)
        {
            var numberOfDLVs = binaryReader.ReadByte();

            record.SpatialValues = new List <SpatialValue>();

            for (int i = 0; i < numberOfDLVs; i++)
            {
                var order = binaryReader.ReadByte();
                var value = binaryReader.ReadInt32();

                record.SpatialValues.Add(CreateSpatialValue(tim, order, value));
            }
        }
Ejemplo n.º 14
0
        private void SetEnumeratedMeterValue(ISOSpatialRow isoSpatialRow, EnumeratedWorkingData meter, SpatialRecord spatialRecord)
        {
            var isoValue = isoSpatialRow.SpatialValues.SingleOrDefault(v => v.Id == meter.Id.FindIntIsoId());

            if (isoValue != null)
            {
                var isoEnumeratedMeter = meter as ISOEnumeratedMeter;
                var enumeratedValue    = isoEnumeratedMeter.GetEnumeratedValue(isoValue, isoEnumeratedMeter);
                spatialRecord.SetMeterValue(meter, enumeratedValue);
                _representationValueInterpolator.SetMostRecentMeterValue(meter, enumeratedValue);
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as EnumeratedValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 15
0
        public void GivenIsoSpatialRowsWithoutNumericMeterOnSecondWhenMapThenSecondValueIsInterpolator()
        {
            var spatialValue = new SpatialValue
            {
                Id    = 0,
                Value = 12.3
            };

            _isoSpatialRow.SpatialValues = new List <SpatialValue> {
                spatialValue
            };

            var isoSpatialRow2 = new ISOSpatialRow {
                SpatialValues = new List <SpatialValue>()
            };

            _isoSpatialRows = new List <ISOSpatialRow> {
                _isoSpatialRow, isoSpatialRow2
            };

            var meter = new NumericWorkingData
            {
                Representation     = RepresentationInstanceList.vrAvgHarvestMoisture.ToModelRepresentation(),
                DeviceElementUseId = 1,
                UnitOfMeasure      = UnitSystemManager.GetUnitOfMeasure("prcnt")
            };

            var uniqueId = new UniqueId
            {
                IdType = IdTypeEnum.String,
                Id     = "DLV0"
            };

            meter.Id.UniqueIds.Add(uniqueId);
            _meters.Add(meter);

            var numericRepresentation = new NumericRepresentationValue(meter.Representation as NumericRepresentation,
                                                                       meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, 2.3));

            _spatialValueInterpolator.Setup(s => s.Interpolate(meter)).Returns(numericRepresentation);

            var result = Map().ToList();

            Assert.AreEqual(numericRepresentation, result[1].GetMeterValue(meter) as NumericRepresentationValue);
        }
Ejemplo n.º 16
0
        private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingData meter, SpatialRecord spatialRecord)
        {
            var isoValue = isoSpatialRow.SpatialValues.SingleOrDefault(v => v.Id == meter.Id.FindIntIsoId());


            if (isoValue != null)
            {
                var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                spatialRecord.SetMeterValue(meter, value);

                var other = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                _representationValueInterpolator.SetMostRecentMeterValue(meter, other);
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as NumericRepresentationValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 17
0
        private void SetEnumeratedMeterValue(ISOSpatialRow isoSpatialRow, EnumeratedWorkingData meter, SpatialRecord spatialRecord)
        {
            var isoValue = isoSpatialRow.SpatialValues.FirstOrDefault(v =>
                                                                      v.DataLogValue.DeviceElementIdRef == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].DeviceElementIdRef &&
                                                                      v.DataLogValue.ProcessDataDDI == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].ProcessDataDDI);

            if (isoValue != null)
            {
                var isoEnumeratedMeter = meter as ISOEnumeratedMeter;
                var enumeratedValue    = isoEnumeratedMeter.GetEnumeratedValue(isoValue, isoEnumeratedMeter);
                spatialRecord.SetMeterValue(meter, enumeratedValue);
                _representationValueInterpolator.SetMostRecentMeterValue(meter, enumeratedValue);
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as EnumeratedValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 18
0
        protected override IEnumerable <ISOSpatialRow> ReadTimeLog(ISOTimeLog _timeLog, string _dataPath)
        {
            List <BinaryReaderHelper> readers = new List <BinaryReaderHelper>();

            try
            {
                readers = CreateBinaryReaders();

                // Below alogrithm is using queues for each binary file and matching records on TimeStart/Position.
                // At start of each iteration a single record is read from binary file into queue.
                // Records with earliest TimeStart are merged together and removed from each file queue.
                while (true)
                {
                    // Read next record from each time log
                    var readersWithData = ReadNextRecords(readers);

                    if (readersWithData.Count == 0)
                    {
                        // No more records in each file. Stop processing.
                        break;
                    }

                    // Group records by TimeStart and East/North position, and then grab ones with earliest TimeStart.
                    // This leads to processing earliest records from any file first and keeping other records untouched.
                    // They will be processed in the next loop iteration along with any records read from already processed files.
                    var candidates = readersWithData.GroupBy(x => new { x.CurrentRecord.TimeStart, x.CurrentRecord.EastPosition, x.CurrentRecord.NorthPosition })
                                     .OrderBy(x => x.Key.TimeStart)
                                     .First().ToList();

                    // Merge data from all candidates
                    ISOSpatialRow result = MergeRecords(candidates);

                    yield return(result);
                }
            }
            finally
            {
                // Clean up readers
                DisposeBinaryReaders(readers);
            }
        }
        private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingData meter, SpatialRecord spatialRecord)
        {
            var isoValue = isoSpatialRow.SpatialValues.SingleOrDefault(v =>
                                                                       v.DataLogValue.DeviceElementIdRef == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].DeviceElementIdRef &&
                                                                       v.DataLogValue.ProcessDataDDI == _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId].ProcessDataDDI);


            if (isoValue != null)
            {
                var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                spatialRecord.SetMeterValue(meter, value);

                var other = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                _representationValueInterpolator.SetMostRecentMeterValue(meter, other);
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as NumericRepresentationValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 20
0
        public IEnumerable <ISOSpatialRow> Read(string dataPath, string fileName, TIM tim)
        {
            if (tim == null)
            {
                yield break;
            }

            if (!File.Exists(Path.Combine(dataPath, fileName)))
            {
                yield break;
            }

            using (var binaryReader = new System.IO.BinaryReader(File.Open(Path.Combine(dataPath, fileName), FileMode.Open)))
            {
                while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                {
                    var ptn = tim.Items.FirstOrDefault(x => x.GetType() == typeof(PTN)) as PTN;

                    var record = new ISOSpatialRow {
                        TimeStart = GetStartTime(tim, binaryReader)
                    };

                    if (ptn != null)
                    {
                        record.NorthPosition      = ReadInt32(ptn.A, ptn.ASpecified, binaryReader).GetValueOrDefault(0);
                        record.EastPosition       = ReadInt32(ptn.B, ptn.BSpecified, binaryReader).GetValueOrDefault(0);
                        record.Elevation          = ReadInt32(ptn.C, ptn.CSpecified, binaryReader);
                        record.PositionStatus     = ReadByte(ptn.D, ptn.DSpecified, binaryReader);
                        record.PDOP               = ReadShort(ptn.E, ptn.ESpecified, binaryReader);
                        record.HDOP               = ReadShort(ptn.F, ptn.FSpecified, binaryReader);
                        record.NumberOfSatellites = ReadByte(ptn.G, ptn.GSpecified, binaryReader);

                        SetGpsUtcDateTime(ptn, record, binaryReader);
                    }

                    SetSpatialValues(tim, record, binaryReader);
                    yield return(record);
                }
            }
        }
Ejemplo n.º 21
0
        public void GivenIsoSpatialRowWithoutMeterWhenMapThenInterpolatorIsCalled()
        {
            var spatialValue = new SpatialValue
            {
                Id    = 0,
                Value = 12.3
            };

            _isoSpatialRow.SpatialValues = new List <SpatialValue> {
                spatialValue
            };

            var isoSpatialRow2 = new ISOSpatialRow {
                SpatialValues = new List <SpatialValue>()
            };

            _isoSpatialRows = new List <ISOSpatialRow> {
                _isoSpatialRow, isoSpatialRow2
            };

            var meter = new NumericWorkingData
            {
                Representation     = RepresentationInstanceList.vrAvgHarvestMoisture.ToModelRepresentation(),
                DeviceElementUseId = 1,
                UnitOfMeasure      = UnitSystemManager.GetUnitOfMeasure("prcnt")
            };

            var uniqueId = new UniqueId
            {
                IdType = IdTypeEnum.String,
                Id     = "DLV0"
            };

            meter.Id.UniqueIds.Add(uniqueId);
            _meters.Add(meter);

            Map().ToList();
            _spatialValueInterpolator.Verify(s => s.Interpolate(It.IsAny <WorkingData>()));
        }
        public void GivenCondensedWorkStateWithNoInstalledWhenCreateMetersThenNoMetersCreated()
        {
            var        spatialRow   = new ISOSpatialRow();
            const long value        = 0xFFFFFFFF;
            var        spatialValue = new SpatialValue
            {
                Dlv = new DLV
                {
                    A = "A1"
                },
                Value = value
            };

            spatialRow.SpatialValues = new List <SpatialValue> {
                spatialValue
            };
            _isoSpatialRows = new List <ISOSpatialRow> {
                spatialRow
            };
            var result = new CondensedWorkStateMeterCreator(161, 161).CreateMeters(_isoSpatialRows);

            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 23
0
            public IEnumerable <ISOSpatialRow> Read(string fileName, ISOTime templateTime)
            {
                if (templateTime == null)
                {
                    yield break;
                }

                if (!File.Exists(fileName))
                {
                    yield break;
                }

                using (var binaryReader = new System.IO.BinaryReader(File.Open(fileName, FileMode.Open)))
                {
                    while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                    {
                        ISOPosition templatePosition = templateTime.Positions.FirstOrDefault();

                        var record = new ISOSpatialRow {
                            TimeStart = GetStartTime(templateTime, binaryReader)
                        };

                        if (templatePosition != null)
                        {
                            //North and East are required binary data
                            record.NorthPosition = ReadInt32((double?)templatePosition.PositionNorth, templatePosition.HasPositionNorth, binaryReader).GetValueOrDefault(0);
                            record.EastPosition  = ReadInt32((double?)templatePosition.PositionEast, templatePosition.HasPositionEast, binaryReader).GetValueOrDefault(0);

                            if (templatePosition.HasPositionUp) //Optional position attributes will be included in the binary only if a corresponding attribute is present in the PTN element
                            {
                                record.Elevation = ReadInt32(templatePosition.PositionUp, templatePosition.HasPositionUp, binaryReader);
                            }

                            //Position status is required
                            record.PositionStatus = ReadByte((byte?)templatePosition.PositionStatus, templatePosition.HasPositionStatus, binaryReader);

                            if (templatePosition.HasPDOP)
                            {
                                record.PDOP = ReadUShort((double?)templatePosition.PDOP, templatePosition.HasPDOP, binaryReader);
                            }

                            if (templatePosition.HasHDOP)
                            {
                                record.HDOP = ReadUShort((double?)templatePosition.HDOP, templatePosition.HasHDOP, binaryReader);
                            }

                            if (templatePosition.HasNumberOfSatellites)
                            {
                                record.NumberOfSatellites = ReadByte(templatePosition.NumberOfSatellites, templatePosition.HasNumberOfSatellites, binaryReader);
                            }

                            if (templatePosition.HasGpsUtcTime)
                            {
                                if (templatePosition.GpsUtcTime.HasValue)
                                {
                                    record.GpsUtcTime = Convert.ToUInt32(templatePosition.GpsUtcTime.Value);
                                }
                                else
                                {
                                    record.GpsUtcTime = binaryReader.ReadUInt32();
                                }
                            }

                            if (templatePosition.HasGpsUtcDate)
                            {
                                if (templatePosition.GpsUtcDate.HasValue)
                                {
                                    record.GpsUtcDate = (ushort)templatePosition.GpsUtcDate.Value;
                                }
                                else
                                {
                                    record.GpsUtcDate = binaryReader.ReadUInt16();
                                }
                            }

                            if (record.GpsUtcDate != null && record.GpsUtcTime != null)
                            {
                                record.GpsUtcDateTime = _firstDayOf1980.AddDays((double)record.GpsUtcDate).AddMilliseconds((double)record.GpsUtcTime);
                            }
                        }

                        var numberOfDLVs = binaryReader.ReadByte();
                        record.SpatialValues = new List <SpatialValue>();

                        //Read DLVs out of the TLG.bin
                        for (int i = 0; i < numberOfDLVs; i++)
                        {
                            var order = binaryReader.ReadByte();
                            var value = binaryReader.ReadInt32();

                            record.SpatialValues.Add(CreateSpatialValue(templateTime, order, value));
                        }

                        //Add any fixed values from the TLG.xml
                        foreach (ISODataLogValue fixedValue in templateTime.DataLogValues.Where(dlv => dlv.ProcessDataValue.HasValue && !EnumeratedMeterFactory.IsCondensedMeter(dlv.ProcessDataDDI.AsInt32DDI())))
                        {
                            byte order = (byte)templateTime.DataLogValues.IndexOf(fixedValue);
                            if (record.SpatialValues.Any(s => s.Id == order)) //Check to ensure the binary data didn't already write this value
                            {
                                //Per the spec, any fixed value in the XML applies to all rows; as such, replace what was read from the binary
                                SpatialValue matchingValue = record.SpatialValues.Single(s => s.Id == order);
                                matchingValue.DataLogValue = fixedValue;
                            }
                        }

                        yield return(record);
                    }
                }
            }
Ejemplo n.º 24
0
            public static IEnumerable <ISOSpatialRow> Read(string fileName, ISOTime templateTime, DeviceElementHierarchies deviceHierarchies)
            {
                if (templateTime == null)
                {
                    yield break;
                }

                if (!File.Exists(fileName))
                {
                    yield break;
                }

                using (var binaryReader = new System.IO.BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                    {
                        ISOPosition templatePosition = templateTime.Positions.FirstOrDefault();

                        var record = new ISOSpatialRow {
                            TimeStart = GetStartTime(templateTime, binaryReader).GetValueOrDefault()
                        };

                        if (templatePosition != null)
                        {
                            //North and East are required binary data
                            record.NorthPosition = ReadInt32((double?)templatePosition.PositionNorth, templatePosition.HasPositionNorth, binaryReader).GetValueOrDefault(0);
                            record.EastPosition  = ReadInt32((double?)templatePosition.PositionEast, templatePosition.HasPositionEast, binaryReader).GetValueOrDefault(0);

                            //Optional position attributes will be included in the binary only if a corresponding attribute is present in the PTN element
                            record.Elevation = ReadInt32(templatePosition.PositionUp, templatePosition.HasPositionUp, binaryReader);

                            //Position status is required
                            record.PositionStatus = ReadByte((byte?)templatePosition.PositionStatus, templatePosition.HasPositionStatus, binaryReader);

                            record.PDOP = ReadUShort((double?)templatePosition.PDOP, templatePosition.HasPDOP, binaryReader);

                            record.HDOP = ReadUShort((double?)templatePosition.HDOP, templatePosition.HasHDOP, binaryReader);

                            record.NumberOfSatellites = ReadByte(templatePosition.NumberOfSatellites, templatePosition.HasNumberOfSatellites, binaryReader);

                            record.GpsUtcTime = ReadUInt32(templatePosition.GpsUtcTime, templatePosition.HasGpsUtcTime, binaryReader).GetValueOrDefault();

                            record.GpsUtcDate = ReadUShort(templatePosition.GpsUtcDate, templatePosition.HasGpsUtcDate, binaryReader);

                            if (record.GpsUtcDate != null && record.GpsUtcTime != null)
                            {
                                record.GpsUtcDateTime = _firstDayOf1980.AddDays((double)record.GpsUtcDate).AddMilliseconds((double)record.GpsUtcTime);
                            }
                        }

                        //Some datasets end here
                        if (binaryReader.BaseStream.Position >= binaryReader.BaseStream.Length)
                        {
                            break;
                        }

                        var numberOfDLVs = ReadByte(null, true, binaryReader).GetValueOrDefault(0);
                        // There should be some values but no more data exists in file, stop processing
                        if (numberOfDLVs > 0 && binaryReader.BaseStream.Position >= binaryReader.BaseStream.Length)
                        {
                            break;
                        }

                        //If the reported number of values does not fit into the stream, correct the numberOfDLVs
                        numberOfDLVs = ConfirmNumberOfDLVs(binaryReader, numberOfDLVs);

                        record.SpatialValues = new List <SpatialValue>();

                        bool unexpectedEndOfStream = false;
                        //Read DLVs out of the TLG.bin
                        for (int i = 0; i < numberOfDLVs; i++)
                        {
                            var order = ReadByte(null, true, binaryReader).GetValueOrDefault();
                            var value = ReadInt32(null, true, binaryReader).GetValueOrDefault();
                            // Can't read either order or value or both, stop processing
                            if (i < numberOfDLVs - 1 && binaryReader.BaseStream.Position >= binaryReader.BaseStream.Length)
                            {
                                unexpectedEndOfStream = true;
                                break;
                            }

                            SpatialValue spatialValue = CreateSpatialValue(templateTime, order, value, deviceHierarchies);
                            if (spatialValue != null)
                            {
                                record.SpatialValues.Add(spatialValue);
                            }
                        }
                        // Unable to read some of the expected DLVs, stop processing
                        if (unexpectedEndOfStream)
                        {
                            break;
                        }

                        //Add any fixed values from the TLG.xml
                        foreach (ISODataLogValue fixedValue in templateTime.DataLogValues.Where(dlv => dlv.ProcessDataValue.HasValue && !EnumeratedMeterFactory.IsCondensedMeter(dlv.ProcessDataDDI.AsInt32DDI())))
                        {
                            byte order = (byte)templateTime.DataLogValues.IndexOf(fixedValue);
                            if (record.SpatialValues.Any(s => s.Id == order)) //Check to ensure the binary data didn't already write this value
                            {
                                //Per the spec, any fixed value in the XML applies to all rows; as such, replace what was read from the binary
                                SpatialValue matchingValue = record.SpatialValues.Single(s => s.Id == order);
                                matchingValue.DataLogValue = fixedValue;
                            }
                        }

                        yield return(record);
                    }
                }
            }
Ejemplo n.º 25
0
        private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingData meter, SpatialRecord spatialRecord, Dictionary <string, List <ISOProductAllocation> > productAllocations)
        {
            var dataLogValue = _workingDataMapper.DataLogValuesByWorkingDataID.ContainsKey(meter.Id.ReferenceId)
                ? _workingDataMapper.DataLogValuesByWorkingDataID[meter.Id.ReferenceId]
                : null;
            var isoValue = dataLogValue != null
                           ? isoSpatialRow.SpatialValues.FirstOrDefault(v =>
                                                                        v.DataLogValue.ProcessDataDDI != "DFFE" &&
                                                                        v.DataLogValue.DeviceElementIdRef == dataLogValue.DeviceElementIdRef &&
                                                                        v.DataLogValue.ProcessDataDDI == dataLogValue.ProcessDataDDI)
                           : null;


            if (isoValue != null)
            {
                ADAPT.ApplicationDataModel.Common.UnitOfMeasure userProvidedUnitOfMeasure = meter.UnitOfMeasure; //Default; no display uom provided.
                var dvp = isoValue.DeviceProcessData?.DeviceValuePresentation;
                if (dvp != null)
                {
                    //If a DVP element is present, report out the desired display unit of measure as the UserProvidedUnitOfMeasure.
                    //This will not necessarily map to the Representation.UnitSystem.
                    userProvidedUnitOfMeasure = new ApplicationDataModel.Common.UnitOfMeasure()
                    {
                        Code = dvp.UnitDesignator, Scale = dvp.Scale, Offset = dvp.Offset
                    };
                }

                var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, userProvidedUnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                spatialRecord.SetMeterValue(meter, value);

                var other = new NumericRepresentationValue(meter.Representation as NumericRepresentation, userProvidedUnitOfMeasure, new NumericValue(meter.UnitOfMeasure, isoValue.Value));
                _representationValueInterpolator.SetMostRecentMeterValue(meter, other);
            }
            else if (meter.Representation.Code == "vrProductIndex")
            {
                string detID = _workingDataMapper.ISODeviceElementIDsByWorkingDataID[meter.Id.ReferenceId];
                if (productAllocations.ContainsKey(detID)) //The DeviceElement for this meter exists in the list of allocations
                {
                    var    productAllocationsForDeviceElement = productAllocations[detID];
                    double numericValue = 0d;
                    if (productAllocationsForDeviceElement.Count == 1 || TimeLogMapper.GetDistinctProductIDs(_taskDataMapper, productAllocations).Count == 1)
                    {
                        //This product is consistent throughout the task on this device element
                        int?adaptProductID = _taskDataMapper.InstanceIDMap.GetADAPTID(productAllocationsForDeviceElement.Single().ProductIdRef);
                        numericValue = adaptProductID.HasValue ? adaptProductID.Value : 0d;
                    }
                    else if (productAllocationsForDeviceElement.Count > 1)
                    {
                        //There are multiple product allocations for the device element
                        //Find the product allocation that governs this timestamp
                        ISOProductAllocation relevantPan = productAllocationsForDeviceElement.FirstOrDefault(p => Offset(p.AllocationStamp.Start) <= spatialRecord.Timestamp &&
                                                                                                             (p.AllocationStamp.Stop == null ||
                                                                                                              Offset(p.AllocationStamp.Stop) >= spatialRecord.Timestamp));
                        if (relevantPan == null)
                        {
                            //We couldn't correlate strictly based on time.  Check for a more general match on date alone before returning null.
                            var pansMatchingDate = productAllocationsForDeviceElement.Where(p => p.AllocationStamp.Start?.Date == p.AllocationStamp.Stop?.Date &&
                                                                                            p.AllocationStamp.Start?.Date == spatialRecord.Timestamp.Date);
                            if (pansMatchingDate.Count() == 1)
                            {
                                //Only one PAN on this date, use it.
                                relevantPan = pansMatchingDate.Single();
                            }
                        }

                        if (relevantPan != null)
                        {
                            int?adaptProductID = _taskDataMapper.InstanceIDMap.GetADAPTID(relevantPan.ProductIdRef);
                            numericValue = adaptProductID.HasValue ? adaptProductID.Value : 0d;
                        }
                    }
                    var value = new NumericRepresentationValue(meter.Representation as NumericRepresentation, meter.UnitOfMeasure, new NumericValue(meter.UnitOfMeasure, numericValue));
                    spatialRecord.SetMeterValue(meter, value);
                }
            }
            else
            {
                var value = _representationValueInterpolator.Interpolate(meter) as NumericRepresentationValue;
                spatialRecord.SetMeterValue(meter, value);
            }
        }
Ejemplo n.º 26
0
            public IEnumerable <ISOSpatialRow> Read(string fileName, ISOTime templateTime)
            {
                if (templateTime == null)
                {
                    yield break;
                }

                if (!File.Exists(fileName))
                {
                    yield break;
                }

                using (var binaryReader = new System.IO.BinaryReader(File.Open(fileName, FileMode.Open)))
                {
                    while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
                    {
                        ISOPosition templatePosition = templateTime.Positions.FirstOrDefault();

                        var record = new ISOSpatialRow {
                            TimeStart = GetStartTime(templateTime, binaryReader)
                        };

                        if (templatePosition != null)
                        {
                            //North and East are required binary data
                            record.NorthPosition = ReadInt32((double?)templatePosition.PositionNorth, templatePosition.HasPositionNorth, binaryReader).GetValueOrDefault(0);
                            record.EastPosition  = ReadInt32((double?)templatePosition.PositionEast, templatePosition.HasPositionEast, binaryReader).GetValueOrDefault(0);

                            if (templatePosition.HasPositionUp) //Optional position attributes will be included in the binary only if a corresponding attribute is present in the PTN element
                            {
                                record.Elevation = ReadInt32(templatePosition.PositionUp, templatePosition.HasPositionUp, binaryReader);
                            }

                            //Position status is required
                            record.PositionStatus = ReadByte((byte?)templatePosition.PositionStatus, templatePosition.HasPositionStatus, binaryReader);

                            if (templatePosition.HasPDOP)
                            {
                                record.PDOP = ReadUShort((double?)templatePosition.PDOP, templatePosition.HasPDOP, binaryReader);
                            }

                            if (templatePosition.HasHDOP)
                            {
                                record.HDOP = ReadUShort((double?)templatePosition.HDOP, templatePosition.HasHDOP, binaryReader);
                            }

                            if (templatePosition.HasNumberOfSatellites)
                            {
                                record.NumberOfSatellites = ReadByte(templatePosition.NumberOfSatellites, templatePosition.HasNumberOfSatellites, binaryReader);
                            }

                            if (templatePosition.HasGpsUtcTime)
                            {
                                if (templatePosition.GpsUtcTime.HasValue)
                                {
                                    record.GpsUtcTime = Convert.ToUInt32(templatePosition.GpsUtcTime.Value);
                                }
                                else
                                {
                                    record.GpsUtcTime = binaryReader.ReadUInt32();
                                }
                            }

                            if (templatePosition.HasGpsUtcDate)
                            {
                                if (templatePosition.GpsUtcDate.HasValue)
                                {
                                    record.GpsUtcDate = (ushort)templatePosition.GpsUtcDate.Value;
                                }
                                else
                                {
                                    record.GpsUtcDate = binaryReader.ReadUInt16();
                                }
                            }

                            if (record.GpsUtcDate != null && record.GpsUtcTime != null)
                            {
                                record.GpsUtcDateTime = _firstDayOf1980.AddDays((double)record.GpsUtcDate).AddMilliseconds((double)record.GpsUtcTime);
                            }
                        }

                        var numberOfDLVs = binaryReader.ReadByte();
                        record.SpatialValues = new List <SpatialValue>();

                        for (int i = 0; i < numberOfDLVs; i++)
                        {
                            var order = binaryReader.ReadByte();
                            var value = binaryReader.ReadInt32();

                            record.SpatialValues.Add(CreateSpatialValue(templateTime, order, value));
                        }

                        yield return(record);
                    }
                }
            }
Ejemplo n.º 27
0
        public static void AreEqual(ISOSpatialRow isoSpatialRow, SpatialRecord adaptSpatialRecord, List <WorkingData> meters)
        {
            foreach (var meter in meters)
            {
                var adaptValue = adaptSpatialRecord.GetMeterValue(meter);
                if (adaptValue == null)
                {
                    continue;
                }

                var isoValue = isoSpatialRow.SpatialValues.Single(v => v.Id == meter.Id.FindIntIsoId());

                var adaptEnumeratedValue = adaptValue as EnumeratedValue;
                if (adaptEnumeratedValue != null)
                {
                    var representation = RepresentationManager.Instance.Representations.First(x => x.DomainId == adaptEnumeratedValue.Representation.Code);
                    Assert.AreEqual(representation.Ddi.GetValueOrDefault(), Convert.ToInt32(isoValue.Dlv.A, 16));

                    if (representation.Ddi == 141)
                    {
                        CompareWorkState(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 157)
                    {
                        CompareConnectorType(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 158)
                    {
                        ComparePrescriptionControl(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 160)
                    {
                        CompareSectionControlState(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi >= 161 && representation.Ddi <= 176)
                    {
                        CompareCondensedWorkState(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 210)
                    {
                        CompareSkyConditions(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 230)
                    {
                        CompareNetWeightState(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi == 240)
                    {
                        CompareActualLoadingSystemStatus(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi >= 290 && representation.Ddi < 305)
                    {
                        CompareCondensedWorkState(adaptEnumeratedValue, isoValue);
                    }
                    if (representation.Ddi >= 367 && representation.Ddi <= 382)
                    {
                        CompareCondensedSectionOverrideState(adaptEnumeratedValue, isoValue);
                    }
                }

                var adaptNumericValue = adaptValue as NumericRepresentationValue;
                if (adaptNumericValue != null)
                {
                    var representation = RepresentationManager.Instance.Representations.FirstOrDefault(x => x.DomainId == adaptNumericValue.Representation.Code);
                    if (representation != null)
                    {
                        Assert.AreEqual(representation.Ddi, Convert.ToInt32(isoValue.Dlv.A, 16));
                    }

                    Assert.AreEqual(adaptNumericValue.Value.Value, isoValue.Value);
                }
            }
        }