mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 20:54:55 +00:00
Working bridge class for room source messages and POST responses to server
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -37,6 +37,8 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
ConfigObject.TemplateUrl= doubleObj["template_url"].Value<string>();
|
ConfigObject.TemplateUrl= doubleObj["template_url"].Value<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
@@ -56,6 +56,12 @@ namespace PepperDash.Essentials
|
|||||||
return new ConsoleCommMockDevice(key, name, props, comm);
|
return new ConsoleCommMockDevice(key, name, props, comm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (typeName == "webserver")
|
||||||
|
{
|
||||||
|
var props = JsonConvert.DeserializeObject<CotijaConfig>(properties.ToString());
|
||||||
|
return new CotijaSystemController(key, name, props);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ namespace PepperDash.Essentials
|
|||||||
public string SystemUrl { get; set; }
|
public string SystemUrl { get; set; }
|
||||||
public string TemplateUrl { get; set; }
|
public string TemplateUrl { get; set; }
|
||||||
|
|
||||||
|
public CotijaConfig Cotija { get; private set; }
|
||||||
|
|
||||||
public string SystemUuid
|
public string SystemUuid
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -53,5 +55,7 @@ namespace PepperDash.Essentials
|
|||||||
public EssentialsConfig System { get; set; }
|
public EssentialsConfig System { get; set; }
|
||||||
|
|
||||||
public EssentialsConfig Template { get; set; }
|
public EssentialsConfig Template { get; set; }
|
||||||
|
|
||||||
|
//public CotijaConfig Cotija { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,8 +29,8 @@ namespace PepperDash.Essentials
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override void InitializeSystem()
|
public override void InitializeSystem()
|
||||||
{
|
{
|
||||||
CrestronConsole.AddNewConsoleCommand(s => GoWithLoad(), "go", "Reloads configuration file",
|
//CrestronConsole.AddNewConsoleCommand(s => GoWithLoad(), "go", "Reloads configuration file",
|
||||||
ConsoleAccessLevelEnum.AccessOperator);
|
// ConsoleAccessLevelEnum.AccessOperator);
|
||||||
//CrestronConsole.AddNewConsoleCommand(s => TearDown(), "ungo", "Reloads configuration file",
|
//CrestronConsole.AddNewConsoleCommand(s => TearDown(), "ungo", "Reloads configuration file",
|
||||||
// ConsoleAccessLevelEnum.AccessOperator);
|
// ConsoleAccessLevelEnum.AccessOperator);
|
||||||
CrestronConsole.AddNewConsoleCommand(s =>
|
CrestronConsole.AddNewConsoleCommand(s =>
|
||||||
@@ -57,11 +57,6 @@ namespace PepperDash.Essentials
|
|||||||
|
|
||||||
//PortalSync = new PepperDashPortalSyncClient();
|
//PortalSync = new PepperDashPortalSyncClient();
|
||||||
|
|
||||||
//Temp Cotija testing
|
|
||||||
CotijaInterfaceController CotijaInterface = new CotijaInterfaceController("WebClient1");
|
|
||||||
|
|
||||||
//CotijaInterface.InitializeClientAndEventStream("http://localhost:3000/api/system/stream/abcdefgh");
|
|
||||||
|
|
||||||
Debug.Console(0, "Starting Essentials load from configuration");
|
Debug.Console(0, "Starting Essentials load from configuration");
|
||||||
ConfigReader.LoadConfig2();
|
ConfigReader.LoadConfig2();
|
||||||
LoadDevices();
|
LoadDevices();
|
||||||
@@ -69,6 +64,10 @@ namespace PepperDash.Essentials
|
|||||||
LoadRooms();
|
LoadRooms();
|
||||||
// FUSION - should go per room I believe. See CreateSystems in original Configuration.cs
|
// FUSION - should go per room I believe. See CreateSystems in original Configuration.cs
|
||||||
// ???
|
// ???
|
||||||
|
|
||||||
|
//Temp Cotija testing
|
||||||
|
//CotijaSystemController CotijaInterface = new CotijaSystemController("WebClient1");
|
||||||
|
|
||||||
DeviceManager.ActivateAll();
|
DeviceManager.ActivateAll();
|
||||||
Debug.Console(0, "Essentials load complete\r" +
|
Debug.Console(0, "Essentials load complete\r" +
|
||||||
"-------------------------------------------------------------");
|
"-------------------------------------------------------------");
|
||||||
@@ -103,6 +102,7 @@ namespace PepperDash.Essentials
|
|||||||
Debug.Console(0, "Tear down COMPLETE");
|
Debug.Console(0, "Tear down COMPLETE");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads all devices from config and adds them to DeviceManager
|
/// Reads all devices from config and adds them to DeviceManager
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -164,6 +164,13 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
Debug.Console(1, "Room is EssentialsHuddleSpaceRoom, attempting to add to DeviceManager with Fusion");
|
Debug.Console(1, "Room is EssentialsHuddleSpaceRoom, attempting to add to DeviceManager with Fusion");
|
||||||
DeviceManager.AddDevice(new EssentialsHuddleSpaceFusionSystemController((EssentialsHuddleSpaceRoom)room, 0xf1));
|
DeviceManager.AddDevice(new EssentialsHuddleSpaceFusionSystemController((EssentialsHuddleSpaceRoom)room, 0xf1));
|
||||||
|
|
||||||
|
var cotija = DeviceManager.GetDeviceForKey("cotijaServer") as CotijaSystemController;
|
||||||
|
|
||||||
|
if (cotija != null)
|
||||||
|
{
|
||||||
|
cotija.CotijaRooms.Add(new CotijaEssentialsHuddleSpaceRoomBridge(cotija, room as EssentialsHuddleSpaceRoom));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -140,6 +140,8 @@
|
|||||||
<Compile Include="HttpApiHandler.cs" />
|
<Compile Include="HttpApiHandler.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="FOR REFERENCE Room\EssentialsRoom.cs" />
|
<Compile Include="FOR REFERENCE Room\EssentialsRoom.cs" />
|
||||||
|
<Compile Include="Room\Cotija\CotijaConfig.cs" />
|
||||||
|
<Compile Include="Room\Cotija\CotijaRoomBridge.cs" />
|
||||||
<Compile Include="Room\EssentialsPresentationRoom.cs" />
|
<Compile Include="Room\EssentialsPresentationRoom.cs" />
|
||||||
<Compile Include="Room\EssentialsRoomBase.cs" />
|
<Compile Include="Room\EssentialsRoomBase.cs" />
|
||||||
<Compile Include="Room\EssentialsRoomConfig.cs" />
|
<Compile Include="Room\EssentialsRoomConfig.cs" />
|
||||||
@@ -164,7 +166,7 @@
|
|||||||
<Compile Include="UI Drivers\SingleSubpageModalAndBackDriver.cs" />
|
<Compile Include="UI Drivers\SingleSubpageModalAndBackDriver.cs" />
|
||||||
<Compile Include="UI Drivers\SmartObjectRoomsList.cs" />
|
<Compile Include="UI Drivers\SmartObjectRoomsList.cs" />
|
||||||
<Compile Include="UI Drivers\UIBoolJoin.cs" />
|
<Compile Include="UI Drivers\UIBoolJoin.cs" />
|
||||||
<Compile Include="UI\CotijaInterfaceController.cs" />
|
<Compile Include="Room\Cotija\CotijaSystemController.cs" />
|
||||||
<Compile Include="UI\DualDisplaySourceSRLController.cs" />
|
<Compile Include="UI\DualDisplaySourceSRLController.cs" />
|
||||||
<Compile Include="UI\SubpageReferenceListActivityItem.cs" />
|
<Compile Include="UI\SubpageReferenceListActivityItem.cs" />
|
||||||
<Compile Include="UI\CrestronTouchpanelPropertiesConfig.cs" />
|
<Compile Include="UI\CrestronTouchpanelPropertiesConfig.cs" />
|
||||||
|
|||||||
Binary file not shown.
14
Essentials/PepperDashEssentials/Room/Cotija/CotijaConfig.cs
Normal file
14
Essentials/PepperDashEssentials/Room/Cotija/CotijaConfig.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Crestron.SimplSharp;
|
||||||
|
using PepperDash.Essentials.Core.Config;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials
|
||||||
|
{
|
||||||
|
public class CotijaConfig : DeviceConfig
|
||||||
|
{
|
||||||
|
public string serverUrl { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,336 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Crestron.SimplSharp;
|
||||||
|
using Crestron.SimplSharp.CrestronIO;
|
||||||
|
using Crestron.SimplSharpPro;
|
||||||
|
using Crestron.SimplSharp.Net.Http;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using PepperDash.Core;
|
||||||
|
using PepperDash.Essentials.Core;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials
|
||||||
|
{
|
||||||
|
public class CotijaSystemController : Device
|
||||||
|
{
|
||||||
|
GenericHttpSseClient SseClient;
|
||||||
|
|
||||||
|
CCriticalSection FileLock;
|
||||||
|
|
||||||
|
CotijaConfig Config;
|
||||||
|
|
||||||
|
HttpClient Client;
|
||||||
|
|
||||||
|
Dictionary<string, Object> ActionDictionary;
|
||||||
|
|
||||||
|
CTimer Heartbeat;
|
||||||
|
|
||||||
|
CTimer Reconnect;
|
||||||
|
|
||||||
|
string SystemUuid;
|
||||||
|
|
||||||
|
public List<CotijaEssentialsHuddleSpaceRoomBridge> CotijaRooms { get; private set; }
|
||||||
|
|
||||||
|
public CotijaSystemController(string key, string name, CotijaConfig config) : base(key, name)
|
||||||
|
{
|
||||||
|
Config = config;
|
||||||
|
|
||||||
|
ActionDictionary = new Dictionary<string, Object>();
|
||||||
|
|
||||||
|
CotijaRooms = new List<CotijaEssentialsHuddleSpaceRoomBridge>();
|
||||||
|
|
||||||
|
CrestronConsole.AddNewConsoleCommand(ConnectSseClient, "InitializeHttpClient", "Initializes a new HTTP client connection to a specified URL", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
|
CrestronConsole.AddNewConsoleCommand(DisconnectSseClient, "CloseHttpClient", "Closes the active HTTP client", ConsoleAccessLevelEnum.AccessOperator);
|
||||||
|
|
||||||
|
AddPostActivationAction(() => RegisterSystemToServer(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddAction(string key, object action)
|
||||||
|
{
|
||||||
|
// This might blow up if an action with that key already exists
|
||||||
|
ActionDictionary.Add(key, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveAction(string key)
|
||||||
|
{
|
||||||
|
if (ActionDictionary.ContainsKey(key))
|
||||||
|
ActionDictionary.Remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReconnectToServer(object o)
|
||||||
|
{
|
||||||
|
RegisterSystemToServer(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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 RegisterSystemToServer(string command)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string filePath = string.Format(@"\NVRAM\Program{0}\configurationFile.json", Global.ControlSystem.ProgramNumber);
|
||||||
|
string postBody = null;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(filePath))
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
Client = new HttpClient();
|
||||||
|
|
||||||
|
HttpClientRequest request = new HttpClientRequest();
|
||||||
|
|
||||||
|
Client.Verbose = true;
|
||||||
|
Client.KeepAlive = true;
|
||||||
|
|
||||||
|
SystemUuid = Essentials.ConfigReader.ConfigObject.SystemUuid;
|
||||||
|
|
||||||
|
string url = string.Format("http://{0}/api/system/join/{1}", Config.serverUrl, SystemUuid);
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Error Initilizing Room: {0}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Posts a message to the server from a room
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="room">room from which the message originates</param>
|
||||||
|
/// <param name="o">object to be serialized and sent in post body</param>
|
||||||
|
public void PostToServer(EssentialsRoomBase room, JObject o)
|
||||||
|
{
|
||||||
|
if(Client == null)
|
||||||
|
Client = new HttpClient();
|
||||||
|
|
||||||
|
HttpClientRequest request = new HttpClientRequest();
|
||||||
|
|
||||||
|
Client.Verbose = true;
|
||||||
|
Client.KeepAlive = true;
|
||||||
|
|
||||||
|
string url = string.Format("http://{0}/api/room/{1}", Config.serverUrl, string.Format("{0}-{1}", SystemUuid, room.Key));
|
||||||
|
|
||||||
|
request.Url.Parse(url);
|
||||||
|
request.RequestType = RequestType.Post;
|
||||||
|
request.Header.SetHeaderValue("Content-Type", "application/json");
|
||||||
|
request.ContentString = o.ToString();
|
||||||
|
|
||||||
|
Client.DispatchAsync(request, PostConnectionCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disconnects the SSE Client and stops the heartbeat timer
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command"></param>
|
||||||
|
void DisconnectSseClient(string command)
|
||||||
|
{
|
||||||
|
if(SseClient != null)
|
||||||
|
SseClient.Disconnect();
|
||||||
|
|
||||||
|
if (Heartbeat != null)
|
||||||
|
{
|
||||||
|
Heartbeat.Stop();
|
||||||
|
|
||||||
|
Heartbeat = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (resp != null && resp.Code == 200)
|
||||||
|
{
|
||||||
|
if (SseClient == null)
|
||||||
|
{
|
||||||
|
ConnectSseClient(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Unable to initialize SSE Client");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Error Initializeing SSE Client: {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes when we don't get a heartbeat message in time. Triggers reconnect.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="o"></param>
|
||||||
|
void HeartbeatExpired(object o)
|
||||||
|
{
|
||||||
|
if (Heartbeat != null)
|
||||||
|
{
|
||||||
|
Heartbeat.Stop();
|
||||||
|
|
||||||
|
Heartbeat = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the reconnect timer
|
||||||
|
Reconnect = new CTimer(ReconnectToServer, null, 5000, 5000);
|
||||||
|
|
||||||
|
Reconnect.Reset(5000, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Connects the SSE Client
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="o"></param>
|
||||||
|
void ConnectSseClient(object o)
|
||||||
|
{
|
||||||
|
Debug.Console(0, this, "Initializing SSE Client.");
|
||||||
|
|
||||||
|
if (SseClient == null)
|
||||||
|
{
|
||||||
|
SseClient = new GenericHttpSseClient(string.Format("{0}-SseClient", Key), Name);
|
||||||
|
|
||||||
|
CommunicationGather LineGathered = new CommunicationGather(SseClient, "\x0d\x0a");
|
||||||
|
|
||||||
|
LineGathered.LineReceived += new EventHandler<GenericCommMethodReceiveTextArgs>(LineGathered_LineReceived);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (SseClient.IsConnected)
|
||||||
|
{
|
||||||
|
SseClient.Disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string uuid = Essentials.ConfigReader.ConfigObject.SystemUuid;
|
||||||
|
|
||||||
|
SseClient.Url = string.Format("http://{0}/api/system/stream/{1}", Config.serverUrl, uuid);
|
||||||
|
|
||||||
|
SseClient.Connect();
|
||||||
|
|
||||||
|
//Heartbeat = new CTimer(HeartbeatExpired, null, 20000, 20000);
|
||||||
|
|
||||||
|
//Heartbeat.Reset(20000, 20000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineGathered_LineReceived(object sender, GenericCommMethodReceiveTextArgs e)
|
||||||
|
{
|
||||||
|
//Debug.Console(1, this, "Received from Server: '{0}'", e.Text);
|
||||||
|
|
||||||
|
if(e.Text.IndexOf("data:") > -1)
|
||||||
|
{
|
||||||
|
var message = e.Text.Substring(6);
|
||||||
|
|
||||||
|
string roomId = null;
|
||||||
|
|
||||||
|
Debug.Console(1, this, "Message: '{0}'", message);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var messageObj = JObject.Parse(message);
|
||||||
|
|
||||||
|
var type = messageObj["type"].Value<string>();
|
||||||
|
|
||||||
|
if(type == "/system/hearbeat")
|
||||||
|
{
|
||||||
|
//Heartbeat.Reset(20000, 20000);
|
||||||
|
}
|
||||||
|
else if(type == "close")
|
||||||
|
{
|
||||||
|
SseClient.Disconnect();
|
||||||
|
|
||||||
|
// Start the reconnect timer
|
||||||
|
Reconnect = new CTimer(ConnectSseClient, null, 5000, 5000);
|
||||||
|
|
||||||
|
Reconnect.Reset(5000, 5000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// Check path against Action dictionary
|
||||||
|
if (ActionDictionary.ContainsKey(type))
|
||||||
|
{
|
||||||
|
var action = ActionDictionary[type];
|
||||||
|
|
||||||
|
if (action is Action<bool>)
|
||||||
|
{
|
||||||
|
var stateString = messageObj["content"]["state"].Value<string>();
|
||||||
|
|
||||||
|
// Look for a button press event
|
||||||
|
if(!string.IsNullOrEmpty(stateString))
|
||||||
|
{
|
||||||
|
#warning deal with held state later
|
||||||
|
if (stateString == "held")
|
||||||
|
return;
|
||||||
|
|
||||||
|
(action as Action<bool>)(stateString == "true");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action is Action<ushort>)
|
||||||
|
{
|
||||||
|
(action as Action<ushort>)(messageObj["content"]["value"].Value<ushort>());
|
||||||
|
}
|
||||||
|
else if (action is Action<string>)
|
||||||
|
{
|
||||||
|
(action as Action<string>)(messageObj["content"]["value"].Value<string>());
|
||||||
|
}
|
||||||
|
else if (action is Action<SourceSelectMessageContent>)
|
||||||
|
{
|
||||||
|
(action as Action<SourceSelectMessageContent>)(messageObj["content"]
|
||||||
|
.ToObject<SourceSelectMessageContent>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
Debug.Console(1, this, "Unable to parse message: {0}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using Crestron.SimplSharp;
|
|
||||||
using Crestron.SimplSharp.CrestronIO;
|
|
||||||
using Crestron.SimplSharpPro;
|
|
||||||
using Crestron.SimplSharp.Net.Http;
|
|
||||||
using PepperDash.Core;
|
|
||||||
using PepperDash.Essentials.Core;
|
|
||||||
using PepperDash.Essentials.Core.PageManagers;
|
|
||||||
|
|
||||||
namespace PepperDash.Essentials
|
|
||||||
{
|
|
||||||
public class CotijaInterfaceController : Device
|
|
||||||
{
|
|
||||||
GenericHttpSseClient SseClient;
|
|
||||||
|
|
||||||
CCriticalSection FileLock;
|
|
||||||
|
|
||||||
string ServerUrl;
|
|
||||||
|
|
||||||
public CotijaInterfaceController(string key) : base(key)
|
|
||||||
{
|
|
||||||
CrestronConsole.AddNewConsoleCommand(RegisterRoomToServer, "InitializeHttpClient", "Initializes a new HTTP client connection to a specified URL", ConsoleAccessLevelEnum.AccessOperator);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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
|
|
||||||
{
|
|
||||||
ServerUrl = url;
|
|
||||||
|
|
||||||
string filePath = string.Format(@"\NVRAM\Program{0}\configurationFile.json", Global.ControlSystem.ProgramNumber);
|
|
||||||
string postBody = null;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(filePath))
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
Debug.Console(0, this, "Error Initilizing Room: {0}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (resp.Code == 200)
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
string uuid = Essentials.ConfigReader.ConfigObject.SystemUuid;
|
|
||||||
|
|
||||||
SseClient.Url = string.Format("http://{0}/api/system/stream/{1}", ServerUrl, uuid);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user