using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using PepperDash.Essentials.Core.Presets; using System; using System.Collections.Generic; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Represents a DevicePresetsModelMessenger /// public class DevicePresetsModelMessenger : MessengerBase { private readonly ITvPresetsProvider _presetsDevice; public DevicePresetsModelMessenger(string key, string messagePath, ITvPresetsProvider presetsDevice) : base(key, messagePath, presetsDevice as Device) { _presetsDevice = presetsDevice; } private void SendPresets() { PostStatusMessage(new PresetStateMessage { Favorites = _presetsDevice.TvPresets.PresetsList }); } 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(); } catch (Exception ex) { Debug.LogMessage(ex, "Exception sending preset full status", this); } }); 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 { [JsonProperty("preset")] /// /// Gets or sets the Preset /// public PresetChannel Preset; [JsonProperty("deviceKey")] /// /// Gets or sets the DeviceKey /// public string DeviceKey; } /// /// Represents a PresetStateMessage /// public class PresetStateMessage : DeviceStateMessageBase { [JsonProperty("favorites", NullValueHandling = NullValueHandling.Ignore)] /// /// Gets or sets the Favorites /// public List Favorites { get; set; } = new List(); } }