diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-01-15 01:14:27 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-01-15 01:14:27 +0200 |
| commit | 6a0ff2744f0f13a2070b53e9eff20ea9379ee350 (patch) | |
| tree | 324dfe2162f545c92789a8badfe7d6d9e2279d07 /Software/Visual_Studio/Tango.Integration/ExternalBridge | |
| parent | 0791a0ee675d6bddd54233bc9511cd6c01ce3a88 (diff) | |
| download | Tango-6a0ff2744f0f13a2070b53e9eff20ea9379ee350.tar.gz Tango-6a0ff2744f0f13a2070b53e9eff20ea9379ee350.zip | |
Working on SignalR ExternalBridge...
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/ExternalBridge')
8 files changed, 227 insertions, 25 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeReceiver.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeReceiver.cs index c43598746..366f1bbdb 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeReceiver.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeReceiver.cs @@ -124,6 +124,11 @@ namespace Tango.Integration.ExternalBridge Adapter = new TcpTransportAdapter(tcpClient); } + public ExternalBridgeReceiver(SignalRTransportAdapter signalRAdapter, IMachineOperator machineOperator) : this(machineOperator) + { + Adapter = signalRAdapter; + } + #endregion #region Override Methods diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs index d365de5c6..97f15f6b4 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.AspNet.SignalR.Client; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; @@ -14,6 +15,7 @@ using System.Threading; using System.Threading.Tasks; using Tango.Core; using Tango.Core.Helpers; +using Tango.Integration.ExternalBridge.Web; using Tango.Logging; using Tango.PMR; using Tango.PMR.Common; @@ -31,8 +33,11 @@ namespace Tango.Integration.ExternalBridge { private Thread _tcpDiscoveryThread; private Thread _usbDiscoveryThread; + private Thread _signalRDiscoveryThread; private UdpClient _server; private IntegrationSettings _settings; + private HubConnection _connection; + private IHubProxy _proxy; private SynchronizedObservableCollection<IExternalBridgeClient> _availableMachines; /// <summary> @@ -44,6 +49,11 @@ namespace Tango.Integration.ExternalBridge private set { _availableMachines = value; RaisePropertyChangedAuto(); } } + /// <summary> + /// Gets or sets the SignalR configuration. + /// </summary> + public ExternalBridgeSignalRConfiguration SignalRConfiguration { get; set; } + private bool _isStarted; /// <summary> /// Gets or sets a value indicating whether this instance is started. @@ -61,6 +71,7 @@ namespace Tango.Integration.ExternalBridge { _settings = SettingsManager.Default.GetOrCreate<IntegrationSettings>(); AvailableMachines = new SynchronizedObservableCollection<IExternalBridgeClient>(); + SignalRConfiguration = new ExternalBridgeSignalRConfiguration(); } /// <summary> @@ -93,6 +104,24 @@ namespace Tango.Integration.ExternalBridge _usbDiscoveryThread = new Thread(UsbDiscoveryThreadMethod); _usbDiscoveryThread.IsBackground = true; _usbDiscoveryThread.Start(); + + if (SignalRConfiguration.Enabled) + { + try + { + _connection = new HubConnection(SignalRConfiguration.Address); + _proxy = _connection.CreateHubProxy(SignalRConfiguration.Hub); + _connection.Start(); + + _signalRDiscoveryThread = new Thread(SignalRDiscoveryThreadMethod); + _signalRDiscoveryThread.IsBackground = true; + _signalRDiscoveryThread.Start(); + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } } } @@ -191,6 +220,41 @@ namespace Tango.Integration.ExternalBridge } } + private void SignalRDiscoveryThreadMethod() + { + while (IsStarted && SignalRConfiguration.Enabled) + { + if (_connection.State == ConnectionState.Connected) + { + try + { + var machines = _proxy.Invoke<List<MachineInfo>>("GetAvailableMachines").Result; + + foreach (var machine in AvailableMachines.OfType<ExternalBridgeSignalRClient>().ToList().Where(x => !machines.Exists(y => y.SerialNumber == x.SerialNumber))) + { + AvailableMachines.Remove(machine); + } + + foreach (var machine in machines.Where(x => !AvailableMachines.OfType<ExternalBridgeSignalRClient>().ToList().Exists(y => y.SerialNumber == x.SerialNumber))) + { + ExternalBridgeSignalRClient newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, machine.SerialNumber); + + ThreadsHelper.InvokeUINow(() => + { + AvailableMachines.Insert(1, newMachine); + }); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } + + Thread.Sleep(5000); + } + } + private DateTime ParseDateTime(String dateTime) { try diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs index a0f81c835..b4d2e4df4 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeService.cs @@ -23,6 +23,8 @@ using Tango.Core.ExtensionMethods; using Tango.PMR.MachineStatus; using Tango.PMR.Power; using Tango.Core; +using Microsoft.AspNet.SignalR.Client; +using Tango.Integration.ExternalBridge.Web; namespace Tango.Integration.ExternalBridge { @@ -33,6 +35,8 @@ namespace Tango.Integration.ExternalBridge private TcpServer _tcpServer; private int _discovery_port = 8888; //Will be overridden by settings in constructor.. private int _external_bridge_port = 1984; //Will be overridden by settings in constructor.. + private HubConnection _connection; + private IHubProxy _proxy; #region Events @@ -55,6 +59,11 @@ namespace Tango.Integration.ExternalBridge #region Properties + /// <summary> + /// Gets or sets the SignalR configuration. + /// </summary> + public ExternalBridgeSignalRConfiguration SignalRConfiguration { get; set; } + private IMachineOperator _machineOperator; /// <summary> /// Gets or sets the machine operator. @@ -128,6 +137,8 @@ namespace Tango.Integration.ExternalBridge /// </summary> public ExternalBridgeService() { + SignalRConfiguration = new ExternalBridgeSignalRConfiguration(); + _receivers = new List<ExternalBridgeReceiver>(); var settings = SettingsManager.Default.GetOrCreate<IntegrationSettings>(); @@ -205,7 +216,7 @@ namespace Tango.Integration.ExternalBridge /// <param name="e">The <see cref="ClientConnectedEventArgs"/> instance containing the event data.</param> private async void _tcpServer_ClientConnected(object sender, ClientConnectedEventArgs e) { - LogManager.Log("External bridge client connected from: " + e.Socket.GetIPAddress()); + LogManager.Log("External bridge TCP client connected from: " + e.Socket.GetIPAddress()); ExternalBridgeReceiver receiver = new ExternalBridgeReceiver(e.Socket, MachineOperator); receiver.LoginRequest += Receiver_LoginRequest; receiver.ColorProfileRequest += Receiver_ColorProfileRequest; @@ -214,6 +225,21 @@ namespace Tango.Integration.ExternalBridge await receiver.Connect(); } + private async void OnSignalRSessionCreated(String sessionID) + { + LogManager.Log("External bridge SignalR client connected."); + + var adapter = new SignalRTransportAdapter(SignalRConfiguration.Address, SignalRConfiguration.Hub, SignalRTransportAdapter.SignalRTransportAdapterMode.JoinSession, Machine.SerialNumber, sessionID); ; + + ExternalBridgeReceiver receiver = new ExternalBridgeReceiver(adapter, MachineOperator); + receiver.LoginRequest += Receiver_LoginRequest; + receiver.ColorProfileRequest += Receiver_ColorProfileRequest; + receiver.Disconnected += Receiver_Disconnected; + _receivers.Add(receiver); + await receiver.Connect(); + //await _proxy.Invoke("NotifySessionCreated"); + } + private void MachineOperator_StatusChanged(object sender, MachineStatuses status) { try @@ -255,7 +281,13 @@ namespace Tango.Integration.ExternalBridge private void Receiver_Disconnected(object sender, EventArgs e) { var receiver = sender as ExternalBridgeReceiver; + + receiver.LoginRequest -= Receiver_LoginRequest; + receiver.ColorProfileRequest -= Receiver_ColorProfileRequest; + receiver.Disconnected -= Receiver_Disconnected; + _receivers.Remove(receiver); + if (receiver.LoginIntent == ExternalBridgeLoginIntent.FullControl) { ClientDisconnected?.Invoke(this, new EventArgs()); @@ -317,6 +349,25 @@ namespace Tango.Integration.ExternalBridge _tcpServer.Start(); _discoveryService.Start(); + if (SignalRConfiguration.Enabled) + { + _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) + { + _proxy.Invoke("RegisterMachine", new MachineInfo() + { + SerialNumber = Machine.SerialNumber, + Organization = Machine.Organization.Name, + }); + } + }; + } + IsStarted = true; _enabled = true; RaisePropertyChanged(nameof(Enabled)); @@ -338,6 +389,13 @@ namespace Tango.Integration.ExternalBridge await receiver.Disconnect(); } + if (SignalRConfiguration.Enabled) + { + await _proxy.Invoke("UnregisterMachine"); + _connection.Stop(); + _connection.Dispose(); + } + IsStarted = false; IsInSession = false; _enabled = false; diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs new file mode 100644 index 000000000..3a26e6a25 --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRClient.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.Integration.Operation; +using Tango.PMR.Integration; +using Tango.Settings; +using Tango.Transport; +using Tango.Transport.Adapters; + +namespace Tango.Integration.ExternalBridge +{ + public class ExternalBridgeSignalRClient : ExternalBridgeTcpClient + { + public ExternalBridgeSignalRClient(String url, String hub, String serialNumber) + { + SerialNumber = serialNumber; + Machine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber); + Adapter = new SignalRTransportAdapter(url, hub, SignalRTransportAdapter.SignalRTransportAdapterMode.CreateSession, serialNumber); + } + + public override async Task Connect(ExternalBridgeLoginRequest login) + { + if (State != TransportComponentState.Connected) + { + try + { + await Adapter.Connect(); + + State = TransportComponentState.Connected; + StartThreads(); + + LogManager.Log("External Bridge TCP Client Connected..."); + + + LogRequestSent(login); + + var response = await SendRequest<ExternalBridgeLoginRequest, ExternalBridgeLoginResponse>(login); + + SessionLogger.CreateSession(); + + DeviceInformation = response.Message.DeviceInformation; + if (!response.Message.Authenticated) + { + await Adapter.Disconnect(); + throw new AuthenticationException(response.Container.ErrorMessage); + } + } + catch (Exception ex) + { + LogRequestFailed(login, ex); + try + { + await Adapter.Disconnect(); + } + catch { } + + throw ex; + } + + OnEnableDiagnosticsChanged(EnableDiagnostics); + OnEnableEmbeddedDebuggingChanged(EnableEmbeddedDebugging); + OnEnableEventsNotification(EnableEventsNotification); + OnEnableApplicationLogsChanged(EnableApplicationLogs); + OnEnableMachineStatusUpdatesChanged(EnableMachineStatusUpdates); + //TODO: Uncomment this only when Machine Studio enables automatic thread loading (ExternalBridgeTCPClient). + //OnEnableAutomaticThreadLoadingChanged(EnableAutomaticThreadLoading); + } + } + } +} diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRConfiguration.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRConfiguration.cs new file mode 100644 index 000000000..cd511aa9b --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeSignalRConfiguration.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Integration.ExternalBridge +{ + public class ExternalBridgeSignalRConfiguration + { + public bool Enabled { get; set; } + public String Address { get; set; } + public String Hub { get; set; } + } +} diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs index e7190ae0f..064b673bc 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeTcpClient.cs @@ -90,7 +90,7 @@ namespace Tango.Integration.ExternalBridge /// <param name="login">The login.</param> /// <returns></returns> /// <exception cref="AuthenticationException">The machine password is invalid.</exception> - public async Task Connect(ExternalBridgeLoginRequest login) + public virtual async Task Connect(ExternalBridgeLoginRequest login) { if (State != TransportComponentState.Connected) { @@ -140,7 +140,7 @@ namespace Tango.Integration.ExternalBridge } } - private async void OnEnableApplicationLogsChanged(bool value) + protected async void OnEnableApplicationLogsChanged(bool value) { if (value && State == TransportComponentState.Connected && !_logs_sent) { @@ -323,7 +323,7 @@ namespace Tango.Integration.ExternalBridge /// <summary> /// Gets the database machine associated with this client. /// </summary> - public Machine Machine { get; private set; } + public Machine Machine { get; protected set; } /// <summary> /// Sets the database machine. diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/IExternalBridgeService.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/IExternalBridgeService.cs index 966c8d569..39b2f4e07 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/IExternalBridgeService.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/IExternalBridgeService.cs @@ -72,5 +72,10 @@ namespace Tango.Integration.ExternalBridge /// Gets the current session login intent. /// </summary> ExternalBridgeLoginIntent SessionIntent { get; } + + /// <summary> + /// Gets or sets the SignalR configuration. + /// </summary> + ExternalBridgeSignalRConfiguration SignalRConfiguration { get; set; } } } diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/SignalRExternalBridgeClient.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/SignalRExternalBridgeClient.cs deleted file mode 100644 index df5076598..000000000 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/SignalRExternalBridgeClient.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.Entities; -using Tango.Integration.Operation; -using Tango.PMR.Integration; -using Tango.Transport.Adapters; - -namespace Tango.Integration.ExternalBridge -{ - public class SignalRExternalBridgeClient : ExternalBridgeTcpClient - { - public SignalRExternalBridgeClient(String url, String hub, String serialNumber) - { - Adapter = new SignalRTransportAdapter(url, hub); - } - } -} |
