Esempio n. 1
0
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return(Host.CreateDefaultBuilder(args)
                   .ConfigureHostConfiguration(configHost =>
            {
                configHost.AddEnvironmentVariables();
                configHost.AddJsonFile("appsettings.json", true);
            })
                   .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService <ModbusWorker>();
                services.AddHostedService <WeatherWorker>();

                services.AddSingleton <IModbusLogger, ModbusLogger>();
                services.AddSingleton(serviceProvider =>
                {
                    var config = serviceProvider.GetRequiredService <IConfiguration>();

                    var configuration = new ModbusConfiguration();

                    config.GetSection("Modbus").Bind(configuration);

                    return configuration;
                });
                services.AddSingleton(serviceProvider =>
                {
                    var config = serviceProvider.GetRequiredService <IConfiguration>();

                    var configuration = new WeatherConfiguration();

                    config.GetSection("Weather").Bind(configuration);

                    return configuration;
                });

                services.AddSingleton <IModbusFactory>(serviceProvider =>
                {
                    var deviceConfiguration = serviceProvider.GetRequiredService <ModbusConfiguration>();

                    if (deviceConfiguration.Verbose)
                    {
                        var modbusLogger = serviceProvider.GetRequiredService <IModbusLogger>();

                        return new ModbusFactory(logger: modbusLogger);
                    }

                    return new ModbusFactory();
                });

                //We're not supposed to use a subject for this. I'm being lazy.
                var conditionsSubject = new Subject <PwsObservationsResponse>();

                services.AddSingleton(conditionsSubject);
                services.AddSingleton <IObservable <PwsObservationsResponse> >(conditionsSubject);

                //Device Services
                services.AddSingleton <DeviceService, TcpDeviceService>();
                services.AddSingleton <DeviceService, UdpDeviceService>();
            }));
        }
Esempio n. 2
0
 public ModbusWorker(
     ILogger <ModbusWorker> logger,
     ModbusConfiguration configuration,
     IEnumerable <DeviceService> deviceServices)
 {
     _logger         = logger;
     _configuration  = configuration;
     _deviceServices = deviceServices.ToArray();
 }