using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using System; using System.Collections.Generic; namespace PepperDash.Essentials.AppServer.Messengers { public class IHasInputsMessenger : MessengerBase { private readonly IHasInputs itemDevice; /// /// Constructs a messenger for a device that implements IHasInputs /// /// /// /// public IHasInputsMessenger(string key, string messagePath, IHasInputs device) : base(key, messagePath, device) { itemDevice = device; } protected override void RegisterActions() { base.RegisterActions(); AddAction("/fullStatus", (id, context) => { SendFullStatus(); }); itemDevice.Inputs.ItemsUpdated += (sender, args) => { SendFullStatus(); }; itemDevice.Inputs.CurrentItemChanged += (sender, args) => { SendFullStatus(); }; foreach (var input in itemDevice.Inputs.Items) { var key = input.Key; var localItem = input.Value; AddAction($"/{key}", (id, content) => { localItem.Select(); }); localItem.ItemUpdated += (sender, args) => { SendFullStatus(); }; } } private void SendFullStatus() { try { this.LogInformation("Sending full status"); var stateObject = new IHasInputsStateMessage { Inputs = new Inputs { Items = itemDevice.Inputs.Items, CurrentItem = itemDevice.Inputs.CurrentItem } }; PostStatusMessage(stateObject); } catch (Exception e) { this.LogError("Error sending full status: {0}", e.Message); } } } public class IHasInputsStateMessage : DeviceStateMessageBase { [JsonProperty("inputs")] public Inputs Inputs { get; set; } } public class Inputs { [JsonProperty("items")] public Dictionary Items { get; set; } [JsonProperty("currentItem")] public TKey CurrentItem { get; set; } } }