コード例 #1
0
 public void Refresh()
 {
     OfficialServer.FileUrlService.GetNTMinerFilesAsync(App.AppType, (ntMinerFiles, e) => {
         this.NTMinerFiles = (ntMinerFiles ?? new List <NTMinerFileData>()).Select(a => new NTMinerFileViewModel(a)).OrderByDescending(a => a.VersionData).ToList();
         if (this.NTMinerFiles == null || this.NTMinerFiles.Count == 0)
         {
             LocalIsLatest = true;
         }
         else
         {
             ServerLatestVm = this.NTMinerFiles.OrderByDescending(a => a.VersionData).FirstOrDefault();
             if (ServerLatestVm.VersionData > LocalNTMinerVersion)
             {
                 this.SelectedNTMinerFile = ServerLatestVm;
                 LocalIsLatest            = false;
             }
             else
             {
                 LocalIsLatest = true;
             }
         }
         OnPropertyChanged(nameof(IsBtnInstallVisible));
         IsReady = true;
         if (!string.IsNullOrEmpty(CommandLineArgs.NTMinerFileName))
         {
             NTMinerFileViewModel ntminerFileVm = this.NTMinerFiles.FirstOrDefault(a => a.FileName == CommandLineArgs.NTMinerFileName);
             if (ntminerFileVm != null)
             {
                 IsHistoryVisible         = Visibility.Visible;
                 this.SelectedNTMinerFile = ntminerFileVm;
                 Install.Execute(null);
             }
         }
     });
 }
