using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PepperDash.Essentials.Devices.Common.Codec; using PepperDash.Essentials.Devices.Common.VideoCodec; namespace PepperDash.Essentials.AppServer.Messengers { /// /// Provides a messaging bridge for a VideoCodecBase /// public class VideoCodecBaseMessenger { /// /// /// public VideoCodecBase Codec { get; private set; } public CotijaSystemController AppServerController { get; private set; } public string MessagePath { get; private set; } /// /// /// /// public VideoCodecBaseMessenger(VideoCodecBase codec, string messagePath) { if (codec == null) throw new ArgumentNullException("codec"); if (string.IsNullOrEmpty(messagePath)) throw new ArgumentException("messagePath must not be empty or null"); MessagePath = messagePath; Codec = codec; codec.CallStatusChange += new EventHandler(codec_CallStatusChange); codec.IsReadyChange += new EventHandler(codec_IsReadyChange); var dirCodec = codec as IHasDirectory; if (dirCodec != null) { dirCodec.DirectoryResultReturned += new EventHandler(dirCodec_DirectoryResultReturned); } } /// /// /// /// /// void dirCodec_DirectoryResultReturned(object sender, DirectoryEventArgs e) { var dir = e.Directory; PostStatusMessage(new { currentDirectory = e.Directory }); } /// /// /// /// /// void codec_IsReadyChange(object sender, EventArgs e) { PostStatusMessage(new { isReady = true }); } /// /// Registers this codec's messaging with an app server controller /// /// public void RegisterWithAppServer(CotijaSystemController appServerController) { if (appServerController == null) throw new ArgumentNullException("appServerController"); AppServerController = appServerController; appServerController.AddAction("/device/videoCodec/isReady", new Action(SendIsReady)); appServerController.AddAction("/device/videoCodec/fullStatus", new Action(SendVtcFullMessageObject)); appServerController.AddAction("/device/videoCodec/dial", new Action(s => Codec.Dial(s))); appServerController.AddAction("/device/videoCodec/endCallById", new Action(s => { var call = GetCallWithId(s); if (call != null) Codec.EndCall(call); })); appServerController.AddAction(MessagePath + "/endAllCalls", new Action(Codec.EndAllCalls)); appServerController.AddAction(MessagePath + "/dtmf", new Action(s => Codec.SendDtmf(s))); appServerController.AddAction(MessagePath + "/rejectById", new Action(s => { var call = GetCallWithId(s); if (call != null) Codec.RejectCall(call); })); appServerController.AddAction(MessagePath + "/acceptById", new Action(s => { var call = GetCallWithId(s); if (call != null) Codec.AcceptCall(call); })); appServerController.AddAction(MessagePath + "/directoryRoot", new Action(GetDirectoryRoot)); appServerController.AddAction(MessagePath + "/directoryById", new Action(s => GetDirectory(s))); appServerController.AddAction(MessagePath + "/directorySearch", new Action(s => DirectorySearch(s))); appServerController.AddAction(MessagePath + "/privacyModeOn", new Action(Codec.PrivacyModeOn)); appServerController.AddAction(MessagePath + "/privacyModeOff", new Action(Codec.PrivacyModeOff)); appServerController.AddAction(MessagePath + "/privacyModeToggle", new Action(Codec.PrivacyModeToggle)); appServerController.AddAction(MessagePath + "/sharingStart", new Action(Codec.StartSharing)); appServerController.AddAction(MessagePath + "/sharingStop", new Action(Codec.StopSharing)); appServerController.AddAction(MessagePath + "/standbyOn", new Action(Codec.StandbyActivate)); appServerController.AddAction(MessagePath + "/standbyOff", new Action(Codec.StandbyDeactivate)); } public void GetFullStatusMessage() { } /// /// Helper to grab a call with string ID /// /// /// CodecActiveCallItem GetCallWithId(string id) { return Codec.ActiveCalls.FirstOrDefault(c => c.Id == id); } /// /// /// /// void DirectorySearch(string s) { var dirCodec = Codec as IHasDirectory; if (dirCodec != null) { dirCodec.SearchDirectory(s); } } /// /// /// /// void GetDirectory(string id) { var dirCodec = Codec as IHasDirectory; if(dirCodec == null) { return; } dirCodec.GetDirectoryFolderContents(id); } /// /// /// void GetDirectoryRoot() { var dirCodec = Codec as IHasDirectory; if (dirCodec == null) { // do something else? return; } if (!dirCodec.PhonebookSyncState.InitialSyncComplete) { PostStatusMessage(new { initialSyncComplete = false }); return; } PostStatusMessage(new { currentDirectory = dirCodec.DirectoryRoot }); } /// /// Handler for codec changes /// void codec_CallStatusChange(object sender, CodecCallStatusItemChangeEventArgs e) { SendVtcFullMessageObject(); } /// /// /// void SendIsReady() { PostStatusMessage(new { isReady = Codec.IsReady }); } /// /// Helper method to build call status for vtc /// /// void SendVtcFullMessageObject() { if (!Codec.IsReady) { return; } var info = Codec.CodecInfo; PostStatusMessage(new { isInCall = Codec.IsInCall, privacyModeIsOn = Codec.PrivacyModeIsOnFeedback.BoolValue, sharingContentIsOn = Codec.SharingContentIsOnFeedback.BoolValue, sharingSource = Codec.SharingSourceFeedback.StringValue, standbyIsOn = Codec.StandbyIsOnFeedback.StringValue, calls = Codec.ActiveCalls, info = new { autoAnswerEnabled = info.AutoAnswerEnabled, e164Alias = info.E164Alias, h323Id = info.H323Id, ipAddress = info.IpAddress, sipPhoneNumber = info.SipPhoneNumber, sipURI = info.SipUri }, showSelfViewByDefault = Codec.ShowSelfViewByDefault, hasDirectory = Codec is IHasDirectory }); } /// /// Helper for posting status message /// /// The contents of the content object void PostStatusMessage(object contentObject) { AppServerController.SendMessageToServer(JObject.FromObject(new { type = MessagePath, content = contentObject })); } } }