|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | +using OATCommunications.ClientAdapters; |
| 6 | +using static OATCommunications.ClientAdapters.UdpClientAdapter; |
| 7 | +using OATCommunications.CommunicationHandlers; |
| 8 | +using System.Collections.ObjectModel; |
| 9 | + |
| 10 | +namespace OATCommunications.CommunicationHandlers |
| 11 | +{ |
| 12 | + public static class CommunicationHandlerFactory |
| 13 | + { |
| 14 | + static ObservableCollection<string> _available = new ObservableCollection<string>(); |
| 15 | + public static void DiscoverDevices() |
| 16 | + { |
| 17 | + _available.Clear(); |
| 18 | + //foreach (var port in SerialPort.GetPortNames()) |
| 19 | + //{ |
| 20 | + // _available.Add("Serial : " + port); |
| 21 | + //} |
| 22 | + |
| 23 | + var searcher = new UdpClientAdapter("OAT", 4031); |
| 24 | + searcher.ClientFound += OnWifiClientFound; |
| 25 | + searcher.StartClientSearch(); |
| 26 | + } |
| 27 | + |
| 28 | + private static void OnWifiClientFound(object sender, ClientFoundEventArgs e) |
| 29 | + { |
| 30 | + _available.Insert(0, $"WiFi : {e.Name} ({e.Address}:4030)"); |
| 31 | + } |
| 32 | + |
| 33 | + public static ObservableCollection<String> AvailableDevices { get { return _available; } } |
| 34 | + |
| 35 | + public static ICommunicationHandler ConnectToDevice(string device) |
| 36 | + { |
| 37 | + //if (device.StartsWith("Serial : ")) |
| 38 | + //{ |
| 39 | + // string comPort = device.Substring("Serial : ".Length); |
| 40 | + // return new SerialCommunicationHandler(comPort); |
| 41 | + //} |
| 42 | + //else |
| 43 | + if (device.StartsWith("WiFi : ")) |
| 44 | + { |
| 45 | + var parts = device.Split("()".ToCharArray()); |
| 46 | + string ipAddress = parts[1]; |
| 47 | + return new TcpCommunicationHandler(ipAddress); |
| 48 | + } |
| 49 | + |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | +} |
0 commit comments