コード例 #2
0
 private MainWindowViewModel()
 {
     if (WpfUtil.IsInDesignMode)
     {
         return;
     }
     this.ShowServerLatestDescription = new DelegateCommand(() => {
         VirtualRoot.Out.ShowInfo(ServerLatestVm.Description, header: $"{ServerLatestVm.Version}({ServerLatestVm.VersionTag})", autoHideSeconds: 0);
     });
     this.CancelDownload = new DelegateCommand(() => {
         this._cancel?.Invoke();
     });
     this.Install = new DelegateCommand(() => {
         this.IsDownloading = true;
         string ntminerFile = string.Empty;
         string version     = string.Empty;
         if (IsHistoryVisible == Visibility.Collapsed)
         {
             if (ServerLatestVm != null)
             {
                 ntminerFile = ServerLatestVm.FileName;
                 version     = ServerLatestVm.Version;
             }
         }
         else
         {
             ntminerFile = SelectedNTMinerFile.FileName;
             version     = SelectedNTMinerFile.Version;
         }
         Download(ntminerFile, version,
                  progressChanged: (percent) => {
             this.DownloadMessage = percent + "%";
             this.DownloadPercent = (double)percent / 100;
         },
                  downloadComplete: (isSuccess, message, saveFileFullName) => {
             this.DownloadMessage = message;
             this.DownloadPercent = 0;
             if (isSuccess)
             {
                 this.DownloadMessage = "更新成功,正在重启";
                 void callback()
                 {
                     3.SecondsDelay().ContinueWith((t) => {
                         string location = NTMinerRegistry.GetLocation(App.AppType);
                         if (string.IsNullOrEmpty(location) || !File.Exists(location))
                         {
                             location = Path.Combine(HomePath.AppDomainBaseDirectory, ntminerFile);
                         }
                         try {
                             Process process = Process.GetProcessesByName(Path.GetFileName(location)).FirstOrDefault();
                             if (process != null)
                             {
                                 process.Kill();
                             }
                         }
                         catch (Exception e) {
                             Logger.ErrorDebugLine(e);
                         }
                         try {
                             if (File.Exists(location))
                             {
                                 Guid kernelBrandId = VirtualRoot.GetBrandId(location, NTKeyword.KernelBrandId);
                                 if (kernelBrandId != Guid.Empty)
                                 {
                                     VirtualRoot.TagBrandId(NTKeyword.KernelBrandId, kernelBrandId, saveFileFullName, saveFileFullName);
                                 }
                                 Guid poolBrandId = VirtualRoot.GetBrandId(location, NTKeyword.PoolBrandId);
                                 if (poolBrandId != Guid.Empty)
                                 {
                                     VirtualRoot.TagBrandId(NTKeyword.PoolBrandId, poolBrandId, saveFileFullName, saveFileFullName);
                                 }
                             }
                         }
                         catch (Exception e) {
                             Logger.ErrorDebugLine(e);
                         }
                         int failCount = 0;
                         while (true)
                         {
                             try {
                                 File.Copy(saveFileFullName, location, overwrite: true);
                                 break;
                             }
                             catch (Exception e) {
                                 failCount++;
                                 if (failCount == 3)
                                 {
                                     VirtualRoot.Out.ShowError(e.Message);
                                     break;
                                 }
                                 else
                                 {
                                     System.Threading.Thread.Sleep(3000);
                                 }
                             }
                         }
                         try {
                             File.Delete(saveFileFullName);
                         }
                         catch (Exception e) {
                             Logger.ErrorDebugLine(e);
                         }
                         string arguments = NTMinerRegistry.GetMinerClientArguments(App.AppType);
                         Process.Start(location, arguments);
                         this.IsDownloading = false;
                         2.SecondsDelay().ContinueWith(_ => {
                             UIThread.Execute(() => {
                                 Application.Current.MainWindow?.Close();
                             });
                         });
                     });
                 }
                 if (AppStatic.IsMinerStudio)
                 {
                     RpcRoot.Client.MinerStudioService.CloseMinerStudioAsync(callback);
                 }
                 else
                 {
                     RpcRoot.Client.MinerClientService.CloseNTMinerAsync(callback);
                 }
             }
             else
             {
                 2.SecondsDelay().ContinueWith((t) => {
                     this.IsDownloading = false;
                 });
             }
         }, cancel: out _cancel);
     }, () => !IsDownloading);
     this.ShowHistory = new DelegateCommand(() => {
         if (IsHistoryVisible == Visibility.Visible)
         {
             IsHistoryVisible = Visibility.Collapsed;
         }
         else
         {
             IsHistoryVisible = Visibility.Visible;
         }
     });
     this.AddNTMinerFile = new DelegateCommand(() => {
         NTMinerFileEdit window = new NTMinerFileEdit("Icon_Add", new NTMinerFileViewModel()
         {
             AppType = App.AppType
         });
         window.ShowSoftDialog();
     });
     VirtualRoot.BuildEventPath <NTMinerFileSetInitedEvent>("开源矿工程序版本文件集初始化后刷新Vm内存", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
         var ntminerFiles  = _readOnlyNTMinerFileSet.AsEnumerable().Where(a => a.AppType == App.AppType);
         this.NTMinerFiles = ntminerFiles.Select(a => new NTMinerFileViewModel(a)).OrderByDescending(a => a.VersionData).ToList();
         if (this.NTMinerFiles == null || this.NTMinerFiles.Count == 0)
         {
             LocalIsLatest = true;
         }
         else
         {
             ServerLatestVm = this.NTMinerFiles.OrderByDescending(a => a.VersionData).FirstOrDefault();
             if (ServerLatestVm.VersionData > LocalNTMinerVersion)
             {
                 this.SelectedNTMinerFile = ServerLatestVm;
                 LocalIsLatest            = false;
             }
             else
             {
                 LocalIsLatest = true;
             }
         }
         OnPropertyChanged(nameof(IsBtnInstallVisible));
         IsReady = true;
         if (!string.IsNullOrEmpty(CommandLineArgs.NTMinerFileName))
         {
             NTMinerFileViewModel ntminerFileVm = this.NTMinerFiles.FirstOrDefault(a => a.FileName == CommandLineArgs.NTMinerFileName);
             if (ntminerFileVm != null)
             {
                 IsHistoryVisible         = Visibility.Visible;
                 this.SelectedNTMinerFile = ntminerFileVm;
                 Install.Execute(null);
             }
         }
     });
     this.Refresh();
 }