示例#1
0
文件: FrameParser.cs 项目: xj0229/gsf
        // Reset file watcher
        private void ResetFileWatcher()
        {
            if (m_configurationFileWatcher != null)
            {
                m_configurationFileWatcher.Changed -= m_configurationFileWatcher_Changed;
                m_configurationFileWatcher.Dispose();
            }
            m_configurationFileWatcher = null;

            string configurationFile = ConfigurationFileName;

            if (m_refreshConfigurationFileOnChange && !string.IsNullOrEmpty(configurationFile) && File.Exists(configurationFile))
            {
                try
                {
                    // Create a new file watcher for configuration file - we'll automatically refresh configuration file
                    // when this file gets updated...
                    m_configurationFileWatcher                       = new SafeFileWatcher(FilePath.GetDirectoryName(configurationFile), FilePath.GetFileName(configurationFile));
                    m_configurationFileWatcher.Changed              += m_configurationFileWatcher_Changed;
                    m_configurationFileWatcher.EnableRaisingEvents   = true;
                    m_configurationFileWatcher.IncludeSubdirectories = false;
                    m_configurationFileWatcher.NotifyFilter          = NotifyFilters.LastWrite;
                }
                catch (Exception ex)
                {
                    OnParsingException(ex);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Creates a new <see cref="WebServer"/>.
        /// </summary>
        /// <param name="options">Web server options to use; set to <c>null</c> for defaults.</param>
        /// <param name="razorEngineCS">Razor engine instance for .cshtml templates; uses default instance if not provided.</param>
        /// <param name="razorEngineVB">Razor engine instance for .vbhtml templates; uses default instance if not provided.</param>
        public WebServer(WebServerOptions options = null, IRazorEngine razorEngineCS = null, IRazorEngine razorEngineVB = null)
        {
            m_releaseMode = !AssemblyInfo.EntryAssembly.Debuggable;

            if ((object)options == null)
            {
                options = new WebServerOptions();
            }

            m_options           = options;
            RazorEngineCS       = razorEngineCS ?? (m_releaseMode ? RazorEngine <CSharp> .Default : RazorEngine <CSharpDebug> .Default as IRazorEngine);
            RazorEngineVB       = razorEngineVB ?? (m_releaseMode ? RazorEngine <VisualBasic> .Default : RazorEngine <VisualBasicDebug> .Default as IRazorEngine);
            PagedViewModelTypes = new ConcurrentDictionary <string, Tuple <Type, Type> >(StringComparer.InvariantCultureIgnoreCase);
            m_etagCache         = new ConcurrentDictionary <string, long>(StringComparer.InvariantCultureIgnoreCase);
            m_handlerTypeCache  = new ConcurrentDictionary <string, Type>(StringComparer.InvariantCultureIgnoreCase);

            // Validate web root path
            m_options.WebRootPath = FilePath.AddPathSuffix(m_options.WebRootPath ?? RazorEngineCS.TemplatePath);

            m_fileWatcher = new SafeFileWatcher(m_options.WebRootPath)
            {
                IncludeSubdirectories = true,
                EnableRaisingEvents   = true
            };

            m_fileWatcher.Changed += m_fileWatcher_FileChange;
            m_fileWatcher.Deleted += m_fileWatcher_FileChange;
            m_fileWatcher.Renamed += m_fileWatcher_FileChange;
        }
示例#3
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="FileImporter"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_fileSystemWatcher != null)
                        {
                            m_fileSystemWatcher.Created -= m_fileSystemWatcher_Created;
                            m_fileSystemWatcher.Renamed -= m_fileSystemWatcher_Renamed;
                            m_fileSystemWatcher.Dispose();
                        }
                        m_fileSystemWatcher = null;

                        if (UseHighResolutionInputTimer)
                        {
                            PrecisionInputTimer.Detach(ref m_precisionTimer);
                        }
                        else if ((object)m_looseTimer != null)
                        {
                            m_looseTimer.Stop();
                            m_looseTimer.Dispose();
                        }
                        m_looseTimer = null;

                        if ((object)m_fileStream != null)
                        {
                            m_fileStream.Close();
                            m_fileStream.Dispose();
                        }
                        m_fileStream = null;

                        if ((object)m_fileProcessQueue != null)
                        {
                            m_fileProcessQueue.ProcessException -= m_fileProcessQueue_ProcessException;
                        }

                        m_fileProcessQueue = null;

                        // Clear minimum timer resolution.
                        PrecisionTimer.ClearMinimumTimerResolution(1);
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
示例#4
0
            public FileWatchDebugTemplateManager(string layoutRoot, InvalidatingCachingProvider cachingProvider)
            {
                m_cachingProvider = cachingProvider;
                m_templateManager = new ResolvePathTemplateManager(new[] { layoutRoot });

                m_fileWatcher = new SafeFileWatcher(Path.GetFullPath(layoutRoot), "*.*")
                {
                    EnableRaisingEvents   = true,
                    IncludeSubdirectories = true
                };

                m_fileWatcher.Changed += m_fileWatcher_Changed;
                m_fileWatcher.Created += m_fileWatcher_Changed;
                m_fileWatcher.Deleted += m_fileWatcher_Changed;
                m_fileWatcher.Renamed += m_fileWatcher_Changed;
            }
示例#5
0
        public BenRunner(int deviceId, int taskId)
        {
            m_adoDataConnection = new AdoDataConnection(Program.OpenMiConfigurationFile.Settings["systemSettings"]["ConnectionString"].Value, Program.OpenMiConfigurationFile.Settings["systemSettings"]["DataProviderString"].Value);

            try
            {
                string taskSettingsString = m_adoDataConnection.ExecuteScalar <string>("Select Settings From ConnectionProfileTask WHERE ID = {0}", taskId);
                m_connectionProfileTaskSettings = taskSettingsString.ParseKeyValuePairs();
                m_deviceRecord = m_adoDataConnection.RetrieveRow("Select * From Device WHERE ID = {0}", deviceId);
                Dictionary <string, string> deviceConnection = m_deviceRecord["connectionString"].ToString().ParseKeyValuePairs();

                m_connectionProfile = m_adoDataConnection.RetrieveRow("SELECT * FROM connectionProfile WHERE ID = {0}", deviceConnection["connectionProfileID"]);
                m_folder            = m_deviceRecord["OriginalSource"].ToString();
                m_ipAddress         = deviceConnection["connectionUserName"].Split('&')[0];
                m_localPath         = m_connectionProfileTaskSettings["localPath"];
                m_siteName          = m_deviceRecord["Name"].ToString();
                m_serialNumber      = deviceConnection["connectionUserName"].Split('&')[1];

                string tempDirectory = System.IO.Path.GetTempPath();
                System.IO.Directory.CreateDirectory(tempDirectory + "\\BenDownloader\\" + m_siteName);
                m_tempDirectoryName = tempDirectory + "BenDownloader\\" + m_siteName + "\\";
                //Console.WriteLine(m_tempDirectoryName);

                m_fileWatcher = new SafeFileWatcher(m_tempDirectoryName);
                m_fileWatcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName;
                m_fileWatcher.Changed     += m_fileWatcher_Changed;
                m_fileWatcher.Created     += m_fileWatcher_Changed;

                m_activityMonitor           = new System.Timers.Timer(5000.0D);
                m_activityMonitor.Elapsed  += m_activityMonitor_Elapsed;
                m_activityMonitor.AutoReset = true;

                m_lastFileDownloaded            = GetLastDownloadedFile();
                m_lastFileDownloadedThisSession = "";
            }
            catch (Exception ex)
            {
                Program.Log(ex.ToString(), m_tempDirectoryName);
            }
        }
示例#6
0
文件: WebServer.cs 项目: ellen50/gsf
        /// <summary>
        /// Creates a new <see cref="WebServer"/>.
        /// </summary>
        /// <param name="webRootPath">Root path for web server; defaults to template path for <paramref name="razorEngineCS"/>.</param>
        /// <param name="razorEngineCS">Razor engine instance for .cshtml templates; uses default instance if not provided.</param>
        /// <param name="razorEngineVB">Razor engine instance for .vbhtml templates; uses default instance if not provided.</param>
        public WebServer(string webRootPath = null, IRazorEngine razorEngineCS = null, IRazorEngine razorEngineVB = null)
        {
            bool releaseMode = !AssemblyInfo.EntryAssembly.Debuggable;

            m_razorEngineCS       = razorEngineCS ?? (releaseMode ? RazorEngine <CSharp> .Default : RazorEngine <CSharpDebug> .Default as IRazorEngine);
            m_razorEngineVB       = razorEngineVB ?? (releaseMode ? RazorEngine <VisualBasic> .Default : RazorEngine <VisualBasicDebug> .Default as IRazorEngine);
            m_webRootPath         = FilePath.AddPathSuffix(webRootPath ?? m_razorEngineCS.TemplatePath);
            m_etagCache           = new ConcurrentDictionary <string, uint>(StringComparer.InvariantCultureIgnoreCase);
            m_pagedViewModelTypes = new ConcurrentDictionary <string, Tuple <Type, Type> >(StringComparer.InvariantCultureIgnoreCase);

            m_fileWatcher = new SafeFileWatcher(m_webRootPath)
            {
                IncludeSubdirectories = true,
                EnableRaisingEvents   = true
            };

            m_fileWatcher.Changed += m_fileWatcher_FileChange;
            m_fileWatcher.Deleted += m_fileWatcher_FileChange;
            m_fileWatcher.Renamed += m_fileWatcher_FileChange;

            ClientCacheEnabled = true;
        }
示例#7
0
文件: FrameParser.cs 项目: xj0229/gsf
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="FrameParser"/> object and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (!m_disposed)
     {
         try
         {
             if (disposing)
             {
                 if (m_configurationFileWatcher != null)
                 {
                     m_configurationFileWatcher.Changed -= m_configurationFileWatcher_Changed;
                     m_configurationFileWatcher.Dispose();
                 }
                 m_configurationFileWatcher = null;
             }
         }
         finally
         {
             m_disposed = true;          // Prevent duplicate dispose.
             base.Dispose(disposing);    // Call base class Dispose().
         }
     }
 }
示例#8
0
        // Picks up files that were missed by the file watchers.
        private void FileWatchTimer_Elapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            // Check each of the existing file watchers to determine whether an
            // error occurred that forced the file watcher to stop raising events
            lock (m_fileWatchers)
            {
                for (int i = 0; i < m_fileWatchers.Count; i++)
                {
                    if (!m_fileWatchers[i].EnableRaisingEvents)
                    {
                        try
                        {
                            // This file watcher is no longer raising events so
                            // attempt to create a new file watcher for that file path
                            SafeFileWatcher newWatcher = new SafeFileWatcher(m_fileWatchers[i].Path);

                            DateTime enumerationStart;
                            HashSet<string> enumeratedFiles;
                            IEnumerator<string> enumerator;

                            newWatcher.IncludeSubdirectories = true;
                            newWatcher.Created += Watcher_Created;
                            newWatcher.Changed += Watcher_Changed;
                            newWatcher.Renamed += Watcher_Renamed;
                            newWatcher.Deleted += Watcher_Deleted;
                            newWatcher.Error += Watcher_Error;

                            m_fileWatchers[i].Created -= Watcher_Created;
                            m_fileWatchers[i].Changed -= Watcher_Changed;
                            m_fileWatchers[i].Renamed -= Watcher_Renamed;
                            m_fileWatchers[i].Deleted -= Watcher_Deleted;
                            m_fileWatchers[i].Error -= Watcher_Error;
                            m_fileWatchers[i].Dispose();

                            m_fileWatchers[i] = newWatcher;
                            newWatcher.EnableRaisingEvents = true;

                            // Files may have been dropped or removed while the file watcher
                            // was disconnected so we need to enumerate the files again
                            enumerationStart = DateTime.UtcNow;
                            enumeratedFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                            enumerator = Directory.EnumerateFiles(newWatcher.Path, "*.*", SearchOption.AllDirectories).GetEnumerator();
                            m_processingQueue.Add(() => EnumerateNextFile(newWatcher.Path, enumerationStart, enumeratedFiles, enumerator));

                        }
                        catch (Exception ex)
                        {
                            OnError(ex);
                        }
                    }
                }
            }
        }
