Exemplo n.º 1
0
 private void RefreshServerJsonFile()
 {
     RpcRoot.OfficialServer.AppSettingService.GetJsonFileVersionAsync(ClientAppType.AppType, HomePath.ExportServerJsonFileName, serverState => {
         AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion);
         string localServerJsonFileVersion = GetServerJsonVersion();
         if (!string.IsNullOrEmpty(serverState.JsonFileVersion) && localServerJsonFileVersion != serverState.JsonFileVersion)
         {
             RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson((data) => {
                 if (data == null || data.Length == 0)
                 {
                     return;
                 }
                 string rawJson = Encoding.UTF8.GetString(data);
                 HomePath.WriteServerJsonFile(rawJson);
                 SetServerJsonVersion(serverState.JsonFileVersion);
                 ContextReInit();
                 VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), $"刷新server.json配置", toConsole: true);
             });
         }
         else
         {
             NTMinerConsole.DevDebug("server.json没有新版本");
         }
         if (serverState.WsStatus == WsStatus.Online)
         {
             VirtualRoot.RaiseEvent(new WsServerOkEvent());
         }
     });
 }
Exemplo n.º 2
0
 public ResponseBase StartMine([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"通过群控开始挖矿", toConsole: true);
         WorkType workType = WorkType.None;
         if (request.WorkId != Guid.Empty)
         {
             workType = WorkType.MineWork;
             if (request.WorkId.IsSelfMineWorkId())
             {
                 workType = WorkType.SelfWork;
             }
         }
         NTMinerContext.Instance.RestartMine(workType, request.WorkerName);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 3
0
 private static void SwitchRadeonGpuMode(bool on)
 {
     SwitchRadeonGpu.SwitchRadeonGpu.Run(on, (isSuccess, e) => {
         if (isSuccess)
         {
             if (on)
             {
                 VirtualRoot.ThisLocalInfo(nameof(App), "开启A卡计算模式成功", OutEnum.Success);
             }
             else
             {
                 VirtualRoot.ThisLocalInfo(nameof(App), "关闭A卡计算模式成功", OutEnum.Success);
             }
         }
         else if (e != null)
         {
             VirtualRoot.Out.ShowError(e.Message, delaySeconds: 4);
         }
         else
         {
             if (on)
             {
                 VirtualRoot.ThisLocalError(nameof(App), "开启A卡计算模式失败", OutEnum.Warn);
             }
             else
             {
                 VirtualRoot.ThisLocalError(nameof(App), "关闭A卡计算模式失败", OutEnum.Warn);
             }
         }
     });
 }
Exemplo n.º 4
0
 public void RunAction([FromBody] DataRequest <MinerClientActionType> request)
 {
     if (request == null)
     {
         return;
     }
     VirtualRoot.ThisLocalInfo(nameof(MinerClientController), request.Data.GetDescription(), toConsole: true);
     VirtualRoot.Execute(new MinerClientActionCommand(request.Data));
 }
Exemplo n.º 5
0
 private static void ShowSwitchRadeonGpu(bool on)
 {
     if (on)
     {
         VirtualRoot.ThisLocalInfo(nameof(AppRoot), "开启A卡计算模式成功", OutEnum.Success);
     }
     else
     {
         VirtualRoot.ThisLocalInfo(nameof(AppRoot), "关闭A卡计算模式成功", OutEnum.Success);
     }
 }
            private static void KernelProcessDaemon(IMineContext mineContext, Action clear)
            {
                if (_kernelProcessDaemon != null)
                {
                    VirtualRoot.DeletePath(_kernelProcessDaemon);
                    _kernelProcessDaemon = null;
                    clear?.Invoke();
                }
                string processName = mineContext.Kernel.GetProcessName();

                _kernelProcessDaemon = VirtualRoot.BuildEventPath <Per1MinuteEvent>("周期性检查挖矿内核是否消失,如果消失尝试重启", LogEnum.DevConsole,
                                                                                    action: message => {
                    if (mineContext == Instance.LockedMineContext)
                    {
                        if (!string.IsNullOrEmpty(processName))
                        {
                            Process[] processes = Process.GetProcessesByName(processName);
                            if (processes.Length == 0)
                            {
                                mineContext.AutoRestartKernelCount += 1;
                                VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), processName + $"挖矿内核进程消失", toConsole: true);
                                if (Instance.MinerProfile.IsAutoRestartKernel && mineContext.AutoRestartKernelCount <= Instance.MinerProfile.AutoRestartKernelTimes)
                                {
                                    VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"尝试第{mineContext.AutoRestartKernelCount.ToString()}次重启,共{Instance.MinerProfile.AutoRestartKernelTimes.ToString()}次", toConsole: true);
                                    Instance.RestartMine();
                                    Instance.LockedMineContext.AutoRestartKernelCount = mineContext.AutoRestartKernelCount;
                                }
                                else
                                {
                                    Instance.StopMineAsync(StopMineReason.KernelProcessLost);
                                }
                                if (_kernelProcessDaemon != null)
                                {
                                    VirtualRoot.DeletePath(_kernelProcessDaemon);
                                    clear?.Invoke();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (_kernelProcessDaemon != null)
                        {
                            VirtualRoot.DeletePath(_kernelProcessDaemon);
                            _kernelProcessDaemon = null;
                            clear?.Invoke();
                        }
                    }
                });
            }
Exemplo n.º 7
0
        public void BenchmarkTest()
        {
            VirtualRoot.Execute(new ClearLocalMessageSetCommand());
            int times = 2000;

            Assert.IsTrue(times > NTKeyword.LocalMessageSetCapacity);
            string content = "this is a test";

            for (int i = 0; i < times; i++)
            {
                VirtualRoot.ThisLocalInfo(nameof(LocalMessageTests), content);
            }
            Assert.IsTrue(NTMinerContext.Instance.LocalMessageSet.AsEnumerable().Count() == NTKeyword.LocalMessageSetCapacity);
        }
Exemplo n.º 8
0
 public ResponseBase StopMine([FromBody] object request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), "通过群控停止挖矿", toConsole: true);
         VirtualRoot.Execute(new StopMineCommand());
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 9
0
 public ResponseBase StartMine([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"开始挖矿", toConsole: true);
         NTMinerRoot.Instance.RestartMine(isWork: request.WorkId != Guid.Empty);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 10
0
 public ResponseBase StopMine([FromBody] SignRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"停止挖矿", toConsole: true);
         NTMinerRoot.Instance.StopMineAsync(StopMineReason.RPCUserAction);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 11
0
 public ResponseBase SetMinerProfileProperty([FromBody] SetMinerProfilePropertyRequest request)
 {
     if (request == null || string.IsNullOrEmpty(request.PropertyName))
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"设置挖矿参数");
         NTMinerRoot.Instance.MinerProfile.SetMinerProfileProperty(request.PropertyName, request.Value);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 12
0
 public ResponseBase CloseNTMiner([FromBody] SignRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"退出挖矿端");
         TimeSpan.FromMilliseconds(100).Delay().ContinueWith((t) => {
             VirtualRoot.Execute(new CloseNTMinerCommand());
         });
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 13
0
 private void RefreshServerJsonFile()
 {
     RpcRoot.OfficialServer.GetJsonFileVersionAsync(EntryAssemblyInfo.ServerJsonFileName, serverState => {
         AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion);
         string localServerJsonFileVersion = GetServerJsonVersion();
         if (!string.IsNullOrEmpty(serverState.JsonFileVersion) && localServerJsonFileVersion != serverState.JsonFileVersion)
         {
             GetAliyunServerJson((data) => {
                 string rawJson = Encoding.UTF8.GetString(data);
                 SpecialPath.WriteServerJsonFile(rawJson);
                 SetServerJsonVersion(serverState.JsonFileVersion);
                 // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false
                 ContextReInit(isWork: VirtualRoot.IsMinerStudio);
                 VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"刷新server.json配置", toConsole: true);
             });
         }
         else
         {
             Write.DevDebug("server.json没有新版本");
         }
     });
 }
