mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 04:34:56 +00:00
Working HTTP Server and Client communicating with codec. Need to fix issue with deserializing configuration.xml upon connection (null ref exception).
This commit is contained in:
@@ -3,7 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharp.Net.Https;
|
||||
using Crestron.SimplSharp.Net.Http;
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
using Crestron.SimplSharp.CrestronXml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Cisco_One_Button_To_Push;
|
||||
using Cisco_SX80_Corporate_Phone_Book;
|
||||
@@ -13,6 +15,8 @@ using PepperDash.Essentials.Core;
|
||||
|
||||
namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
{
|
||||
enum eCommandType { SessionStart, SessionEnd, Command, GetStatus, GetConfiguration };
|
||||
|
||||
public class CiscoCodec : VideoCodecBase
|
||||
{
|
||||
public IBasicCommunication Communication { get; private set; }
|
||||
@@ -23,49 +27,254 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
|
||||
private Corporate_Phone_Book PhoneBook;
|
||||
|
||||
private HttpsClient Client;
|
||||
private CiscoCodecConfiguration.Configuration CodecConfiguration;
|
||||
|
||||
private CiscoCodecStatus.Status CodecStatus;
|
||||
|
||||
private HttpClient Client;
|
||||
|
||||
private HttpApiServer Server;
|
||||
|
||||
private int ServerPort;
|
||||
|
||||
private string CodecUrl;
|
||||
|
||||
private string HttpSessionId;
|
||||
|
||||
private string FeedbackRegistrationExpression;
|
||||
|
||||
// Constructor for IBasicCommunication
|
||||
public CiscoCodec(string key, string name, IBasicCommunication comm)
|
||||
public CiscoCodec(string key, string name, IBasicCommunication comm, int serverPort)
|
||||
: base(key, name)
|
||||
{
|
||||
Communication = comm;
|
||||
Communication.TextReceived += new EventHandler<GenericCommMethodReceiveTextArgs>(Communication_TextReceived);
|
||||
|
||||
ServerPort = serverPort;
|
||||
|
||||
CodecObtp = new CiscoOneButtonToPush();
|
||||
|
||||
PhoneBook = new Corporate_Phone_Book();
|
||||
|
||||
Client = new HttpsClient();
|
||||
CodecConfiguration = new CiscoCodecConfiguration.Configuration();
|
||||
|
||||
CodecStatus = new CiscoCodecStatus.Status();
|
||||
|
||||
Client = new HttpClient();
|
||||
|
||||
Server = new HttpApiServer();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the HTTP feedback server and syncronizes state of codec
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool CustomActivate()
|
||||
{
|
||||
Debug.Console(1, this, "Starting Cisco API Server");
|
||||
|
||||
Server.Start(8080);
|
||||
Server.Start(ServerPort);
|
||||
|
||||
Server.ApiRequest += new EventHandler<Crestron.SimplSharp.Net.Http.OnHttpRequestArgs>(Server_ApiRequest);
|
||||
|
||||
CodecUrl = string.Format("http://{0}", (Communication as GenericSshClient).Hostname);
|
||||
|
||||
CommunicationMonitor = new GenericCommunicationMonitor(this, Communication, 2000, 120000, 300000, "xStatus SystemUnit Software Version\r");
|
||||
DeviceManager.AddDevice(CommunicationMonitor);
|
||||
|
||||
CodecObtp.Initialize();
|
||||
Client = new HttpClient();
|
||||
|
||||
CodecObtp.GetMeetings();
|
||||
Client.Verbose = true;
|
||||
Client.KeepAlive = true;
|
||||
|
||||
|
||||
// Temp feedback registration
|
||||
|
||||
FeedbackRegistrationExpression =
|
||||
"<Command><HttpFeedback><Register command=\"True\"><FeedbackSlot>1</FeedbackSlot>" +
|
||||
string.Format("<ServerUrl>http://{0}:{1}/cisco/api</ServerUrl>", CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0), ServerPort) +
|
||||
"<Format>JSON</Format>" +
|
||||
"<Expression item=\"1\">/Configuration</Expression>" +
|
||||
"<Expression item=\"2\">/Event/CallDisconnect</Expression>" +
|
||||
"<Expression item=\"3\">/Status/Call</Expression>" +
|
||||
"</Register>" +
|
||||
"</HttpFeedback>" +
|
||||
"</Command>";
|
||||
|
||||
StartHttpsSession();
|
||||
|
||||
//CodecObtp.Initialize();
|
||||
|
||||
//CodecObtp.GetMeetings();
|
||||
|
||||
PhoneBook.DownloadPhoneBook(Corporate_Phone_Book.ePhoneBookLocation.Corporate);
|
||||
|
||||
return base.CustomActivate();
|
||||
}
|
||||
|
||||
private void StartHttpsSession()
|
||||
{
|
||||
SendHttpCommand("", eCommandType.SessionStart);
|
||||
}
|
||||
|
||||
private void EndHttpsSession()
|
||||
{
|
||||
SendHttpCommand("", eCommandType.SessionEnd);
|
||||
}
|
||||
|
||||
private void SendHttpCommand(string command, eCommandType commandType)
|
||||
{
|
||||
HttpClientRequest request = new HttpClientRequest();
|
||||
|
||||
string urlSuffix = null;
|
||||
|
||||
Client.UserName = null;
|
||||
Client.Password = null;
|
||||
|
||||
request.RequestType = RequestType.Post;
|
||||
|
||||
if(!string.IsNullOrEmpty(HttpSessionId))
|
||||
request.Header.SetHeaderValue("Cookie", HttpSessionId);
|
||||
|
||||
switch (commandType)
|
||||
{
|
||||
case eCommandType.Command:
|
||||
{
|
||||
urlSuffix = "/putxml";
|
||||
request.ContentString = command;
|
||||
request.Header.SetHeaderValue("Content-Type", "text/xml");
|
||||
break;
|
||||
}
|
||||
case eCommandType.SessionStart:
|
||||
{
|
||||
|
||||
urlSuffix = "/xmlapi/session/begin";
|
||||
|
||||
Client.UserName = (Communication as GenericSshClient).Username;
|
||||
Client.Password = (Communication as GenericSshClient).Password;
|
||||
|
||||
break;
|
||||
}
|
||||
case eCommandType.SessionEnd:
|
||||
{
|
||||
urlSuffix = "/xmlapi/session/end";
|
||||
request.Header.SetHeaderValue("Cookie", HttpSessionId);
|
||||
break;
|
||||
}
|
||||
case eCommandType.GetStatus:
|
||||
{
|
||||
request.RequestType = RequestType.Get;
|
||||
urlSuffix = "/status.xml";
|
||||
break;
|
||||
}
|
||||
case eCommandType.GetConfiguration:
|
||||
{
|
||||
request.RequestType = RequestType.Get;
|
||||
urlSuffix = "/configuration.xml";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var requestUrl = CodecUrl + urlSuffix;
|
||||
request.Header.RequestVersion = "HTTP/1.1";
|
||||
request.Url.Parse(requestUrl);
|
||||
|
||||
Debug.Console(1, this, "Sending HTTP request to Cisco Codec at {0}\nHeader:\n{1}\nContent:\n{2}", requestUrl, request.Header, request.ContentString);
|
||||
|
||||
Client.DispatchAsync(request, PostConnectionCallback);
|
||||
}
|
||||
|
||||
void PostConnectionCallback(HttpClientResponse resp, HTTP_CALLBACK_ERROR err)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (resp != null)
|
||||
{
|
||||
if (resp.Code == 200)
|
||||
{
|
||||
Debug.Console(1, this, "Http Post to Cisco Codec Successful. Code: {0}\nContent: {1}", resp.Code, resp.ContentString);
|
||||
|
||||
if (resp.ContentString.IndexOf("<HttpFeedbackRegisterResult status=\"OK\">") > 1)
|
||||
{
|
||||
// Get the initial configruation for sync purposes
|
||||
SendHttpCommand("", eCommandType.GetConfiguration);
|
||||
|
||||
// Get the initial status for sync purposes
|
||||
SendHttpCommand("", eCommandType.GetStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
if (resp.ContentString.IndexOf("</Configuration>") > -1)
|
||||
{
|
||||
CodecConfiguration = CrestronXMLSerialization.DeSerializeObject<CiscoCodecConfiguration.Configuration>(resp.ContentString);
|
||||
|
||||
Debug.Console(1, this, "Product Name: {0} Software Version: {1} ApiVersion: {2}", CodecConfiguration.Product, CodecConfiguration.Version, CodecConfiguration.ApiVersion);
|
||||
}
|
||||
else if (resp.ContentString.IndexOf("</Status>") > -1)
|
||||
{
|
||||
CodecStatus = CrestronXMLSerialization.DeSerializeObject<CiscoCodecStatus.Status>(resp.ContentString);
|
||||
|
||||
Debug.Console(1, this, "Product Name: {0} Software Version: {1} ApiVersion: {2} Volume: {3}", CodecStatus.Product, CodecStatus.Version, CodecStatus.ApiVersion, CodecStatus.Audio.Volume);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.Console(1, this, "Error Deserializing feedback from codec: {0}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (resp.Code == 204)
|
||||
{
|
||||
Debug.Console(1, this, "Response Code: {0}\nHeader:\n{1}Content:\n{1}", resp.Code, resp.Header, resp.ContentString);
|
||||
|
||||
HttpSessionId = resp.Header.GetHeaderValue("Set-Cookie");
|
||||
//var chunks = HttpSessionId.Split(';');
|
||||
//HttpSessionId = chunks[0];
|
||||
//HttpSessionId = HttpSessionId.Substring(HttpSessionId.IndexOf("=") + 1);
|
||||
|
||||
|
||||
// Register for feedbacks once we have a valid session
|
||||
SendHttpCommand(FeedbackRegistrationExpression, eCommandType.Command);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Console(1, this, "Response Code: {0}\nHeader:\n{1}Content:\n{1}Err:\n{2}", resp.Code, resp.Header, resp.ContentString, err);
|
||||
}
|
||||
}
|
||||
else
|
||||
Debug.Console(1, this, "Null response received from server");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.Console(1, this, "Error Initializing HTTPS Client: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
void Server_ApiRequest(object sender, Crestron.SimplSharp.Net.Http.OnHttpRequestArgs e)
|
||||
{
|
||||
Debug.Console(1, this, "Api Reqeust from Codec: {0}", e.Request.ContentString);
|
||||
e.Response.Code = 200;
|
||||
e.Response.ContentString = "HelloWorld";
|
||||
e.Response.ContentString = "OK";
|
||||
|
||||
try
|
||||
{
|
||||
// Serializer settings. We want to ignore null values and mising members
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings();
|
||||
settings.NullValueHandling = NullValueHandling.Ignore;
|
||||
settings.MissingMemberHandling = MissingMemberHandling.Ignore;
|
||||
|
||||
if (e.Request.ContentString.IndexOf("\"Configuration\":{") > -1)
|
||||
CodecConfiguration = JsonConvert.DeserializeObject<CiscoCodecConfiguration.Configuration>(e.Request.ContentString, settings);
|
||||
else if (e.Request.ContentString.IndexOf("\"Status\":{") > -1)
|
||||
CodecStatus = JsonConvert.DeserializeObject<CiscoCodecStatus.Status>(e.Request.ContentString, settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.Console(1, this, "Error Deserializing feedback from codec: {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
void Communication_TextReceived(object sender, GenericCommMethodReceiveTextArgs e)
|
||||
@@ -80,6 +289,8 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
|
||||
protected override Func<bool> InCallFeedbackFunc { get { return () => false; } }
|
||||
|
||||
protected override Func<bool> IncomingCallFeedbackFunc { get { return () => false; } }
|
||||
|
||||
protected override Func<bool> TransmitMuteFeedbackFunc { get { return () => false; } }
|
||||
|
||||
protected override Func<bool> ReceiveMuteFeedbackFunc { get { return () => false; } }
|
||||
@@ -96,6 +307,16 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
|
||||
}
|
||||
|
||||
public override void AcceptCall()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void RejectCall()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void ReceiveMuteOff()
|
||||
{
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
if (a == eProgramStatusEventType.Stopping)
|
||||
{
|
||||
HttpServer.Close();
|
||||
Debug.Console(1, "Shutting down HTTP, port {0}", HttpServer.Port);
|
||||
Debug.Console(1, "Shutting down HTTP Server on port {0}", HttpServer.Port);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -78,7 +78,7 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
|
||||
string path = Uri.UnescapeDataString(args.Request.Path);
|
||||
var host = args.Request.DataConnection.RemoteEndPointAddress;
|
||||
string authToken;
|
||||
//string authToken;
|
||||
|
||||
Debug.Console(2, "HTTP Request: {2}: Path='{0}' ?'{1}'", path, args.Request.QueryString, host);
|
||||
|
||||
@@ -89,27 +89,6 @@ namespace PepperDash.Essentials.Devices.VideoCodec.Cisco
|
||||
if (ApiRequest != null)
|
||||
ApiRequest(this, args);
|
||||
}
|
||||
// Basic file handling
|
||||
else
|
||||
{
|
||||
// Default path
|
||||
//if (path.EndsWith("/")) path = path + "index.html";
|
||||
//if (string.IsNullOrEmpty(Path.GetExtension(path))) path = path + "/index.html";
|
||||
|
||||
//string filePath = path.Replace('/', '\\');
|
||||
//string localPath = string.Format(@"{0}{1}", HtmlRoot, filePath);
|
||||
//if (File.Exists(localPath))
|
||||
//{
|
||||
// args.Response.Header.ContentType = GetContentType(new FileInfo(localPath).Extension);
|
||||
// args.Response.ContentStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
|
||||
// //args.Response.CloseStream = true;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// args.Response.ContentString = string.Format("Not found: '{0}'", filePath);
|
||||
// args.Response.Code = 404;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetContentType(string extension)
|
||||
|
||||
@@ -24,8 +24,10 @@ namespace PepperDash.Essentials.Devices.VideoCodec
|
||||
#endregion
|
||||
|
||||
public BoolFeedback InCallFeedback { get; protected set; }
|
||||
public BoolFeedback IncomingCallFeedback { get; protected set; }
|
||||
|
||||
abstract protected Func<bool> InCallFeedbackFunc { get; }
|
||||
abstract protected Func<bool> IncomingCallFeedbackFunc { get; }
|
||||
abstract protected Func<bool> TransmitMuteFeedbackFunc { get; }
|
||||
abstract protected Func<bool> ReceiveMuteFeedbackFunc { get; }
|
||||
abstract protected Func<bool> PrivacyModeFeedbackFunc { get; }
|
||||
@@ -34,6 +36,7 @@ namespace PepperDash.Essentials.Devices.VideoCodec
|
||||
: base(key, name)
|
||||
{
|
||||
InCallFeedback = new BoolFeedback(InCallFeedbackFunc);
|
||||
IncomingCallFeedback = new BoolFeedback(IncomingCallFeedbackFunc);
|
||||
ReceiveMuteIsOnFeedback = new BoolFeedback(ReceiveMuteFeedbackFunc);
|
||||
TransmitMuteIsOnFeedback = new BoolFeedback(TransmitMuteFeedbackFunc);
|
||||
PrivacyModeIsOnFeedback = new BoolFeedback(PrivacyModeFeedbackFunc);
|
||||
@@ -56,6 +59,8 @@ namespace PepperDash.Essentials.Devices.VideoCodec
|
||||
|
||||
public abstract void Dial();
|
||||
public abstract void EndCall();
|
||||
public abstract void AcceptCall();
|
||||
public abstract void RejectCall();
|
||||
|
||||
public virtual List<Feedback> Feedbacks
|
||||
{
|
||||
@@ -64,6 +69,7 @@ namespace PepperDash.Essentials.Devices.VideoCodec
|
||||
return new List<Feedback>
|
||||
{
|
||||
InCallFeedback,
|
||||
IncomingCallFeedback,
|
||||
ReceiveMuteIsOnFeedback,
|
||||
TransmitMuteIsOnFeedback,
|
||||
PrivacyModeIsOnFeedback
|
||||
|
||||
Reference in New Issue
Block a user