示例#9
0
        /// <summary>
        /// Initializes this <see cref="FileImporter"/>.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            Dictionary <string, string> settings = Settings;
            string setting;

            // Load optional parameters

            if (settings.TryGetValue("importPath", out setting))
            {
                m_importPath = setting;
            }
            else
            {
                throw new InvalidOperationException("No import path was specified for EPRI file importer - this is a required setting.");
            }

            if (settings.TryGetValue("inputInterval", out setting))
            {
                m_inputInterval = double.Parse(setting);
            }

            if (settings.TryGetValue("measurementsPerInterval", out setting))
            {
                m_measurementsPerInterval = int.Parse(setting);
            }

            if (settings.TryGetValue("simulateTimestamp", out setting))
            {
                m_simulateTimestamp = setting.ParseBoolean();
            }

            if (settings.TryGetValue("timestampFormat", out setting))
            {
                m_timestampFormat = setting;
            }

            if (settings.TryGetValue("skipRows", out setting))
            {
                int.TryParse(setting, out m_skipRows);
            }

            if (m_skipRows < 0)
            {
                m_skipRows = 0;
            }

            settings.TryGetValue("useHighResolutionInputTimer", out setting);

            if (string.IsNullOrEmpty(setting))
            {
                setting = "false";
            }

            UseHighResolutionInputTimer = setting.ParseBoolean();

            if (!UseHighResolutionInputTimer)
            {
                m_looseTimer = new Timer();
            }

            // Load column mappings:
            if (settings.TryGetValue("columnMappings", out setting))
            {
                Dictionary <int, string> columnMappings = new Dictionary <int, string>();
                int index;

                foreach (KeyValuePair <string, string> mapping in setting.ParseKeyValuePairs())
                {
                    if (int.TryParse(mapping.Key, out index))
                    {
                        columnMappings[index] = mapping.Value;
                    }
                }

                if (!m_simulateTimestamp && !columnMappings.Values.Contains("Timestamp", StringComparer.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException("One of the column mappings must be defined as a \"Timestamp\": e.g., columnMappings={0=Timestamp; 1=PPA:12; 2=PPA13}.");
                }

                // In transverse mode, maximum measurements per interval is set to maximum columns in input file
                m_measurementsPerInterval = columnMappings.Keys.Max();

                // Auto-assign output measurements based on column mappings
                OutputMeasurements = columnMappings.Where(kvp => string.Compare(kvp.Value, "Timestamp", StringComparison.OrdinalIgnoreCase) != 0).Select(kvp =>
                {
                    string measurementID     = kvp.Value;
                    IMeasurement measurement = new Measurement();
                    MeasurementKey key;
                    Guid id;

                    if (Guid.TryParse(measurementID, out id))
                    {
                        key = MeasurementKey.LookUpBySignalID(id);
                    }
                    else
                    {
                        MeasurementKey.TryParse(measurementID, out key);
                    }

                    if (key.SignalID != Guid.Empty)
                    {
                        measurement.Metadata = key.Metadata;

                        // Associate measurement with column index
                        m_columnMappings[kvp.Key] = measurement;
                    }

                    return(measurement);
                }).ToArray();

                int timestampColumn = columnMappings.First(kvp => string.Compare(kvp.Value, "Timestamp", StringComparison.OrdinalIgnoreCase) == 0).Key;

                // Reserve a column mapping for timestamp value
                IMeasurement timestampMeasurement = new Measurement
                {
                    Metadata = new MeasurementMetadata(null, "Timestamp", 0, 1, null)
                };

                m_columnMappings[timestampColumn] = timestampMeasurement;
            }

            // Override input interval based on temporal processing interval if it's not set to default
            if (ProcessingInterval > -1)
            {
                m_inputInterval = ProcessingInterval == 0 ? 1 : ProcessingInterval;
            }

            if ((object)m_looseTimer != null)
            {
                m_looseTimer.Interval  = m_inputInterval;
                m_looseTimer.AutoReset = true;
                m_looseTimer.Elapsed  += m_looseTimer_Elapsed;
            }

            m_fileSystemWatcher          = new SafeFileWatcher(m_importPath, "EPRI-VS-Output-*.csv");
            m_fileSystemWatcher.Created += m_fileSystemWatcher_Created;
            m_fileSystemWatcher.Renamed += m_fileSystemWatcher_Renamed;
        }
