mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-16 13:15:03 +00:00
- Added support for Node server communication implementing GenericHttpSseClient
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -25,6 +25,18 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
var doubleObj = JObject.Parse(fs.ReadToEnd());
|
var doubleObj = JObject.Parse(fs.ReadToEnd());
|
||||||
ConfigObject = MergeConfigs(doubleObj).ToObject<EssentialsConfig>();
|
ConfigObject = MergeConfigs(doubleObj).ToObject<EssentialsConfig>();
|
||||||
|
|
||||||
|
// Extract SystemUrl and TemplateUrl
|
||||||
|
|
||||||
|
if (doubleObj["system_url"] != null)
|
||||||
|
{
|
||||||
|
ConfigObject.SystemUrl = doubleObj["system_url"].Value<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (doubleObj["template_url"] != null)
|
||||||
|
{
|
||||||
|
ConfigObject.TemplateUrl= doubleObj["template_url"].Value<string>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using Crestron.SimplSharp.CrestronIO;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using PepperDash.Core;
|
using PepperDash.Core;
|
||||||
using PepperDash.Essentials.Core.Config;
|
using PepperDash.Essentials.Core.Config;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace PepperDash.Essentials
|
namespace PepperDash.Essentials
|
||||||
{
|
{
|
||||||
@@ -13,6 +14,33 @@ namespace PepperDash.Essentials
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class EssentialsConfig : BasicConfig
|
public class EssentialsConfig : BasicConfig
|
||||||
{
|
{
|
||||||
|
public string SystemUrl { get; set; }
|
||||||
|
public string TemplateUrl { get; set; }
|
||||||
|
|
||||||
|
public string SystemUuid
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var result = Regex.Match(SystemUrl, @"https?:\/\/.*\/templates\/(.*)#.*");
|
||||||
|
|
||||||
|
string uuid = result.Groups[1].Value;
|
||||||
|
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string TemplateUuid
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var result = Regex.Match(TemplateUrl, @"https?:\/\/.*\/templates\/(.*)#.*");
|
||||||
|
|
||||||
|
string uuid = result.Groups[1].Value;
|
||||||
|
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[JsonProperty("rooms")]
|
[JsonProperty("rooms")]
|
||||||
public List<EssentialsRoomConfig> Rooms { get; private set; }
|
public List<EssentialsRoomConfig> Rooms { get; private set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
"</Parameters>\n" +
|
"</Parameters>\n" +
|
||||||
"</RequestAction>\n";
|
"</RequestAction>\n";
|
||||||
|
|
||||||
Debug.Console(1, this, "Sending Fusion ActionRequest: \n{0}", actionRequest);
|
Debug.Console(2, this, "Sending Fusion ActionRequest: \n{0}", actionRequest);
|
||||||
|
|
||||||
FusionRoom.ExtenderFusionRoomDataReservedSigs.ActionQuery.StringValue = actionRequest;
|
FusionRoom.ExtenderFusionRoomDataReservedSigs.ActionQuery.StringValue = actionRequest;
|
||||||
}
|
}
|
||||||
@@ -336,21 +336,12 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
{
|
{
|
||||||
DateTime now = DateTime.Today;
|
DateTime now = DateTime.Today;
|
||||||
|
|
||||||
//string currentTime = string.Format("Current time: {0:D4}-{1:D2}-{2:D2}T{3:D2}:{4:D2}:{5:D2}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
|
|
||||||
|
|
||||||
string currentTime = now.ToString("s");
|
string currentTime = now.ToString("s");
|
||||||
|
|
||||||
Debug.Console(1, this, "Current time: {0}", currentTime);
|
|
||||||
|
|
||||||
//Debug.Console(1, this, "Current time: {0}", now.ToString("d"));
|
|
||||||
|
|
||||||
//string requestTest =
|
|
||||||
// string.Format("<RequestSchedule><RequestID>{0}</RequestID><RoomID>{1}</RoomID><Start>2017-05-02T00:00:00</Start><HourSpan>24</HourSpan></RequestSchedule>", requestID, GUID);
|
|
||||||
|
|
||||||
string requestTest =
|
string requestTest =
|
||||||
string.Format("<RequestSchedule><RequestID>FullSchedleRequest</RequestID><RoomID>{0}</RoomID><Start>{1}</Start><HourSpan>24</HourSpan></RequestSchedule>", RoomGuid, currentTime);
|
string.Format("<RequestSchedule><RequestID>FullSchedleRequest</RequestID><RoomID>{0}</RoomID><Start>{1}</Start><HourSpan>24</HourSpan></RequestSchedule>", RoomGuid, currentTime);
|
||||||
|
|
||||||
Debug.Console(1, this, "Sending Fusion ScheduleQuery: \n{0}", requestTest);
|
Debug.Console(2, this, "Sending Fusion ScheduleQuery: \n{0}", requestTest);
|
||||||
|
|
||||||
FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.ScheduleQuery.StringValue = requestTest;
|
FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.ScheduleQuery.StringValue = requestTest;
|
||||||
|
|
||||||
@@ -465,7 +456,7 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
"</Event>" +
|
"</Event>" +
|
||||||
"</CreateSchedule>";
|
"</CreateSchedule>";
|
||||||
|
|
||||||
Debug.Console(1, this, "Sending CreateMeeting Request: \n{0}", createMeetingRequest);
|
Debug.Console(2, this, "Sending CreateMeeting Request: \n{0}", createMeetingRequest);
|
||||||
|
|
||||||
FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.CreateMeeting.StringValue = createMeetingRequest;
|
FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.CreateMeeting.StringValue = createMeetingRequest;
|
||||||
|
|
||||||
@@ -482,7 +473,7 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
void ExtenderFusionRoomDataReservedSigs_DeviceExtenderSigChange(DeviceExtender currentDeviceExtender, SigEventArgs args)
|
void ExtenderFusionRoomDataReservedSigs_DeviceExtenderSigChange(DeviceExtender currentDeviceExtender, SigEventArgs args)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
Debug.Console(2, this, "Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
||||||
|
|
||||||
|
|
||||||
if (args.Sig == FusionRoom.ExtenderFusionRoomDataReservedSigs.ActionQueryResponse)
|
if (args.Sig == FusionRoom.ExtenderFusionRoomDataReservedSigs.ActionQueryResponse)
|
||||||
@@ -578,7 +569,7 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
void FusionRoomSchedule_DeviceExtenderSigChange(DeviceExtender currentDeviceExtender, SigEventArgs args)
|
void FusionRoomSchedule_DeviceExtenderSigChange(DeviceExtender currentDeviceExtender, SigEventArgs args)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Scehdule Response Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
Debug.Console(2, this, "Scehdule Response Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
||||||
|
|
||||||
|
|
||||||
if (args.Sig == FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.ScheduleResponse)
|
if (args.Sig == FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.ScheduleResponse)
|
||||||
@@ -674,7 +665,7 @@ namespace PepperDash.Essentials.Fusion
|
|||||||
}
|
}
|
||||||
else if (args.Sig == FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.CreateResponse)
|
else if (args.Sig == FusionRoom.ExtenderRoomViewSchedulingDataReservedSigs.CreateResponse)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Create Meeting Response Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
Debug.Console(2, this, "Create Meeting Response Event: {0}\n Sig: {1}\nFusionResponse:\n{2}", args.Event, args.Sig.Name, args.Sig.StringValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -14,77 +14,134 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
public class CotijaInterfaceController : Device
|
public class CotijaInterfaceController : Device
|
||||||
{
|
{
|
||||||
StreamReader Reader;
|
GenericHttpSseClient SseClient;
|
||||||
|
|
||||||
Crestron.SimplSharp.Net.Connection Connection;
|
CCriticalSection FileLock;
|
||||||
|
|
||||||
|
string ServerUrl;
|
||||||
|
|
||||||
public CotijaInterfaceController(string key) : base(key)
|
public CotijaInterfaceController(string key) : base(key)
|
||||||
{
|
{
|
||||||
CrestronConsole.AddNewConsoleCommand(InitializeClientAndEventStream, "InitializeHttpClient", "Initializes a new HTTP client connection to a specified URL", ConsoleAccessLevelEnum.AccessOperator);
|
CrestronConsole.AddNewConsoleCommand(RegisterRoomToServer, "InitializeHttpClient", "Initializes a new HTTP client connection to a specified URL", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InitializeClientAndEventStream(string url)
|
/// <summary>
|
||||||
|
/// Registers the room with the server
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">URL of the server, including the port number, if not 80. Format: "serverUrlOrIp:port"</param>
|
||||||
|
void RegisterRoomToServer(string url)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpClient webClient = new HttpClient();
|
ServerUrl = url;
|
||||||
webClient.Verbose = true;
|
|
||||||
HttpClientRequest request = new HttpClientRequest();
|
|
||||||
request.Url = new UrlParser(url);
|
|
||||||
request.Header.AddHeader(new HttpHeader("Accept", "text/event-stream"));
|
|
||||||
request.KeepAlive = true;
|
|
||||||
|
|
||||||
/// Experimenting with grabbing connection before the request
|
string filePath = string.Format(@"\NVRAM\Program{0}\configurationFile.json", Global.ControlSystem.ProgramNumber);
|
||||||
Connection = webClient.ConnectNew("192.168.1.120", 3000);
|
string postBody = null;
|
||||||
Debug.Console(1, this, "Connection Port: {0}", Connection.LocalEndPointPort);
|
|
||||||
Reader = new StreamReader(Connection);
|
|
||||||
Connection.OnBytesReceived += new EventHandler(DataConnection_OnBytesReceived);
|
|
||||||
|
|
||||||
/// Not sure if this is starting a new connection
|
if (string.IsNullOrEmpty(filePath))
|
||||||
webClient.DispatchAsync(request, StreamConnectionCallback);
|
{
|
||||||
|
Debug.Console(0, this, "Error reading file. No path specified.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileLock = new CCriticalSection();
|
||||||
|
|
||||||
|
if (FileLock.TryEnter())
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Reading Configuration File");
|
||||||
|
|
||||||
|
postBody = File.ReadToEnd(filePath, Encoding.ASCII);
|
||||||
|
|
||||||
|
Debug.Console(2, this, "{0}", postBody);
|
||||||
|
|
||||||
|
FileLock.Leave();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(postBody))
|
||||||
|
{
|
||||||
|
Debug.Console(1, "Post Body is null or empty");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HttpClient Client = new HttpClient();
|
||||||
|
|
||||||
|
HttpClientRequest Request = new HttpClientRequest();
|
||||||
|
|
||||||
|
Client.Verbose = true;
|
||||||
|
Client.KeepAlive = true;
|
||||||
|
|
||||||
|
string uuid = Essentials.ConfigReader.ConfigObject.SystemUuid;
|
||||||
|
|
||||||
|
url = string.Format("http://{0}/api/system/join/{1}", ServerUrl, uuid);
|
||||||
|
|
||||||
|
Request.Url.Parse(url);
|
||||||
|
Request.RequestType = RequestType.Post;
|
||||||
|
Request.Header.SetHeaderValue("Content-Type", "application/json");
|
||||||
|
Request.ContentString = postBody;
|
||||||
|
|
||||||
|
Client.DispatchAsync(Request, PostConnectionCallback);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Error Initializing Cotija client:\n{0}", e);
|
Debug.Console(0, this, "Error Initilizing Room: {0}", e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This callback happens but none of the events would fire (when not commented out)
|
}
|
||||||
void StreamConnectionCallback(HttpClientResponse resp, HTTP_CALLBACK_ERROR err)
|
|
||||||
|
/// <summary>
|
||||||
|
/// The callback that fires when we get a response from our registration attempt
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resp"></param>
|
||||||
|
/// <param name="err"></param>
|
||||||
|
void PostConnectionCallback(HttpClientResponse resp, HTTP_CALLBACK_ERROR err)
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "Connection Port: {0}", resp.DataConnection.LocalEndPointPort);
|
try
|
||||||
Debug.Console(1, this, "Connections are equal {0}", resp.DataConnection == Connection);
|
|
||||||
Debug.Console(1, this, "Callback Fired");
|
|
||||||
//resp.DataConnection.OnBytesReceived += new EventHandler(DataConnection_OnBytesReceived);
|
|
||||||
//Debug.Console(1, this, "Received: '{0}'", resp.ContentString);
|
|
||||||
//resp.OnTransferEnd += new Crestron.SimplSharp.Net.TransferEndEventHandler(resp_OnTransferEnd);
|
|
||||||
//resp.OnTransferProgress += new Crestron.SimplSharp.Net.TransferProgressEventHandler(resp_OnTransferProgress);
|
|
||||||
//resp.OnTransferStart += new Crestron.SimplSharp.Net.TransferStartEventHandler(resp_OnTransferStart);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We could never get this event handler to fire
|
|
||||||
void DataConnection_OnBytesReceived(object sender, EventArgs e)
|
|
||||||
{
|
{
|
||||||
Debug.Console(1, this, "OnBytesReceived Event Fired.");
|
if (resp.Code == 200)
|
||||||
Debug.Console(1, this, "Event: Received: '{0}'", Reader.ReadToEnd());
|
{
|
||||||
|
Debug.Console(0, this, "Initializing SSE Client.");
|
||||||
|
|
||||||
|
if (SseClient == null)
|
||||||
|
{
|
||||||
|
SseClient = new GenericHttpSseClient(string.Format("{0}-SseClient", Key), Name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (SseClient.IsConnected)
|
||||||
|
{
|
||||||
|
SseClient.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
//void resp_OnTransferStart(object sender, Crestron.SimplSharp.Net.TransferStartEventArgs e)
|
string uuid = Essentials.ConfigReader.ConfigObject.SystemUuid;
|
||||||
//{
|
|
||||||
// Debug.Console(1, this, "OnTransferStart");
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void resp_OnTransferProgress(object sender, Crestron.SimplSharp.Net.TransferProgressEventArgs e)
|
SseClient.Url = string.Format("http://{0}/api/system/stream/{1}", ServerUrl, uuid);
|
||||||
//{
|
|
||||||
// Debug.Console(1, this, "OnTransferProgress");
|
SseClient.Connect();
|
||||||
//}
|
}
|
||||||
|
|
||||||
|
CommunicationGather LineGathered = new CommunicationGather(SseClient, "\x0d\x0a");
|
||||||
|
|
||||||
|
LineGathered.LineReceived += new EventHandler<GenericCommMethodReceiveTextArgs>(LineGathered_LineReceived);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Unable to initialize SSE Client");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Error Initializeing SSE Client: {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineGathered_LineReceived(object sender, GenericCommMethodReceiveTextArgs e)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Received from Node Server: '{0}'", e.Text);
|
||||||
|
}
|
||||||
|
|
||||||
//void resp_OnTransferEnd(object sender, Crestron.SimplSharp.Net.TransferEndEventArgs e)
|
|
||||||
//{
|
|
||||||
// Debug.Console(1, this, "OnTransferEnd");
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user