using System; using System.Collections.Generic; using Newtonsoft.Json; using PepperDash.Core; using PepperDash.Core.Logging; using PepperDash.Essentials.Core.DeviceTypeInterfaces; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Represents a IHasInputsMessenger /// 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(id)); AddAction("/inputStatus", (id, content) => SendFullStatus(id)); 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(string id = null) { try { this.LogInformation("Sending full status"); var stateObject = new IHasInputsStateMessage { Inputs = new Inputs { Items = itemDevice.Inputs.Items, CurrentItem = itemDevice.Inputs.CurrentItem } }; PostStatusMessage(stateObject, id); } catch (Exception e) { this.LogError("Error sending full status: {0}", e.Message); } } } /// /// Represents a IHasInputsStateMessage /// public class IHasInputsStateMessage : DeviceStateMessageBase { [JsonProperty("inputs")] /// /// Gets or sets the Inputs /// public Inputs Inputs { get; set; } } /// /// Represents a Inputs /// public class Inputs { [JsonProperty("items")] public Dictionary Items { get; set; } [JsonProperty("currentItem")] /// /// Gets or sets the CurrentItem /// public TKey CurrentItem { get; set; } } }