Пример #1
0
        public MinerProfileViewModel()
        {
            if (WpfUtil.IsInDesignMode)
            {
                return;
            }
            if (Instance != null)
            {
                throw new InvalidProgramException();
            }
            if (this.IsCreateShortcut)
            {
                CreateShortcut();
            }
            this.Up = new DelegateCommand <string>(propertyName => {
                WpfUtil.Up(this, propertyName);
            });
            this.Down = new DelegateCommand <string>(propertyName => {
                WpfUtil.Down(this, propertyName);
            });
            this.WsRetry = new DelegateCommand(() => {
                RpcRoot.Client.NTMinerDaemonService.StartOrStopWsAsync(isResetFailCount: true);
                IsConnecting = true;
            });
            if (ClientAppType.IsMinerClient)
            {
                VirtualRoot.AddCmdPath <SetAutoStartCommand>(message => {
                    this.IsAutoStart = message.IsAutoStart;
                    this.IsAutoBoot  = message.IsAutoBoot;
                }, this.GetType(), LogEnum.None);
                VirtualRoot.AddEventPath <StartingMineFailedEvent>("开始挖矿失败", LogEnum.DevConsole,
                                                                   action: message => {
                    IsMining = false;
                    NTMinerConsole.UserError(message.Message);
                }, location: this.GetType());
                // 群控客户端已经有一个执行RefreshWsStateCommand命令的路径了
                VirtualRoot.AddCmdPath <RefreshWsStateCommand>(message => {
                    #region
                    if (message.WsClientState != null)
                    {
                        this.IsWsOnline = message.WsClientState.Status == WsClientStatus.Open;
                        if (message.WsClientState.ToOut)
                        {
                            VirtualRoot.Out.ShowWarn(message.WsClientState.Description, autoHideSeconds: 3);
                        }
                        if (!message.WsClientState.ToOut || !this.IsWsOnline)
                        {
                            this.WsDescription = message.WsClientState.Description;
                        }
                        if (!this.IsWsOnline)
                        {
                            if (message.WsClientState.LastTryOn != DateTime.MinValue)
                            {
                                this.WsLastTryOn = message.WsClientState.LastTryOn;
                            }
                            if (message.WsClientState.NextTrySecondsDelay > 0)
                            {
                                WsNextTrySecondsDelay = message.WsClientState.NextTrySecondsDelay;
                            }
                        }
                    }
                    #endregion
                }, this.GetType(), LogEnum.DevConsole);
                VirtualRoot.AddEventPath <Per1SecondEvent>("外网群控重试秒表倒计时", LogEnum.None, action: message => {
                    if (IsOuterUserEnabled && !IsWsOnline)
                    {
                        if (WsNextTrySecondsDelay > 0)
                        {
                            WsNextTrySecondsDelay--;
                        }
                        else if (WsLastTryOn == DateTime.MinValue)
                        {
                            this.RefreshWsDaemonState();
                        }
                        OnPropertyChanged(nameof(WsLastTryOnText));
                    }
                }, this.GetType());
                VirtualRoot.AddEventPath <WsServerOkEvent>("服务器Ws服务已可用", LogEnum.DevConsole, action: message => {
                    if (IsOuterUserEnabled && !IsWsOnline)
                    {
                        StartOrStopWs();
                    }
                }, this.GetType());
            }
            NTMinerContext.SetRefreshArgsAssembly((reason) => {
                NTMinerConsole.DevDebug(() => $"RefreshArgsAssembly" + reason, ConsoleColor.Cyan);
                #region 确保双挖权重在合法的范围内
                if (CoinVm != null && CoinVm.CoinKernel != null && CoinVm.CoinKernel.Kernel != null)
                {
                    var coinKernelProfile = CoinVm.CoinKernel.CoinKernelProfile;
                    var kernelInput       = CoinVm.CoinKernel.Kernel.KernelInputVm;
                    if (coinKernelProfile != null && kernelInput != null)
                    {
                        if (coinKernelProfile.IsDualCoinEnabled && !kernelInput.IsAutoDualWeight)
                        {
                            if (coinKernelProfile.DualCoinWeight > kernelInput.DualWeightMax)
                            {
                                coinKernelProfile.DualCoinWeight = kernelInput.DualWeightMax;
                            }
                            else if (coinKernelProfile.DualCoinWeight < kernelInput.DualWeightMin)
                            {
                                coinKernelProfile.DualCoinWeight = kernelInput.DualWeightMin;
                            }
                            NTMinerContext.Instance.MinerProfile.SetCoinKernelProfileProperty(coinKernelProfile.CoinKernelId, nameof(coinKernelProfile.DualCoinWeight), coinKernelProfile.DualCoinWeight);
                        }
                    }
                }
                #endregion
                NTMinerContext.Instance.CurrentMineContext = NTMinerContext.Instance.CreateMineContext();
                if (NTMinerContext.Instance.CurrentMineContext != null)
                {
                    this.ArgsAssembly = NTMinerContext.Instance.CurrentMineContext.CommandLine;
                }
                else
                {
                    this.ArgsAssembly = string.Empty;
                }
            });
            AppRoot.AddCmdPath <RefreshAutoBootStartCommand>("刷新开机启动和自动挖矿的展示", LogEnum.DevConsole,
                                                             action: message => {
                this.OnPropertyChanged(nameof(IsAutoBoot));
                this.OnPropertyChanged(nameof(IsAutoStart));
            }, location: this.GetType());
            AppRoot.AddEventPath <MinerProfilePropertyChangedEvent>("MinerProfile设置变更后刷新VM内存", LogEnum.DevConsole,
                                                                    action: message => {
                OnPropertyChanged(message.PropertyName);
            }, location: this.GetType());

            VirtualRoot.AddEventPath <LocalContextReInitedEventHandledEvent>("本地上下文视图模型集刷新后刷新界面", LogEnum.DevConsole,
                                                                             action: message => {
                AllPropertyChanged();
                if (CoinVm != null)
                {
                    CoinVm.OnPropertyChanged(nameof(CoinVm.Wallets));
                    CoinVm.CoinKernel?.CoinKernelProfile.SelectedDualCoin?.OnPropertyChanged(nameof(CoinVm.Wallets));
                    CoinVm.CoinProfile?.OnPropertyChanged(nameof(CoinVm.CoinProfile.SelectedWallet));
                    CoinVm.CoinKernel?.CoinKernelProfile.SelectedDualCoin?.CoinProfile?.OnPropertyChanged(nameof(CoinVm.CoinProfile.SelectedDualCoinWallet));
                }
            }, location: this.GetType());
            VirtualRoot.AddEventPath <CoinVmAddedEvent>("Vm集添加了新币种后刷新MinerProfileVm内存", LogEnum.DevConsole, action: message => {
                OnPropertyChanged(nameof(CoinVm));
            }, this.GetType());
            VirtualRoot.AddEventPath <CoinVmRemovedEvent>("Vm集删除了新币种后刷新MinerProfileVm内存", LogEnum.DevConsole, action: message => {
                OnPropertyChanged(nameof(CoinVm));
            }, this.GetType());
        }
