//private static async Task ResumeAllMiners()
        //{
        //    foreach (var groupMiner in _runningMiners.Values)
        //    {
        //        await groupMiner.StartMinerTask(stopMiningManager, _miningLocation, _username);
        //    }
        //    // TODO resume devices to Mining state
        //}

        private static async Task CheckGroupingAndUpdateMiners(CommandType commandType)
        {
            // first update mining devices
            if (commandType == CommandType.DevicesStartStop)
            {
                //// TODO should we check new and old miningDevices???
                //var oldMiningDevices = _miningDevices;
                _miningDevices = GroupSetupUtils.GetMiningDevices(_devices, true);
                if (_miningDevices.Count > 0)
                {
                    GroupSetupUtils.AvarageSpeeds(_miningDevices);
                    //// TODO enable StabilityAnalyzer
                    //// set benchmarked speeds for BenchmarkingAnalyzer
                    //foreach (var miningDev in _miningDevices)
                    //{
                    //    var deviceUuid = miningDev.Device.Uuid;
                    //    foreach (var algorithm in miningDev.Algorithms)
                    //    {
                    //        var speedID = $"{deviceUuid}-{algorithm.AlgorithmStringID}";
                    //        var benchmarkSpeed = new BenchmarkingAnalyzer.BenchmarkSpeed {
                    //            PrimarySpeed = algorithm.BenchmarkSpeed,
                    //            SecondarySpeed = algorithm.SecondaryAveragedSpeed,
                    //        };
                    //        BenchmarkingAnalyzer.SetBenchmarkSpeeds(speedID, benchmarkSpeed);
                    //    }
                    //}
                }
            }
            if (commandType == CommandType.MiningLocationChanged)
            {
                _runningMiners.Clear();
                // re-init mining devices
                _miningDevices = GroupSetupUtils.GetMiningDevices(_devices, true);
                if (_miningDevices.Count > 0)
                {
                    GroupSetupUtils.AvarageSpeeds(_miningDevices);
                }
            }
            // TODO check if there is nothing to check maybe
            if (_normalizedProfits == null)
            {
                Logger.Error(Tag, "Profits is null");
                await PauseAllMiners();

                return;
            }
            if (_miningDevices.Count == 0)
            {
                return;
            }
            if (_miningLocation == null)
            {
                Logger.Error(Tag, "_miningLocation is null");
                await PauseAllMiners();

                return;
            }

            await SwichMostProfitableGroupUpMethodTask(_normalizedProfits, CommandType.MiningProfitSettingsChanged == commandType || CommandType.MinerRestartLoopNotify == commandType);
        }
Esempio n. 2
0
        public MiningSession(List <ComputeDevice> devices,
                             IMainFormRatesComunication mainFormRatesComunication,
                             string miningLocation, string worker, string btcAdress)
        {
            // init fixed
            _mainFormRatesComunication = mainFormRatesComunication;
            _miningLocation            = miningLocation;

            _btcAdress = btcAdress;
            _worker    = worker;

            // initial settup
            _miningDevices = GroupSetupUtils.GetMiningDevices(devices, true);
            if (_miningDevices.Count > 0)
            {
                GroupSetupUtils.AvarageSpeeds(_miningDevices);
            }

            // init timer stuff
            _preventSleepTimer          = new Timer();
            _preventSleepTimer.Elapsed += PreventSleepTimer_Tick;
            // sleep time is minimal 1 minute
            _preventSleepTimer.Interval = 20 * 1000; // leave this interval, it works

            // set internet checking
            _internetCheckTimer          = new Timer();
            _internetCheckTimer.Elapsed += InternetCheckTimer_Tick;
            _internetCheckTimer.Interval = 1 * 1000 * 60; // every minute

            //// check worker stats
            //_checkWorkerStats = new Timer();
            //_checkWorkerStats.Elapsed += _checkWorkerStats_Elapsed;
            //_checkWorkerStats.Interval = 2 * 1000 * 60; // every 2 minutes check, miners have to mine 5 minutes to take into account

            // assume profitable
            IsProfitable = true;
            // assume we have internet
            IsConnectedToInternet = true;

            if (IsMiningEnabled)
            {
                _preventSleepTimer.Start();
                _internetCheckTimer.Start();
                //_checkWorkerStats.Start();
            }

            IsMiningRegardlesOfProfit = ConfigManager.GeneralConfig.MinimumProfit == 0;
        }
