void PortScanned(object sender, PortScannedEventArgs e) { if (SwitchToMainThread(sender, e)) { return; } //update the Ports collection var port = Ports.FirstOrDefault(i => i.Port == e.Port); if (port != null) { Ports.Remove(port); } if (!e.IsRemoved) { if (port == null) { port = new PortVM() { Port = e.Port, PossibleArm = e.PossibleArm } } ; if (e.ErrorMessage != null) { port.Message = e.ErrorMessage; } else if (e.HasArm) { port.Message = $"{e.Model}" + (!string.IsNullOrWhiteSpace(e.NickName) ? $" ({e.NickName})" : string.Empty); } else if (port.PossibleArm) { port.Message = "possible robot needing firmware"; } _bestScannedPort = PickBetterPort(_bestScannedPort, e); Ports.Add(port); } //reset settings if (e.NeedsSettingsReset) { ResetRequired(e); } } PortScannedEventArgs PickBetterPort(PortScannedEventArgs a, PortScannedEventArgs b) { if (a == null && b == null) { return(a); } if (a == null) { return(b); } if (b == null) { return(a); } if (b.PossibleArm && !a.PossibleArm) { return(b); } if (b.HasArm && !a.HasArm) { return(b); } return(a); } async void ResetRequired(PortScannedEventArgs e) { //confirm var result = await App.Instance.ShowMessageAsync("Robot may require a reset to factory defaults", "If you continue to receive this message and if a new firmware was flashed that is incompatible with the robots previous setting, then the connected robot may require its settings to be reset to factory defaults. Would you like to do this now?", MahApps.Metro.Controls.Dialogs.MessageDialogStyle.AffirmativeAndNegative); if (result != MahApps.Metro.Controls.Dialogs.MessageDialogResult.Affirmative) { return; } e.Arm.Communication.Connect(e.Port); await e.Arm.ResetSettingsAsync(); e.Arm.Communication.Disconnect(); Rescan(); }
void CurrentRobotChanged(ConnectionVM sender) { FlashRequired = false; if (sender.CurrentArm != null) { CurrentFirmware = $"v{sender.CurrentArm.Settings.Version}"; //select current robots port var selectedPort = Ports.FirstOrDefault(i => i.Port == sender.CurrentArm.Communication.ConnectedPort); if (selectedPort != null) { SelectedPort = selectedPort; } if (sender.CurrentArm.Settings.Version < RequiredFirmware) { FlashInstructions = $"An upgrade to you robots firmware is required in order to use this software. Your robot must be updated to a firmware version of v{RequiredFirmware} or beyond"; FlashCommand.Enabled = true; FlashActionText = "Upgrade Firmware"; FlashRequired = true; } else { //select current robots model var model = sender.CurrentArm.Settings.ModelNumber; var robotModel = FirmwareModels.FirstOrDefault(i => i.Model == model); if (robotModel != null) { SelectedFirmwareModel = robotModel; } var newerFirmwareAvailible = sender.CurrentArm.Settings.Version < SelectedFirmwareImage.Firmware.Version; if (newerFirmwareAvailible) { FlashInstructions = "A new firmware version for your robot is availible"; FlashCommand.Enabled = true; FlashActionText = "Upgrade Firmware"; } else { FlashInstructions = "Your robot is up to date"; FlashCommand.Enabled = false; FlashActionText = "Upgrade Firmware"; } } } else { CurrentFirmware = null; //select best guess port var selectedPort = Ports.FirstOrDefault(i => i.PossibleArm); if (selectedPort != null) { SelectedPort = selectedPort; } if (Ports.Count == 0) { FlashInstructions = "Please connect your robot to your PC by a USB cable"; FlashCommand.Enabled = false; FlashActionText = "Install Firmware"; } else { FlashInstructions = "No robot recognized. Install firmware onto your robot."; FlashCommand.Enabled = true; FlashActionText = "Install Firmware"; } } }