示例#10
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="MetricImporter"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_fileSystemWatcher != null)
                        {
                            m_fileSystemWatcher.Created -= m_fileSystemWatcher_Created;
                            m_fileSystemWatcher.Renamed -= m_fileSystemWatcher_Renamed;
                            m_fileSystemWatcher.Dispose();
                        }
                        m_fileSystemWatcher = null;

                        if (UseHighResolutionInputTimer)
                        {
                            PrecisionInputTimer.Detach(ref m_precisionTimer);
                        }
                        else if ((object)m_looseTimer != null)
                        {
                            m_looseTimer.Stop();
                            m_looseTimer.Dispose();
                        }
                        m_looseTimer = null;

                        if ((object)m_fileStream != null)
                        {
                            m_fileStream.Close();
                            m_fileStream.Dispose();
                        }
                        m_fileStream = null;

                        if ((object)m_fileProcessQueue != null)
                            m_fileProcessQueue.ProcessException -= m_fileProcessQueue_ProcessException;

                        m_fileProcessQueue = null;

                        // Clear minimum timer resolution.
                        PrecisionTimer.ClearMinimumTimerResolution(1);
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
示例#11
0
        /// <summary>
        /// Initializes this <see cref="MetricImporter"/>.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            Dictionary<string, string> settings = Settings;
            string setting;

            // Load optional parameters

            if (settings.TryGetValue("importPath", out setting))
                m_importPath = setting;
            else
                throw new InvalidOperationException("No import path was specified for EPRI metric importer - this is a required setting.");

            if (settings.TryGetValue("inputInterval", out setting))
                m_inputInterval = double.Parse(setting);

            if (settings.TryGetValue("measurementsPerInterval", out setting))
                m_measurementsPerInterval = int.Parse(setting);

            if (settings.TryGetValue("simulateTimestamp", out setting))
                m_simulateTimestamp = setting.ParseBoolean();

            if (settings.TryGetValue("timestampFormat", out setting))
                m_timestampFormat = setting;

            if (settings.TryGetValue("skipRows", out setting))
                int.TryParse(setting, out m_skipRows);

            if (m_skipRows < 0)
                m_skipRows = 0;

            settings.TryGetValue("useHighResolutionInputTimer", out setting);

            if (string.IsNullOrEmpty(setting))
                setting = "false";

            UseHighResolutionInputTimer = setting.ParseBoolean();

            if (!UseHighResolutionInputTimer)
                m_looseTimer = new System.Timers.Timer();

            // Load column mappings:
            if (settings.TryGetValue("columnMappings", out setting))
            {
                Dictionary<int, string> columnMappings = new Dictionary<int, string>();
                int index;

                foreach (KeyValuePair<string, string> mapping in setting.ParseKeyValuePairs())
                {
                    if (int.TryParse(mapping.Key, out index))
                        columnMappings[index] = mapping.Value;
                }

                if (!m_simulateTimestamp && !columnMappings.Values.Contains("Timestamp", StringComparer.OrdinalIgnoreCase))
                    throw new InvalidOperationException("One of the column mappings must be defined as a \"Timestamp\": e.g., columnMappings={1=Timestamp; 2=PPA:12; 3=PPA13}.");

                // In transverse mode, maximum measurements per interval is set to maximum columns in input file
                m_measurementsPerInterval = columnMappings.Keys.Max();

                // Auto-assign output measurements based on column mappings
                OutputMeasurements = columnMappings.Where(kvp => string.Compare(kvp.Value, "Timestamp", true) != 0).Select(kvp =>
                {
                    string measurementID = kvp.Value;
                    IMeasurement measurement = new Measurement();
                    MeasurementKey key;
                    Guid id;

                    if (Guid.TryParse(measurementID, out id))
                    {
                        measurement.Key = MeasurementKey.LookUpBySignalID(id);
                    }
                    else if (MeasurementKey.TryParse(measurementID, out key))
                    {
                        measurement.Key = key;
                    }

                    if (measurement.ID != Guid.Empty)
                    {
                        try
                        {
                            DataRow[] filteredRows = DataSource.Tables["ActiveMeasurements"].Select(string.Format("SignalID = '{0}'", measurement.ID));

                            if (filteredRows.Length > 0)
                            {
                                DataRow row = filteredRows[0];

                                // Assign other attributes
                                measurement.TagName = row["PointTag"].ToNonNullString();
                                measurement.Multiplier = double.Parse(row["Multiplier"].ToString());
                                measurement.Adder = double.Parse(row["Adder"].ToString());
                            }
                        }
                        catch
                        {
                            // Failure to lookup extra metadata is not catastrophic
                        }

                        // Associate measurement with column index
                        m_columnMappings[kvp.Key] = measurement;
                    }

                    return measurement;
                }).ToArray();

                int timestampColumn = columnMappings.First(kvp => string.Compare(kvp.Value, "Timestamp", true) == 0).Key;

                // Reserve a column mapping for timestamp value
                IMeasurement timestampMeasurement = new Measurement()
                {
                    TagName = "Timestamp"
                };

                m_columnMappings[timestampColumn] = timestampMeasurement;
            }

            // Override input interval based on temporal processing interval if it's not set to default
            if (ProcessingInterval > -1)
                m_inputInterval = ProcessingInterval == 0 ? 1 : ProcessingInterval;

            if ((object)m_looseTimer != null)
            {
                m_looseTimer.Interval = m_inputInterval;
                m_looseTimer.AutoReset = true;
                m_looseTimer.Elapsed += m_looseTimer_Elapsed;
            }

            m_fileSystemWatcher = new SafeFileWatcher(m_importPath, "EPRI-VS-Metrics-*.csv");
            m_fileSystemWatcher.Created += m_fileSystemWatcher_Created;
            m_fileSystemWatcher.Renamed += m_fileSystemWatcher_Renamed;
        }