Esempio n. 3
0
        public MiningSession(List <ComputeDevice> devices,
                             IMainFormRatesComunication mainFormRatesComunication,
                             string miningLocation, string worker, string btcAdress)
        {
            // init fixed
            _mainFormRatesComunication = mainFormRatesComunication;
            _miningLocation            = miningLocation;

            _switchingManager           = new AlgorithmSwitchingManager();
            _switchingManager.SmaCheck += SwichMostProfitableGroupUpMethod;

            _btcAdress = btcAdress;
            _worker    = worker;

            // initial settup
            _miningDevices = GroupSetupUtils.GetMiningDevices(devices, true);
            if (_miningDevices.Count > 0)
            {
                GroupSetupUtils.AvarageSpeeds(_miningDevices);
            }

            // init timer stuff
            _preventSleepTimer          = new Timer();
            _preventSleepTimer.Elapsed += PreventSleepTimer_Tick;
            // sleep time is minimal 1 minute
            _preventSleepTimer.Interval = 20 * 1000; // leave this interval, it works

            // set internet checking
            _internetCheckTimer          = new Timer();
            _internetCheckTimer.Elapsed += InternetCheckTimer_Tick;
            _internetCheckTimer.Interval = 1 * 1000 * 60; // every minute

            // assume profitable
            _isProfitable = true;
            // assume we have internet
            _isConnectedToInternet = true;

            if (IsMiningEnabled)
            {
                _preventSleepTimer.Start();
                _internetCheckTimer.Start();
            }

            _switchingManager.Start();

            _isMiningRegardlesOfProfit = ConfigManager.GeneralConfig.MinimumProfit == 0;
        }
        // TODO make Task
        public static async Task UpdateMiningSession(IEnumerable <ComputeDevice> devices, string username)
        {
            _switchingManager?.Stop();
            await _semaphore.WaitAsync();

            try
            {
                _username = username;
                // TODO check out if there is a change
                _miningDevices = GroupSetupUtils.GetMiningDevices(devices, true);
                if (_miningDevices.Count > 0)
                {
                    GroupSetupUtils.AvarageSpeeds(_miningDevices);
                    //// TODO enable StabilityAnalyzer
                    //// set benchmarked speeds for BenchmarkingAnalyzer
                    //foreach (var miningDev in _miningDevices)
                    //{
                    //    var deviceUuid = miningDev.Device.Uuid;
                    //    foreach (var algorithm in miningDev.Algorithms)
                    //    {
                    //        var speedID = $"{deviceUuid}-{algorithm.AlgorithmStringID}";
                    //        var benchmarkSpeed = new BenchmarkingAnalyzer.BenchmarkSpeed {
                    //            PrimarySpeed = algorithm.BenchmarkSpeed,
                    //            SecondarySpeed = algorithm.SecondaryAveragedSpeed,
                    //        };
                    //        BenchmarkingAnalyzer.SetBenchmarkSpeeds(speedID, benchmarkSpeed);
                    //    }
                    //}
                }
            }
            finally
            {
                _semaphore.Release();
            }
            _switchingManager?.ForceUpdate();
        }
