using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using PepperDash.Core; using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.Routing; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Represents a IHasCurrentSourceInfoMessenger /// public class CurrentSourcesMessenger : MessengerBase { private readonly ICurrentSources sourceDevice; /// /// Initializes a new instance of the class. /// /// The key. /// The message path. /// The device. public CurrentSourcesMessenger(string key, string messagePath, ICurrentSources device) : base(key, messagePath, device as IKeyName) { sourceDevice = device; } /// /// Registers the actions for the messenger. /// protected override void RegisterActions() { base.RegisterActions(); AddAction("/fullStatus", (id, content) => { var message = new CurrentSourcesStateMessage { CurrentSourceKeys = sourceDevice.CurrentSourceKeys, CurrentSources = sourceDevice.CurrentSources }; PostStatusMessage(message); }); sourceDevice.CurrentSourcesChanged += (sender, e) => { PostStatusMessage(JToken.FromObject(new { currentSourceKeys = sourceDevice.CurrentSourceKeys, currentSources = sourceDevice.CurrentSources })); }; } } /// /// Represents a CurrentSourcesStateMessage /// public class CurrentSourcesStateMessage : DeviceStateMessageBase { /// /// Gets or sets the CurrentSourceKey /// [JsonProperty("currentSourceKeys", NullValueHandling = NullValueHandling.Ignore)] public Dictionary CurrentSourceKeys { get; set; } /// /// Gets or sets the CurrentSource /// [JsonProperty("currentSources")] public Dictionary CurrentSources { get; set; } } }