Example #1
0
        /// <summary>
        /// Reads the TAG file schema dictionary from the TAG file data using the TAG file reader
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public bool Read(TAGReader reader)
        {
            string fieldName;

            while ((fieldName = reader.ReadANSIString()) != string.Empty)
            {
                uint tempFieldType = reader.ReadUnSignedIntegerValue(1);

                // If field type is 15, then read an extra var int to determine extended data type
                if (tempFieldType == 15)
                {
                    if (!reader.ReadVarInt(out short tempFieldTypeExtended))
                    {
                        return(false);
                    }
                    tempFieldType += (uint)tempFieldTypeExtended;
                }

                var fieldType = (TAGDataType)tempFieldType;

                if (!reader.ReadVarInt(out var id))
                {
                    return(false);
                }

                Entries.Add(id, new TAGDictionaryItem(fieldName, fieldType, id));
            }

            if (Entries.Count > DEFAULT_TAG_FILE_SCHEMA_DICTIONARY_CAPACITY)
            {
                Log.LogInformation($"TAG file schema dictionary final size of {Entries.Count} exceeds default capacity for dictionary of {DEFAULT_TAG_FILE_SCHEMA_DICTIONARY_CAPACITY}. Consider increasing it.");
            }

            return(true);
        }
Example #2
0
 public void Test_TAGReader_Creation()
 {
     using (var reader = new TAGReader(new MemoryStream(Consts.TREX_DEFAULT_MEMORY_STREAM_CAPACITY_ON_CREATION)))
     {
         Assert.NotNull(reader);
     }
 }
Example #3
0
        /// <summary>
        /// Execute the pre-scan operation on the TAG file, returning a boolean success result.
        /// Sets up local state detailing the pre-scan fields retried from the ATG file
        /// </summary>
        public bool Execute(Stream TAGData)
        {
            try
            {
                Initialise();

                using (var Processor = new TAGProcessorPreScanState())
                {
                    var Sink = new TAGVisionLinkPrerequisitesValueSink(Processor);
                    using (var Reader = new TAGReader(TAGData))
                    {
                        var TagFile = new TAGFile();

                        ReadResult = TagFile.Read(Reader, Sink);
                    }

                    if (ReadResult != TAGReadResult.NoError)
                    {
                        return(false);
                    }

                    IsCSIBCoordSystemTypeOnly = Processor.IsCSIBCoordSystemTypeOnly;

                    SetPublishedState(Processor);
                }
            }
            catch // (Exception E) // make sure any exception is trapped to return correct response to caller
            {
                return(false);
            }

            return(true);
        }
Example #4
0
        }                                                 // This is a byte offset

        /// <summary>
        /// Reads the contents of the TAG file header using the provided reader
        /// </summary>
        /// <param name="reader"></param>
        public void Read(TAGReader reader)
        {
            MajorVer                = reader.ReadUnSignedIntegerValue(1);
            MinorVer                = reader.ReadUnSignedIntegerValue(1);
            DictionaryID            = reader.ReadUnSignedIntegerValue(4);
            DictionaryMajorVer      = reader.ReadUnSignedIntegerValue(1);
            DictionaryMinorVer      = reader.ReadUnSignedIntegerValue(1);
            FieldAndTypeTableOffset = reader.ReadUnSignedIntegerValue(8);
        }
Example #5
0
 /// <summary>
 /// Read the content of a TAG file and routes it to the provided sink
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="sink"></param>
 /// <returns></returns>
 public TAGReadResult Read(string fileName, TAGValueSinkBase sink)
 {
     using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
     {
         using (var reader = new TAGReader(fs))
         {
             return(Read(reader, sink));
         }
     }
 }
