示例#1
0
 private void GetSpeed()
 {
     Task.Factory.StartNew(() => {
         while (true)
         {
             DateTime now = DateTime.Now;
             if (_getSpeedOn.AddSeconds(10) <= now)
             {
                 if (HostRoot.Current.HostConfig.IsPull)
                 {
                     Write.DevLine("周期拉取数据更新拍照源数据");
                     Task.Factory.StartNew(() => {
                         ClientData[] clientDatas = _dicByObjectId.Values.ToArray();
                         Task[] tasks             = clientDatas.Select(CreatePullTask).ToArray();
                         Task.WaitAll(tasks, 5 * 1000);
                     });
                 }
                 _getSpeedOn = now;
             }
             else
             {
                 System.Threading.Thread.Sleep((int)(_getSpeedOn.AddSeconds(10) - now).TotalMilliseconds);
             }
         }
     });
 }
示例#2
0
        public void SetCoinProfileProperty(Guid coinId, string propertyName, object value)
        {
            string coinCode = "意外的币种";

            if (_root.CoinSet.TryGetCoin(coinId, out ICoin coin))
            {
                _coinProfileSet.SetCoinProfileProperty(coinId, propertyName, value);
                coinCode = coin.Code;
            }
            Write.DevLine($"SetCoinProfileProperty({coinCode}, {propertyName}, {value})");
        }
示例#3
0
        public void Register <TMessage>(DelegateHandler <TMessage> handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            lock (_locker) {
                var keyType = typeof(TMessage);

                var handlerId = handler.HandlerId;
                if (!_paths.Contains(handlerId.HandlerPath))
                {
                    _paths.Add(handlerId.HandlerPath);
                }
                else
                {
                    Write.DevLine($"重复的路径:{handlerId.HandlerPath}", ConsoleColor.Red);
                }
                if (_handlers.ContainsKey(keyType))
                {
                    var registeredHandlers = _handlers[keyType];
                    if (registeredHandlers.Count > 0 && typeof(ICmd).IsAssignableFrom(keyType))
                    {
                        throw new Exception($"one {typeof(TMessage).Name} cmd can be handle and only be handle by one handler");
                    }
                    if (registeredHandlers != null)
                    {
                        if (!registeredHandlers.Contains(handler))
                        {
                            registeredHandlers.Add(handler);
                        }
                    }
                    else
                    {
                        registeredHandlers = new List <dynamic> {
                            handler
                        };
                        _handlers.Add(keyType, registeredHandlers);
                    }
                }
                else
                {
                    var registeredHandlers = new List <dynamic> {
                        handler
                    };
                    _handlers.Add(keyType, registeredHandlers);
                }
            }
        }
示例#4
0
        public void SetPoolProfileProperty(Guid poolId, string propertyName, object value)
        {
            string poolName = "意外的矿池";

            if (_root.PoolSet.TryGetPool(poolId, out IPool pool))
            {
                poolName = pool.Name;
                if (!pool.IsUserMode)
                {
                    Write.DevLine("不是用户名密码模式矿池", ConsoleColor.Green);
                    return;
                }
                _poolProfileSet.SetPoolProfileProperty(poolId, propertyName, value);
            }
            Write.DevLine($"SetPoolProfileProperty({poolName}, {propertyName}, {value})");
        }
示例#5
0
        public void DispatchMessage <TMessage>(TMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            var messageType = typeof(TMessage);
            MessageTypeAttribute messageTypeDescription = MessageTypeAttribute.GetMessageTypeDescription(messageType);

            if (_handlers.ContainsKey(messageType))
            {
                var messageHandlers = _handlers[messageType].ToArray();
                foreach (var messageHandler in messageHandlers)
                {
                    var tMessageHandler = (DelegateHandler <TMessage>)messageHandler;
                    var evtArgs         = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler);
                    switch (tMessageHandler.HandlerId.LogType)
                    {
                    case LogEnum.DevConsole:
                        if (DevMode.IsDevMode)
                        {
                            Write.DevLine($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}");
                        }
                        break;

                    case LogEnum.UserConsole:
                        Write.UserLine($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}", ConsoleColor.Gray);
                        break;

                    case LogEnum.Log:
                        Logger.InfoDebugLine($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}");
                        break;

                    case LogEnum.None:
                    default:
                        break;
                    }
                    tMessageHandler.Handle(message);
                }
            }
            else if (!messageTypeDescription.IsCanNoHandler)
            {
                Write.DevLine(messageType.FullName + "类型的消息没有对应的处理器", ConsoleColor.Red);
            }
        }
