protected override async Task ExecuteAsync(CancellationToken stoppingToken) { int counter = 0; _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); while (!stoppingToken.IsCancellationRequested) { try { if (counter > 0) { using (HttpClient client = new HttpClient()) { var res = client.GetStringAsync(PiHoleURL).Result; var jsonDoc = JsonDocument.Parse(Encoding.Default.GetBytes(res.ToString())); DomainBlocked = jsonDoc.RootElement.GetProperty("domains_being_blocked").GetString(); //.Dump("Domains Blocked"); DNSQueryToday = jsonDoc.RootElement.GetProperty("dns_queries_today").GetString(); //.Dump("DNS Queries Today"); AdsBlocked = jsonDoc.RootElement.GetProperty("ads_blocked_today").GetString(); //.Dump("Ads Blocked"); AdsBlockedPercentage = jsonDoc.RootElement.GetProperty("ads_percentage_today").GetString(); //.Dump("Ads Percentage"); } if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Ad Domans: {DomainBlocked,9}"); _logger.LogTrace($"DNS Qry2D: {DNSQueryToday,9}"); _logger.LogTrace($"Ads Blokd: {AdsBlocked,9}"); _logger.LogTrace($"Ads Blok%: {$"{AdsBlockedPercentage}%",9}"); } _lcd.CursorLine(LiquidCrystal_I2C.LINE1); _lcd.PrintLine($"Ad Domans: {DomainBlocked,9}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE2); _lcd.PrintLine($"DNS Qry2D: {DNSQueryToday,9}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE3); _lcd.PrintLine($"Ads Blokd: {AdsBlocked,9}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE4); _lcd.PrintLine($"Ads Blok%: {$"{AdsBlockedPercentage}%",9}"); if (counter != 5) { counter++; } else { counter = -1; } await Task.Delay(1000, stoppingToken); } else { LastResult = _dHT.RetrieveSensorData(); //dhtData.Dump("DHData"); if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace($"Temp C : { LastResult.Temperature,4:#0.00}°C"); _logger.LogTrace($"Humidity : { LastResult.Humidity,4:P0}%"); _logger.LogTrace($"Temp F : { LastResult.Temperature,4:#0.00}°F"); _logger.LogTrace($"Heat Index : { LastResult.Humidity,4:#0.00}"); } _lcd.CursorLine(LiquidCrystal_I2C.LINE1); _lcd.PrintLine($"Temp C : { LastResult.Temperature,6:#0.00}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE2); _lcd.PrintLine($"Temp F : { LastResult.TemperatureFarenheight,6:#0.00}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE3); _lcd.PrintLine($"Humidity : { LastResult.Humidity,6:P0}"); _lcd.CursorLine(LiquidCrystal_I2C.LINE4); _lcd.PrintLine($"Heat Index : { LastResult.HeatIndex,6:#0.00}"); if (counter != -5) { counter--; } else { counter = 1; } await Task.Delay(2000, stoppingToken); } } catch (Exception ex) { if (ex is TaskCanceledException) { _logger.LogInformation("Worker stopping at: {time}", DateTimeOffset.Now); _ = $"Domains Blocked : {DomainBlocked} | DNS Queries Today : {DNSQueryToday} | Ads Blocked : {AdsBlocked} | Ads Percentage {AdsBlockedPercentage}".Dump("Last Statistics"); _ = $"Temp C : {LastResult.Temperature} | Temp F : {LastResult.TemperatureFarenheight} | Humidity : {LastResult.Humidity:P0} | Heat Index : {LastResult.HeatIndex}".Dump("Last Temp & Humidity"); } else { _logger.LogError("Worker error at: {time}", DateTimeOffset.Now); _ = $"Domains Blocked : {DomainBlocked} | DNS Queries Today : {DNSQueryToday} | Ads Blocked : {AdsBlocked} | Ads Percentage {AdsBlockedPercentage}".Dump("Last Statistics"); _ = $"Temp C : {LastResult.Temperature} | Temp F : {LastResult.TemperatureFarenheight} | Humidity : {LastResult.Humidity:P0} | Heat Index : {LastResult.HeatIndex}".Dump("Last Temp & Humidity"); ex.Dump(); await Task.Delay(2000, stoppingToken); } } } }
/// <summary> /// Retrieves the sensor data. /// </summary> /// <returns>The event arguments that will be read from the sensor.</returns> public DHT11Data RetrieveSensorData() { // Prepare buffer to store measure and checksum var data = new byte[5]; // Start to communicate with sensor // Inform sensor that must finish last execution and put it's state in idle _dataPin.PinMode = GpioPinDriveMode.Output; // Send request to transmission from board to sensor _dataPin.Write(GpioPinValue.Low); Pi.Timing.SleepMicroseconds(PullDownMicroseconds); _dataPin.Write(GpioPinValue.High); // Wait for sensor response _dataPin.PinMode = GpioPinDriveMode.Input; try { // Read acknowledgement from sensor _dataPin.WaitForValue(GpioPinValue.Low, 50); _dataPin.WaitForValue(GpioPinValue.High, 50); // Begins data transmission _dataPin.WaitForValue(GpioPinValue.Low, 50); // Read 40 bits to acquire: // 16 bit -> Humidity // 16 bit -> Temperature // 8 bit -> Checksum var stopwatch = new HighResolutionTimer(); for (var i = 0; i < 40; i++) { stopwatch.Reset(); _dataPin.WaitForValue(GpioPinValue.High, 50); stopwatch.Start(); _dataPin.WaitForValue(GpioPinValue.Low, 50); stopwatch.Stop(); data[i / 8] <<= 1; // Check if signal is 1 or 0 if (stopwatch.ElapsedMicroseconds > BitPulseMidMicroseconds) { data[i / 8] |= 1; } } // End transmission _dataPin.WaitForValue(GpioPinValue.High, 50); var _validData = IsDataValid(data) ? new DHT11Data(_humid: ((data[0] + (data[1] * 0.1)) / 100.0), _temp: (data[2] + ((data[3] & 0x0f) * 0.1))) : new DHT11Data(); if (_validData.IsInitialized) { _lastResult = _validData; } // Compute the checksum return(_lastResult); } catch { return(new DHT11Data() { Humidity = 0.0, Temperature = 0.0 }); } }