Ejemplo n.º 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);

                _logger.LogInformation($"{nameof(SampleWorker)} started!");

                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);

                        if (_sessionService.IsActive())
                        {
                            var sessionId = _sessionService.GetSessionId();
                            if (sessionId.HasValue)
                            {
                                var sampleRequest = new CreateSampleForSessionRequest
                                {
                                    IsSaunaPowered    = await _gpioService.IsSaunaOn(),
                                    IsInfraredPowered = await _gpioService.IsInfraredOn(),
                                    Temperature       = await _gpioService.ReadTemperature(),
                                    TimeStamp         = DateTime.UtcNow
                                };

                                await _sampleClient.CreateSampleForSession(sessionId.Value, sampleRequest);

                                _logger.LogInformation("Sample for active session sent.");
                            }
                            else
                            {
                                _logger.LogInformation("No active session.");
                            }
                        }
                        else
                        {
                            _logger.LogInformation("No active session.");
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // This is most likely due to the Task.Delay being cancelled.
                    }
                }

                _logger.LogInformation($"{nameof(SampleWorker)} stopped!");
            }
            catch (Exception ex)
            {
                _logger.LogError($"{nameof(SampleWorker)} throws Exception: {ex.Message}!");
                await _logService.LogException(
                    "SessionWorker throws Exception!",
                    "SessionWorker throws Exception!", ex);
            }
        }
Ejemplo n.º 2
0
        public override async Task <GetTemperatureResponse> GetTemperature(GetTemperatureRequest request, ServerCallContext context)
        {
            _logger.LogInformation("Temperature requested by gRPC!");

            await _gpioService.Initialize();

            var temperature = await _gpioService.ReadTemperature();

            return(new GetTemperatureResponse
            {
                Temperature = temperature
            });
        }
Ejemplo n.º 3
0
        public async Task UpdateSession(GetActiveSessionResponse activeSession)
        {
            SetSessionId(activeSession.SessionId);

            // Read the current temperature inside the sauna booth.
            var temperature = await _gpioService.ReadTemperature();

            // If a sauna session should be active and the sauna GPIO is not turned on...
            if (activeSession.IsSauna && !await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires sauna but sauna is off!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                // If the current temperature is below the temperature goal...
                if (temperature < activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Sauna should be turned on!");
                    await _gpioService.TurnSaunaOn();
                }
            }

            // If a sauna session should be active and the sauna GPIO is turned on...
            if (activeSession.IsSauna && await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires sauna and sauna is on!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                // If the current temperature is equal or higher then the temperature goal...
                if (temperature >= activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Sauna should be turned off!");
                    await _gpioService.TurnSaunaOff();
                }
            }

            // If a sauna session should not be active and the sauna GPIO is turned on...
            if (!activeSession.IsSauna && await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires no sauna and sauna is on!");
                _logger.LogInformation("Sauna should be turned off!");
                await _gpioService.TurnSaunaOff();
            }

            // If an infrared session should be active and the infrared GPIO is not turned on...
            if (activeSession.IsInfrared && !await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires infrared but infrared is off!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                // If the current temperature is lower then the temperature goal...
                if (temperature < activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Infrared should be turned on!");
                    await _gpioService.TurnInfraredOn();
                }
            }

            // If an infrared session should be active and the sauna GPIO is turned on...
            if (activeSession.IsInfrared && await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires infrared and infrared is on!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                // If the current temperature is equal or higher then the temperature goal...
                if (temperature >= activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Infrared should be turned off!");
                    await _gpioService.TurnInfraredOff();
                }
            }

            // If an infrared session should not be active and the sauna GPIO is turned on...
            if (!activeSession.IsInfrared && await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires no infrared and infrared is on!");
                _logger.LogInformation("Infrared should be turned off!");
                await _gpioService.TurnInfraredOff();
            }

            // If a sauna session should be active and the infrared boost is not turned on...
            if (activeSession.IsSauna && temperature < 50 && !await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires sauna and can benefit from infrared boost!");
                await _gpioService.TurnInfraredOn();
            }

            // If a sauna session should be active and the infrared boost is turned on...
            if (activeSession.IsSauna && temperature >= 50 && await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires sauna and should stop boosting from infrared!");
                await _gpioService.TurnInfraredOff();
            }
        }
Ejemplo n.º 4
0
        public async Task UpdateSession(GetActiveSessionResponse activeSession)
        {
            SetSessionId(activeSession.SessionId);

            var temperature = await _gpioService.ReadTemperature();

            if (activeSession.IsSauna && !await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires sauna but sauna is off!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                if (temperature < activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Sauna should be turned on!");
                    await _gpioService.TurnSaunaOn();
                }
            }

            if (activeSession.IsSauna && await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires sauna and sauna is on!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                if (temperature >= activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Sauna should be turned off!");
                    await _gpioService.TurnSaunaOff();
                }
            }

            if (!activeSession.IsSauna && await _gpioService.IsSaunaOn())
            {
                _logger.LogInformation("Active session requires no sauna and sauna is on!");
                _logger.LogInformation("Sauna should be turned off!");
                await _gpioService.TurnSaunaOff();
            }

            if (activeSession.IsInfrared && !await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires infrared but infrared is off!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                if (temperature < activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Infrared should be turned on!");
                    await _gpioService.TurnInfraredOn();
                }
            }

            if (activeSession.IsInfrared && await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires infrared and infrared is on!");
                _logger.LogInformation($"Temperature goal is {activeSession.TemperatureGoal} and actual temperature is {temperature}.");

                if (temperature >= activeSession.TemperatureGoal)
                {
                    _logger.LogInformation("Infrared should be turned off!");
                    await _gpioService.TurnInfraredOff();
                }
            }

            if (!activeSession.IsInfrared && await _gpioService.IsInfraredOn())
            {
                _logger.LogInformation("Active session requires no infrared and infrared is on!");
                _logger.LogInformation("Infrared should be turned off!");
                await _gpioService.TurnInfraredOff();
            }
        }