Skip to content

Commit

Permalink
fix: fixes issue with non CS LAN processors at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorin committed Sep 16, 2024
1 parent 64f62a5 commit 58ec192
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 44 deletions.
2 changes: 1 addition & 1 deletion 3-series/MobileControlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override EssentialsDevice BuildDevice(DeviceConfig dc)
}
catch (Exception e)
{
Debug.Console(2, "Error building MobileConttrolSystemController", e);
Debug.LogMessage(e, "Error building Mobile Control System Controller");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,13 @@ public MobileControlWebsocketServer(string key, int customPort, MobileControlSys
Port = customPort;
}

if(CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter) != -1 &&
parent.Config.DirectServer.AutomaticallyForwardPortToCSLAN == true)
if(parent.Config.DirectServer.AutomaticallyForwardPortToCSLAN == true)
{
try
{
CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter);


Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Automatically forwarding port {0} to CS LAN", Port);

var csAdapterId = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType.EthernetCSAdapter);
Expand All @@ -302,6 +304,10 @@ public MobileControlWebsocketServer(string key, int customPort, MobileControlSys
Debug.LogMessage(Serilog.Events.LogEventLevel.Error, "Error adding port forwarding: {0}", result);
}
}
catch (ArgumentException)
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "This processor does not have a CS LAN", this);
}
catch (Exception ex)
{
Debug.LogMessage(ex, "Error automatically forwarding port to CS LAN");
Expand Down Expand Up @@ -571,58 +577,90 @@ private MobileControlApplicationConfig GetApplicationConfig()
/// </summary>
private void RetrieveSecret()
{
// Add secret provider
_secretProvider = new WebSocketServerSecretProvider(SecretProviderKey);

// Check for existing secrets
var secret = _secretProvider.GetSecret(SecretProviderKey);

if (secret != null)
try
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Secret successfully retrieved", this);
// Add secret provider
_secretProvider = new WebSocketServerSecretProvider(SecretProviderKey);

// populate the local secrets object
_secret = JsonConvert.DeserializeObject<ServerTokenSecrets>(secret.Value.ToString());
// Check for existing secrets
var secret = _secretProvider.GetSecret(SecretProviderKey);

// populate the _uiClient collection
foreach (var token in _secret.Tokens)
if (secret != null)
{
UiClients.Add(token.Key, new UiClientContext(token.Value));
}
Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Secret successfully retrieved", this);

foreach (var client in UiClients)
{
var key = client.Key;
var path = _wsPath + key;
var roomKey = client.Value.Token.RoomKey;
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Secret: {0}", this, secret.Value.ToString());


// populate the local secrets object
_secret = JsonConvert.DeserializeObject<ServerTokenSecrets>(secret.Value.ToString());

if (_secret != null && _secret.Tokens != null)
{
// populate the _uiClient collection
foreach (var token in _secret.Tokens)
{
if(token.Value == null)
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Warning, "Token value is null", this);
continue;
}

_server.AddWebSocketService(path, () =>
Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Adding token: {0} for room: {1}", this, token.Key, token.Value.RoomKey);

if(UiClients == null)
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Warning, "UiClients is null", this);
UiClients = new Dictionary<string, UiClientContext>();
}

UiClients.Add(token.Key, new UiClientContext(token.Value));
}
}

if (UiClients.Count > 0)
{
var c = new UiClient();
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Constructing UiClient with id: {key}", this, key);

c.Controller = _parent;
c.RoomKey = roomKey;
UiClients[key].SetClient(c);
return c;
});


//_server.WebSocketServices.AddService<UiClient>(path, (c) =>
//{
// Debug.Console(2, this, "Constructing UiClient with id: {0}", key);
// c.Controller = _parent;
// c.RoomKey = roomKey;
// UiClients[key].SetClient(c);
//});
Debug.LogMessage(Serilog.Events.LogEventLevel.Information, "Restored {uiClientCount} UiClients from secrets data", this, UiClients.Count);

foreach (var client in UiClients)
{
var key = client.Key;
var path = _wsPath + key;
var roomKey = client.Value.Token.RoomKey;

_server.AddWebSocketService(path, () =>
{
var c = new UiClient();
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Constructing UiClient with id: {key}", this, key);

c.Controller = _parent;
c.RoomKey = roomKey;
UiClients[key].SetClient(c);
return c;
});


//_server.WebSocketServices.AddService<UiClient>(path, (c) =>
//{
// Debug.Console(2, this, "Constructing UiClient with id: {0}", key);
// c.Controller = _parent;
// c.RoomKey = roomKey;
// UiClients[key].SetClient(c);
//});
}
}
}
else
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Warning, "No secret found");
}

Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "{uiClientCount} UiClients restored from secrets data", this, UiClients.Count);
}
else
catch (Exception ex)
{
Debug.LogMessage(Serilog.Events.LogEventLevel.Warning, "No secret found");
Debug.LogMessage(ex, "Exception retrieving secret", this);
}

Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "{uiClientCount} UiClients restored from secrets data", this, UiClients.Count);
}

/// <summary>
Expand Down

0 comments on commit 58ec192

Please sign in to comment.