Пример #2
0
        private void DoSave()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                VirtualRoot.Out.ShowError("作业名称是必须的", autoHideSeconds: 4);
            }
            bool isMinerProfileChanged = false;
            // 表示是否随后打开编辑界面,如果是新建的作业则保存后随即会打开作业编辑界面
            bool         isShowEdit   = false;
            MineWorkData mineWorkData = new MineWorkData().Update(this);

            if (MinerStudioRoot.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel vm))
            {
                string sha1 = NTMinerContext.Instance.MinerProfile.GetSha1();
                // 如果作业设置变更了则一定变更了
                if (this.Sha1 != sha1)
                {
                    isMinerProfileChanged = true;
                }
                else
                {
                    // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了
                    LocalJsonDb  localJsonObj  = new LocalJsonDb(NTMinerContext.Instance, mineWorkData);
                    ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerContext.Instance, localJsonObj);
                    var          serverJson    = VirtualRoot.JsonSerializer.Serialize(serverJsonObj);
                    sha1 = HashUtil.Sha1(serverJson);
                    if (sha1 != this.ServerJsonSha1)
                    {
                        isMinerProfileChanged = true;
                    }
                }
            }
            else
            {
                isMinerProfileChanged = true;
                isShowEdit            = true;
                NTMinerConsole.UserInfo("保存作业。");
            }
            if (RpcRoot.IsOuterNet)
            {
                if (!this.Id.IsSelfMineWorkId())
                {
                    RpcRoot.OfficialServer.UserMineWorkService.AddOrUpdateMineWorkAsync(new MineWorkData().Update(this), (r, ex) => {
                        if (r.IsSuccess())
                        {
                            if (isMinerProfileChanged)
                            {
                                NTMinerContext.ExportWorkJson(mineWorkData, out string localJson, out string serverJson);
                                if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson))
                                {
                                    RpcRoot.OfficialServer.UserMineWorkService.ExportMineWorkAsync(this.Id, localJson, serverJson, (response, e) => {
                                        if (response.IsSuccess())
                                        {
                                            if (isShowEdit)
                                            {
                                                VirtualRoot.RaiseEvent(new MineWorkAddedEvent(Guid.Empty, this));
                                            }
                                            else
                                            {
                                                VirtualRoot.RaiseEvent(new MineWorkUpdatedEvent(Guid.Empty, this));
                                            }
                                            if (isShowEdit)
                                            {
                                                this.Edit.Execute(FormType.Edit);
                                            }
                                        }
                                        else
                                        {
                                            VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4);
                                        }
                                    });
                                }
                                if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1)
                                {
                                    this.ServerJsonSha1 = mineWorkData.ServerJsonSha1;
                                }
                            }
                        }
                        else
                        {
                            VirtualRoot.Out.ShowError(r.ReadMessage(ex), autoHideSeconds: 4);
                        }
                    });
