diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs | 171 |
1 files changed, 9 insertions, 162 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs index 694502d2e..b84942643 100644 --- a/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs +++ b/Software/Visual_Studio/Tango.Integration/ExternalBridge/ExternalBridgeScanner.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNet.SignalR.Client; -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; @@ -13,10 +12,8 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; -using Tango.BL.Entities; using Tango.Core; using Tango.Core.Helpers; -using Tango.Integration.ExternalBridge.Web; using Tango.Logging; using Tango.PMR; using Tango.PMR.Common; @@ -34,15 +31,8 @@ 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; - - public event EventHandler<IExternalBridgeClient> MachineDiscovered; - - public event EventHandler<IExternalBridgeClient> MachineLost; private SynchronizedObservableCollection<IExternalBridgeClient> _availableMachines; /// <summary> @@ -54,16 +44,6 @@ namespace Tango.Integration.ExternalBridge private set { _availableMachines = value; RaisePropertyChangedAuto(); } } - /// <summary> - /// Gets or sets the SignalR configuration. - /// </summary> - public ExternalBridgeSignalRConfiguration SignalRConfiguration { get; set; } - - /// <summary> - /// Gets or sets the known machines. - /// </summary> - public List<Machine> KnownMachines { get; set; } - private bool _isStarted; /// <summary> /// Gets or sets a value indicating whether this instance is started. @@ -80,14 +60,8 @@ namespace Tango.Integration.ExternalBridge public ExternalBridgeScanner() { _settings = SettingsManager.Default.GetOrCreate<IntegrationSettings>(); + _server = new UdpClient(_settings.ExternalBridgeServiceDiscoveryPort); AvailableMachines = new SynchronizedObservableCollection<IExternalBridgeClient>(); - KnownMachines = new List<Machine>(); - SignalRConfiguration = new ExternalBridgeSignalRConfiguration(); - } - - public ExternalBridgeScanner(List<Machine> knownMachines) : this() - { - KnownMachines = knownMachines; } /// <summary> @@ -99,13 +73,6 @@ namespace Tango.Integration.ExternalBridge { LogManager.Log("External bridge scanner started..."); - if (_server == null) - { - _server = new UdpClient(); - _server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - _server.Client.Bind(new IPEndPoint(IPAddress.Any, _settings.ExternalBridgeServiceDiscoveryPort)); - } - IsStarted = true; foreach (var machine in AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList()) @@ -113,11 +80,6 @@ namespace Tango.Integration.ExternalBridge AvailableMachines.Remove(machine); } - foreach (var machine in AvailableMachines.OfType<ExternalBridgeSignalRClient>().ToList()) - { - AvailableMachines.Remove(machine); - } - _tcpDiscoveryThread = new Thread(TcpDiscoveryThreadMethod); _tcpDiscoveryThread.IsBackground = true; _tcpDiscoveryThread.Start(); @@ -125,37 +87,6 @@ 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); - - bool signalRStarted = false; - - _connection.StateChanged += (x) => - { - if (x.NewState == ConnectionState.Connected) - { - if (!signalRStarted) - { - signalRStarted = true; - _signalRDiscoveryThread = new Thread(SignalRDiscoveryThreadMethod); - _signalRDiscoveryThread.IsBackground = true; - _signalRDiscoveryThread.Start(); - } - } - }; - - _connection.Start(); - } - catch (Exception ex) - { - Debug.WriteLine(ex); - } - } } } @@ -187,9 +118,7 @@ namespace Tango.Integration.ExternalBridge LogManager.Log("Found a new machine via USB " + device.Description); ThreadsHelper.InvokeUINow(() => { - var machine = new ExternalBridgeUsbClient(device.Port, device.Description, _settings.EmbeddedSerialBaudRate); - AvailableMachines.Add(machine); - MachineDiscovered?.Invoke(this, machine); + AvailableMachines.Add(new ExternalBridgeUsbClient(device.Port, device.Description, _settings.EmbeddedSerialBaudRate)); }); } } @@ -202,7 +131,7 @@ namespace Tango.Integration.ExternalBridge /// <summary> /// TCP discovery thread method. /// </summary> - //[DebuggerStepThrough] + [DebuggerStepThrough] private void TcpDiscoveryThreadMethod() { while (IsStarted) @@ -232,7 +161,6 @@ namespace Tango.Integration.ExternalBridge { LogManager.Log("Disconnected machine detected via TCP: " + disconnected_machine.SerialNumber); AvailableMachines.Remove(disconnected_machine); - MachineLost?.Invoke(this, disconnected_machine); } continue; @@ -240,35 +168,14 @@ namespace Tango.Integration.ExternalBridge if (!AvailableMachines.OfType<ExternalBridgeTcpClient>().ToList().Exists(x => x.SerialNumber == packet.SerialNumber && x.IPAddress == address)) { - ExternalBridgeTcpClient newMachine = null; - var knownMachine = KnownMachines.FirstOrDefault(x => x.SerialNumber == packet.SerialNumber); + ExternalBridgeTcpClient newMachine = new ExternalBridgeTcpClient(packet.SerialNumber, address); - if (knownMachine == null && KnownMachines.Count == 0) - { - newMachine = new ExternalBridgeTcpClient(packet.SerialNumber, address); - } - else if (knownMachine != null) - { - newMachine = new ExternalBridgeTcpClient(knownMachine, address); - } + LogManager.Log("Found a new machine via TCP " + newMachine.SerialNumber); - if (newMachine != null) + ThreadsHelper.InvokeUINow(() => { - LogManager.Log("Found a new machine via TCP " + newMachine.SerialNumber); - - ThreadsHelper.InvokeUINow(() => - { - if (AvailableMachines.Count > 0) - { - AvailableMachines.Insert(1, newMachine); - } - else - { - AvailableMachines.Add(newMachine); - } - MachineDiscovered?.Invoke(this, newMachine); - }); - } + AvailableMachines.Insert(1, newMachine); + }); } } catch (Exception ex) @@ -278,65 +185,6 @@ 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); - MachineLost?.Invoke(this, machine); - } - - foreach (var machine in machines.Where(x => !AvailableMachines.OfType<ExternalBridgeSignalRClient>().ToList().Exists(y => y.SerialNumber == x.SerialNumber))) - { - ExternalBridgeSignalRClient newMachine = null; - var knownMachine = KnownMachines.FirstOrDefault(x => x.SerialNumber == machine.SerialNumber); - - if (knownMachine == null && KnownMachines.Count == 0) - { - newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, machine); - } - else if (knownMachine != null) - { - newMachine = new ExternalBridgeSignalRClient(SignalRConfiguration.Address, SignalRConfiguration.Hub, knownMachine, machine); - } - - if (newMachine != null) - { - LogManager.Log("Found a new machine via SignalR " + newMachine.SerialNumber); - - ThreadsHelper.InvokeUINow(() => - { - if (AvailableMachines.Count > 0) - { - AvailableMachines.Insert(1, newMachine); - } - else - { - AvailableMachines.Add(newMachine); - } - MachineDiscovered?.Invoke(this, newMachine); - }); - } - } - } - catch (Exception ex) - { - Debug.WriteLine(ex); - } - } - - Thread.Sleep(5000); - } - } - private DateTime ParseDateTime(String dateTime) { try @@ -360,7 +208,6 @@ namespace Tango.Integration.ExternalBridge { LogManager.Log("External bridge client failed or disposed. Removing from available machines..."); AvailableMachines.Remove(sender as IExternalBridgeClient); - MachineLost?.Invoke(this, sender as IExternalBridgeClient); } } } |