示例#12
0
文件: ArchiveFile.cs 项目: rmc00/gsf
        /// <summary>
        /// Initializes the <see cref="ArchiveFile"/>.
        /// </summary>
        /// <remarks>
        /// <see cref="Initialize()"/> is to be called by user-code directly only if the <see cref="ArchiveFile"/> is not consumed through the designer surface of the IDE.
        /// </remarks>
        public void Initialize()
        {
            if (!m_initialized)
            {
                LoadSettings();         // Load settings from the config file.

                m_initialized = true;   // Initialize only once.

                if (m_monitorNewArchiveFiles)
                {
                    m_currentLocationFileWatcher = new SafeFileWatcher();
                    m_currentLocationFileWatcher.IncludeSubdirectories = true;
                    m_currentLocationFileWatcher.Renamed += FileWatcher_Renamed;
                    m_currentLocationFileWatcher.Deleted += FileWatcher_Deleted;
                    m_currentLocationFileWatcher.Created += FileWatcher_Created;

                    m_offloadLocationFileWatcher = new SafeFileWatcher();
                    m_offloadLocationFileWatcher.IncludeSubdirectories = true;
                    m_offloadLocationFileWatcher.Renamed += FileWatcher_Renamed;
                    m_offloadLocationFileWatcher.Deleted += FileWatcher_Deleted;
                    m_offloadLocationFileWatcher.Created += FileWatcher_Created;
                }
            }
        }
