Ejemplo n.º 1
0
        private async void BlinkLed()
        {
            if (!_controllerOptions.StatusLedGpio.HasValue)
            {
                return;
            }

            var led = _pi2GpioService.GetOutput(_controllerOptions.StatusLedGpio.Value);

            led.Write(BinaryState.Low);

            var ledState = false;

            while (true)
            {
                if (ledState)
                {
                    led.Write(BinaryState.High);
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
                else
                {
                    led.Write(BinaryState.Low);
                    await Task.Delay(TimeSpan.FromMilliseconds(200));
                }

                ledState = !ledState;
            }
        }
Ejemplo n.º 2
0
        public HealthService(
            IConfigurationService configurationService,
            IController controller,
            ITimerService timerService,
            IGpioService gpioService,
            IDateTimeService dateTimeService,
            ISystemInformationService systemInformationService,
            ILogService logService)
        {
            if (configurationService == null)
            {
                throw new ArgumentNullException(nameof(configurationService));
            }
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }
            if (timerService == null)
            {
                throw new ArgumentNullException(nameof(timerService));
            }
            if (systemInformationService == null)
            {
                throw new ArgumentNullException(nameof(systemInformationService));
            }

            _log = logService?.CreatePublisher(nameof(HealthService)) ?? throw new ArgumentNullException(nameof(logService));

            var startupTimestamp = dateTimeService.Now;

            systemInformationService.Set("Health/SystemTime", () => dateTimeService.Now);
            systemInformationService.Set("Health/TimerDuration/Average", () => _averageTimerDuration);
            systemInformationService.Set("Health/TimerDuration/AverageMax", () => _maxTimerDuration);
            systemInformationService.Set("Health/TimerDuration/AverageMin", () => _minTimerDuration);
            systemInformationService.Set("Health/StartupTimestamp", startupTimestamp);
            systemInformationService.Set("Health/UpTime", () => dateTimeService.Now - startupTimestamp);
            systemInformationService.Set("Log/Errors/Count", () => logService.ErrorsCount);
            systemInformationService.Set("Log/Warnings/Count", () => logService.WarningsCount);

            controller.StartupCompleted += (sender, args) =>
            {
                _log.Info("Startup completed after " + args.Duration);
                systemInformationService.Set("Health/StartupDuration", args.Duration);
            };

            controller.StartupFailed += (sender, args) =>
            {
                _log.Error(args.Exception, "Startup failed after " + args.Duration);
            };

            timerService.Tick += UpdateStatistics;

            var configuration = configurationService.GetConfiguration <HealthServiceConfiguration>("HealthServiceConfiguration");

            if (configuration.StatusLed.Gpio.HasValue)
            {
                var gpio = gpioService.GetOutput(configuration.StatusLed.Gpio.Value);
                if (configuration.StatusLed.IsInverted)
                {
                    gpio = gpio.WithInvertedState();
                }

                var statusLed = new StatusLed(gpio);
                timerService.Tick += (s, e) => statusLed.Update();
            }
        }