示例#1
0
        /// <summary>
        /// Starts the HTTP listener.
        /// </summary>
        private void StartHttpListener()
        {
            try
            {
                //create http listener
                _logger.WriteMessage("Service", $"Creating HTTP listener..");
                _httpListener = new SimpleHttpListener(_config.ListenPort, _errorHandler);

                //add statistics handler
                _logger.WriteMessage("Service", $"Registering 'Statistics' HTTP handler..");
                StatisticsHandler statisticsHandler = new StatisticsHandler(_errorHandler, _config, _logger, _serviceStats, _statsWriters);
                _httpListener.RegisterHandler("statistics", statisticsHandler);

                //add config handler
                _logger.WriteMessage("Service", $"Registering 'Config' HTTP handler..");
                ConfigHandler configHandler = new ConfigHandler(_errorHandler, _config, _logger, _serviceStats);
                _httpListener.RegisterHandler("config", configHandler);

                //add gpio handler
                _logger.WriteMessage("Service", $"Registering 'GPIO' HTTP handler..");
                GpioHandler gpioHandler = new GpioHandler(_errorHandler, _config, _logger, _serviceStats, _gpio);
                _httpListener.RegisterHandler("gpio", gpioHandler);

                //pre-init
                _logger.WriteMessage("Service", $"Running pre-init handler functions..");
                foreach (IHttpHandler handler in _httpListener.GetHandlers())
                {
                    handler.PreInitialize();
                }

                //start http listener
                _logger.WriteMessage("Service", $"Starting HTTP listener on port {_config.ListenPort}..");
                _httpListener.Start();

                //pre-init
                _logger.WriteMessage("Service", $"Running post-init handler functions..");
                foreach (IHttpHandler handler in _httpListener.GetHandlers())
                {
                    handler.PostInitialize();
                }

                //success
                _logger.WriteMessage("Service", $"HTTP listener successfully connected");

                //state
                _serviceState.SetState(ServiceStateType.Up);
            }
            catch (Exception ex)
            {
                _errorHandler?.LogError(ex);
            }
        }