示例#13
0
        /// <summary>
        /// Initializes this <see cref="FileImporter"/>.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            Dictionary<string, string> settings = Settings;
            string setting;

            // Load optional parameters

            if (settings.TryGetValue("importPath", out setting))
                m_importPath = setting;
            else
                throw new InvalidOperationException("No import path was specified for EPRI file importer - this is a required setting.");

            if (settings.TryGetValue("inputInterval", out setting))
                m_inputInterval = double.Parse(setting);

            if (settings.TryGetValue("measurementsPerInterval", out setting))
                m_measurementsPerInterval = int.Parse(setting);

            if (settings.TryGetValue("simulateTimestamp", out setting))
                m_simulateTimestamp = setting.ParseBoolean();

            if (settings.TryGetValue("timestampFormat", out setting))
                m_timestampFormat = setting;

            if (settings.TryGetValue("skipRows", out setting))
                int.TryParse(setting, out m_skipRows);

            if (m_skipRows < 0)
                m_skipRows = 0;

            settings.TryGetValue("useHighResolutionInputTimer", out setting);

            if (string.IsNullOrEmpty(setting))
                setting = "false";

            UseHighResolutionInputTimer = setting.ParseBoolean();

            if (!UseHighResolutionInputTimer)
                m_looseTimer = new Timer();

            // Load column mappings:
            if (settings.TryGetValue("columnMappings", out setting))
            {
                Dictionary<int, string> columnMappings = new Dictionary<int, string>();
                int index;

                foreach (KeyValuePair<string, string> mapping in setting.ParseKeyValuePairs())
                {
                    if (int.TryParse(mapping.Key, out index))
                        columnMappings[index] = mapping.Value;
                }

                if (!m_simulateTimestamp && !columnMappings.Values.Contains("Timestamp", StringComparer.OrdinalIgnoreCase))
                    throw new InvalidOperationException("One of the column mappings must be defined as a \"Timestamp\": e.g., columnMappings={0=Timestamp; 1=PPA:12; 2=PPA13}.");

                // In transverse mode, maximum measurements per interval is set to maximum columns in input file
                m_measurementsPerInterval = columnMappings.Keys.Max();

                // Auto-assign output measurements based on column mappings
                OutputMeasurements = columnMappings.Where(kvp => string.Compare(kvp.Value, "Timestamp", StringComparison.OrdinalIgnoreCase) != 0).Select(kvp =>
                {
                    string measurementID = kvp.Value;
                    IMeasurement measurement = new Measurement();
                    MeasurementKey key;
                    Guid id;

                    if (Guid.TryParse(measurementID, out id))
                    {
                        key = MeasurementKey.LookUpBySignalID(id);
                    }
                    else
                    {
                        MeasurementKey.TryParse(measurementID, out key);
                    }

                    if (key.SignalID != Guid.Empty)
                    {
                        measurement.Metadata = key.Metadata;

                        // Associate measurement with column index
                        m_columnMappings[kvp.Key] = measurement;
                    }

                    return measurement;
                }).ToArray();

                int timestampColumn = columnMappings.First(kvp => string.Compare(kvp.Value, "Timestamp", StringComparison.OrdinalIgnoreCase) == 0).Key;

                // Reserve a column mapping for timestamp value
                IMeasurement timestampMeasurement = new Measurement
                {
                    Metadata = new MeasurementMetadata(null, "Timestamp", 0, 1, null)
                };

                m_columnMappings[timestampColumn] = timestampMeasurement;
            }

            // Override input interval based on temporal processing interval if it's not set to default
            if (ProcessingInterval > -1)
                m_inputInterval = ProcessingInterval == 0 ? 1 : ProcessingInterval;

            if ((object)m_looseTimer != null)
            {
                m_looseTimer.Interval = m_inputInterval;
                m_looseTimer.AutoReset = true;
                m_looseTimer.Elapsed += m_looseTimer_Elapsed;
            }

            m_fileSystemWatcher = new SafeFileWatcher(m_importPath, "EPRI-VS-Output-*.csv");
            m_fileSystemWatcher.Created += m_fileSystemWatcher_Created;
            m_fileSystemWatcher.Renamed += m_fileSystemWatcher_Renamed;
        }