Esempio n. 5
0
        private static async Task HandleDeferredCommands(List <DeferredDeviceCommand> deferredCommands)
        {
            try
            {
                var deviceActions = deferredCommands
                                    .GroupBy(ddc => ddc.device)
                                    .Select(g => g.Select(c => c).ToArray())
                                    .Select(commands => (finalCommand: commands.LastOrDefault(), redundantCommands: commands.Reverse().Skip(1)))
                                    .ToArray();

                var redundantCommands = deviceActions
                                        .SelectMany(p => p.redundantCommands)
                                        .ToArray();
                // mark redundan't actions as complete
                foreach (var redundantCommand in redundantCommands)
                {
                    redundantCommand.Tsc.TrySetResult(false);
                }

                var validCommands = deviceActions.Select(p => p.finalCommand)
                                    .Where(c => c != null)
                                    .ToArray();

                var stopCommands = validCommands.Where(c => c is StopDeviceCommand).ToArray();

                var partitionedStartCommands = validCommands
                                               .Where(c => c is StartDeviceCommand)
                                               .Select(c => (command: c, anyAlgoToBenchmark: c.device.AnyEnabledAlgorithmsNeedBenchmarking(), anyAlgoToMine: c.device.AlgorithmSettings.Any(GroupSetupUtils.IsAlgoMiningCapable)))
                                               .ToArray();

                var startBenchmarkingCommands = partitionedStartCommands
                                                .Where(t => t.anyAlgoToBenchmark)
                                                .Select(t => t.command)
                                                .ToArray();

                var startMiningCommands = partitionedStartCommands
                                          .Where(t => !t.anyAlgoToBenchmark && t.anyAlgoToMine)
                                          .Select(t => t.command)
                                          .ToArray();

                var startErrorCommands = partitionedStartCommands
                                         .Where(t => !t.anyAlgoToBenchmark && !t.anyAlgoToMine)
                                         .Select(t => t.command)
                                         .ToArray();

                var nonMiningCommands = stopCommands
                                        .Concat(startErrorCommands)
                                        .Concat(startBenchmarkingCommands)
                                        .ToArray();

                var nonBenchmarkingCommands = stopCommands
                                              .Concat(startErrorCommands)
                                              .Concat(startMiningCommands)
                                              .ToArray();

                // stop all newly obsolete miners
                foreach (var stopMiner in nonMiningCommands)
                {
                    await StopAndRemoveMiner(stopMiner);
                }
                // stop all newly obsolete benchmarks
                foreach (var stopBenchmark in nonBenchmarkingCommands)
                {
                    await StopAndRemoveBenchmark(stopBenchmark);
                }
                // set the stop and error states
                foreach (var stop in stopCommands)
                {
                    stop.device.State = DeviceState.Stopped;                                // THIS TRIGERS STATE CHANGE TODO change this at the point where we initiate the actual change
                }
                foreach (var stop in startErrorCommands)
                {
                    stop.device.State = DeviceState.Error;                                      // THIS TRIGERS STATE CHANGE TODO change this at the point where we initiate the actual change
                }
                // start and group devices for mining
                var devicesToMineChange = startMiningCommands
                                          .Select(c => _miningDevices.FirstOrDefault(miningDev => miningDev.Device == c.device))
                                          .Any(MiningDevice.ShouldUpdate);
                Func <ComputeDevice, bool> isValidMiningDevice = (ComputeDevice dev) => nonMiningCommands.All(c => c.device != dev);
                var devicesToMine = _miningDevices
                                    .Select(md => md.Device)
                                    .Concat(startMiningCommands.Select(c => c.device))
                                    .Distinct()
                                    .Where(isValidMiningDevice)
                                    .ToArray();
                var anyMiningDeviceInactive = devicesToMine.Any(dev => !_runningMiners.Keys.Any(key => key.Contains(dev.Uuid)));
                var miningDevicesMissmatch  = _miningDevices.Count != devicesToMine.Length;
                // check if there is a difference
                if (devicesToMineChange || anyMiningDeviceInactive || miningDevicesMissmatch)
                {
                    _miningDevices = GroupSetupUtils.GetMiningDevices(devicesToMine, true);
                    GroupSetupUtils.AvarageSpeeds(_miningDevices);
                    //// TODO enable StabilityAnalyzer
                    //// set benchmarked speeds for BenchmarkingAnalyzer
                    //foreach (var miningDev in _miningDevices)
                    //{
                    //    var deviceUuid = miningDev.Device.Uuid;
                    //    foreach (var algorithm in miningDev.Algorithms)
                    //    {
                    //        var speedID = $"{deviceUuid}-{algorithm.AlgorithmStringID}";
                    //        var benchmarkSpeed = new BenchmarkingAnalyzer.BenchmarkSpeed {
                    //            PrimarySpeed = algorithm.BenchmarkSpeed,
                    //            SecondarySpeed = algorithm.SecondaryAveragedSpeed,
                    //        };
                    //        BenchmarkingAnalyzer.SetBenchmarkSpeeds(speedID, benchmarkSpeed);
                    //    }
                    //}
                    await CheckGroupingAndUpdateMiners(new MainCommand());
                }
                foreach (var startMining in startMiningCommands)
                {
                    startMining.device.State = DeviceState.Mining;                                              // THIS TRIGERS STATE CHANGE TODO change this at the point where we initiate the actual change
                }
                // start devices to benchmark or update existing benchmarks algorithms
                var devicesToBenchmark = startBenchmarkingCommands.Select(c => c.device)
                                         .Select(dev => (dev, benchmarkingDev: _benchmarkingDevices.FirstOrDefault(benchDev => benchDev.Device == dev)))
                                         .ToArray();
                // to update
                devicesToBenchmark
                .Where(p => p.benchmarkingDev != null)
                .Select(p => p.benchmarkingDev)
                .ToList()
                .ForEach(benchDev => benchDev.Update());
                // new benchmarks
                devicesToBenchmark
                .Where(p => p.benchmarkingDev == null)
                .Select(p => new BenchmarkingDevice(p.dev))
                .ToList()
                .ForEach(benchDev => {
                    benchDev.StartBenchmark();
                    _benchmarkingDevices.Add(benchDev);
                });
                foreach (var startMining in startBenchmarkingCommands)
                {
                    startMining.device.State = DeviceState.Benchmarking;
                }

                foreach (var c in validCommands)
                {
                    c.Tsc.TrySetResult(true);
                    c.device.IsPendingChange = false;
                }
            }
            catch (Exception e)
            {
                Logger.Info(Tag, $"HandleDeferredCommands error {e.Message}");
            }
            // TODO finally on the deferred commands??
        }