public static void Main(string[] args) { var isService = args.Contains(RunAsServiceArg); var builder = CreateWebHostBuilder(args.Where(a => a != RunAsServiceArg).ToArray()); if (isService) { var pathToExe = Process.GetCurrentProcess().MainModule.FileName; var pathToContentRoot = Path.GetDirectoryName(pathToExe); builder.UseContentRoot(pathToContentRoot); } _context = new ApplicationContext(); InitControllers(); LightsService.Init(); ModbusControllerAdapter.InvokePollingFinished(); var host = builder.Build(); if (isService) { host.RunAsService(); } else { host.Run(); } }
private static void InitControllers() { if (!_context.Database.CanConnect()) { return; } _controllers = new ConcurrentDictionary <long, IControllerAdapter>(); foreach (var c in _context.Controllers) { try { var controller = new ModbusControllerAdapter(c.Id, c.SlaveAddress, c.Address, c.Port); controller.Init(receiveTimeout, sendTimeout); if (!_controllers.TryAdd(c.Id, controller)) { throw new Exception("Could not add controller to collection"); } } catch { } } }