Exemplo n.º 14
0
 public void Init(Action callback)
 {
     Task.Factory.StartNew(() => {
         bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase);
         if (isWork)   // 是作业
         {
             DoInit(isWork, callback);
             if (VirtualRoot.IsMinerClient)
             {
                 NTMinerRegistry.SetIsLastIsWork(true);
             }
         }
         else   // 不是作业
         {
             if (VirtualRoot.IsMinerClient)
             {
                 NTMinerRegistry.SetIsLastIsWork(false);
             }
             // 如果是Debug模式且不是群控客户端则使用本地数据库初始化
             bool useLocalDb = DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio;
             if (useLocalDb)
             {
                 DoInit(isWork: false, callback: callback);
             }
             else
             {
                 Logger.InfoDebugLine(nameof(GetAliyunServerJson));
                 GetAliyunServerJson((data) => {
                     // 如果server.json未下载成功则不覆写本地server.json
                     if (data != null && data.Length != 0)
                     {
                         Logger.InfoDebugLine($"{nameof(GetAliyunServerJson)}成功");
                         var serverJson = Encoding.UTF8.GetString(data);
                         if (!string.IsNullOrEmpty(serverJson))
                         {
                             SpecialPath.WriteServerJsonFile(serverJson);
                         }
                         OfficialServer.GetJsonFileVersionAsync(MainAssemblyInfo.ServerJsonFileName, serverState => {
                             SetServerJsonVersion(serverState.JsonFileVersion);
                             AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion);
                             if (Math.Abs((long)Timestamp.GetTimestamp() - (long)serverState.Time) < Timestamp.DesyncSeconds)
                             {
                                 Logger.OkDebugLine($"本机和服务器时间一致或相差不超过 {Timestamp.DesyncSeconds.ToString()} 秒");
                             }
                             else
                             {
                                 Write.UserWarn($"本机和服务器时间不同步,请调整,本地:{DateTime.Now.ToString()},服务器:{Timestamp.FromTimestamp(serverState.Time).ToString()}。此问题不影响挖矿。");
                             }
                         });
                     }
                     else
                     {
                         if (!File.Exists(SpecialPath.ServerJsonFileFullName))
                         {
                             VirtualRoot.ThisLocalError(nameof(NTMinerRoot), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn);
                         }
                         else
                         {
                             VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), "配置文件下载失败,使用最近一次成功下载的配置文件", OutEnum.Warn);
                         }
                     }
                     DoInit(isWork, callback);
                 });
                 #region 发生了用户活动时检查serverJson是否有新版本
                 VirtualRoot.BuildEventPath <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole,
                                                              action: message => {
                     RefreshServerJsonFile();
                 });
                 #endregion
             }
         }
         VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"启动{VirtualRoot.AppName}");
     });
 }