示例#6
0
        public void SetCoinKernelProfileProperty(Guid coinKernelId, string propertyName, object value)
        {
            string coinCode   = "意外的币种";
            string kernelName = "意外的内核";

            if (_root.CoinKernelSet.TryGetCoinKernel(coinKernelId, out ICoinKernel coinKernel))
            {
                if (_root.CoinSet.TryGetCoin(coinKernel.CoinId, out ICoin coin))
                {
                    coinCode = coin.Code;
                }
                if (_root.KernelSet.TryGetKernel(coinKernel.KernelId, out IKernel kernel))
                {
                    kernelName = kernel.GetFullName();
                }
                _coinKernelProfileSet.SetCoinKernelProfileProperty(coinKernelId, propertyName, value);
            }
            Write.DevLine($"SetCoinKernelProfileProperty({coinCode}, {kernelName}, {propertyName}, {value})");
        }
示例#7
0
 public ResponseBase SetAppSetting([FromBody] DataRequest <AppSettingData> request)
 {
     if (request == null || request.Data == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         if (!request.IsValid(HostRoot.Current.UserSet.GetUser, ClientIp, out ResponseBase response))
         {
             return(response);
         }
         VirtualRoot.Execute(new ChangeAppSettingCommand(request.Data));
         Write.DevLine($"{request.Data.Key} {request.Data.Value}");
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e.Message, e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
示例#8
0
 public ResponseBase SaveCalcConfigs([FromBody] SaveCalcConfigsRequest request)
 {
     if (request == null || request.Data == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         if (!request.IsValid(HostRoot.Current.UserSet.GetUser, ClientIp, out ResponseBase response))
         {
             return(response);
         }
         HostRoot.Current.CalcConfigSet.SaveCalcConfigs(request.Data);
         Write.DevLine("SaveCalcConfigs");
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e.Message, e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
示例#9
0
 public ResponseBase LoginControlCenter([FromBody] SignatureRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         Write.DevLine($"{request.LoginName} {request.Sign} {request.Timestamp}");
         if (!request.IsValid(HostRoot.Current.UserSet.GetUser, ClientIp, out IUser user, out ResponseBase response))
         {
             return(response);
         }
         Write.DevLine($"{request.LoginName}登录成功");
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e.Message, e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
示例#10
0
 private void Init()
 {
     lock (_locker) {
         if (!_isInited)
         {
             // 将最近20分钟的快照载入内存
             DateTime now = DateTime.Now;
             using (LiteDatabase db = HostRoot.CreateReportDb()) {
                 var col = db.GetCollection <CoinSnapshotData>();
                 col.EnsureIndex(nameof(CoinSnapshotData.Timestamp), unique: false);
                 foreach (var item in col.Find(
                              Query.And(
                                  Query.GT(nameof(CoinSnapshotData.Timestamp), now.AddMinutes(-20)),
                                  Query.LTE(nameof(CoinSnapshotData.Timestamp), now))))
                 {
                     _dataList.Add(item);
                 }
             }
             Write.DevLine("将最近20分钟的快照载入内存");
             _isInited = true;
         }
     }
 }
 public void FatalDebugLine(object message, Exception exception)
 {
     Write.DevLine(message?.ToString() + exception.StackTrace, ConsoleColor.Red);
     _log.Fatal(message, exception);
 }
示例#12
0
 public void Debug(object message)
 {
     Write.DevLine(message?.ToString());
     _log.Debug(message);
 }
 public void ErrorDebugLine(object message)
 {
     Write.DevLine(message?.ToString(), ConsoleColor.Red);
     _log.Error(message);
 }
 public void FatalDebugLine(object message)
 {
     Write.DevLine(message?.ToString(), ConsoleColor.Red);
     _log.Fatal(message);
 }
 public void WarnDebugLine(object message, Exception exception)
 {
     Write.DevLine(message?.ToString() + exception.StackTrace, ConsoleColor.Yellow);
     _log.Warn(message, exception);
 }
 public void WarnDebugLine(object message)
 {
     Write.DevLine(message?.ToString(), ConsoleColor.Yellow);
     _log.Warn(message);
 }
 public void OkDebugLine(object message)
 {
     Write.DevLine(message?.ToString(), ConsoleColor.Green);
     _log.Info(message);
 }
 public void Debug(object message)
 {
     Write.DevLine(message?.ToString(), ConsoleColor.White);
     _log.Debug(message);
 }
示例#19
0
        /// <summary>
        ///
        /// </summary>
        private void Snapshot(DateTime now)
        {
            InitOnece();
            try {
                int onlineCount = 0;
                int miningCount = 0;
                Dictionary <string, CoinSnapshotData> dicByCoinCode = new Dictionary <string, CoinSnapshotData>();
                foreach (var clientData in _root.ClientSet)
                {
                    if (HostRoot.Current.HostConfig.IsPull)
                    {
                        if (clientData.ModifiedOn.AddSeconds(15) < now)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (clientData.ModifiedOn.AddSeconds(130) < now)
                        {
                            continue;
                        }
                    }

                    onlineCount++;

                    if (string.IsNullOrEmpty(clientData.MainCoinCode))
                    {
                        continue;
                    }

                    CoinSnapshotData mainCoinSnapshotData;
                    if (!dicByCoinCode.TryGetValue(clientData.MainCoinCode, out mainCoinSnapshotData))
                    {
                        mainCoinSnapshotData = new CoinSnapshotData()
                        {
                            Timestamp = now,
                            CoinCode  = clientData.MainCoinCode
                        };
                        dicByCoinCode.Add(clientData.MainCoinCode, mainCoinSnapshotData);
                    }

                    if (clientData.IsMining)
                    {
                        miningCount++;
                        mainCoinSnapshotData.MainCoinMiningCount += 1;
                        mainCoinSnapshotData.Speed            += clientData.MainCoinSpeed;
                        mainCoinSnapshotData.ShareDelta       += clientData.GetMainCoinShareDelta();
                        mainCoinSnapshotData.RejectShareDelta += clientData.GetMainCoinRejectShareDelta();
                    }

                    mainCoinSnapshotData.MainCoinOnlineCount += 1;

                    if (!string.IsNullOrEmpty(clientData.DualCoinCode) && clientData.IsDualCoinEnabled)
                    {
                        CoinSnapshotData dualCoinSnapshotData;
                        if (!dicByCoinCode.TryGetValue(clientData.DualCoinCode, out dualCoinSnapshotData))
                        {
                            dualCoinSnapshotData = new CoinSnapshotData()
                            {
                                Timestamp = now,
                                CoinCode  = clientData.DualCoinCode
                            };
                            dicByCoinCode.Add(clientData.DualCoinCode, dualCoinSnapshotData);
                        }

                        if (clientData.IsMining)
                        {
                            dualCoinSnapshotData.DualCoinMiningCount += 1;
                            dualCoinSnapshotData.Speed            += clientData.DualCoinSpeed;
                            dualCoinSnapshotData.ShareDelta       += clientData.GetDualCoinShareDelta();
                            dualCoinSnapshotData.RejectShareDelta += clientData.GetDualCoinRejectShareDelta();
                        }

                        dualCoinSnapshotData.DualCoinOnlineCount += 1;
                    }
                }

                HostRoot.ClientCount.Update(onlineCount, miningCount);
                if (dicByCoinCode.Count > 0)
                {
                    _dataList.AddRange(dicByCoinCode.Values);
                    using (LiteDatabase db = HostRoot.CreateReportDb()) {
                        var col = db.GetCollection <CoinSnapshotData>();
                        col.Insert(dicByCoinCode.Values);
                    }
                    Write.DevLine("拍摄快照" + dicByCoinCode.Count + "张,快照时间戳:" + now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                }
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e.Message, e);
            }
        }
示例#20
0
 public MineWorkViewModel(Guid id)
 {
     _id       = id;
     this.Save = new DelegateCommand(() => {
         if (this.Id == Guid.Empty)
         {
             return;
         }
         if (string.IsNullOrEmpty(this.Name))
         {
             NotiCenterWindowViewModel.Current.Manager.ShowErrorMessage("作业名称是必须的");
         }
         bool isMinerProfileChanged = false;
         IMineWork entity;
         if (NTMinerRoot.Current.MineWorkSet.TryGetMineWork(this.Id, out entity))
         {
             string sha1 = NTMinerRoot.Current.MinerProfile.GetSha1();
             if (this.Sha1 != sha1)
             {
                 isMinerProfileChanged = true;
             }
             if (entity.Name != this.Name || entity.Description != this.Description)
             {
                 VirtualRoot.Execute(new UpdateMineWorkCommand(this));
             }
             CloseWindow?.Invoke();
         }
         else
         {
             isMinerProfileChanged = true;
             VirtualRoot.Execute(new AddMineWorkCommand(this));
             CloseWindow?.Invoke();
             this.Edit.Execute(FormType.Edit);
         }
         if (isMinerProfileChanged)
         {
             Write.DevLine("检测到MinerProfile状态变更");
             string localJson;
             string serverJson;
             NTMinerRoot.ExportWorkJson(new MineWorkData(this), out localJson, out serverJson);
             if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson))
             {
                 Server.ControlCenterService.ExportMineWorkAsync(this.Id, localJson, serverJson, callback: null);
             }
         }
     });
     this.Edit = new DelegateCommand <FormType?>((formType) => {
         if (this.Id == Guid.Empty)
         {
             return;
         }
         MineWorkViewModel mineWorkVm;
         if (!MineWorkViewModels.Current.TryGetMineWorkVm(this.Id, out mineWorkVm))
         {
             InputWindow.ShowDialog("作业名称", string.Empty, workName => {
                 if (string.IsNullOrEmpty(workName))
                 {
                     return("作业名称是必须的");
                 }
                 return(string.Empty);
             }, onOk: workName => {
                 new MineWorkViewModel(this)
                 {
                     Name = workName
                 }.Save.Execute(null);
             });
         }
         else
         {
             // 编辑作业前切换上下文
             // 根据workId下载json保存到本地并调用LocalJson.Instance.ReInit()
             string json = Server.ControlCenterService.GetLocalJson(this.Id);
             if (!string.IsNullOrEmpty(json))
             {
                 File.WriteAllText(SpecialPath.LocalJsonFileFullName, json);
             }
             else
             {
                 File.Delete(SpecialPath.LocalJsonFileFullName);
             }
             NTMinerRoot.Current.ReInitMinerProfile();
             this.Sha1 = NTMinerRoot.Current.MinerProfile.GetSha1();
             MineWorkEdit.ShowWindow(formType ?? FormType.Edit, new MineWorkViewModel(this));
         }
     }, formType => {
         if (this == PleaseSelect)
         {
             return(false);
         }
         return(true);
     });
     this.Remove = new DelegateCommand(() => {
         if (this.Id == Guid.Empty)
         {
             return;
         }
         DialogWindow.ShowDialog(message: $"您确定删除吗?", title: "确认", onYes: () => {
             VirtualRoot.Execute(new RemoveMineWorkCommand(this.Id));
         }, icon: IconConst.IconConfirm);
     }, () => {
         if (this == PleaseSelect)
         {
             return(false);
         }
         return(true);
     });
 }
示例#21
0
        private void RefreshTotalSpeedChart(int limit)
        {
            //NTMinerRoot.Current.DebugLine($"获取总算力数据,范围{leftTime} - {rightTime}");
            var coinCodes = NTMinerRoot.Current.CoinSet.Select(a => a.Code).ToList();

            Server.ControlCenterService.GetLatestSnapshotsAsync(
                limit,
                coinCodes,
                (response, exception) => {
                if (response == null)
                {
                    return;
                }

                if (!response.IsSuccess())
                {
                    Write.UserLine(response.Description, ConsoleColor.Red);
                    return;
                }
                if (exception != null)
                {
                    Write.DevLine(exception.Message);
                    return;
                }
                UIThread.Execute(() => {
                    bool isOnlyOne      = limit == 1;
                    Vm.TotalMiningCount = response.TotalMiningCount;
                    Vm.TotalOnlineCount = response.TotalOnlineCount;
                    foreach (var chartVm in Vm.ChartVms)
                    {
                        var list = response.Data.Where(a => a.CoinCode == chartVm.CoinVm.Code).ToList();
                        if (list.Count == 0)
                        {
                            list.Add(new CoinSnapshotData {
                                CoinCode            = chartVm.CoinVm.Code,
                                DualCoinOnlineCount = 0,
                                DualCoinMiningCount = 0,
                                MainCoinOnlineCount = 0,
                                MainCoinMiningCount = 0,
                                RejectShareDelta    = 0,
                                ShareDelta          = 0,
                                Speed     = 0,
                                Timestamp = DateTime.Now.AddSeconds(-5)
                            });
                        }
                        CoinSnapshotData one = null;
                        if (isOnlyOne)
                        {
                            one = list.Last();
                        }
                        else
                        {
                            list = list.OrderBy(a => a.Timestamp).ToList();
                        }
                        CoinSnapshotData latestData = list.Last();
                        chartVm.SnapshotDataVm.Update(latestData);
                        foreach (var riser in chartVm.Series)
                        {
                            if (riser.Title == "speed")
                            {
                                if (list.Count > 0)
                                {
                                    if (isOnlyOne)
                                    {
                                        riser.Values.Add(new MeasureModel()
                                        {
                                            DateTime = one.Timestamp,
                                            Value    = one.Speed
                                        });
                                    }
                                    else
                                    {
                                        foreach (var item in list)
                                        {
                                            riser.Values.Add(new MeasureModel()
                                            {
                                                DateTime = item.Timestamp,
                                                Value    = item.Speed
                                            });
                                        }
                                    }
                                }
                            }
                            else if (riser.Title == "onlineCount")
                            {
                                if (isOnlyOne)
                                {
                                    riser.Values.Add(new MeasureModel()
                                    {
                                        DateTime = one.Timestamp,
                                        Value    = one.MainCoinOnlineCount + one.DualCoinOnlineCount
                                    });
                                }
                                else
                                {
                                    foreach (var item in list)
                                    {
                                        riser.Values.Add(new MeasureModel()
                                        {
                                            DateTime = item.Timestamp,
                                            Value    = item.MainCoinOnlineCount + item.DualCoinOnlineCount
                                        });
                                    }
                                }
                            }
                            else if (riser.Title == "miningCount")
                            {
                                if (isOnlyOne)
                                {
                                    riser.Values.Add(new MeasureModel()
                                    {
                                        DateTime = one.Timestamp,
                                        Value    = one.MainCoinMiningCount + one.DualCoinMiningCount
                                    });
                                }
                                else
                                {
                                    foreach (var item in list)
                                    {
                                        riser.Values.Add(new MeasureModel()
                                        {
                                            DateTime = item.Timestamp,
                                            Value    = item.MainCoinMiningCount + item.DualCoinMiningCount
                                        });
                                    }
                                }
                            }
                            else if (riser.Title == "rejectShare")
                            {
                                if (isOnlyOne)
                                {
                                    riser.Values.Add(new MeasureModel()
                                    {
                                        DateTime = one.Timestamp,
                                        Value    = one.RejectShareDelta
                                    });
                                }
                                else
                                {
                                    foreach (var item in list)
                                    {
                                        riser.Values.Add(new MeasureModel()
                                        {
                                            DateTime = item.Timestamp,
                                            Value    = item.RejectShareDelta
                                        });
                                    }
                                }
                            }
                            else if (riser.Title == "acceptShare")
                            {
                                if (isOnlyOne)
                                {
                                    riser.Values.Add(new MeasureModel()
                                    {
                                        DateTime = one.Timestamp,
                                        Value    = one.ShareDelta
                                    });
                                }
                                else
                                {
                                    foreach (var item in list)
                                    {
                                        riser.Values.Add(new MeasureModel()
                                        {
                                            DateTime = item.Timestamp,
                                            Value    = item.ShareDelta
                                        });
                                    }
                                }
                            }
                        }
                        DateTime now = DateTime.Now.AddSeconds(10);
                        foreach (var riser in chartVm.Series)
                        {
                            IChartValues valuesTotal = riser.Values;
                            if (valuesTotal.Count > 0 && ((MeasureModel)valuesTotal[0]).DateTime.AddMinutes(NTMinerRoot.Current.SpeedHistoryLengthByMinute) < now)
                            {
                                valuesTotal.RemoveAt(0);
                            }
                        }
                        chartVm.SetAxisLimits(now);
                    }
                });
            });
        }