Пример #3
0
 public MineWorkViewModel(Guid id)
 {
     _id       = id;
     this.Save = new DelegateCommand(() => {
         if (this.Id == Guid.Empty)
         {
             return;
         }
         if (string.IsNullOrEmpty(this.Name))
         {
             VirtualRoot.Out.ShowError("作业名称是必须的", autoHideSeconds: 4);
         }
         bool isMinerProfileChanged = false;
         bool isShowEdit            = false;
         MineWorkData mineWorkData  = new MineWorkData().Update(this);
         if (MinerStudioRoot.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel vm))
         {
             string sha1 = NTMinerContext.Instance.MinerProfile.GetSha1();
             // 如果作业设置变更了则一定变更了
             if (this.Sha1 != sha1)
             {
                 isMinerProfileChanged = true;
             }
             else
             {
                 // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了
                 LocalJsonDb localJsonObj   = new LocalJsonDb(NTMinerContext.Instance, mineWorkData);
                 ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerContext.Instance, localJsonObj);
                 var serverJson             = VirtualRoot.JsonSerializer.Serialize(serverJsonObj);
                 sha1 = HashUtil.Sha1(serverJson);
                 if (sha1 != this.ServerJsonSha1)
                 {
                     isMinerProfileChanged = true;
                 }
             }
         }
         else
         {
             isMinerProfileChanged = true;
             isShowEdit            = true;
         }
         if (RpcRoot.IsOuterNet)
         {
             RpcRoot.OfficialServer.UserMineWorkService.AddOrUpdateMineWorkAsync(new MineWorkData().Update(this), (r, ex) => {
                 if (r.IsSuccess())
                 {
                     if (isMinerProfileChanged)
                     {
                         Write.DevDebug("检测到MinerProfile状态变更");
                         NTMinerContext.ExportWorkJson(mineWorkData, out string localJson, out string serverJson);
                         if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson))
                         {
                             RpcRoot.OfficialServer.UserMineWorkService.ExportMineWorkAsync(this.Id, localJson, serverJson, (response, e) => {
                                 if (response.IsSuccess())
                                 {
                                     if (isShowEdit)
                                     {
                                         VirtualRoot.RaiseEvent(new MineWorkAddedEvent(Guid.Empty, this));
                                     }
                                     else
                                     {
                                         VirtualRoot.RaiseEvent(new MineWorkUpdatedEvent(Guid.Empty, this));
                                     }
                                     if (isShowEdit)
                                     {
                                         this.Edit.Execute(FormType.Edit);
                                     }
                                 }
                                 else
                                 {
                                     VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4);
                                 }
                             });
                         }
                         if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1)
                         {
                             this.ServerJsonSha1 = mineWorkData.ServerJsonSha1;
                         }
                     }
                 }