Exemplo n.º 15
0
        private void Link()
        {
            VirtualRoot.BuildCmdPath <RegCmdHereCommand>(action: message => {
                try {
                    Windows.Cmd.RegCmdHere();
                    VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), "windows右键命令行添加成功", OutEnum.Success);
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                    VirtualRoot.ThisLocalError(nameof(NTMinerRoot), "windows右键命令行添加失败", OutEnum.Warn);
                }
            });
            VirtualRoot.BuildEventPath <Per1MinuteEvent>("每1分钟阻止系统休眠", LogEnum.None,
                                                         action: message => {
                Windows.Power.PreventSleep();
            });
            #region 挖矿开始时将无份额内核重启份额计数置0
            int      shareCount = 0;
            DateTime shareOn    = DateTime.Now;
            VirtualRoot.BuildEventPath <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole,
                                                          action: message => {
                // 将无份额内核重启份额计数置0
                shareCount = 0;
                if (!message.MineContext.IsRestart)
                {
                    shareOn = DateTime.Now;
                }
            });
            #endregion
            #region 每20秒钟检查是否需要重启
            VirtualRoot.BuildEventPath <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None,
                                                          action: message => {
                #region 重启电脑
                try {
                    if (MinerProfile.IsPeriodicRestartComputer)
                    {
                        if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes)
                        {
                            string content = $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartComputerMinutes.ToString()}分钟重启电脑";
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), content, toConsole: true);
                            Windows.Power.Restart(60);
                            VirtualRoot.Execute(new CloseNTMinerCommand(content));
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 周期重启内核
                try {
                    if (IsMining && MinerProfile.IsPeriodicRestartKernel)
                    {
                        if ((DateTime.Now - LockedMineContext.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartKernelHours + MinerProfile.PeriodicRestartKernelMinutes)
                        {
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartKernelMinutes.ToString()}分钟重启内核", toConsole: true);
                            RestartMine();
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 无份额重启内核
                try {
                    if (IsMining && this.LockedMineContext.MainCoin != null)
                    {
                        int totalShare       = 0;
                        bool restartComputer = MinerProfile.IsNoShareRestartComputer && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes;
                        bool restartKernel   = MinerProfile.IsNoShareRestartKernel && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes;
                        if (restartComputer || restartKernel)
                        {
                            ICoinShare mainCoinShare = this.CoinShareSet.GetOrCreate(this.LockedMineContext.MainCoin.GetId());
                            totalShare = mainCoinShare.TotalShareCount;
                            if ((this.LockedMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null)
                            {
                                ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId());
                                totalShare += dualCoinShare.TotalShareCount;
                            }
                            // 如果份额没有增加
                            if (shareCount == totalShare)
                            {
                                if (restartComputer)
                                {
                                    if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart)
                                    {
                                        VirtualRoot.Execute(new SetAutoStartCommand(true, true));
                                    }
                                    string content = $"{MinerProfile.NoShareRestartComputerMinutes.ToString()}分钟无份额重启电脑";
                                    VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), content, toConsole: true);
                                    Windows.Power.Restart(60);
                                    VirtualRoot.Execute(new CloseNTMinerCommand(content));
                                    return;    // 退出
                                }
                                // 产生过份额或者已经两倍重启内核时间了
                                if (restartKernel && (totalShare > 0 || (DateTime.Now - shareOn).TotalMinutes > 2 * MinerProfile.NoShareRestartKernelMinutes))
                                {
                                    VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"{MinerProfile.NoShareRestartKernelMinutes.ToString()}分钟无份额重启内核", toConsole: true);
                                    RestartMine();
                                    return;    // 退出
                                }
                            }
                            if (totalShare > shareCount)
                            {
                                shareCount = totalShare;
                                shareOn    = DateTime.Now;
                            }
                        }
Exemplo n.º 16
0
 private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
 {
     VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), $"Windows 会话切换, 因为{((WindowsSessionSwitchReason)e.Reason).GetDescription()}");
 }
