Пример #1
0
        private void ProcessFile(string fileName)
        {
            DateTime timeStarted;
            DateTime timeProcessed;

            string rootFileName;
            ICollection <Device> devices;
            FaultLocationDataSet faultDataSet;

            Device disturbanceRecorder;
            ICollection <DisturbanceFile> disturbanceFiles;
            ICollection <Tuple <Line, FaultLocationDataSet> > lineDataSets;

            List <double> largestCurrentDistances;
            List <double> boundedDistances;

            timeStarted     = DateTime.UtcNow;
            m_currentLogger = null;
            rootFileName    = null;

            try
            {
                rootFileName = FilePath.GetFileNameWithoutExtension(fileName);

                if (m_debugLevel >= 1)
                {
                    m_currentLogger = Logger.Open(string.Format("{0}{1}.log", m_debugFolder, rootFileName));
                }

                OnStatusMessage(string.Format("Processing {0}...", fileName));

                // Make sure device definitions exist, and attempt to load them if they don't
                devices = m_devices;

                if ((object)devices == null)
                {
                    devices = CreateDevices(m_deviceDefinitionsFile);

                    foreach (IFaultResultsWriter resultsWriter in m_resultsWriters.Adapters)
                    {
                        if (resultsWriter.Enabled)
                        {
                            TryWriteConfiguration(resultsWriter, devices);
                        }
                    }

                    m_devices = devices;
                }

                // Get the definition for the device that captured this event
                disturbanceRecorder = GetDevice(devices, rootFileName);
                disturbanceFiles    = CreateDisturbanceFiles(rootFileName);
                lineDataSets        = new Collection <Tuple <Line, FaultLocationDataSet> >();

                if ((object)disturbanceRecorder == null)
                {
                    throw new InvalidOperationException(string.Format("No device record found for \"{0}\" in configuration.", fileName));
                }

                foreach (Line line in disturbanceRecorder.Lines)
                {
                    try
                    {
                        // Provide status information about which line is being processed
                        OnStatusMessage("Detecting faults on line {0}...", line.Name);

                        // Get the fault data set for this line
                        faultDataSet = GetFaultDataSet(fileName, line);

                        // Get the maximum rated current from the line definition
                        faultDataSet.RatedCurrent = line.Rating50F;

                        // Export data to CSV for validation
                        if (m_debugLevel >= 1)
                        {
                            MeasurementDataSet.ExportToCSV(FilePath.GetAbsolutePath(string.Format("{0}{1}.{2}_measurementData.csv", m_debugFolder, rootFileName, line.ID)), faultDataSet.Voltages, faultDataSet.Currents);
                            CycleDataSet.ExportToCSV(FilePath.GetAbsolutePath(string.Format("{0}{1}.{2}_cycleData.csv", m_debugFolder, rootFileName, line.ID)), faultDataSet.Cycles);
                        }

                        // Run fault trigger, type, and location algorithms
                        if (ExecuteFaultTriggerAlgorithm(line.FaultAlgorithmsSet.FaultTriggerAlgorithm, faultDataSet, line.FaultAlgorithmsSet.FaultTriggerParameters))
                        {
                            faultDataSet.FaultType      = ExecuteFaultTypeAlgorithm(line.FaultAlgorithmsSet.FaultTypeAlgorithm, faultDataSet, line.FaultAlgorithmsSet.FaultTypeParameters);
                            faultDataSet.FaultDistances = line.FaultAlgorithmsSet.FaultLocationAlgorithms.ToDictionary(algorithm => algorithm.Method.Name, algorithm => ExecuteFaultLocationAlgorithm(algorithm, faultDataSet, null));

                            // Get the set of distance calculations for the cycle with the largest current
                            largestCurrentDistances = faultDataSet.FaultDistances.Values
                                                      .Select(distances => distances[faultDataSet.Cycles.GetLargestCurrentIndex()])
                                                      .OrderBy(dist => dist)
                                                      .ToList();

                            // Filter the set down to the distance values that are reasonable
                            boundedDistances = largestCurrentDistances
                                               .Where(dist => dist >= 0.0D && dist <= faultDataSet.LineDistance)
                                               .ToList();

                            // Get the representative fault distance as the median of the bounded distances,
                            // falling back on the median of the unbounded distances if there are no bounded ones
                            faultDataSet.FaultDistance = (boundedDistances.Count > 0)
                                ? boundedDistances[boundedDistances.Count / 2]
                                : largestCurrentDistances[largestCurrentDistances.Count / 2];

                            OnStatusMessage("Distance to fault: {0} {1}", faultDataSet.FaultDistance, m_lengthUnits);

                            // Add the line-specific parameters to the faultResultsWriter
                            lineDataSets.Add(Tuple.Create(line, faultDataSet));
                        }
                        else
                        {
                            OnStatusMessage("No fault detected.");
                        }
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("Error detecting faults on line {0}: {1}", line.Name, ex.Message);
                        OnProcessException(new Exception(message, ex));
                    }
                }

                try
                {
                    // Write results to the output source
                    if (lineDataSets.Count > 0)
                    {
                        timeProcessed = DateTime.UtcNow;

                        foreach (DisturbanceFile disturbanceFile in disturbanceFiles)
                        {
                            disturbanceFile.FLETimeStarted   = timeStarted;
                            disturbanceFile.FLETimeProcessed = timeProcessed;
                        }

                        foreach (IFaultResultsWriter resultsWriter in m_resultsWriters.Adapters)
                        {
                            if (resultsWriter.Enabled)
                            {
                                TryWriteResults(resultsWriter, disturbanceRecorder, disturbanceFiles, lineDataSets);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errorMessage = string.Format("Unable to write results for file \"{0}\" due to exception: {1}", fileName, ex.Message);
                    OnProcessException(new InvalidOperationException(errorMessage, ex));
                }
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Unable to process file \"{0}\" due to exception: {1}", fileName, ex.Message);
                OnProcessException(new InvalidOperationException(errorMessage, ex));
            }

            try
            {
                if ((object)m_currentLogger != null)
                {
                    m_currentLogger.Close();
                    m_currentLogger = null;
                }
            }
            catch (Exception ex)
            {
                OnProcessException(ex);
            }

            try
            {
                if ((object)rootFileName != null)
                {
                    // Get a list of processed files based on file prefix
                    List <string> processedFiles = FilePath.GetFileList(string.Format("{0}{1}.*", m_dropFolder, rootFileName))
                                                   .Concat(FilePath.GetFileList(string.Format("{0}{1}_*", m_dropFolder, rootFileName)))
                                                   .ToList();

                    // Delete processed files from the drop folder
                    foreach (string file in processedFiles)
                    {
                        File.Delete(file);
                    }
                }
            }
            catch (Exception ex)
            {
                OnProcessException(ex);
            }

            OnStatusMessage("");
        }