Ejemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            // Read the configuration file
            IConfiguration configuration = new ConfigurationBuilder()
                                           .SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
                                           .AddJsonFile(APPSETTINGS_FILENAME, optional: false, reloadOnChange: true)
                                           .Build();

            // Create an HttpClient that doesn't validate the server certificate
            HttpClientHandler customHttpClientHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return(true); }
            };

            TelemetryConfiguration telemetryConfig = AppInsights.GetTelemetryConfiguration(configuration);

            using (AppInsights.InitializeDependencyTracking(telemetryConfig))
                using (AppInsights.InitializePerformanceTracking(telemetryConfig))
                    using (HttpClient _httpClient = new HttpClient(customHttpClientHandler))
                    {
                        _telemetryClient = new TelemetryClient(telemetryConfig);

                        // Abstraction representing the home automation system
                        _hub = new Hubitat(configuration, _httpClient);

                        // Start the MQTT service, if applicable.
                        MqttOptions mqttOptions = configuration.GetSection("MQTT").Get <MqttOptions>();
                        if (mqttOptions?.Enabled ?? false)
                        {
                            _mqtt = new MqttService(await MqttClientFactory.GetClient(mqttOptions), mqttOptions, _hub);
                            await _mqtt.Start();
                        }

                        // Class to manage long-running tasks
                        _taskManager = new AutomationTaskManager(configuration);

                        // Bind a method to handle the events raised
                        // by the Hubitat device
                        _hub.AutomationEvent += Hub_AutomationEvent;
                        var hubTask = _hub.StartAutomationEventWatcher();

                        // Wait forever, this is a daemon process
                        await hubTask;
                    }
        }