using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PepperDash.Core.Logging; using PepperDash.Essentials.Core; using PepperDash.Essentials.Devices.Common.Codec; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Messenger for devices implementing /// public class IHasCallHistoryMessenger : MessengerBase { private readonly IHasCallHistory _callHistory; /// /// Initializes a new instance of the class. /// public IHasCallHistoryMessenger(string key, string messagePath, EssentialsDevice device) : base(key, messagePath, device) { _callHistory = device as IHasCallHistory ?? throw new ArgumentException("device must implement IHasCallHistory", nameof(device)); _callHistory.CallHistory.RecentCallsListHasChanged += CallHistory_RecentCallsListHasChanged; } /// protected override void RegisterActions() { base.RegisterActions(); AddAction("/fullStatus", (id, content) => PostCallHistory()); AddAction("/getCallHistory", (id, content) => PostCallHistory()); } private void CallHistory_RecentCallsListHasChanged(object sender, EventArgs e) { try { if (!(sender is CodecCallHistory codecCallHistory)) return; var recents = codecCallHistory.RecentCalls; if (recents != null) { PostStatusMessage(new IHasCallHistoryStateMessage { RecentCalls = recents }); } } catch (Exception ex) { this.LogError(ex, "Error posting call history"); } } private void PostCallHistory() { try { var recents = _callHistory.CallHistory.RecentCalls; if (recents != null) { PostStatusMessage(new IHasCallHistoryStateMessage { RecentCalls = recents, }); } } catch (Exception ex) { this.LogError(ex, "Error posting call history"); } } } /// /// State message for /// /// public class IHasCallHistoryStateMessage : DeviceStateMessageBase { /// /// Gets or sets the list of recent calls. Null if unknown or not applicable. /// [JsonProperty("recentCalls", NullValueHandling = NullValueHandling.Ignore)] public List RecentCalls { get; set; } /// /// Gets or sets a value indicating whether the device has call history functionality. Null if unknown or not applicable. /// [JsonProperty("hasRecents", NullValueHandling = NullValueHandling.Ignore)] public bool? HasRecents { get; set; } = true; // Since this messenger should only be used for devices with call history, default to true unless specified otherwise. } }