Exemplo n.º 17
0
 private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
 {
     VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"Windows SessionSwitch, Reason:{e.Reason}");
 }
Exemplo n.º 18
0
        private void Link()
        {
            VirtualRoot.BuildCmdPath <RegCmdHereCommand>(path: message => {
                try {
                    Cmd.RegCmdHere();
                    VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), "添加windows右键命令行成功");
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                    VirtualRoot.ThisLocalError(nameof(NTMinerContext), "添加windows右键命令行失败", OutEnum.Warn);
                }
            }, location: this.GetType());
            VirtualRoot.BuildCmdPath <UnRegCmdHereCommand>(path: message => {
                try {
                    Cmd.UnRegCmdHere();
                    VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), "移除windows右键命令行成功");
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                    VirtualRoot.ThisLocalError(nameof(NTMinerContext), "移除windows右键命令行失败", OutEnum.Warn);
                }
            }, location: this.GetType());
            VirtualRoot.BuildEventPath <Per1MinuteEvent>("每1分钟阻止系统休眠", LogEnum.None,
                                                         path: message => {
                Power.PreventSleep(MinerProfile.IsPreventDisplaySleep);
            }, location: this.GetType());
            #region 挖矿开始时将无份额内核重启份额计数置0
            int      shareCount           = 0;
            DateTime shareOn              = DateTime.Now;
            DateTime highSpeedOn          = DateTime.Now;
            DateTime overClockHighSpeedOn = DateTime.Now;
            VirtualRoot.BuildEventPath <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole,
                                                          path: message => {
                // 将无份额内核重启份额计数置0
                shareCount           = 0;
                highSpeedOn          = DateTime.Now;
                overClockHighSpeedOn = DateTime.Now;
                if (!message.MineContext.IsRestart)
                {
                    // 当不是内核重启时更新shareOn,如果是内核重启不用更新shareOn从而给不干扰无内核矿机重启的逻辑
                    shareOn = DateTime.Now;
                }
            }, location: this.GetType());
            #endregion
            #region 每20秒钟检查是否需要重启
            VirtualRoot.BuildEventPath <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None,
                                                          path: message => {
                #region 低算力重启电脑
                if (IsMining && LockedMineContext.ProcessCreatedOn != DateTime.MinValue)
                {
                    var coinProfile = MinerProfile.GetCoinProfile(MinerProfile.CoinId);
                    if (coinProfile.IsLowSpeedRestartComputer && coinProfile.LowSpeed != 0 && coinProfile.LowSpeedRestartComputerMinutes > 0)
                    {
                        IGpuSpeed totalSpeed = GpusSpeed.CurrentSpeed(GpuAllId);
                        if (totalSpeed.MainCoinSpeed.SpeedOn.AddMinutes(coinProfile.LowSpeedRestartComputerMinutes) >= message.BornOn)
                        {
                            if (totalSpeed.MainCoinSpeed.Value.ToNearSpeed(coinProfile.LowSpeed) >= coinProfile.LowSpeed)
                            {
                                highSpeedOn = message.BornOn;
                            }
                        }
                        if (highSpeedOn.AddMinutes(coinProfile.LowSpeedRestartComputerMinutes) < message.BornOn)
                        {
                            string coinCode = string.Empty;
                            if (ServerContext.CoinSet.TryGetCoin(MinerProfile.CoinId, out ICoin coin))
                            {
                                coinCode = coin.Code;
                            }
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{coinCode}总算力持续{coinProfile.LowSpeedRestartComputerMinutes}分钟低于{coinProfile.LowSpeed}重启电脑", toConsole: true);
                            VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10));
                            if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart)
                            {
                                VirtualRoot.Execute(new SetAutoStartCommand(true, true));
                            }
                            return;
                        }
                    }
                    else
                    {
                        highSpeedOn = message.BornOn;
                    }
                }
                #endregion

                #region 低算力重新应用超频
                if (IsMining && LockedMineContext.ProcessCreatedOn != DateTime.MinValue)
                {
                    var coinProfile = MinerProfile.GetCoinProfile(MinerProfile.CoinId);
                    if (coinProfile.IsLowSpeedReOverClock && coinProfile.OverClockLowSpeed != 0 && coinProfile.LowSpeedReOverClockMinutes > 0)
                    {
                        IGpuSpeed totalSpeed = GpusSpeed.CurrentSpeed(GpuAllId);
                        if (totalSpeed.MainCoinSpeed.SpeedOn.AddMinutes(coinProfile.LowSpeedReOverClockMinutes) >= message.BornOn)
                        {
                            if (totalSpeed.MainCoinSpeed.Value.ToNearSpeed(coinProfile.OverClockLowSpeed) >= coinProfile.OverClockLowSpeed)
                            {
                                overClockHighSpeedOn = message.BornOn;
                            }
                        }
                        if (overClockHighSpeedOn.AddMinutes(coinProfile.LowSpeedReOverClockMinutes) < message.BornOn)
                        {
                            string coinCode = string.Empty;
                            if (ServerContext.CoinSet.TryGetCoin(MinerProfile.CoinId, out ICoin coin))
                            {
                                coinCode = coin.Code;
                            }
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{coinCode}总算力持续{coinProfile.LowSpeedReOverClockMinutes}分钟低于{coinProfile.OverClockLowSpeed}重新应用超频", toConsole: true);
                            VirtualRoot.Execute(new CoinOverClockCommand(MinerProfile.CoinId));
                        }
                    }
                    else
                    {
                        overClockHighSpeedOn = message.BornOn;
                    }
                }
                #endregion

                #region 周期重启电脑
                try {
                    if (MinerProfile.IsPeriodicRestartComputer)
                    {
                        if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes)
                        {
                            string content = $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartComputerMinutes.ToString()}分钟重启电脑";
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), content, toConsole: true);
                            VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10));
                            if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart)
                            {
                                VirtualRoot.Execute(new SetAutoStartCommand(true, true));
                            }
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 周期重启内核
                try {
                    if (IsMining && MinerProfile.IsPeriodicRestartKernel && LockedMineContext.MineStartedOn != DateTime.MinValue)
                    {
                        DateTime dt = GetKernelRestartBaseOnTime();
                        if ((DateTime.Now - dt).TotalMinutes > 60 * MinerProfile.PeriodicRestartKernelHours + MinerProfile.PeriodicRestartKernelMinutes)
                        {
                            VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"每运行{MinerProfile.PeriodicRestartKernelHours.ToString()}小时{MinerProfile.PeriodicRestartKernelMinutes.ToString()}分钟重启内核", toConsole: true);
                            RestartMine();
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 无份额重启内核
                try {
                    if (IsMining && this.LockedMineContext.MainCoin != null)
                    {
                        int totalShare       = 0;
                        bool restartComputer = MinerProfile.NoShareRestartComputerMinutes > 0 && MinerProfile.IsNoShareRestartComputer && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes;
                        bool restartKernel   = MinerProfile.NoShareRestartKernelMinutes > 0 && MinerProfile.IsNoShareRestartKernel && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes;
                        if (restartComputer || restartKernel)
                        {
                            ICoinShare mainCoinShare = this.CoinShareSet.GetOrCreate(this.LockedMineContext.MainCoin.GetId());
                            totalShare = mainCoinShare.TotalShareCount;
                            if ((this.LockedMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null)
                            {
                                ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId());
                                totalShare += dualCoinShare.TotalShareCount;
                            }
                            // 如果份额没有增加
                            if (shareCount == totalShare)
                            {
                                // 重启电脑,基于MineStartedOn
                                bool isRestartComputerMinutes = (DateTime.Now - this.LockedMineContext.MineStartedOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes;
                                if (restartComputer && isRestartComputerMinutes)
                                {
                                    if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart)
                                    {
                                        VirtualRoot.Execute(new SetAutoStartCommand(true, true));
                                    }
                                    string content = $"{MinerProfile.NoShareRestartComputerMinutes.ToString()}分钟无份额重启电脑";
                                    VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), content, toConsole: true);
                                    VirtualRoot.Execute(new ShowRestartWindowsCommand(countDownSeconds: 10));
                                    return;    // 退出
                                }
                                // 重启内核,如果MineRestartedOn不是DateTime.MineValue则基于MineRestartedOn
                                DateTime dt = GetKernelRestartBaseOnTime();
                                bool isRestartKernelMinutes = (DateTime.Now - dt).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes;
                                if (restartKernel && isRestartKernelMinutes)
                                {
                                    VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), $"{MinerProfile.NoShareRestartKernelMinutes.ToString()}分钟无份额重启内核", toConsole: true);
                                    RestartMine();
                                    return;    // 退出
                                }
                            }
                            if (totalShare > shareCount)
                            {
                                shareCount = totalShare;
                                shareOn    = DateTime.Now;
                            }
                        }
