public static void RunScheduler() { Program.DataReadersManager = new DataReadersManager(); Program.DataWriter = new DataWriter(); // Here are added the configured data collectors foreach (DataCollectorSettings collectorSettings in Config.Settings.DataCollectorsSettings) { try { if (!string.IsNullOrEmpty(collectorSettings.PluginFileName)) { var applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? ""; Assembly pluginAssembly = Assembly.LoadFrom(Path.Combine(applicationDirectory,"plugins",collectorSettings.PluginFileName)); foreach (var type in pluginAssembly.GetTypes()) { if (type.GetInterface(typeof (IDataCollector).Name) != null) { var newCollector = Activator.CreateInstance(type) as IDataCollector; if (newCollector != null) { newCollector.SetSettings(collectorSettings); _logger.InfoFormat("Adding task of type: {0} to scheduler with frequency {1}. Task description: {2}",type.Name,collectorSettings.DataCollectionPeriod,collectorSettings.ReaderTaskDescription); _scheduler.AddTask(collectorSettings.ReaderTaskDescription, collectorSettings.DataCollectionPeriod, new Action(newCollector.CollectData)); DataReadersManager.DataReaders.Add(newCollector); } else { _logger.Error("Data Collector could not be loaded"); } } } } } catch (Exception ex) { _logger.Error(ex); } _scheduler.Start(); } _logger.InfoFormat("AF Elements structure refresh period is set to : {0}", Config.Settings.MainSettings.CronPeriod_ConfigurationRefresh); _logger.InfoFormat("Data write period is set to : {0}", Config.Settings.MainSettings.CronPeriod_DataWrite); _logger.InfoFormat("Web Application port is set to : {0}", Config.Settings.MainSettings.WebConfigPort); _logger.InfoFormat("Readers Initialization"); Program.DataReadersManager.InitializeReaders(); //_logger.InfoFormat("configuring scheduler to run tasks periodically"); _scheduler.AddTask("Refreshing configuration", Config.Settings.MainSettings.CronPeriod_ConfigurationRefresh, new Action(DataReadersManager.RefreshReadersConfiguration)); _scheduler.AddTask("Writing data to the PI System", Config.Settings.MainSettings.CronPeriod_DataWrite, new Action(DataWriter.FlushData)); _scheduler.Start(); }
/// <summary> /// Service Main Entry Point /// </summary> private static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; if (Environment.UserInteractive) { _logger.Info("Starting service interractively"); var options = new CommandLineOptions(); if (Parser.Default.ParseArguments(args, options)) { if (options.Run) { if(!Config.IsLoaded()) Environment.Exit(-1); WebHost.Instance.Start(); Core.Program.RunScheduler(); Console.WriteLine("press a key to stop the data collection"); Console.ReadKey(); Core.Program.StopScheduler(); WebHost.Instance.Dispose(); Console.WriteLine("Stopped"); } if (options.Test) { var dataWriter = new DataWriter(); // Here are added the configured data collectors foreach (DataCollectorSettings collectorSettings in Config.Settings.DataCollectorsSettings) { try { if (!string.IsNullOrEmpty(collectorSettings.PluginFileName)) { var applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? ""; Assembly pluginAssembly = Assembly.LoadFrom(Path.Combine(applicationDirectory, "plugins", collectorSettings.PluginFileName)); foreach (var type in pluginAssembly.GetTypes()) { if (type.GetInterface(typeof(IDataCollector).Name) != null) { var newCollector = Activator.CreateInstance(type) as IDataCollector; if (newCollector != null) { newCollector.SetSettings(collectorSettings); newCollector.Inititialize(); newCollector.CollectData(); } else { _logger.Error("Data Collector could not be loaded"); } } } } } catch (Exception ex) { _logger.Error(ex); } dataWriter.FlushData(); } } if (options.Install) { ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location }); } if (options.Uninstall) { ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location }); } // exit ok Environment.Exit(0); } } else { ServiceBase[] ServicesToRun = { new Service() }; ServiceBase.Run(ServicesToRun); } }