예제 #1
0
        private static ISOPosition ExportPosition(Location location)
        {
            ISOPosition position = new ISOPosition();

            position.PositionEast  = (decimal)location.Position.X;
            position.PositionNorth = (decimal)location.Position.Y;
            position.PositionUp    = (int)location.Position.Z;
            if (location.GpsSource != null)
            {
                position.NumberOfSatellites = (byte?)location.GpsSource.NumberOfSatellites;
            }
            return(position);
        }
예제 #2
0
        private ISOTimeLog ExportTimeLog(OperationData operation, IEnumerable <SpatialRecord> spatialRecords, string dataPath)
        {
            ISOTimeLog isoTimeLog = new ISOTimeLog();

            //ID
            string id = operation.Id.FindIsoId() ?? GenerateId(5);

            isoTimeLog.Filename    = id;
            isoTimeLog.TimeLogType = 1; // TimeLogType TLG.C is a required attribute. Currently only the value "1" is defined.
            ExportIDs(operation.Id, id);

            List <DeviceElementUse> deviceElementUses = operation.GetAllSections();
            List <WorkingData>      workingDatas      = deviceElementUses.SelectMany(x => x.GetWorkingDatas()).ToList();

            ISOTime isoTime = new ISOTime();

            isoTime.HasStart      = true;
            isoTime.Type          = ISOTimeType.Effective;
            isoTime.DataLogValues = ExportDataLogValues(workingDatas, deviceElementUses).ToList();

            //Set the timelog data definition for PTN
            ISOPosition position = new ISOPosition();

            position.HasPositionNorth      = true;
            position.HasPositionEast       = true;
            position.HasPositionUp         = true;
            position.HasPositionStatus     = true;
            position.HasPDOP               = false;
            position.HasHDOP               = false;
            position.HasNumberOfSatellites = false;
            position.HasGpsUtcTime         = false;
            position.HasGpsUtcTime         = false;
            isoTime.Positions.Add(position);

            //Write XML
            TaskDocumentWriter xmlWriter = new TaskDocumentWriter();

            xmlWriter.WriteTimeLog(dataPath, isoTimeLog, isoTime);

            //Write BIN
            var          binFilePath = Path.Combine(dataPath, isoTimeLog.Filename + ".bin");
            BinaryWriter writer      = new BinaryWriter(_dataLogValueOrdersByWorkingDataID);

            writer.Write(binFilePath, workingDatas.ToList(), spatialRecords);

            return(isoTimeLog);
        }
예제 #3
0
        private static Location ImportPosition(ISOPosition position)
        {
            Location location = new Location();

            location.Position   = new Point();
            location.Position.X = (double)position.PositionEast;
            location.Position.Y = (double)position.PositionNorth;
            //[Check] if there is a PositionUp
            if (position.PositionUp != null)
            {
                location.Position.Z = (double)position.PositionUp;
            }

            if (position.HasNumberOfSatellites)
            {
                location.GpsSource = new GpsSource();
                location.GpsSource.NumberOfSatellites = (int?)position.NumberOfSatellites;
            }
            return(location);
        }
예제 #4
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);
                    }
                }
            }
예제 #5
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);
                    }
                }
            }
예제 #6
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);
                    }
                }
            }
예제 #7
0
            public static Dictionary <byte, int> ReadImplementGeometryValues(string filePath, ISOTime templateTime, IEnumerable <byte> desiredDLVIndices)
            {
                Dictionary <byte, int> output         = new Dictionary <byte, int>();
                List <byte>            desiredIndexes = desiredDLVIndices.ToList();

                //Determine the number of header bytes in each position
                short headerCount = 0;

                SkipBytes(templateTime.HasStart && templateTime.Start == null, 6, ref headerCount);
                ISOPosition templatePosition = templateTime.Positions.FirstOrDefault();

                if (templatePosition != null)
                {
                    SkipBytes(templatePosition.HasPositionNorth && templatePosition.PositionNorth == null, 4, ref headerCount);
                    SkipBytes(templatePosition.HasPositionEast && templatePosition.PositionEast == null, 4, ref headerCount);
                    SkipBytes(templatePosition.HasPositionUp && templatePosition.PositionUp == null, 4, ref headerCount);
                    SkipBytes(templatePosition.HasPositionStatus && templatePosition.PositionStatus == null, 1, ref headerCount);
                    SkipBytes(templatePosition.HasPDOP && templatePosition.PDOP == null, 2, ref headerCount);
                    SkipBytes(templatePosition.HasHDOP && templatePosition.HDOP == null, 2, ref headerCount);
                    SkipBytes(templatePosition.HasNumberOfSatellites && templatePosition.NumberOfSatellites == null, 1, ref headerCount);
                    SkipBytes(templatePosition.HasGpsUtcTime && templatePosition.GpsUtcTime == null, 4, ref headerCount);
                    SkipBytes(templatePosition.HasGpsUtcDate && templatePosition.GpsUtcDate == null, 2, ref headerCount);
                }

                using (var binaryReader = new System.IO.BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    while (ContinueReading(binaryReader))
                    {
                        binaryReader.BaseStream.Position += headerCount; //Skip over the header
                        if (ContinueReading(binaryReader))
                        {
                            var numberOfDLVs = ReadByte(null, true, binaryReader).GetValueOrDefault(0);
                            if (ContinueReading(binaryReader))
                            {
                                numberOfDLVs = ConfirmNumberOfDLVs(binaryReader, numberOfDLVs); //Validate we are not at the end of a truncated file
                                for (byte i = 0; i < numberOfDLVs; i++)
                                {
                                    byte dlvIndex = ReadByte(null, true, binaryReader).GetValueOrDefault(); //This is the current DLV reported
                                    if (desiredIndexes.Contains(dlvIndex))
                                    {
                                        //A desired DLV is reported here
                                        int value = ReadInt32(null, true, binaryReader).GetValueOrDefault();
                                        if (!output.ContainsKey(dlvIndex))
                                        {
                                            output.Add(dlvIndex, value);
                                        }
                                        else if (Math.Abs(value) > Math.Abs(output[dlvIndex]))
                                        {
                                            //Values should be all the same, but prefer the furthest from 0
                                            output[dlvIndex] = value;
                                        }
                                    }
                                    else
                                    {
                                        binaryReader.BaseStream.Position += 4;
                                    }
                                }
                            }
                        }
                    }
                }
                return(output);
            }