private async Task <bool> NotifyTemperatureIfNeeded(INotifyTemperatureProvider[] notifyTemperatureProviders) { double?temperature = await _thermometerService.GetTempratureAsync(); if (!temperature.HasValue) { _logger.LogError($"thermometerService failed"); return(false); } double?previouslyKnownTemperature = ThermalNotifierServiceTemperatureHistory.LastKnownTemperature; ThermalNotifierServiceTemperatureHistory.LastKnownTemperature = temperature; INotifyTemperatureProvider notifyTemperatureProvider = FindNotificationProvider(notifyTemperatureProviders, temperature.Value, previouslyKnownTemperature); if (notifyTemperatureProvider == null) { _logger.LogDebug($"requestTemperature succeeded, but temperature is OK: {temperature}℃"); return(true); } var notificationRequestUrlBuilder = new UriBuilder(SlackEndpoint); string encodedMessage = UrlEncoder.Default.Encode(notifyTemperatureProvider.GenerateMessage(temperature.Value)); notificationRequestUrlBuilder.Query = $"payload={encodedMessage}"; bool success = await _slackNotifierService.NotifyAsync(SlackEndpoint, encodedMessage); return(true); }
private async Task <bool> NotifyTemperatureIfNeeded(INotifyTemperatureProvider[] notifyTemperatureProviders) { string requestTemperatureUrl = "https://localhost:7001/Thermometer"; HttpResponseMessage responseTemperature = await _httpClient.GetAsync(requestTemperatureUrl); if (responseTemperature == null || responseTemperature.StatusCode != HttpStatusCode.OK) { _logger.LogError($"{requestTemperatureUrl} failed with status {responseTemperature?.StatusCode}"); return(false); } string temperatureText = await responseTemperature.Content.ReadAsStringAsync(); if (!double.TryParse(temperatureText, out double temperature)) { _logger.LogError($"{requestTemperatureUrl} failed with non-double temperature {temperatureText}"); return(false); } double?previouslyKnownTemperature = ThermalNotifierServiceTemperatureHistory.LastKnownTemperature; ThermalNotifierServiceTemperatureHistory.LastKnownTemperature = temperature; INotifyTemperatureProvider notifyTemperatureProvider = FindNotificationProvider(notifyTemperatureProviders, temperature, previouslyKnownTemperature); if (notifyTemperatureProvider == null) { _logger.LogDebug($"{requestTemperatureUrl} succeeded, but temperature is OK: {temperature}℃"); return(true); } string requestNotificationUrl = "https://localhost:6001/SlackNotifier"; var notificationRequestUrlBuilder = new UriBuilder(requestNotificationUrl); string encodedMessage = UrlEncoder.Default.Encode(notifyTemperatureProvider.GenerateMessage(temperature)); notificationRequestUrlBuilder.Query = $"message={encodedMessage}"; HttpResponseMessage responseNotification = await _httpClient.GetAsync(notificationRequestUrlBuilder.ToString()); if (responseNotification == null || responseNotification.StatusCode != HttpStatusCode.OK) { _logger.LogError($"{requestNotificationUrl} failed with status {responseNotification?.StatusCode}"); return(false); } return(true); }