Пример #1
0
 public NTMinerFileViewModel()
 {
     _id        = Guid.NewGuid();
     _publishOn = DateTime.Now;
     this.Save  = new DelegateCommand(() => {
         LoginWindow.Login(() => {
             RpcRoot.OfficialServer.FileUrlService.AddOrUpdateNTMinerFileAsync(new NTMinerFileData().Update(this), (response, e) => {
                 if (response.IsSuccess())
                 {
                     MainWindowViewModel.Instance.Refresh();
                     UIThread.Execute(() => () => {
                         WpfUtil.GetTopWindow()?.Close();
                     });
                 }
                 else
                 {
                     Logger.ErrorDebugLine($"AddOrUpdateNTMinerFileAsync失败");
                 }
             });
         });
     });
     this.Edit = new DelegateCommand(() => {
         NTMinerFileEdit window = new NTMinerFileEdit("Icon_Edit", new NTMinerFileViewModel(this));
         window.ShowSoftDialog();
     });
     this.Remove = new DelegateCommand(() => {
         LoginWindow.Login(() => {
             this.ShowSoftDialog(new DialogWindowViewModel(message: $"确定删除{this.Version}({this.VersionTag})吗?", title: "确认", onYes: () => {
                 RpcRoot.OfficialServer.FileUrlService.RemoveNTMinerFileAsync(this.Id, (response, e) => {
                     if (response.IsSuccess())
                     {
                         MainWindowViewModel.Instance.Refresh();
                         MainWindowViewModel.Instance.SelectedNTMinerFile = MainWindowViewModel.Instance.NTMinerFiles.FirstOrDefault();
                         if (this == MainWindowViewModel.Instance.ServerLatestVm)
                         {
                             MainWindowViewModel.Instance.ServerLatestVm = MainWindowViewModel.Instance.NTMinerFiles
                                                                           .FirstOrDefault(a => a != this && a.VersionData > MainWindowViewModel.Instance.LocalNTMinerVersion);
                         }
                     }
                     else
                     {
                         VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4);
                     }
                 });
             }));
         });
     });
 }
Пример #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();
 }
Пример #3
0
 private MainWindowViewModel()
 {
     if (WpfUtil.IsInDesignMode)
     {
         return;
     }
     this.Refresh();
     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 {
                             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();
     });
 }