aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/ExternalBridge
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2020-01-29 16:42:59 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2020-01-29 16:42:59 +0200
commita1fdee086eff6b78e863efcc248e70fca248437e (patch)
treed133b346bae839b20a8a6ae0d88509c767cc396a /Software/Visual_Studio/Tango.Integration/ExternalBridge
parentaaa93103ae4db626d29081ce247df3b6143a39f8 (diff)
downloadTango-a1fdee086eff6b78e863efcc248e70fca248437e.tar.gz
Tango-a1fdee086eff6b78e863efcc248e70fca248437e.zip
Improved ExternalBridge SignalR error handling...
Improved logging of adapters connected/disconnected...
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/ExternalBridge')
-rw-r--r--Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs129
1 files changed, 95 insertions, 34 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
index a9f243812..b0d66a54e 100644
--- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
+++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs
@@ -25,6 +25,7 @@ using Tango.PMR.Power;
using Tango.Core;
using Microsoft.AspNet.SignalR.Client;
using Tango.Integration.ExternalBridge.Web;
+using Tango.Core.Threading;
namespace Tango.Integration.ExternalBridge
{
@@ -37,6 +38,7 @@ namespace Tango.Integration.ExternalBridge
private int _external_bridge_port = 1984; //Will be overridden by settings in constructor..
private HubConnection _connection;
private IHubProxy _proxy;
+ private bool _isSignalRConnected;
#region Events
@@ -381,40 +383,77 @@ namespace Tango.Integration.ExternalBridge
if (SignalRConfiguration.Enabled)
{
- try
+ StartSignalR();
+ }
+
+ IsStarted = true;
+ _enabled = true;
+ RaisePropertyChanged(nameof(Enabled));
+ }
+ }
+
+ private void StartSignalR()
+ {
+ if (!_enabled || _isSignalRConnected) return;
+
+ try
+ {
+ try
+ {
+ if (_connection != null)
{
- _connection = new HubConnection(SignalRConfiguration.Address);
- _proxy = _connection.CreateHubProxy(SignalRConfiguration.Hub);
- _proxy.On<String>("OnSessionCreated", OnSignalRSessionCreated);
- _connection.Start();
- _connection.StateChanged += (x) =>
+ _connection.Dispose();
+ }
+ }
+ catch { }
+
+ _connection = new HubConnection(SignalRConfiguration.Address);
+ _proxy = _connection.CreateHubProxy(SignalRConfiguration.Hub);
+ _proxy.On<String>("OnSessionCreated", OnSignalRSessionCreated);
+ _connection.Start();
+ _connection.StateChanged += (x) =>
+ {
+ if (x.NewState == ConnectionState.Connected)
+ {
+ try
{
- if (x.NewState == ConnectionState.Connected)
+ LogManager.Log("External Bridge Service SignalR Connected. Registering machine...");
+
+ _proxy.Invoke("RegisterMachine", new MachineInfo()
{
- try
- {
- _proxy.Invoke("RegisterMachine", new MachineInfo()
- {
- SerialNumber = Machine.SerialNumber,
- Organization = Machine.Organization.Name,
- });
- }
- catch (Exception ex)
- {
- LogManager.Log(ex, "Error registering machine via SignalR.");
- }
- }
- };
+ SerialNumber = Machine.SerialNumber,
+ Organization = Machine.Organization.Name,
+ });
+
+ _isSignalRConnected = true;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error registering machine via SignalR.");
+ }
}
- catch (Exception ex)
+ else if (x.NewState == ConnectionState.Disconnected)
{
- LogManager.Log(ex, "Error initializing ExternalBridge SignalR.");
+ _isSignalRConnected = false;
+
+ if (Enabled)
+ {
+ LogManager.Log("External Bridge Service SignalR Disconnected. Reconnecting in 1 minute...");
+ TimeoutTask.StartNew(StartSignalR, TimeSpan.FromMinutes(1));
+ }
}
- }
+ else if (x.NewState == ConnectionState.Reconnecting)
+ {
+ LogManager.Log("External Bridge Service SignalR Connection Lost. Reconnecting...");
+ //Will go invoke state change again with "connected" if successful...
+ }
+ };
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error initializing ExternalBridge SignalR. Will try again in 5 minutes...");
- IsStarted = true;
- _enabled = true;
- RaisePropertyChanged(nameof(Enabled));
+ TimeoutTask.StartNew(StartSignalR, TimeSpan.FromMinutes(5));
}
}
@@ -425,24 +464,46 @@ namespace Tango.Integration.ExternalBridge
{
if (IsStarted)
{
- _tcpServer.Stop();
- _discoveryService.Stop();
+ try
+ {
+ _tcpServer.Stop();
+ _discoveryService.Stop();
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error disposing TCP discovery services.");
+ }
foreach (var receiver in _receivers.ToList())
{
- await receiver.Disconnect();
+ try
+ {
+ await receiver.Disconnect();
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, $"Error disconnecting receiver {receiver.ComponentName}.");
+ }
}
+ _enabled = false;
+
if (SignalRConfiguration.Enabled)
{
- await _proxy.Invoke("UnregisterMachine");
- _connection.Stop();
- _connection.Dispose();
+ try
+ {
+ await _proxy.Invoke("UnregisterMachine");
+ _connection.Stop();
+ _connection.Dispose();
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error disposing SignalR connection.");
+ }
}
IsStarted = false;
HasSessions = false;
- _enabled = false;
RaisePropertyChanged(nameof(Enabled));
}
}