Пример #4
0
        public MinerProfileViewModel()
        {
#if DEBUG
            NTStopwatch.Start();
#endif
            if (WpfUtil.IsInDesignMode)
            {
                return;
            }
            if (Instance != null)
            {
                throw new InvalidProgramException();
            }
            if (this.IsCreateShortcut)
            {
                CreateShortcut();
            }
            this.Up = new DelegateCommand <string>(propertyName => {
                #region
                if (!_propertyInfos.TryGetValue(propertyName, out PropertyInfo propertyInfo))
                {
                    propertyInfo = this.GetType().GetProperty(propertyName);
                    if (propertyInfo == null)
                    {
                        Write.DevError(() => $"类型{this.GetType().FullName}不具有名称为{propertyName}的属性");
                        return;
                    }
                    _propertyInfos.Add(propertyName, propertyInfo);
                }
                if (propertyInfo.PropertyType == typeof(int))
                {
                    propertyInfo.SetValue(this, (int)propertyInfo.GetValue(this, null) + 1, null);
                }
                else if (propertyInfo.PropertyType == typeof(double))
                {
                    propertyInfo.SetValue(this, Math.Round((double)propertyInfo.GetValue(this, null) + 0.1, 2), null);
                }
                #endregion
            });
            this.Down = new DelegateCommand <string>(propertyName => {
                #region
                if (!_propertyInfos.TryGetValue(propertyName, out PropertyInfo propertyInfo))
                {
                    propertyInfo = this.GetType().GetProperty(propertyName);
                    if (propertyInfo == null)
                    {
                        Write.DevError(() => $"类型{this.GetType().FullName}不具有名称为{propertyName}的属性");
                        return;
                    }
                    _propertyInfos.Add(propertyName, propertyInfo);
                }
                if (propertyInfo.PropertyType == typeof(int))
                {
                    int value = (int)propertyInfo.GetValue(this, null);
                    if (value > 0)
                    {
                        propertyInfo.SetValue(this, value - 1, null);
                    }
                }
                else if (propertyInfo.PropertyType == typeof(double))
                {
                    double value = (double)propertyInfo.GetValue(this, null);
                    if (value > 0.1)
                    {
                        propertyInfo.SetValue(this, Math.Round(value - 0.1, 2), null);
                    }
                }
                #endregion
            });
            this.WsRetry = new DelegateCommand(() => {
                RpcRoot.Client.NTMinerDaemonService.StartOrStopWsAsync(isResetFailCount: true);
                IsConnecting = true;
            });
            bool isRefreshed = false;
            if (ClientAppType.IsMinerClient)
            {
                VirtualRoot.AddEventPath <StartingMineFailedEvent>("开始挖矿失败", LogEnum.DevConsole,
                                                                   action: message => {
                    IsMining = false;
                    Write.UserError(message.Message);
                }, location: this.GetType());
                // 群控客户端已经有一个执行RefreshWsStateCommand命令的路径了
                VirtualRoot.AddCmdPath <RefreshWsStateCommand>(message => {
                    #region
                    if (message.WsClientState != null)
                    {
                        isRefreshed     = true;
                        this.IsWsOnline = message.WsClientState.Status == WsClientStatus.Open;
                        if (message.WsClientState.ToOut)
                        {
                            VirtualRoot.Out.ShowWarn(message.WsClientState.Description, autoHideSeconds: 3);
                        }
                        if (!message.WsClientState.ToOut || !this.IsWsOnline)
                        {
                            this.WsDescription = message.WsClientState.Description;
                        }
                        if (!this.IsWsOnline)
                        {
                            if (message.WsClientState.LastTryOn != DateTime.MinValue)
                            {
                                this.WsLastTryOn = message.WsClientState.LastTryOn;
                            }
                            if (message.WsClientState.NextTrySecondsDelay > 0)
                            {
                                WsNextTrySecondsDelay = message.WsClientState.NextTrySecondsDelay;
                            }
                        }
                    }
                    #endregion
                }, this.GetType(), LogEnum.DevConsole);
                VirtualRoot.AddEventPath <Per1SecondEvent>("外网群控重试秒表倒计时", LogEnum.None, action: message => {
                    if (IsOuterUserEnabled && !IsWsOnline)
                    {
                        if (WsNextTrySecondsDelay > 0)
                        {
                            WsNextTrySecondsDelay--;
                        }
                        OnPropertyChanged(nameof(WsLastTryOnText));
                    }
                }, this.GetType());
                VirtualRoot.AddEventPath <WsServerOkEvent>("服务器Ws服务已可用", LogEnum.DevConsole, action: message => {
                    if (IsOuterUserEnabled && !IsWsOnline)
                    {
                        StartOrStopWs();
                    }
                }, this.GetType());
                if (IsOuterUserEnabled)
                {
                    RpcRoot.Client.NTMinerDaemonService.GetWsDaemonStateAsync((WsClientState state, Exception e) => {
                        if (state != null && !isRefreshed)
                        {
                            this.IsWsOnline    = state.Status == WsClientStatus.Open;
                            this.WsDescription = state.Description;
                            if (state.NextTrySecondsDelay > 0)
                            {
                                this.WsNextTrySecondsDelay = state.NextTrySecondsDelay;
                            }
                            this.WsLastTryOn = state.LastTryOn;
                        }
                    });
                }
            }
            NTMinerContext.SetRefreshArgsAssembly((reason) => {
                Write.DevDebug(() => $"RefreshArgsAssembly" + reason, ConsoleColor.Cyan);
#if DEBUG
                NTStopwatch.Start();
#endif
                #region 确保双挖权重在合法的范围内
                if (CoinVm != null && CoinVm.CoinKernel != null && CoinVm.CoinKernel.Kernel != null)
                {
                    var coinKernelProfile = CoinVm.CoinKernel.CoinKernelProfile;
                    var kernelInput       = CoinVm.CoinKernel.Kernel.KernelInputVm;
                    if (coinKernelProfile != null && kernelInput != null)
                    {
                        if (coinKernelProfile.IsDualCoinEnabled && !kernelInput.IsAutoDualWeight)
                        {
                            if (coinKernelProfile.DualCoinWeight > kernelInput.DualWeightMax)
                            {
                                coinKernelProfile.DualCoinWeight = kernelInput.DualWeightMax;
                            }
                            else if (coinKernelProfile.DualCoinWeight < kernelInput.DualWeightMin)
                            {
                                coinKernelProfile.DualCoinWeight = kernelInput.DualWeightMin;
                            }
                            NTMinerContext.Instance.MinerProfile.SetCoinKernelProfileProperty(coinKernelProfile.CoinKernelId, nameof(coinKernelProfile.DualCoinWeight), coinKernelProfile.DualCoinWeight);
                        }
                    }
                }
                #endregion
                NTMinerContext.Instance.CurrentMineContext = NTMinerContext.Instance.CreateMineContext();
                if (NTMinerContext.Instance.CurrentMineContext != null)
                {
                    this.ArgsAssembly = NTMinerContext.Instance.CurrentMineContext.CommandLine;
                }
                else
                {
                    this.ArgsAssembly = string.Empty;
                }
#if DEBUG
                var milliseconds = NTStopwatch.Stop();
                if (milliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
                {
                    Write.DevTimeSpan($"耗时{milliseconds} {this.GetType().Name}.SetRefreshArgsAssembly");
                }
#endif
            });
            VirtualRoot.AddEventPath <ServerContextVmsReInitedEvent>("ServerContext的VM集刷新后刷新视图界面", LogEnum.DevConsole,
                                                                     action: message => {
                OnPropertyChanged(nameof(CoinVm));
            }, location: this.GetType());
            AppRoot.AddCmdPath <RefreshAutoBootStartCommand>("刷新开机启动和自动挖矿的展示", LogEnum.DevConsole,
                                                             action: message => {
                MinerProfileData data = NTMinerContext.Instance.ServerContext.CreateLocalRepository <MinerProfileData>().GetByKey(this.Id);
                if (data != null)
                {
                    this.IsAutoBoot  = data.IsAutoBoot;
                    this.IsAutoStart = data.IsAutoStart;
                }
            }, location: this.GetType());
            AppRoot.AddEventPath <MinerProfilePropertyChangedEvent>("MinerProfile设置变更后刷新VM内存", LogEnum.DevConsole,
                                                                    action: message => {
                OnPropertyChanged(message.PropertyName);
            }, location: this.GetType());

            VirtualRoot.AddEventPath <LocalContextVmsReInitedEvent>("本地上下文视图模型集刷新后刷新界面", LogEnum.DevConsole,
                                                                    action: message => {
                AllPropertyChanged();
                if (CoinVm != null)
                {
                    CoinVm.OnPropertyChanged(nameof(CoinVm.Wallets));
                    CoinVm.CoinKernel?.CoinKernelProfile.SelectedDualCoin?.OnPropertyChanged(nameof(CoinVm.Wallets));
                    CoinVm.CoinProfile?.OnPropertyChanged(nameof(CoinVm.CoinProfile.SelectedWallet));
                    CoinVm.CoinKernel?.CoinKernelProfile.SelectedDualCoin?.CoinProfile?.OnPropertyChanged(nameof(CoinVm.CoinProfile.SelectedDualCoinWallet));
                }
            }, location: this.GetType());
            VirtualRoot.AddEventPath <CoinVmAddedEvent>("Vm集添加了新币种后刷新MinerProfileVm内存", LogEnum.DevConsole, action: message => {
                OnPropertyChanged(nameof(CoinVm));
            }, this.GetType());
            VirtualRoot.AddEventPath <CoinVmRemovedEvent>("Vm集删除了新币种后刷新MinerProfileVm内存", LogEnum.DevConsole, action: message => {
                OnPropertyChanged(nameof(CoinVm));
            }, this.GetType());
#if DEBUG
            var elapsedMilliseconds = NTStopwatch.Stop();
            if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
            {
                Write.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
            }
#endif
        }