/// <summary> /// 本机同步网络调用 /// </summary> public void CloseMinerStudioAsync(Action callback) { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerStudio); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { callback?.Invoke(); return; } string processName = Path.GetFileNameWithoutExtension(location); if (Process.GetProcessesByName(processName).Length == 0) { callback?.Invoke(); return; } RpcRoot.JsonRpc.PostAsync <ResponseBase>(NTKeyword.Localhost, NTKeyword.MinerStudioPort, _controllerName, nameof(IMinerStudioController.CloseMinerStudio), new object(), (response, e) => { if (!response.IsSuccess()) { try { Windows.TaskKill.Kill(processName, waitForExit: true); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } callback?.Invoke(); }, timeountMilliseconds: 2000); }
public ResponseBase StartMine(WorkRequest request) { ResponseBase response; if (request == null) { response = ResponseBase.InvalidInput("参数错误"); } else { try { // 单机作业的localJson和serverJson是在编辑单机作业时提前传递到挖矿端的所以不需要在开始挖矿时再传递 if (request.WorkId != Guid.Empty && !request.WorkId.IsSelfMineWorkId()) { SpecialPath.WriteMineWorkLocalJsonFile(request.LocalJson); SpecialPath.WriteMineWorkServerJsonFile(request.ServerJson); } if (IsNTMinerOpened()) { WorkRequest innerRequest = new WorkRequest { WorkId = request.WorkId, WorkerName = request.WorkerName }; JsonRpcRoot.PostAsync <ResponseBase>(NTKeyword.Localhost, NTKeyword.MinerClientPort, _minerClientControllerName, nameof(IMinerClientController.StartMine), innerRequest, callback: null, timeountMilliseconds: 3000); response = ResponseBase.Ok("开始挖矿"); } else { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string arguments = NTKeyword.AutoStartCmdParameterName; if (request.WorkId != Guid.Empty) { if (request.WorkId.IsSelfMineWorkId()) { arguments = "--selfWork " + arguments; } else { arguments = "--work " + arguments; } } Windows.Cmd.RunClose(location, arguments); response = ResponseBase.Ok("开始挖矿"); } else { response = ResponseBase.ServerError("开始挖矿,未找到挖矿端程序"); } } } catch (Exception e) { Logger.ErrorDebugLine(e); response = ResponseBase.ServerError(e.Message); } } VirtualRoot.OperationResultSet.Add(response.ToOperationResult()); return(response); }
public ResponseBase UpgradeNTMiner(UpgradeNTMinerRequest request) { ResponseBase response; if (request == null || string.IsNullOrEmpty(request.NTMinerFileName)) { response = ResponseBase.InvalidInput("参数错误"); } else { Task.Factory.StartNew(() => { try { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string arguments = NTKeyword.UpgradeCmdParameterName + request.NTMinerFileName; Windows.Cmd.RunClose(location, arguments); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); response = ResponseBase.Ok("升级挖矿端"); } VirtualRoot.OperationResultSet.Add(response.ToOperationResult()); return(response); }
/// <summary> /// 本机同步网络调用 /// </summary> public void CloseNTMiner() { string location = NTMinerRegistry.GetLocation(); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { return; } string processName = Path.GetFileNameWithoutExtension(location); if (Process.GetProcessesByName(processName).Length == 0) { return; } RpcRoot.PostAsync("localhost", NTKeyword.MinerClientPort, _controllerName, nameof(IMinerClientController.CloseNTMiner), new SignRequest { }, (ResponseBase response, Exception e) => { if (!response.IsSuccess()) { try { Windows.TaskKill.Kill(processName, waitForExit: true); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } }, timeountMilliseconds: 2000); }
public ResponseBase StartMine(WorkRequest request) { ResponseBase response; if (request == null) { response = ResponseBase.InvalidInput("参数错误"); } else { try { if (request.WorkId != Guid.Empty) { File.WriteAllText(SpecialPath.NTMinerLocalJsonFileFullName, request.LocalJson); File.WriteAllText(SpecialPath.NTMinerServerJsonFileFullName, request.ServerJson); } string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (IsNTMinerOpened()) { WorkRequest innerRequest = new WorkRequest { WorkId = request.WorkId }; response = RpcRoot.Post <ResponseBase>(NTKeyword.Localhost, NTKeyword.MinerClientPort, _minerClientControllerName, nameof(IMinerClientController.StartMine), innerRequest); response.Description = "开始挖矿"; } else { if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string arguments = NTKeyword.AutoStartCmdParameterName; if (request.WorkId != Guid.Empty) { arguments += " --work"; } Windows.Cmd.RunClose(location, arguments); response = ResponseBase.Ok("开始挖矿"); } else { response = ResponseBase.ServerError("开始挖矿,未找到挖矿端程序"); } } } catch (Exception e) { Logger.ErrorDebugLine(e); response = ResponseBase.ServerError(e.Message); } } VirtualRoot.OperationResultSet.Add(response.ToOperationResult()); return(response); }
private static void CloseNTMiner() { JsonRpcRoot.PostAsync <ResponseBase>(NTKeyword.Localhost, NTKeyword.MinerClientPort, _minerClientControllerName, nameof(IMinerClientController.CloseNTMiner), new object { }, (response, e) => { bool isClosed = response.IsSuccess(); if (!isClosed) { try { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string processName = Path.GetFileNameWithoutExtension(location); Windows.TaskKill.Kill(processName); } } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } }, timeountMilliseconds: 3000); }
private static void CloseNTMiner() { bool isClosed = false; ResponseBase response = RpcRoot.Post <ResponseBase>(NTKeyword.Localhost, NTKeyword.MinerClientPort, _minerClientControllerName, nameof(IMinerClientController.CloseNTMiner), new SignRequest { }); isClosed = response.IsSuccess(); if (!isClosed) { try { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (!string.IsNullOrEmpty(location) && File.Exists(location)) { string processName = Path.GetFileNameWithoutExtension(location); Windows.TaskKill.Kill(processName); } } catch (Exception e) { Logger.ErrorDebugLine(e); } } }
/// <summary> /// 本机同步网络调用 /// </summary> public void CloseMinerStudio() { string location = NTMinerRegistry.GetLocation(); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { return; } string processName = Path.GetFileNameWithoutExtension(location); if (Process.GetProcessesByName(processName).Length == 0) { return; } bool isClosed = false; try { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerStudioPort.ToString()}/api/{_controllerName}/{nameof(IMinerStudioController.CloseMinerStudio)}", new SignRequest { }); ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result; isClosed = response.IsSuccess(); } } catch (Exception e) { Logger.ErrorDebugLine(e); } if (!isClosed) { try { Windows.TaskKill.Kill(processName, waitForExit: true); } catch (Exception e) { Logger.ErrorDebugLine(e); } } }
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(); }
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.GetBrandId(location, Consts.KernelBrandId); if (kernelBrandId != Guid.Empty) { Logger.InfoDebugLine("内核品牌打码开始"); VirtualRoot.TagBrandId(Consts.KernelBrandId, kernelBrandId, saveFileFullName, saveFileFullName); Logger.OkDebugLine("内核品牌打码成功"); } Guid poolBrandId = VirtualRoot.GetBrandId(location, Consts.PoolBrandId); if (poolBrandId != Guid.Empty) { Logger.InfoDebugLine("矿池打码开始"); VirtualRoot.TagBrandId(Consts.PoolBrandId, poolBrandId, saveFileFullName, saveFileFullName); Logger.OkDebugLine("矿池打码成功"); } } } catch (Exception e) { Logger.ErrorDebugLine(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(); }); }
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(); }); }
private static bool TryGetMinerClientLocation(out string location) { location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); return(!string.IsNullOrEmpty(location) && File.Exists(location)); }
private MainWindowViewModel() { if (App.IsInDesignMode) { return; } this.Refresh(); this.CancelDownload = new DelegateCommand(() => { this.cancel?.Invoke(); this.IsDownloading = false; }); this.Install = new DelegateCommand(() => { if (this.IsDownloading) { return; } 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; this.BtnCancelVisible = Visibility.Collapsed; if (isSuccess) { this.DownloadMessage = "更新成功,正在重启"; if (VirtualRoot.IsMinerStudio) { Client.MinerStudioService.CloseMinerStudio(); } else { Client.MinerClientService.CloseNTMiner(); } TimeSpan.FromSeconds(2).Delay().ContinueWith((t) => { string location = NTMinerRegistry.GetLocation(); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { location = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ntMinerFile); } File.Copy(saveFileFullName, location, overwrite: true); File.Delete(saveFileFullName); string arguments = NTMinerRegistry.GetArguments(); Process.Start(location, arguments); this.IsDownloading = false; UIThread.Execute(() => { Application.Current.MainWindow.Close(); }); }); } else { TimeSpan.FromSeconds(2).Delay().ContinueWith((t) => { this.IsDownloading = false; }); } }, cancel: out cancel); }); 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(); }); }