private void UpdateAvailableDevices() { switch (SelectedTransportType) { case TransportType.Serial: WindowWrapper.Current().Dispatcher.Dispatch(() => { BusySrv.ShowBusy(Res.GetString("HC_Searching")); AvailableDevices = new ObservableCollection <NanoDeviceBase>(SerialDebugService.SerialDebugClient.NanoFrameworkDevices); SerialDebugService.SerialDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged; // if there's just one, select it SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null; BusySrv.HideBusy(); }); break; case TransportType.Usb: WindowWrapper.Current().Dispatcher.Dispatch(() => { BusySrv.ShowBusy(Res.GetString("HC_Searching")); AvailableDevices = new ObservableCollection <NanoDeviceBase>(UsbDebugService.UsbDebugClient.NanoFrameworkDevices); UsbDebugService.UsbDebugClient.NanoFrameworkDevices.CollectionChanged += NanoFrameworkDevices_CollectionChanged; // if there's just one, select it SelectedDevice = (AvailableDevices.Count == 1) ? AvailableDevices.First() : null; BusySrv.HideBusy(); }); break; case TransportType.TcpIp: // TODO //BusySrv.ShowBusy("Not implemented yet! Why not give it a try??"); //await Task.Delay(2500); //await WindowWrapper.Current().Dispatcher.DispatchAsync(() => //{ // AvailableDevices = new ObservableCollection<NanoDeviceBase>(); // SelectedDevice = null; //}); //BusySrv.HideBusy(); break; } }
public async Task DeployFile() { bool success = false; // get only selected files IEnumerable <DeployFile> deployList = FilesList.ToArray().Where(s => s.Selected == true); // sanity check if (deployList.Count() <= 0) { return; } List <StorageFile> sigfiles = new List <StorageFile>(deployList.Count()); // show busy BusySrv.ShowBusy(); try { foreach (DeployFile file in deployList) { // sanity checks if (file.DFile.Path.Trim().Length == 0) { continue; } if (!file.DFile.IsAvailable) { BusySrv.HideBusy(); var dummy = await DialogSrv.ShowMessageAsync(String.Format(Res.GetString("DP_CantOpenFile"), file.DFile.DisplayName)); return; } // add to sigFiles list, if exists var sigFile = await GetSignatureFileName(file.DFile); if (sigFile == null) { var dummy = await DialogSrv.ShowMessageAsync(String.Format(Res.GetString("DP_CanOpenSigFile"), file.DFile.DisplayName)); return; } sigfiles.Add(sigFile); } // the code to deploy file goes here... // fazer ping bool fMicroBooter = (await MainVM.SelectedDevice.PingAsync() == PingConnectionType.NanoBooter); if (fMicroBooter) { await MainVM.SelectedDevice.DebugEngine.PauseExecutionAsync(); } List <uint> executionPoints = new List <uint>(); int cnt = 0; foreach (DeployFile file in deployList) { WindowWrapper.Current().Dispatcher.Dispatch(() => { CurrentDeploymentTokenSource = new CancellationTokenSource(); }); CancellationToken cancellationToken = CurrentDeploymentTokenSource.Token; if (Path.GetExtension(file.DFile.Path).ToLower() == ".nmf") { if (!await MainVM.SelectedDevice.DeployUpdateAsync(file.DFile, cancellationToken, new Progress <ProgressReport>((value) => { // update busy message according to deployment progress BusySrv.ChangeBusyText(value.Status); }) )) { // fail var dummy = await DialogSrv.ShowMessageAsync(String.Format(Res.GetString("DP_CantDeploy"), file.DFile.DisplayName)); return; } } else { var tpl = await MainVM.SelectedDevice.DeployAsync(file.DFile, sigfiles[cnt++], cancellationToken, new Progress <ProgressReport>((value) => { // update busy message according to deployment progress BusySrv.ChangeBusyText(value.Status); }) ); if (!tpl.Item2) { // fail var dummy = await DialogSrv.ShowMessageAsync(String.Format(Res.GetString("DP_CantDeploy"), file.DFile.DisplayName)); return; } if (tpl.Item1 != 0) { executionPoints.Add(tpl.Item1); } } } executionPoints.Add(0); if (!fMicroBooter) { for (int i = 0; i < 10; i++) { if (await MainVM.SelectedDevice.DebugEngine.ConnectAsync(1, 500, true)) { break; } } } // update busy message according to deployment progress BusySrv.ChangeBusyText(Res.GetString("DP_ExecutingApp")); foreach (uint addr in executionPoints) { WindowWrapper.Current().Dispatcher.Dispatch(() => { CurrentDeploymentTokenSource = new CancellationTokenSource(); }); CancellationToken cancellationToken = CurrentDeploymentTokenSource.Token; if (await MainVM.SelectedDevice.ExecuteAync(addr, cancellationToken)) { break; } } success = true; } catch { /* TBD */ } finally { // resume execution if (MainVM.SelectedDevice.DebugEngine != null) { try { if (MainVM.SelectedDevice.DebugEngine.IsConnected && MainVM.SelectedDevice.DebugEngine.ConnectionSource == nanoFramework.Tools.Debugger.WireProtocol.ConnectionSource.NanoCLR) { await MainVM.SelectedDevice.DebugEngine.ResumeExecutionAsync(); } } catch { } } // end busy BusySrv.HideBusy(); // show result to user if (success) { await DialogSrv.ShowMessageAsync(deployList.Count() > 1?Res.GetString("DP_FilesDeployed") : Res.GetString("DP_FileDeployed")); } else { await DialogSrv.ShowMessageAsync(deployList.Count() > 1?Res.GetString("DP_FailToDeployFiles") : Res.GetString("DP_FailToDeployFile")); } } }