Esempio n. 1
0
 private MainWindowViewModel()
 {
     if (App.IsInDesignMode)
     {
         return;
     }
     this.Refresh();
     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 = "更新成功,正在重启";
                 if (VirtualRoot.IsMinerStudio)
                 {
                     Client.MinerStudioService.CloseMinerStudio();
                 }
                 else
                 {
                     Client.MinerClientService.CloseNTMiner();
                 }
                 TimeSpan.FromSeconds(3).Delay().ContinueWith((t) => {
                     string location = NTMinerRegistry.GetLocation();
                     if (string.IsNullOrEmpty(location) || !File.Exists(location))
                     {
                         location = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ntMinerFile);
                     }
                     try {
                         if (File.Exists(location))
                         {
                             Guid kernelBrandId = VirtualRoot.GetKernelBrandId(location);
                             if (kernelBrandId != Guid.Empty)
                             {
                                 Logger.InfoDebugLine("打码开始");
                                 VirtualRoot.TagKernelBrandId(kernelBrandId, saveFileFullName, saveFileFullName);
                                 Logger.OkDebugLine("打码成功");
                             }
                         }
                     }
                     catch (Exception e) {
                         Logger.ErrorDebugLine(e.Message, e);
                     }
                     Logger.InfoDebugLine("复制开始");
                     File.Copy(saveFileFullName, location, overwrite: true);
                     File.Delete(saveFileFullName);
                     Logger.InfoDebugLine("复制完成,删除旧文件");
                     string arguments = NTMinerRegistry.GetArguments();
                     Logger.InfoDebugLine("启动新程序");
                     Process.Start(location, arguments);
                     this.IsDownloading = false;
                     UIThread.Execute(() => {
                         Logger.InfoDebugLine("退出升级器");
                         Application.Current.MainWindow?.Close();
                     });
                 });
             }
             else
             {
                 TimeSpan.FromSeconds(2).Delay().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.ShowDialogEx();
     });
 }