private async Task CheckForTriggeredEvents(TimeSpan timeSpanNow) { bool TriggeredEvent(KeyValuePair <Enums.TimedEvent, TimedEvent> evt) { return(evt.Value.TriggerTime == timeSpanNow && evt.Value.Enabled); } void DisableEvent(KeyValuePair <Enums.TimedEvent, TimedEvent> evt) { _timerEvents.DisableEvent(evt.Key); } foreach (var evt in _timerEvents.GetEnabledEvents()) { switch (evt.Key) { case Enums.TimedEvent.UpdateSunsetSunrise: if (TriggeredEvent(evt)) { await _mediator.Publish(new UpdateSunriseSunsetCommand()); } break; case Enums.TimedEvent.OnSunset: if (TriggeredEvent(evt)) { _lightsController.OnSunset(); } break; case Enums.TimedEvent.OnLightsOff: if (TriggeredEvent(evt)) { await _lightsController.LightsOff(); } break; case Enums.TimedEvent.OnSunrise: if (TriggeredEvent(evt)) { _lightsController.OnSunrise(); } break; case Enums.TimedEvent.StrobeOff: if (TriggeredEvent(evt)) { _relayBoard.Off(Relay.AlarmStrobe); DisableEvent(evt); } break; case Enums.TimedEvent.SirenOff: if (TriggeredEvent(evt)) { _relayBoard .Relay(Relay.AlarmSiren) .Off(); DisableEvent(evt); } break; case Enums.TimedEvent.GarageDoorOperatedEnd: if (TriggeredEvent(evt)) { Config.GarageDoorOperated = false; DisableEvent(evt); } break; default: break; } } }
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"); } }