using System; using System.Collections.Generic; using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using PepperDash.Essentials.Core.Presets; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Represents a DevicePresetsModelMessenger /// public class DevicePresetsModelMessenger : MessengerBase { private readonly ITvPresetsProvider _presetsDevice; /// /// Constructor for DevicePresetsModelMessenger /// /// The key. /// The message path. /// The presets device. public DevicePresetsModelMessenger(string key, string messagePath, ITvPresetsProvider presetsDevice) : base(key, messagePath, presetsDevice as Device) { _presetsDevice = presetsDevice; } private void SendPresets(string id = null) { PostStatusMessage(new PresetStateMessage { Favorites = _presetsDevice.TvPresets.PresetsList }, id); } private void RecallPreset(ISetTopBoxNumericKeypad device, string channel) { _presetsDevice.TvPresets.Dial(channel, device); } private void SavePresets(List presets) { _presetsDevice.TvPresets.UpdatePresets(presets); } #region Overrides of MessengerBase /// protected override void RegisterActions() { AddAction("/fullStatus", (id, content) => { this.LogInformation("getting full status for client {id}", id); try { SendPresets(id); } catch (Exception ex) { Debug.LogMessage(ex, "Exception sending preset full status", this); } }); AddAction("/presetsStatus", (id, content) => SendPresets(id)); AddAction("/recall", (id, content) => { var p = content.ToObject(); if (!(DeviceManager.GetDeviceForKey(p.DeviceKey) is ISetTopBoxNumericKeypad dev)) { this.LogDebug("Unable to find device with key {0}", p.DeviceKey); return; } RecallPreset(dev, p.Preset.Channel); }); AddAction("/save", (id, content) => { var presets = content.ToObject>(); SavePresets(presets); }); _presetsDevice.TvPresets.PresetsSaved += (p) => SendPresets(); } #endregion } /// /// Represents a PresetChannelMessage /// public class PresetChannelMessage { /// /// Gets or sets the Preset /// [JsonProperty("preset")] public PresetChannel Preset; /// /// Gets or sets the DeviceKey /// [JsonProperty("deviceKey")] public string DeviceKey; } /// /// Represents a PresetStateMessage /// public class PresetStateMessage : DeviceStateMessageBase { /// /// Gets or sets the Favorites /// [JsonProperty("favorites", NullValueHandling = NullValueHandling.Ignore)] public List Favorites { get; set; } = new List(); } }