示例#14
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="InterprocessCache"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_fileWatcher != null)
                        {
                            m_fileWatcher.Changed -= m_fileWatcher_Changed;
                            m_fileWatcher.Dispose();
                            m_fileWatcher = null;
                        }

                        if ((object)m_retryTimer != null)
                        {
                            m_retryTimer.Elapsed -= m_retryTimer_Elapsed;
                            m_retryTimer.Dispose();
                            m_retryTimer = null;
                        }

                        if ((object)m_loadIsReady != null)
                        {
                            m_loadIsReady.Dispose();
                            m_loadIsReady = null;
                        }

                        if ((object)m_saveIsReady != null)
                        {
                            m_saveIsReady.Dispose();
                            m_saveIsReady = null;
                        }

                        if ((object)m_fileLock != null)
                        {
                            m_fileLock.Dispose();
                            m_fileLock = null;
                        }

                        m_fileName = null;
                    }
                }
                finally
                {
                    m_disposed = true;  // Prevent duplicate dispose.
                }
            }
        }
示例#15
0
        /// <summary>
        /// Creates a new <see cref="WebServer"/>.
        /// </summary>
        /// <param name="webRootPath">Root path for web server; defaults to template path for <paramref name="razorEngineCS"/>.</param>
        /// <param name="razorEngineCS">Razor engine instance for .cshtml templates; uses default instance if not provided.</param>
        /// <param name="razorEngineVB">Razor engine instance for .vbhtml templates; uses default instance if not provided.</param>
        public WebServer(string webRootPath = null, IRazorEngine razorEngineCS = null, IRazorEngine razorEngineVB = null)
        {
            m_releaseMode = !AssemblyInfo.EntryAssembly.Debuggable;
            m_razorEngineCS = razorEngineCS ?? (m_releaseMode ? RazorEngine<CSharp>.Default : RazorEngine<CSharpDebug>.Default as IRazorEngine);
            m_razorEngineVB = razorEngineVB ?? (m_releaseMode ? RazorEngine<VisualBasic>.Default : RazorEngine<VisualBasicDebug>.Default as IRazorEngine);
            m_webRootPath = FilePath.AddPathSuffix(webRootPath ?? m_razorEngineCS.TemplatePath);
            m_etagCache = new ConcurrentDictionary<string, long>(StringComparer.InvariantCultureIgnoreCase);
            m_handlerTypeCache = new ConcurrentDictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase);
            m_pagedViewModelTypes = new ConcurrentDictionary<string, Tuple<Type, Type>>(StringComparer.InvariantCultureIgnoreCase);

            m_fileWatcher = new SafeFileWatcher(m_webRootPath)
            {
                IncludeSubdirectories = true,
                EnableRaisingEvents = true
            };

            m_fileWatcher.Changed += m_fileWatcher_FileChange;
            m_fileWatcher.Deleted += m_fileWatcher_FileChange;
            m_fileWatcher.Renamed += m_fileWatcher_FileChange;

            ClientCacheEnabled = true;
        }
