public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { var token = deviceToken.Description.Trim('<', '>').Replace(" ", ""); if (token != Config.DeviceToken) { Config.DeviceToken = token; var cmd = new RegisterDeviceCommand { DeviceToken = token, User = Config.Person }; Task.Run(() => ResilientCall.ExecuteWithRetry(() => _azureIoTHub.InvokeMethod("RegisterAppForPushNotifications", JsonConvert.SerializeObject(cmd)))); } }
public async Task <ActiveDing[]> PollActiveDings(CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(_authenticationToken)) { var tokenSuccess = await GetRingToken(cancellationToken); if (tokenSuccess) { return(await PollActiveDings(cancellationToken)); } return(new List <ActiveDing>().ToArray()); } try { var req = await ResilientCall.ExecuteWithRetry( async() => await _restClient.GetAsync(string.Format(ActiveDingsEndpointUrl, _authenticationToken), cancellationToken), retryCount : 1 ); if (req.Outcome != OutcomeType.Successful) { if (req.FinalHandledResult.StatusCode == HttpStatusCode.Unauthorized) { var tokenSuccess = await GetRingToken(cancellationToken); if (tokenSuccess) { return(await PollActiveDings(cancellationToken)); } } Log.Error(req.FinalException, "Error getting active dings"); return(new List <ActiveDing>().ToArray()); } var resp = await req.Result.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <ActiveDing[]>(resp, Config.JsonSettings)); } catch (Exception ex) { Log.Error(ex, "Error getting active dings"); return(new List <ActiveDing>().ToArray()); } }
private async Task <bool> InvokeMethodWithResponseState(string method, bool currentState) { AppCenterHelper.Track($"SecurityPageModel - {method}"); var response = await ResilientCall.ExecuteWithRetry( () => _azureIoTHub.InvokeMethod(method)); if (response.Outcome == OutcomeType.Successful && response.Result.Status == 200) { var stateResponse = JsonConvert.DeserializeObject <StateResponse>(response.Result.ResponsePayload); return(stateResponse.State); } // Error ShowError(); AppCenterHelper.Error(method, response.FinalException); return(currentState); }
private async Task <bool> GetRingToken(CancellationToken cancellationToken) { try { var response = await ResilientCall.ExecuteWithRetry( async() => { var request = new HttpRequestMessage(HttpMethod.Post, SessionEndpointUrl) { Content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("device[os]", "ios"), new KeyValuePair <string, string>("device[hardware_id]", Config.RingHardwareId), new KeyValuePair <string, string>("api_version", "9") }) }; request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Config.RingBasicAuth); return(await _restClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken)); }, retryCount : 1 ); if (response.Outcome != OutcomeType.Successful) { Log.Error(response.FinalException, "Error getting ring token"); return(false); } var resp = await response.Result.Content.ReadAsStringAsync(); var res = JsonConvert.DeserializeObject <Session>(resp, Config.JsonSettings); _authenticationToken = res.Profile.AuthenticationToken; return(true); } catch (Exception ex) { Log.Error(ex, "Error getting ring token"); return(false); } }
public async Task UpdateWeatherData(CancellationToken cancellationToken) { int LightsOffMinutesUnder30(TimeSpan timeSpan) { return(timeSpan.Minutes > 30 ? timeSpan.Minutes - 30 : timeSpan.Minutes); } bool LightsOffEnabled(TimeSpan sunset1, TimeSpan sunrise1) { return(_clock.Now.Within(sunset1, sunrise1)); } bool ShouldLightsBeOn(TimeSpan sunset) { return(_clock.Now.TimeSpan() > sunset); } try { var response = await ResilientCall.ExecuteWithRetry( async() => await _restClient.GetAsync(_openWeatherApiUrl, cancellationToken) ); if (response.Outcome != OutcomeType.Successful) { Log.Error(response.FinalException, "Error getting weather data"); return; } var content = await response.Result.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <OpenWeatherResult>(content, Config.JsonSettings); var sunrise = new TimedEvent(result.Sys.Sunrise.LocalTimeSpanFromUnixTime(), true); var sunset = new TimedEvent(result.Sys.Sunset.LocalTimeSpanFromUnixTime(), true); var lightsOff = new TimedEvent(new TimeSpan(00, LightsOffMinutesUnder30(sunset.TriggerTime), 00), LightsOffEnabled(sunset.TriggerTime, sunrise.TriggerTime)); if (ShouldLightsBeOn(sunset.TriggerTime)) { _lightsController.OnSunset(); } var eventsToUpdate = new ConcurrentDictionary <Enums.TimedEvent, TimedEvent>(); var onSunriseEvent = eventsToUpdate.AddOrUpdate(Enums.TimedEvent.OnSunrise, sunrise, (key, oldValue) => sunrise); var onSunsetEvent = eventsToUpdate.AddOrUpdate(Enums.TimedEvent.OnSunset, sunset, (key, oldValue) => sunset); var onLightsOffEvent = eventsToUpdate.AddOrUpdate(Enums.TimedEvent.OnLightsOff, lightsOff, (key, oldValue) => lightsOff); foreach (var evnt in eventsToUpdate) { _timerEvents.UpdateEvent(evnt.Key, evnt.Value); } await _mediator.Send(new UpdateReportedPropertiesCommand(new TwinCollection { [nameof(Enums.TimedEvent.OnSunrise)] = onSunriseEvent, [nameof(Enums.TimedEvent.OnSunset)] = onSunsetEvent, [nameof(Enums.TimedEvent.OnLightsOff)] = onLightsOffEvent }), cancellationToken); } catch (Exception ex) { Log.Error(ex, "Error updating weather data"); } }