public void Run(IBackgroundTaskInstance taskInstance) { var id = SystemIdentification.GetSystemIdForPublisher().Id; _rpiSensorId = CryptographicBuffer.EncodeToBase64String(id); var deviceInfo = new EasClientDeviceInformation(); var friendlyName = deviceInfo.FriendlyName; // Get the toilet settings for the specific toilet. ToiletSettings toiletSettings = GetToiletSettings(_rpiSensorId, friendlyName); _toiletId = toiletSettings.ToiletId; _updateFrequency = toiletSettings.UpdateFrequency; _fanMode = toiletSettings.FanMode; _fanThreshold = toiletSettings.FanThreshold; _connection = new HubConnectionBuilder() .WithUrl("[REDACTED]") .Build(); _connection.Closed += async error => { await Task.Delay(new Random().Next(0, 5) * 1000); await _connection.StartAsync(); }; Connect(); StartUart(); Task.Run(new Action(SendDataToPubHub)); Task.Run(new Action(UploadSensorValuesToInfluxDb)); while (true) { try { // Read the sensors values. ReadDistanceSensors(); ProcessDistanceReadings(); CheckButtonPressed(); ReadGasValue(); ControlFan(); } catch (Exception ex) { Debug.WriteLine("An error occurred while reading the sensors values: " + ex.Message); } } }
private static ToiletSettings GetToiletSettings(string sensorId, string friendlyName) { var toiletSettings = new ToiletSettings(); var uri = new Uri( $"{Url}/api/ToiletSettings/GetToiletSettings?sensorId={System.Web.HttpUtility.UrlEncode(sensorId)}&name={friendlyName}"); Debug.WriteLine($"Attempting to fetch toilet settings from {uri.AbsoluteUri}"); try { var response = HttpClient.GetStringAsync(uri).Result; Debug.WriteLine($"Response from URL:\n{response}"); toiletSettings = JsonConvert.DeserializeObject <ToiletSettings>(response); } catch (Exception e) { Debug.WriteLine($"Exception: {e.Message}"); Debug.WriteLine($"Stack Trace: {e.StackTrace}"); } return(toiletSettings); }