/// <summary> /// /// </summary> /// <param name="address"></param> /// <param name="defaultPort"></param> /// <returns></returns> internal static async Task <FritzDevice> CreateDeviceAsync(IPAddress address, int defaultPort) { FritzDevice device = new FritzDevice(); device.IPAddress = address; var uriBuilder = new UriBuilder(); uriBuilder.Scheme = "http"; uriBuilder.Host = address.ToString(); uriBuilder.Port = defaultPort; uriBuilder.Port = await new DeviceInfoClient(uriBuilder.Uri.ToString(), 10000).GetSecurityPortAsync(); uriBuilder.Scheme = "https"; device.BaseUrl = uriBuilder.ToString(); if (device.Location == null) { return(null); } else { return(device); } }
public async Task <Tr64Description> ReadDeviceInfoAsync(FritzDevice device) { var uri = CreateRequestUri(device); var httpRequest = WebRequest.CreateHttp(uri); httpRequest.Timeout = 10000; try { using (var response = (HttpWebResponse)(await httpRequest.GetResponseAsync())) { if (response.StatusCode == HttpStatusCode.OK) { using (var responseStream = response.GetResponseStream()) using (var responseReader = new StreamReader(responseStream, Encoding.UTF8)) { var data = await responseReader.ReadToEndAsync(); return(new Tr64Description(device, data)); } } else { throw new FritzDeviceException($"Failed to get device info for device {device.Location.Host}. Response {response.StatusCode} - {response.StatusDescription}."); } } } catch (WebException ex) { throw new FritzDeviceException($"Failed to get device info for device {device.Location.Host}.", ex); } }
/// <summary> /// Method to receive async /// </summary> /// <param name="client">the udp client</param> /// <returns>the result task</returns> private async Task ReceiveAsync(UdpClient client, Action <FritzDevice> deviceCallback) { IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 1900); // delay for 20 seconds Task waitTask = Task.Delay(TimeSpan.FromSeconds(10)); var getDeviceInfoTasks = new List <Task <Tr64Description> >(); Dictionary <IPAddress, FritzDevice> discovered = new Dictionary <IPAddress, FritzDevice>(); var tr64DataReader = new Tr64DataReader(); while (!waitTask.IsCompleted) { var receiveTask = SafeReceiveAsync(client); // check wat for tasks Task finishedTask = await Task.WhenAny(receiveTask, waitTask); if (finishedTask == receiveTask) { UdpReceiveResult result = await receiveTask; // if there is data in buffer read and pars it if (result.Buffer != null && result.Buffer.Length > 0) { string response = Encoding.ASCII.GetString(result.Buffer, 0, result.Buffer.Length); // create device by endpoint data FritzDevice device = FritzDevice.ParseResponse(result.RemoteEndPoint.Address, response); if (!discovered.ContainsKey(result.RemoteEndPoint.Address)) { if (device != null && device.Location != null && device.Location.Scheme != "unknown") { // fetch the device info deviceCallback?.Invoke(device); getDeviceInfoTasks.Add(tr64DataReader.ReadDeviceInfoAsync(device)); discovered.Add(result.RemoteEndPoint.Address, device); } } } } } if (getDeviceInfoTasks.Count > 0) { foreach (var task in getDeviceInfoTasks) { try { var description = await task; description.Device.ParseTR64Desc(description.Data); } catch (FritzDeviceException e) { Console.WriteLine(e.Message); } } } }
private Uri CreateRequestUri(FritzDevice device) { var uriBuilder = new UriBuilder(); uriBuilder.Scheme = "http"; uriBuilder.Host = device.Location.Host; uriBuilder.Port = device.Location.Port; uriBuilder.Path = "tr64desc.xml"; return(uriBuilder.Uri); }
/// <summary> /// Creates a single <see cref="FritzDevice"/>. /// </summary> /// <param name="address">The address the device can be found under.</param> /// <returns>The ready-to-use device.</returns> /// <exception cref="FritzDeviceException">Thrown if a device can not be created or if the data is incomplete.</exception> public async Task <FritzDevice> CreateDeviceAsync(IPAddress address) { var locationBuilder = new UriBuilder("http", address.ToString(), _dataQueryPort); var device = new FritzDevice(address, locationBuilder.Uri); var tr64Reader = new Tr64DataReader(); var tr64Data = await tr64Reader.ReadDeviceInfoAsync(device); device.ParseTR64Desc(tr64Data.Data); return(device); }
/// <summary> /// Method to parse the udp response /// </summary> /// <param name="address"></param> /// <param name="response"></param> /// <returns></returns> internal static FritzDevice ParseResponse(IPAddress address, string response) { FritzDevice device = new FritzDevice(); device.IPAddress = address; device.Location = device.ParseResponse(response); if (device.Location == null) { return(null); } else { return(device); } }
public Tr64Description(FritzDevice device, string data) { if (device == null) { throw new ArgumentNullException(nameof(device)); } if (string.IsNullOrWhiteSpace(data)) { throw new ArgumentException("message", nameof(data)); } Device = device; Data = data; }
/// <summary> /// Creates a single <see cref="FritzDevice"/>. /// </summary> /// <param name="address">The address the device can be found under.</param> /// <param name="defaultPort">The Default pot for accessing the device.</param> /// <returns>The ready-to-use device.</returns> /// <exception cref="FritzDeviceException">Thrown if a device can not be created or if the data is incomplete.</exception> public async Task <FritzDevice> CreateDeviceAsync(IPAddress address, int defaultPort) { return(await FritzDevice.CreateDeviceAsync(address, defaultPort)); }