Exemplo n.º 19
0
        private void BuildPaths()
        {
            VirtualRoot.AddCmdPath <MinerClientActionCommand>(action: message => {
                #region
                try {
                    switch (message.ActionType)
                    {
                    case MinerClientActionType.SwitchRadeonGpuOn:
                        VirtualRoot.Execute(new SwitchRadeonGpuCommand(on: true));
                        break;

                    case MinerClientActionType.SwitchRadeonGpuOff:
                        VirtualRoot.Execute(new SwitchRadeonGpuCommand(on: false));
                        break;

                    case MinerClientActionType.BlockWAU:
                        VirtualRoot.Execute(new BlockWAUCommand());
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion
            }, location: this.GetType());
            #region 处理显示主界面命令
            VirtualRoot.AddCmdPath <ShowMainWindowCommand>(action: message => {
                UIThread.Execute(() => {
                    _appViewFactory.ShowMainWindow(message.IsToggle, out Window _);
                    // 使状态栏显示显示最新状态
                    if (NTMinerContext.Instance.IsMining)
                    {
                        var mainCoin = NTMinerContext.Instance.LockedMineContext.MainCoin;
                        if (mainCoin == null)
                        {
                            return;
                        }
                        var coinShare = NTMinerContext.Instance.CoinShareSet.GetOrCreate(mainCoin.GetId());
                        VirtualRoot.RaiseEvent(new ShareChangedEvent(PathId.Empty, coinShare));
                        if ((NTMinerContext.Instance.LockedMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null)
                        {
                            coinShare = NTMinerContext.Instance.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId());
                            VirtualRoot.RaiseEvent(new ShareChangedEvent(PathId.Empty, coinShare));
                        }
                        AppRoot.GpuSpeedVms.Refresh();
                    }
                });
            }, location: this.GetType());
            #endregion
            #region 周期确保守护进程在运行
            VirtualRoot.AddEventPath <Per1MinuteEvent>("周期确保守护进程在运行", LogEnum.DevConsole,
                                                       action: message => {
                Daemon.DaemonUtil.RunNTMinerDaemon();
                NoDevFee.NoDevFeeUtil.RunNTMinerNoDevFee();
            }, location: this.GetType());
            #endregion
            #region 开始和停止挖矿后
            VirtualRoot.AddEventPath <StartingMineEvent>("开始挖矿时更新挖矿按钮状态", LogEnum.DevConsole,
                                                         action: message => {
                AppRoot.MinerProfileVm.IsMining = true;
                // 因为无界面模式不一定会构建挖矿状态按钮,所以放在这里而不放在挖矿按钮的VM中
                StartStopMineButtonViewModel.Instance.BtnStopText = "正在挖矿";
            }, location: this.GetType());
            VirtualRoot.AddEventPath <MineStartedEvent>("启动1080ti小药丸、启动DevConsole? 更新挖矿按钮状态", LogEnum.DevConsole,
                                                        action: message => {
                // 启动DevConsole
                if (NTMinerContext.IsUseDevConsole)
                {
                    var mineContext     = message.MineContext;
                    string poolIp       = mineContext.MainCoinPool.GetIp();
                    string consoleTitle = mineContext.MainCoinPool.Server;
                    Daemon.DaemonUtil.RunDevConsoleAsync(poolIp, consoleTitle);
                }
                OhGodAnETHlargementPill.OhGodAnETHlargementPillUtil.Start();
            }, location: this.GetType());
            VirtualRoot.AddEventPath <MineStopedEvent>("停止挖矿后停止1080ti小药丸 挖矿停止后更新界面挖矿状态", LogEnum.DevConsole,
                                                       action: message => {
                AppRoot.MinerProfileVm.IsMining = false;
                // 因为无界面模式不一定会构建挖矿状态按钮,所以放在这里而不放在挖矿按钮的VM中
                StartStopMineButtonViewModel.Instance.BtnStopText = "尚未开始";
                OhGodAnETHlargementPill.OhGodAnETHlargementPillUtil.Stop();
            }, location: this.GetType());
            #endregion
            #region 处理禁用win10系统更新
            VirtualRoot.AddCmdPath <BlockWAUCommand>(action: message => {
                NTMiner.Windows.WindowsUtil.BlockWAU().ContinueWith(t => {
                    if (t.Exception == null)
                    {
                        VirtualRoot.ThisLocalInfo(nameof(App), "禁用windows系统更新成功", OutEnum.Success);
                    }
                    else
                    {
                        VirtualRoot.ThisLocalError(nameof(App), "禁用windows系统更新失败", OutEnum.Error);
                    }
                });
            }, location: this.GetType());
            #endregion
            #region 优化windows
            VirtualRoot.AddCmdPath <Win10OptimizeCommand>(action: message => {
                NTMiner.Windows.WindowsUtil.Win10Optimize(e => {
                    if (e == null)
                    {
                        VirtualRoot.ThisLocalInfo(nameof(App), "优化Windows成功", OutEnum.Success);
                    }
                    else
                    {
                        VirtualRoot.ThisLocalError(nameof(App), "优化Windows失败", OutEnum.Error);
                    }
                });
            }, location: this.GetType());
            #endregion
            #region 处理开启A卡计算模式
            VirtualRoot.AddCmdPath <SwitchRadeonGpuCommand>(action: message => {
                if (NTMinerContext.Instance.GpuSet.GpuType == GpuType.AMD)
                {
                    AppRoot.SwitchRadeonGpu(message.On);
                }
            }, location: this.GetType());
            #endregion
            #region 处理A卡驱动签名
            VirtualRoot.AddCmdPath <AtikmdagPatcherCommand>(action: message => {
                if (NTMinerContext.Instance.GpuSet.GpuType == GpuType.AMD)
                {
                    AppRoot.OpenAtikmdagPatcher();
                }
            }, location: this.GetType());
            #endregion
            #region 启用或禁用windows远程桌面
            VirtualRoot.AddCmdPath <EnableRemoteDesktopCommand>(action: message => {
                if (NTMinerRegistry.GetIsRdpEnabled())
                {
                    return;
                }
                string msg = "确定启用Windows远程桌面吗?";
                DialogWindow.ShowSoftDialog(new DialogWindowViewModel(
                                                message: msg,
                                                title: "确认",
                                                onYes: () => {
                    NTMinerRegistry.SetIsRdpEnabled(true);
                    Firewall.AddRdpRule();
                }));
            }, location: this.GetType());
            #endregion
            #region 启用或禁用windows开机自动登录
            VirtualRoot.AddCmdPath <EnableOrDisableWindowsAutoLoginCommand>(action: message => {
                if (NTMiner.Windows.OS.Instance.IsAutoAdminLogon)
                {
                    return;
                }
                if (NTMiner.Windows.OS.Instance.IsGEWindows2004)
                {
                    WindowsAutoLogon.ShowWindow();
                }
                else
                {
                    VirtualRoot.Execute(new UnTopmostCommand());
                    NTMiner.Windows.Cmd.RunClose("control", "userpasswords2");
                }
            }, location: this.GetType());
            #endregion
        }
Exemplo n.º 20
0
 public void Init(Action callback)
 {
     Task.Factory.StartNew(() => {
         bool isSelfWork = Environment.GetCommandLineArgs().Contains("--selfWork", StringComparer.OrdinalIgnoreCase);
         bool isWork     = isSelfWork || Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase);
         _workType       = isSelfWork ? WorkType.SelfWork : (isWork ? WorkType.MineWork : WorkType.None);
         if (ClientAppType.IsMinerClient)
         {
             NTMinerRegistry.SetWorkType(_workType);
         }
         if (isWork)
         {
             DoInit(callback);
         }
         else
         {
             // 如果是Debug模式且不是群控客户端则使用本地数据库初始化
             bool useLocalDb = DevMode.IsDevMode && !ClientAppType.IsMinerStudio;
             if (useLocalDb)
             {
                 DoInit(callback);
             }
             else
             {
                 Logger.InfoDebugLine(nameof(RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson));
                 RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson((data) => {
                     // 如果server.json未下载成功则不覆写本地server.json
                     if (data != null && data.Length != 0)
                     {
                         Logger.InfoDebugLine($"{nameof(RpcRoot.OSSService.AliyunOSSService.GetAliyunServerJson)} ok");
                         var serverJson = Encoding.UTF8.GetString(data);
                         if (!string.IsNullOrEmpty(serverJson))
                         {
                             HomePath.WriteServerJsonFile(serverJson);
                         }
                         RpcRoot.OfficialServer.AppSettingService.GetJsonFileVersionAsync(ClientAppType.AppType, HomePath.ExportServerJsonFileName, serverState => {
                             SetServerJsonVersion(serverState.JsonFileVersion);
                             AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion);
                             if (serverState.Time == 0)
                             {
                                 NTMinerConsole.UserWarn("网络不通或服务器暂时不可用,请检查矿机网络");
                             }
                             else if (Math.Abs((long)Timestamp.GetTimestamp() - (long)serverState.Time) >= Timestamp.DesyncSeconds)
                             {
                                 NTMinerConsole.UserWarn($"本机和服务器时间不同步,请调整,本地:{DateTime.Now.ToString()},服务器:{Timestamp.FromTimestamp(serverState.Time).ToString()}。此问题不影响挖矿。");
                             }
                         });
                     }
                     else
                     {
                         if (!File.Exists(HomePath.ServerJsonFileFullName))
                         {
                             VirtualRoot.ThisLocalError(nameof(NTMinerContext), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn);
                         }
                         else
                         {
                             VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), "配置文件下载失败,使用最近一次成功下载的配置文件");
                         }
                     }
                     DoInit(callback);
                 });
             }
         }
         VirtualRoot.ThisLocalInfo(nameof(NTMinerContext), $"启动{VirtualRoot.AppName}");
     });
 }
Exemplo n.º 21
0
 public void OverClock()
 {
     VirtualRoot.ThisLocalInfo(nameof(MinerClientController), $"通过群控刷新超频", toConsole: true);
     NTMinerContext.Instance.GpuProfileSet.RemoteOverClock();
 }