using IniParser; using NLog; using System.Collections.Generic; using System.CommandLine; using System.CommandLine.Invocation; using System.Globalization; using System.IO; using System.Reflection; using System.Threading; namespace Barsik { class Program { private static RegistersContainer registers; private static List units; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { CultureInfo.CurrentCulture = new CultureInfo("en-US"); var rootCommand = new RootCommand(); var runCommand = new Command("run") { new Option("--config", () => "", "Path to configuration file") }; runCommand.Handler = CommandHandler.Create(RunService); rootCommand.AddCommand(runCommand); rootCommand.InvokeAsync(args).Wait(); } static void RunService(string configPath) { Logging.Setup(); logger.Info("Barsik monitoring service starting"); registers = new RegistersContainer(); var server = new ModbusServer(registers); units = new List(); var configParser = new FileIniDataParser(); if (string.IsNullOrEmpty(configPath)) { string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); configPath = Path.Combine(assemblyFolder, "barsik.ini"); } var data = configParser.ReadFile(configPath); foreach (var section in data.Sections) { if (section.SectionName.StartsWith("Unit")) { var unit = BaseMonitoringUnit.Create(section, registers); if (unit != null) { units.Add(unit); Thread unitThread = new Thread(new ThreadStart(unit.Run)); unitThread.Start(); } } } logger.Info("Configuration loaded, we have " + units.Count + " units"); server.Port = int.Parse(data["Server"]["Port"]); server.Run(); logger.Info("Exiting"); } } }