/// <summary> /// Called when the selected device is available /// Use the device details to switch playback instances /// </summary> /// <param name="oldSelectedDevice"></param> public void SelectPlaybackDevice(PlaybackDevice oldSelectedDevice) { // Don't select a device if the full data has not been read in yet. // This can happen if a local device was last selected if (PlaybackManagerModel.DataValid == true) { // Deselect the old playback instance if there was one if (oldSelectedDevice != null) { selectedPlayback?.Deselect(); } // If there is no new device then clear the selection if (PlaybackManagerModel.AvailableDevice == null) { selectedPlayback = null; } else { selectedPlayback = (PlaybackManagerModel.AvailableDevice.IsLocal == true) ? localPlayback : remotePlayback; selectedPlayback.Select(); } } }
/// <summary> /// Called when a playback device has been discovered. /// If this device has not already been found then report it to all registered interfaces /// </summary> /// <param name="device"></param> private void DeviceDiscovered(PlaybackDevice device) { if (devices.AddDevice(device) == true) { // Report this new device callbacks.ForEach(callback => callback.NewDeviceDetected(device)); } }
/// <summary> /// Add a device to the collection if it is unique /// </summary> /// <param name="deviceToAdd"></param> /// <returns></returns> public bool AddDevice(PlaybackDevice deviceToAdd) { bool deviceUnique = (DeviceCollection.Contains(deviceToAdd) == false); if (deviceUnique == true) { DeviceCollection.Add(deviceToAdd); } return(deviceUnique); }
/// <summary> /// Send a request to the device using TCP protocol and await a response /// </summary> /// <param name="targetDevice"></param> /// <param name="request"></param> /// <returns></returns> public static async Task <string> SendRequest(PlaybackDevice targetDevice, string request) { string response = ""; await socketLock.WaitAsync(); try { using (TcpClient client = new() ) { try { // Connect to the client if (client.ConnectAsync(IPAddress.Parse(targetDevice.IPAddress), targetDevice.Port).Wait(MillisecondsTimeout) == true) { // Get the network stream and send out the request NetworkStream networkStream = client.GetStream(); // Convert to bytes byte[] requestBytes = Encoding.UTF8.GetBytes(request); await networkStream.WriteAsync(requestBytes, 0, requestBytes.Length); int bytesRead; do { bytesRead = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length); if (bytesRead > 0) { response += Encoding.UTF8.GetString(readBuffer, 0, bytesRead); } }while (bytesRead > 0); } else { Logger.Error("Timeout sending DLNA request"); } } catch (SocketException sktProblem) { Logger.Error(sktProblem.ToString()); response = ""; } catch (IOException ioProblem) { Logger.Error(ioProblem.ToString()); response = ""; } catch (AggregateException combinedProblem) { Logger.Error(combinedProblem.ToString()); response = ""; } } } finally { socketLock.Release(); } return(response); }
/// <summary> /// Find the specified device /// </summary> /// <param name="device"></param> /// <returns></returns> public PlaybackDevice FindDevice(PlaybackDevice device) => DeviceCollection.SingleOrDefault(dev => device.Equals(dev));
/// <summary> /// Rem ove this device from the collection if present /// </summary> /// <param name="device"></param> /// <returns></returns> public bool RemoveDevice(PlaybackDevice device) => DeviceCollection.Remove(device);