Ejemplo n.º 1
0
 public void SetAutoBootStart([FromUri] bool autoBoot, [FromUri] bool autoStart)
 {
     NTMinerRegistry.SetIsAutoBoot(autoBoot);
     NTMinerRegistry.SetIsAutoStart(autoStart);
     if (IsNTMinerOpened())
     {
         using (HttpClient client = new HttpClient()) {
             Task <HttpResponseMessage> message = client.PostAsync($"http://localhost:{Consts.MinerClientPort}/api/MinerClient/RefreshAutoBootStart", null);
             Write.DevDebug($"{nameof(SetAutoBootStart)} {message.Result.ReasonPhrase}");
         }
     }
 }
Ejemplo n.º 2
0
        public static void SetAutoStart(bool isAutoBoot, bool isAutoStart)
        {
            var db = GetDb();

            if (db != null)
            {
                using (db) {
                    var col = db.GetCollection <MinerProfileData>();
                    MinerProfileData data = col.FindById(MinerProfileData.DefaultId);
                    if (data != null)
                    {
                        data.IsAutoBoot = isAutoBoot;
                        NTMinerRegistry.SetIsAutoStart(isAutoStart);
                        col.Update(data);
                    }
                }
            }
        }
        // 从磁盘读取local.json反序列化为LocalJson对象
        private static void LocalJsonInit()
        {
            if (!_localJsonInited)
            {
                lock (_locker) {
                    if (!_localJsonInited)
                    {
                        string      localJson   = HomePath.ReadLocalJsonFile(_workType);
                        LocalJsonDb localJsonDb = null;
                        if (!string.IsNullOrEmpty(localJson))
                        {
                            localJsonDb = VirtualRoot.JsonSerializer.Deserialize <LocalJsonDb>(localJson);
                        }
                        if (localJsonDb == null)
                        {
                            if (ClientAppType.IsMinerClient)
                            {
                                localJsonDb = JsonDb.LocalJsonDb.ConvertFromNTMinerContext();
                                VirtualRoot.ThisLocalWarn(nameof(NTMinerContext), "当前作业由本机数据自动生成,因为本机没有作业记录,请先在群控端创建或编辑作业。", OutEnum.Warn, toConsole: true);
                            }
                            else
                            {
                                localJsonDb = new LocalJsonDb();
                            }
                        }
                        _localJsonDb = localJsonDb;

                        if (ClientAppType.IsMinerClient)
                        {
                            #region 因为是群控作业,将开机启动和自动挖矿设置为true
                            var repository = new LiteDbReadWriteRepository <MinerProfileData>(HomePath.LocalDbFileFullName);
                            MinerProfileData localProfile = repository.GetByKey(MinerProfileData.DefaultId);
                            if (localProfile != null)
                            {
                                MinerProfileData.CopyWorkIgnoreValues(localProfile, _localJsonDb.MinerProfile);
                                // 如果是作业模式则必须设置为开机自动重启
                                if (!localProfile.IsAutoBoot)
                                {
                                    localProfile.IsAutoBoot = true;
                                    repository.Update(localProfile);
                                }
                            }
                            _localJsonDb.MinerProfile.IsAutoBoot = true;
                            NTMinerRegistry.SetIsAutoStart(true);
                            #endregion

                            #region 矿机名
                            if (!string.IsNullOrEmpty(_workerName))
                            {
                                _localJsonDb.MinerProfile.MinerName = _workerName;
                            }
                            else
                            {
                                // 当用户使用群控作业但没有指定群控矿机名时使用从local.litedb中读取的矿工名
                                if (string.IsNullOrEmpty(_localJsonDb.MinerProfile.MinerName))
                                {
                                    if (localProfile != null)
                                    {
                                        _localJsonDb.MinerProfile.MinerName = localProfile.MinerName;
                                    }
                                    // 如果local.litedb中也没有矿机名则使用去除了特殊符号的本机机器名作为矿机名
                                    if (string.IsNullOrEmpty(_localJsonDb.MinerProfile.MinerName))
                                    {
                                        _localJsonDb.MinerProfile.MinerName = NTKeyword.GetSafeMinerName(ThisPcName);
                                    }
                                }
                            }
                            #endregion
                        }
                        _localJsonInited = true;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void Link()
        {
            VirtualRoot.Window <RegCmdHereCommand>("处理注册右键打开windows命令行菜单命令", LogEnum.DevConsole,
                                                   action: message => {
                try {
                    RegCmdHere();
                    VirtualRoot.Happened(new RegCmdHereEvent(true, "windows右键命令行添加成功"));
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                    VirtualRoot.Happened(new RegCmdHereEvent(false, "windows右键命令行添加失败"));
                }
            });
            #region 挖矿开始时将无份额内核重启份额计数置0
            int      shareCount = 0;
            DateTime shareOn    = DateTime.Now;
            VirtualRoot.On <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole,
                                              action: message => {
                // 将无份额内核重启份额计数置0
                shareCount = 0;
                if (!message.MineContext.IsRestart)
                {
                    shareOn = DateTime.Now;
                }
            });
            #endregion
            #region 每20秒钟检查是否需要重启
            VirtualRoot.On <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None,
                                              action: message => {
                #region 重启电脑
                try {
                    if (MinerProfile.IsPeriodicRestartComputer)
                    {
                        if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes)
                        {
                            Logger.WarnWriteLine($"每运行{MinerProfile.PeriodicRestartKernelHours}小时{MinerProfile.PeriodicRestartComputerMinutes}分钟重启电脑");
                            Windows.Power.Restart(60);
                            VirtualRoot.Execute(new CloseNTMinerCommand());
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

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

                #region 收益没有增加重启内核
                try {
                    if (IsMining && this.CurrentMineContext.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.CurrentMineContext.MainCoin.GetId());
                            totalShare = mainCoinShare.TotalShareCount;
                            if ((this.CurrentMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null)
                            {
                                ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId());
                                totalShare += dualCoinShare.TotalShareCount;
                            }
                            // 如果份额没有增加
                            if (shareCount == totalShare)
                            {
                                if (restartComputer)
                                {
                                    if (!NTMinerRegistry.GetIsAutoBoot())
                                    {
                                        NTMinerRegistry.SetIsAutoBoot(true);
                                    }
                                    if (!NTMinerRegistry.GetIsAutoStart())
                                    {
                                        NTMinerRegistry.SetIsAutoStart(true);
                                    }
                                    Logger.WarnWriteLine($"{MinerProfile.NoShareRestartComputerMinutes}分钟收益没有增加重启电脑");
                                    Windows.Power.Restart(60);
                                    VirtualRoot.Execute(new CloseNTMinerCommand());
                                    return;    // 退出
                                }
                                // 产生过份额或者已经两倍重启内核时间了
                                if (restartKernel && (totalShare > 0 || (DateTime.Now - shareOn).TotalMinutes > 2 * MinerProfile.NoShareRestartKernelMinutes))
                                {
                                    Logger.WarnWriteLine($"{MinerProfile.NoShareRestartKernelMinutes}分钟收益没有增加重启内核");
                                    RestartMine();
                                    return;    // 退出
                                }
                            }
                            if (totalShare > shareCount)
                            {
                                shareCount = totalShare;
                                shareOn    = DateTime.Now;
                            }
                        }
 private static void Upgrade()
 {
     if (ClientAppType.IsMinerClient && !NTMinerRegistry.GetIsValueNameExist("IsNoUi"))
     {
         try {
             using (var db = new LiteDatabase($"filename={Path.Combine(HomePath.HomeDirFullName, NTKeyword.LocalDbFileName)}")) {
                 var col = db.GetCollection <MinerProfileData>();
                 MinerProfileData data = col.FindById(MinerProfileData.DefaultId);
                 if (data != null)
                 {
                     NTMinerRegistry.SetIsNoUi(data.IsNoUi);
                     NTMinerRegistry.SetIsAutoStart(data.IsAutoStart);
                 }
             }
         }
         catch {
         }
     }
     if (ClientAppType.IsMinerClient &&
         HomePath.IsLocalHome &&
         File.Exists(HomePath.RootConfigFileFullName) &&
         !File.Exists(HomePath.RootLockFileFullName))
     {
         #region 迁移
         string sharePackagesDir = Path.Combine(TempPath.TempDirFullName, NTKeyword.PackagesDirName);
         if (Directory.Exists(sharePackagesDir))
         {
             foreach (var fileFullName in Directory.GetFiles(sharePackagesDir))
             {
                 string destFileName = Path.Combine(HomePath.PackagesDirFullName, Path.GetFileName(fileFullName));
                 if (!File.Exists(destFileName))
                 {
                     File.Copy(fileFullName, destFileName);
                 }
             }
         }
         if (DevMode.IsDevMode)
         {
             string shareServerDbFileFullName = Path.Combine(TempPath.TempDirFullName, NTKeyword.ServerDbFileName);
             if (File.Exists(shareServerDbFileFullName) && !File.Exists(HomePath.ServerDbFileFullName))
             {
                 File.Copy(shareServerDbFileFullName, HomePath.ServerDbFileFullName);
             }
         }
         string shareServerJsonFileFullName = Path.Combine(TempPath.TempDirFullName, NTKeyword.ServerJsonFileName);
         if (File.Exists(shareServerJsonFileFullName) && !File.Exists(HomePath.ServerJsonFileFullName))
         {
             File.Copy(shareServerJsonFileFullName, HomePath.ServerJsonFileFullName);
         }
         string shareLocalDbFileFullName = Path.Combine(TempPath.TempDirFullName, NTKeyword.LocalDbFileName);
         if (File.Exists(shareLocalDbFileFullName) && !File.Exists(HomePath.LocalDbFileFullName))
         {
             File.Copy(shareLocalDbFileFullName, HomePath.LocalDbFileFullName);
         }
         string shareGpuProfilesJsonFileFullName = Path.Combine(TempPath.TempDirFullName, NTKeyword.GpuProfilesFileName);
         if (File.Exists(shareGpuProfilesJsonFileFullName) && !File.Exists(HomePath.GpuProfilesJsonFileFullName))
         {
             File.Copy(shareGpuProfilesJsonFileFullName, HomePath.GpuProfilesJsonFileFullName);
         }
         string shareUpdaterFileFullName = Path.Combine(TempPath.TempDirFullName, NTKeyword.UpdaterDirName, NTKeyword.NTMinerUpdaterFileName);
         if (File.Exists(shareUpdaterFileFullName) && !File.Exists(HomePath.UpdaterFileFullName))
         {
             File.Copy(shareUpdaterFileFullName, HomePath.UpdaterFileFullName);
         }
         #endregion
         File.Move(HomePath.RootConfigFileFullName, HomePath.RootLockFileFullName);
     }
 }