/// <summary> /// Tidy up a device that has just been removed from the collection of connected devices /// </summary> /// <param name="dpvm">The device just removed</param> private void OnDeviceRemoved(DevicePortalViewModel dpvm) { dpvm.PropertyChanged -= this.DevicePropertyChanged; if (dpvm.StopListeningForSystemPerfCommand.CanExecute(this)) { dpvm.StopListeningForSystemPerfCommand.Execute(this); } }
/// <summary> /// Peform the action for the RemoveDevice command /// </summary> /// <param name="obj">The device to be removed from the collection of connected devices</param> private void ExecuteRemoveDeviceCommand(object obj) { DevicePortalViewModel dpvm = obj as DevicePortalViewModel; if (dpvm != null) { this.RemoveDevice(dpvm); } }
/// <summary> /// Event handler to monitor property changes on each of the elements in the collection of connected devices /// </summary> /// <param name="sender">Device that originated the event</param> /// <param name="e">The arguments for the event</param> private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Ready") { DevicePortalViewModel dpvmSender = sender as DevicePortalViewModel; if (dpvmSender != null && !dpvmSender.Ready) { // A device is no-longer ready, so remove it from the current selection List <DevicePortalViewModel> updatedSelection = new List <DevicePortalViewModel>(); foreach (DevicePortalViewModel dpvm in this.SelectedDevices) { if (dpvm.Ready) { updatedSelection.Add(dpvm); } } this.SelectedDevices = updatedSelection; } this.OnPropertyChanged("SomeDevicesReady"); this.OnPropertyChanged("AllDevicesReady"); } if (e.PropertyName == "ConnectionStatus") { DevicePortalViewModel dpvmSender = sender as DevicePortalViewModel; if (dpvmSender.ConnectionStatus == DeviceConnectionStatus.Failed) { // Check for authentication failure and warn the user about bad credentials if (dpvmSender.Portal.ConnectionHttpStatusCode == HttpStatusCode.Unauthorized) { MessageBox.Show( "Connection Unauthorized. Please double check your authentication credentials", "Unauthorized", MessageBoxButton.OK, MessageBoxImage.Exclamation); this.RemoveDevice(dpvmSender); } } } if (e.PropertyName == "Address") { DevicePortalViewModel dpvmSender = sender as DevicePortalViewModel; if (this.DeviceIsDuplicate(dpvmSender)) { MessageBox.Show( string.Format("You already have a connection for this device address: {0}", dpvmSender.Address), "Duplicate", MessageBoxButton.OK, MessageBoxImage.Exclamation); this.RemoveDevice(dpvmSender); } } }
/// <summary> /// Perform additional initialization on a device that has been newly added to the collection of connected devices /// </summary> /// <param name="dpvm">The device that has been added</param> private void OnDeviceAdded(DevicePortalViewModel dpvm) { dpvm.PropertyChanged += this.DevicePropertyChanged; CommandSequence cmdSeq = dpvm.CreateCommandSequence(); cmdSeq.RegisterCommand(dpvm.ReestablishConnectionCommand); cmdSeq.RegisterCommand(dpvm.RefreshDeviceNameCommand); cmdSeq.RegisterCommand(dpvm.StartListeningForSystemPerfCommand); cmdSeq.Execute(null); }
/// <summary> /// Predicate for the RemoveDevice command /// </summary> /// <param name="arg">The device to be removed from the collection</param> /// <returns>Result indicates whether the command can execute</returns> private bool CanExecuteRemoveDeviceCommand(object arg) { DevicePortalViewModel dpvm = arg as DevicePortalViewModel; if (dpvm != null) { return(dpvm.Ready); } return(false); }
/// <summary> /// Determine whether the provided DevicePortalViewModel is already represented in the collection /// </summary> /// <param name="dpvmToCheck">The DevicePortalViewModel for which you want to check for duplicates</param> /// <returns>True if the device is already represented in the collection</returns> private bool DeviceIsDuplicate(DevicePortalViewModel dpvmToCheck) { foreach (DevicePortalViewModel dpvm in this.ConnectedDevices) { if (dpvmToCheck == dpvm) { continue; } if (dpvmToCheck.Address == dpvm.Address) { return(true); } } return(false); }
/// <summary> /// Removes a device from the collection and clears any pending commands /// </summary> /// <param name="removeMe">The device to remove</param> private void RemoveDevice(DevicePortalViewModel removeMe) { removeMe.ClearCommandQueue(); this.ConnectedDevices.Remove(removeMe); }