/// <summary> /// Refreshes the current Denon status. /// </summary> /// <param name="publishChanges">Indiactes if changes should be published or not.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task RefreshStatusAsync(bool publishChanges = false) { // Make all of the calls to get current status var status = new MainStatus(); var mainZoneStatusTask = _httpClient.GetDenonMainZoneStatusAsync(); var secondaryZoneStatusTask = _httpClient.GetAllDenonSecondaryZonesStatusAsync(1); await Task.WhenAll(mainZoneStatusTask, secondaryZoneStatusTask).ConfigureAwait(false); var mainZoneStatus = await mainZoneStatusTask.ConfigureAwait(false); var secondaryZoneStatus = await secondaryZoneStatusTask.ConfigureAwait(false); status.Power = mainZoneStatus.Power.Value.ToUpper() == "ON"; status.Volume = 80 + decimal.Parse(mainZoneStatus.MasterVolume.Value); status.Mute = mainZoneStatus.Mute.Value.ToUpper() == "ON"; status.Input = mainZoneStatus.InputFuncSelect.Value; status.SurroundMode = mainZoneStatus.SurrMode.Value; foreach (var zone in secondaryZoneStatus) { var statusLite = new StatusLite { Power = zone.Value.Power.Value.ToUpper() == "ON", Volume = 80 + decimal.Parse(zone.Value.MasterVolume.Value), Mute = zone.Value.Mute.Value.ToUpper() == "ON", Input = zone.Value.InputFuncSelect.Value }; status.SecondaryZoneStatus.Add($"ZONE{zone.Key}", statusLite); } // Initialize state if (_status == null) { _status = status; } // Compare to current cached status var updates = CompareStatusObjects(_status, status); // If updated, publish changes if (publishChanges && updates.Count > 0) { foreach (var update in updates) { EventReceived?.Invoke(this, new CommandEventArgs(update)); } _status = status; } }
/// <summary> /// Compares twomaster state objects. /// </summary> /// <param name="status1">First status.</param> /// <param name="status2">Second status.</param> /// <returns>List of changes.</returns> private IList <Command> CompareStatusObjects(MainStatus status1, MainStatus status2) { var updates = new List <Command>(); // Power: PW if (status1.Power != status2.Power) { updates.Add(new PowerCommand { Value = status2.Power ? "ON" : "OFF" }); } // Volume: MV if (status1.Volume != status2.Volume) { updates.Add(new VolumeCommand { Value = status2.Volume.ToString() }); } // Mute: MU if (status1.Mute != status2.Mute) { updates.Add(new MuteCommand { Value = status2.Mute ? "ON" : "OFF" }); } // Input: SI if (status1.Input != status2.Input) { updates.Add(new InputCommand { Value = status2.Input }); } // Surround mode: MS if (status1.SurroundMode != status2.SurroundMode) { updates.Add(new SurroundModeCommand { Value = status2.SurroundMode }); } foreach (var zone in status1.SecondaryZoneStatus) { var zoneNumber = int.Parse(zone.Key.Substring(4)); // Zone power: Z# if (status1.SecondaryZoneStatus[zone.Key].Power != status2.SecondaryZoneStatus[zone.Key].Power) { updates.Add(new ZonePowerCommand { ZoneId = zoneNumber, Value = status2.SecondaryZoneStatus[zone.Key].Power ? "ON" : "OFF" }); } // Zone input: Z# if (status1.SecondaryZoneStatus[zone.Key].Input != status2.SecondaryZoneStatus[zone.Key].Input) { updates.Add(new ZoneInputCommand { ZoneId = zoneNumber, Value = status2.SecondaryZoneStatus[zone.Key].Input }); } // Zone volume: Z# if (status1.SecondaryZoneStatus[zone.Key].Volume != status2.SecondaryZoneStatus[zone.Key].Volume) { updates.Add(new ZoneVolumeCommand { ZoneId = zoneNumber, Value = status2.SecondaryZoneStatus[zone.Key].Volume.ToString() }); } // Zone input: Z#MU if (status1.SecondaryZoneStatus[zone.Key].Mute != status2.SecondaryZoneStatus[zone.Key].Mute) { updates.Add(new ZoneMuteCommand { ZoneId = zoneNumber, Value = status2.SecondaryZoneStatus[zone.Key].Mute ? "ON" : "OFF" }); } } return(updates); }