示例#16
0
        /// <summary>
        /// Adds a directory to the list of directories tracked by this <see cref="FileProcessor"/>.
        /// </summary>
        /// <param name="path">The path to the directory to be tracked.</param>
        public void AddTrackedDirectory(string path)
        {
            string fullPath = FilePath.GetAbsolutePath(path);
            SafeFileWatcher watcher;

            DateTime enumerationStart;
            HashSet<string> enumeratedFiles;
            IEnumerator<string> enumerator;

            if (!TrackedDirectories.Contains(fullPath, StringComparer.OrdinalIgnoreCase))
            {
                watcher = new SafeFileWatcher(fullPath);
                watcher.IncludeSubdirectories = true;
                watcher.InternalBufferSize = m_internalBufferSize;

                watcher.Created += Watcher_Created;
                watcher.Changed += Watcher_Changed;
                watcher.Renamed += Watcher_Renamed;
                watcher.Deleted += Watcher_Deleted;
                watcher.Error += Watcher_Error;

                lock (m_fileWatchersLock)
                {
                    if (m_fileWatchers.Count == 0)
                    {
                        m_processingQueue.Start();
                        m_processingQueue.Add(LoadProcessedFiles);
                        m_fileWatchTimer.Start();
                    }

                    m_fileWatchers.Add(watcher);
                }

                watcher.EnableRaisingEvents = true;

                enumerationStart = DateTime.UtcNow;
                enumeratedFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
                enumerator = Directory.EnumerateFiles(fullPath, "*.*", SearchOption.AllDirectories).GetEnumerator();
                m_processingQueue.Add(() => EnumerateNextFile(fullPath, enumerationStart, enumeratedFiles, enumerator));
            }
        }
示例#17
0
        /// <summary>
        /// Initializes this <see cref="MetricImporter"/>.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            Dictionary <string, string> settings = Settings;
            string setting;

            // Load optional parameters

            if (settings.TryGetValue("importPath", out setting))
            {
                m_importPath = setting;
            }
            else
            {
                throw new InvalidOperationException("No import path was specified for EPRI metric importer - this is a required setting.");
            }

            if (settings.TryGetValue("inputInterval", out setting))
            {
                m_inputInterval = double.Parse(setting);
            }

            if (settings.TryGetValue("measurementsPerInterval", out setting))
            {
                m_measurementsPerInterval = int.Parse(setting);
            }

            if (settings.TryGetValue("simulateTimestamp", out setting))
            {
                m_simulateTimestamp = setting.ParseBoolean();
            }

            if (settings.TryGetValue("timestampFormat", out setting))
            {
                m_timestampFormat = setting;
            }

            if (settings.TryGetValue("skipRows", out setting))
            {
                int.TryParse(setting, out m_skipRows);
            }

            if (m_skipRows < 0)
            {
                m_skipRows = 0;
            }

            settings.TryGetValue("useHighResolutionInputTimer", out setting);

            if (string.IsNullOrEmpty(setting))
            {
                setting = "false";
            }

            UseHighResolutionInputTimer = setting.ParseBoolean();

            if (!UseHighResolutionInputTimer)
            {
                m_looseTimer = new System.Timers.Timer();
            }

            // Load column mappings:
            if (settings.TryGetValue("columnMappings", out setting))
            {
                Dictionary <int, string> columnMappings = new Dictionary <int, string>();
                int index;

                foreach (KeyValuePair <string, string> mapping in setting.ParseKeyValuePairs())
                {
                    if (int.TryParse(mapping.Key, out index))
                    {
                        columnMappings[index] = mapping.Value;
                    }
                }

                if (!m_simulateTimestamp && !columnMappings.Values.Contains("Timestamp", StringComparer.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException("One of the column mappings must be defined as a \"Timestamp\": e.g., columnMappings={1=Timestamp; 2=PPA:12; 3=PPA13}.");
                }

                // In transverse mode, maximum measurements per interval is set to maximum columns in input file
                m_measurementsPerInterval = columnMappings.Keys.Max();

                // Auto-assign output measurements based on column mappings
                OutputMeasurements = columnMappings.Where(kvp => string.Compare(kvp.Value, "Timestamp", true) != 0).Select(kvp =>
                {
                    string measurementID     = kvp.Value;
                    IMeasurement measurement = new Measurement();
                    MeasurementKey key;
                    Guid id;

                    if (Guid.TryParse(measurementID, out id))
                    {
                        measurement.Key = MeasurementKey.LookUpBySignalID(id);
                    }
                    else if (MeasurementKey.TryParse(measurementID, out key))
                    {
                        measurement.Key = key;
                    }

                    if (measurement.ID != Guid.Empty)
                    {
                        try
                        {
                            DataRow[] filteredRows = DataSource.Tables["ActiveMeasurements"].Select(string.Format("SignalID = '{0}'", measurement.ID));

                            if (filteredRows.Length > 0)
                            {
                                DataRow row = filteredRows[0];

                                // Assign other attributes
                                measurement.TagName    = row["PointTag"].ToNonNullString();
                                measurement.Multiplier = double.Parse(row["Multiplier"].ToString());
                                measurement.Adder      = double.Parse(row["Adder"].ToString());
                            }
                        }
                        catch
                        {
                            // Failure to lookup extra metadata is not catastrophic
                        }

                        // Associate measurement with column index
                        m_columnMappings[kvp.Key] = measurement;
                    }

                    return(measurement);
                }).ToArray();

                int timestampColumn = columnMappings.First(kvp => string.Compare(kvp.Value, "Timestamp", true) == 0).Key;

                // Reserve a column mapping for timestamp value
                IMeasurement timestampMeasurement = new Measurement()
                {
                    TagName = "Timestamp"
                };

                m_columnMappings[timestampColumn] = timestampMeasurement;
            }

            // Override input interval based on temporal processing interval if it's not set to default
            if (ProcessingInterval > -1)
            {
                m_inputInterval = ProcessingInterval == 0 ? 1 : ProcessingInterval;
            }

            if ((object)m_looseTimer != null)
            {
                m_looseTimer.Interval  = m_inputInterval;
                m_looseTimer.AutoReset = true;
                m_looseTimer.Elapsed  += m_looseTimer_Elapsed;
            }

            m_fileSystemWatcher          = new SafeFileWatcher(m_importPath, "EPRI-VS-Metrics-*.csv");
            m_fileSystemWatcher.Created += m_fileSystemWatcher_Created;
            m_fileSystemWatcher.Renamed += m_fileSystemWatcher_Renamed;
        }