Example #6
0
        /// <summary>
        /// Execute the pre-scan operation on the TAG file, returning a boolean success result.
        /// Sets up local state detailing the pre-scan fields retried from the ATG file
        /// </summary>
        public bool Execute(Stream TAGData, ref List <UTMCoordPointPair> aCSBladePositions, ref List <UTMCoordPointPair> aCSRearAxlePositions, ref List <UTMCoordPointPair> aCSTrackPositions, ref List <UTMCoordPointPair> aCSWheelPositions)
        {
            try
            {
                Initialise();

                using (var processor = new TAGProcessorPreScanACSState())
                {
                    var Sink = new TAGVisionLinkPrerequisitesValueSink(processor);
                    using (var Reader = new TAGReader(TAGData))
                    {
                        var TagFile = new TAGFile();

                        ReadResult = TagFile.Read(Reader, Sink);
                    }

                    if (ReadResult != TAGReadResult.NoError)
                    {
                        return(false);
                    }

                    IsCSIBCoordSystemTypeOnly = processor.IsCSIBCoordSystemTypeOnly;
                    if (!IsCSIBCoordSystemTypeOnly)
                    {
                        if (processor.HaveReceivedValidTipPositions)
                        {
                            aCSBladePositions.AddRange(processor.BladePositions);
                        }
                        if (processor.HaveReceivedValidRearPositions)
                        {
                            aCSRearAxlePositions.AddRange(processor.RearAxlePositions);
                        }
                        if (processor.HaveReceivedValidTrackPositions)
                        {
                            aCSTrackPositions.AddRange(processor.TrackPositions);
                        }
                        if (processor.HaveReceivedValidWheelPositions)
                        {
                            aCSWheelPositions.AddRange(processor.WheelPositions);
                        }
                    }
                    SetPublishedState(processor);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
Example #7
0
        public void Test_TAGFile_Read_Stream()
        {
            // Create the TAG file and reader classes
            TAGFile file = new TAGFile();

            using (var reader = new TAGReader(new FileStream(
                                                  Path.Combine("TestData", "TAGFiles", "TestTAGFile-TAGFile-Read-Stream.tag"), FileMode.Open,
                                                  FileAccess.Read)))
            {
                // Create the state and sink
                TAGProcessorStateBase stateBase = new TAGProcessorStateBase(); // Derivatives to construct later
                TAGValueSink          sink      = new TAGValueSink(stateBase);

                //Read the TAG file
                TAGReadResult result = file.Read(reader, sink);

                Assert.Equal(TAGReadResult.NoError, result);
            }
        }
Example #8
0
        private static void ProcessTagFiles(string directoryToProcess, string[] dataList)
        {
            if (dataList == null)
            {
                Console.WriteLine($"No specific tag attributes requested, using defaults of {DEFAULT_DATA_LIST}");
            }
            var files = new List <string>();

            CollectTagFilesInFolder(directoryToProcess, files);

            TAGReader       reader    = null;
            FileStream      fs        = null;
            CSVTAGProcessor stateBase = null;

            foreach (var filename in files)
            {
                // Create the TAG file and reader classes
                var file = new TAGFile();

                try
                {
                    fs     = new FileStream(Path.GetFullPath(filename), FileMode.Open, FileAccess.Read);
                    reader = new TAGReader(fs);

                    // Create the state and sink
                    stateBase = new CSVTAGProcessor(dataList ?? DEFAULT_DATA_LIST, Path.GetFullPath(filename));
                    var sink = new TAGValueSink(stateBase);

                    //Read the TAG file
                    var result = file.Read(reader, sink);
                    Console.WriteLine($"File {filename} processed with result {result}");
                }
                finally
                {
                    fs?.Dispose();
                    reader?.Dispose();
                    stateBase?.Dispose();
                }
            }

            Console.WriteLine("Complete");
        }
Example #9
0
        public void Test_TAGHeader_Read()
        {
            using (var reader = new TAGReader(new FileStream(Path.Combine("TestData", "TAGFiles", "TestTAGFile-TAG-Header-Read.tag"), FileMode.Open)))
            {
                Assert.NotNull(reader);

                TAGHeader header = new TAGHeader();

                //Read the header
                header.Read(reader);

                Assert.Equal(1U, header.DictionaryID);
                Assert.Equal(1U, header.DictionaryMajorVer);
                Assert.Equal(4U, header.DictionaryMinorVer);
                Assert.Equal(1U, header.MajorVer);
                Assert.Equal(0U, header.MinorVer);
                Assert.True(
                    header.FieldAndTypeTableOffset > 0 && header.FieldAndTypeTableOffset < reader.StreamSizeInNybbles / 2,
                    "Field and type table offset read from header is invalid");
            }
        }
Example #10
0
        /// <summary>
        /// Reads the context of a TAG file using the provided reader and sink
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="sink"></param>
        /// <returns></returns>
        public TAGReadResult Read(TAGReader reader, TAGValueSinkBase sink)
        {
            try
            {
                if (reader.StreamSizeInNybbles == 0)
                {
                    return(TAGReadResult.ZeroLengthFile);
                }

                try
                {
                    Header.Read(reader);
                }
                catch (Exception E)
                {
                    Log.LogError(E, "Invalid tag file. Exception in TTagFile.ReadStream - Header.LoadFromStream:");
                    return(TAGReadResult.InvalidDictionary);
                }

                // If the offset to the dictionary is zero, then it follows immediately after the header
                long DataEndPos;
                try
                {
                    if (Header.FieldAndTypeTableOffset != 0)
                    {
                        long StreamPos = reader.NybblePosition;
                        reader.NybblePosition = Header.FieldAndTypeTableOffset * 2; // FieldAndTypeTableOffset is in bytes

                        if (!Dictionary.Read(reader))
                        {
                            return(TAGReadResult.InvalidDictionary);
                        }

                        reader.NybblePosition = StreamPos;

                        DataEndPos = Header.FieldAndTypeTableOffset * 2; // FieldAndTypeTableOffset is in bytes
                    }
                    else
                    {
                        if (!Dictionary.Read(reader))
                        {
                            return(TAGReadResult.InvalidDictionary);
                        }

                        DataEndPos = reader.StreamSizeInNybbles;
                    }
                }
                catch (Exception E)
                {
                    Log.LogWarning(E, "Exception in TagFile.ReadFile:");
                    return(TAGReadResult.InvalidDictionary);
                }

                // Now read in the data from the file
                if (!sink.Starting())
                {
                    return(TAGReadResult.SinkStartingFailure);
                }

                while (!sink.Aborting() && reader.NybblePosition < DataEndPos)
                {
                    if (!reader.ReadVarInt(out short ValueTypeID))
                    {
                        if (reader.NybblePosition >= DataEndPos)
                        {
                            break;                                // We have finished
                        }
                        return(TAGReadResult.InvalidValueTypeID); // This is an invalid tag file
                    }

                    if (Dictionary.Entries.Keys.Count == 0)
                    {
                        return(TAGReadResult.InvalidDictionary);
                    }

                    if (!Dictionary.Entries.TryGetValue(ValueTypeID, out TAGDictionaryItem DictionaryEntry))
                    {
                        return(TAGReadResult.InvalidValueTypeID);
                    }

                    try
                    {
                        switch (DictionaryEntry.Type)
                        {
                        case TAGDataType.t4bitInt:
                        case TAGDataType.t8bitInt:
                        case TAGDataType.t12bitInt:
                        case TAGDataType.t16bitInt:
                        case TAGDataType.t32bitInt:
                            sink.ReadIntegerValue(DictionaryEntry, reader.ReadSignedIntegerValue(IntegerNybbleSizes.Nybbles[(byte)DictionaryEntry.Type]));
                            break;

                        case TAGDataType.t4bitUInt:
                        case TAGDataType.t8bitUInt:
                        case TAGDataType.t12bitUInt:
                        case TAGDataType.t16bitUInt:
                        case TAGDataType.t32bitUInt:
                            sink.ReadUnsignedIntegerValue(DictionaryEntry, reader.ReadUnSignedIntegerValue(IntegerNybbleSizes.Nybbles[(byte)DictionaryEntry.Type]));
                            break;

                        case TAGDataType.tIEEESingle:
                            sink.ReadIEEESingleValue(DictionaryEntry, reader.ReadSinglePrecisionIEEEValue());
                            break;

                        case TAGDataType.tIEEEDouble:
                            sink.ReadIEEEDoubleValue(DictionaryEntry, reader.ReadDoublePrecisionIEEEValue());
                            break;

                        case TAGDataType.tANSIString:
                            sink.ReadANSIStringValue(DictionaryEntry, reader.ReadANSIString());
                            break;

                        case TAGDataType.tUnicodeString:
                            sink.ReadUnicodeStringValue(DictionaryEntry, reader.ReadUnicodeString());
                            break;

                        case TAGDataType.tEmptyType:
                            sink.ReadEmptyValue(DictionaryEntry);
                            break;
                        }
                    }
                    catch (Exception E)
                    {
                        Log.LogError(E, "Exception in TagFile.ReadFile while reading field value:");
                        return(TAGReadResult.InvalidValue);
                    }
                }

                if (!sink.Finishing())
                {
                    return(TAGReadResult.SinkFinishingFailure);
                }
            }
            catch (IOException E)
            {
                Log.LogDebug(E, "Exception in TagFile.ReadFile:");
                return(TAGReadResult.CouldNotOpenFile);
            }

            return(TAGReadResult.NoError);
        }
Example #11
0
        /// <summary>
        /// Execute the conversion operation on the TAG file, returning a boolean success result.
        /// Sets up local state detailing the pre-scan fields retrieved from the TAG file
        /// </summary>
        public bool ExecuteLegacyTAGFile(string filename, Stream tagData, Guid assetUid, bool isJohnDoe)
        {
            Log.LogInformation($"In {nameof(ExecuteLegacyTAGFile)}: reading file {filename} for asset {assetUid}, JohnDoe: {isJohnDoe}");

            ReadResult = TAGReadResult.NoError;
            List <UTMCoordPointPair> aCSBladePositions    = null;
            List <UTMCoordPointPair> ACSRearAxlePositions = null;
            List <UTMCoordPointPair> ACSTrackPositions    = null;
            List <UTMCoordPointPair> ACSWheelPositions    = null;

            try
            {
                Processor?.Dispose();

                // Locate the machine in the local set of machines, adding one if necessary
                Machine = Machines.Locate(assetUid, isJohnDoe);

                var machineType       = MachineType.Unknown;
                var machineHardwareId = string.Empty;
                var machineId         = string.Empty;

                //Prescan to get all relevant information necessary for processing the tag file. e.g. Machinetype for swather, Type of coordinate system (ACS)
                var tagFilePreScan = new TAGFilePreScan();
                tagFilePreScan.Execute(tagData);
                tagData.Position = 0; // reset
                if (tagFilePreScan.ReadResult == TAGReadResult.NoError)
                {
                    machineType           = tagFilePreScan.MachineType; // used in creation of swather
                    machineHardwareId     = tagFilePreScan.HardwareID;
                    machineId             = tagFilePreScan.MachineID;
                    IsUTMCoordinateSystem = !tagFilePreScan.IsCSIBCoordSystemTypeOnly; // do we need to convert UTM coordinates to project coordinates
                    if (IsUTMCoordinateSystem && tagFilePreScan.ProcessedEpochCount > 0)
                    {
                        Log.LogInformation($"{nameof(ExecuteLegacyTAGFile)}: ACS coordinate system detected. {filename}");
                        aCSBladePositions    = new List <UTMCoordPointPair>();
                        ACSRearAxlePositions = new List <UTMCoordPointPair>();
                        ACSTrackPositions    = new List <UTMCoordPointPair>();
                        ACSWheelPositions    = new List <UTMCoordPointPair>();
                        if (!CollectAndConvertBladePostions(_targetSiteModel, ref tagData, ref aCSBladePositions, ref ACSRearAxlePositions, ref ACSTrackPositions, ref ACSWheelPositions))
                        {
                            Log.LogError($"{nameof(ExecuteLegacyTAGFile)}: Failed to collect and convert blade positions for tagfile processing with ACS. TAG FILE:{filename}");
                            ReadResult = TAGReadResult.CoordinateConversionFailure;
                            return(false);
                        }
                    }
                }
                else
                {
                    Log.LogError($"Unsuccessful prescan of tagfile. {tagFilePreScan.ReadResult}");
                    return(false);
                }

                if (Machine == null)
                {
                    // Now we know more about the machine have another go finding it
                    Machine = Machines.Locate(assetUid, machineId, isJohnDoe);
                }

                if (Machine == null)
                {
                    Log.LogDebug($"Creating new machine in common converter for AssetUid = {assetUid}, JohnDoe = {isJohnDoe}, machineId = {machineId}, machineHardwareId = {machineHardwareId}");

                    Machine = Machines.CreateNew(machineId, machineHardwareId, machineType, DeviceTypeEnum.MANUALDEVICE, isJohnDoe, assetUid);
                }

                if (Machine.MachineType == MachineType.Unknown && machineType != MachineType.Unknown)
                {
                    Machine.MachineType = machineType;
                }

                var holdMachineType = Machine.MachineType;

                // Locate the aggregator, adding one if necessary
                var machineTargetValueChangesAggregator = MachinesTargetValueChangesAggregator[Machine.InternalSiteModelMachineIndex] as ProductionEventLists;
                if (machineTargetValueChangesAggregator == null)
                {
                    machineTargetValueChangesAggregator = new ProductionEventLists(SiteModel, Machine.InternalSiteModelMachineIndex);
                    MachinesTargetValueChangesAggregator.Add(machineTargetValueChangesAggregator);
                }

                Processor = new TAGProcessor(SiteModel, Machine, SiteModelGridAggregator, machineTargetValueChangesAggregator);

                // If ACS coordinate system populate converted UTM coordinates
                if (IsUTMCoordinateSystem && tagFilePreScan.ProcessedEpochCount > 0)
                {
                    if (aCSBladePositions != null && aCSBladePositions.Count > 0)
                    {
                        Processor.ConvertedBladePositions.AddRange(aCSBladePositions);
                    }
                    if (ACSRearAxlePositions != null && ACSRearAxlePositions.Count > 0)
                    {
                        Processor.ConvertedRearAxlePositions.AddRange(ACSRearAxlePositions);
                    }
                    if (ACSTrackPositions != null && ACSTrackPositions.Count > 0)
                    {
                        Processor.ConvertedTrackPositions.AddRange(ACSTrackPositions);
                    }
                    if (ACSWheelPositions != null && ACSWheelPositions.Count > 0)
                    {
                        Processor.ConvertedWheelPositions.AddRange(ACSWheelPositions);
                    }
                }

                var sink = new TAGValueSink(Processor);
                using (var reader = new TAGReader(tagData))
                {
                    var tagFile = new TAGFile();

                    ReadResult = tagFile.Read(reader, sink);

                    // Notify the processor that all reading operations have completed for the file
                    Processor.DoPostProcessFileAction(ReadResult == TAGReadResult.NoError);

                    SetPublishedState(Processor);
                    Machine.MachineType = holdMachineType;

                    if (ReadResult != TAGReadResult.NoError)
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e) // make sure any exception is trapped to return correct response to caller
            {
                Log.LogError(e, "Exception occurred while converting a TAG file");
                return(false);
            }

            return(true);
        }