示例#18
0
        // Picks up files that were missed by the file watchers.
        private void FileWatchTimer_Elapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            // Check each of the existing file watchers to determine whether an
            // error occurred that forced the file watcher to stop raising events
            lock (m_fileWatchers)
            {
                for (int i = 0; i < m_fileWatchers.Count; i++)
                {
                    if (!m_fileWatchers[i].EnableRaisingEvents)
                    {
                        try
                        {
                            // This file watcher is no longer raising events so
                            // attempt to create a new file watcher for that file path
                            SafeFileWatcher newWatcher = new SafeFileWatcher(m_fileWatchers[i].Path);

                            newWatcher.IncludeSubdirectories = true;
                            newWatcher.Created += Watcher_Created;
                            newWatcher.Changed += Watcher_Changed;
                            newWatcher.Renamed += Watcher_Renamed;
                            newWatcher.Deleted += Watcher_Deleted;
                            newWatcher.Error += Watcher_Error;

                            m_fileWatchers[i].Created -= Watcher_Created;
                            m_fileWatchers[i].Changed -= Watcher_Changed;
                            m_fileWatchers[i].Renamed -= Watcher_Renamed;
                            m_fileWatchers[i].Deleted -= Watcher_Deleted;
                            m_fileWatchers[i].Error -= Watcher_Error;
                            m_fileWatchers[i].Dispose();

                            m_fileWatchers[i] = newWatcher;
                            newWatcher.EnableRaisingEvents = true;

                            // Files may have been dropped or removed while the file watcher
                            // was disconnected so we need to enumerate the files again
                            m_enumerator.Enumerate(newWatcher.Path);
                        }
                        catch (Exception ex)
                        {
                            OnError(ex);
                        }
                    }
                }
            }

            // Determine if we need to defragment the lookup table for processed files
            m_processingThread.Push(1, () =>
            {
                if (m_processedFiles.FragmentationCount > m_maxFragmentation)
                {
                    DateTime lastCompactTime = DateTime.UtcNow;
                    m_processedFiles.Compact();
                    m_lastCompactTime = lastCompactTime;
                    m_lastCompactDuration = m_lastCompactTime - DateTime.UtcNow;
                }
            });
        }
示例#19
0
        // Detaches from events, removes the given file watcher from
        // the list of watchers, and disposes of the file watcher.
        private void RemoveFileWatcher(SafeFileWatcher watcher)
        {
            watcher.Created -= Watcher_Created;
            watcher.Changed -= Watcher_Changed;
            watcher.Renamed -= Watcher_Renamed;
            watcher.Deleted -= Watcher_Deleted;
            watcher.Error -= Watcher_Error;

            m_fileWatchers.Remove(watcher);
            watcher.Dispose();
        }
示例#20
0
        /// <summary>
        /// Adds a directory to the list of directories tracked by this <see cref="FileProcessor"/>.
        /// </summary>
        /// <param name="path">The path to the directory to be tracked.</param>
        public void AddTrackedDirectory(string path)
        {
            string fullPath = FilePath.GetAbsolutePath(path);
            SafeFileWatcher watcher;

            if (!TrackedDirectories.Contains(fullPath, StringComparer.OrdinalIgnoreCase))
            {
                watcher = new SafeFileWatcher(fullPath);
                watcher.IncludeSubdirectories = true;
                watcher.InternalBufferSize = m_internalBufferSize;

                watcher.Created += Watcher_Created;
                watcher.Changed += Watcher_Changed;
                watcher.Renamed += Watcher_Renamed;
                watcher.Deleted += Watcher_Deleted;
                watcher.Error += Watcher_Error;

                lock (m_fileWatchersLock)
                {
                    if (m_fileWatchers.Count == 0)
                    {
                        m_processingThread.Push(2, LoadProcessedFiles);
                        m_fileWatchTimer.Start();
                    }

                    m_fileWatchers.Add(watcher);
                }

                watcher.EnableRaisingEvents = true;
                m_enumerator.Enumerate(fullPath);
            }
        }