private static void UpdateDeviceMiningStats(ApiData apiData, string minerUUID, string minerName, string groupKey, string deviceUuid, Dictionary <AlgorithmType, double> payingRates) { DeviceMiningStats stat; if (_devicesMiningStats.TryGetValue(deviceUuid, out stat) == false) { // create if it doesn't exist stat = new DeviceMiningStats { DeviceUUID = deviceUuid }; _devicesMiningStats[deviceUuid] = stat; } stat.Clear(); // re-set miner name and group key stat.MinerName = minerName; stat.GroupKey = groupKey; // update stat var deviceSpeedsInfo = apiData.AlgorithmSpeedsPerDevice .Where(pair => pair.Key == deviceUuid) .Select(pair => pair.Value) .FirstOrDefault(); if (deviceSpeedsInfo != null) { foreach (var speedInfo in deviceSpeedsInfo) { stat.Speeds.Add((speedInfo.AlgorithmType, speedInfo.Speed)); if (payingRates.TryGetValue(speedInfo.AlgorithmType, out var paying) == false) { continue; } var payingRate = paying * speedInfo.Speed * 0.000000001; stat.Rates.Add((speedInfo.AlgorithmType, payingRate)); } } var devicePowerUsageApi = (double)apiData.PowerUsagePerDevice .Where(pair => pair.Key == deviceUuid) .Select(pair => pair.Value) .FirstOrDefault(); stat.PowerUsageAPI = devicePowerUsageApi / 1000d; // TODO Globals var dev = AvailableDevices.GetDeviceWithUuid(deviceUuid); if (dev != null) { stat.PowerUsageDeviceReading = dev.PowerUsage; var algo = dev.GetAlgorithm(minerUUID, apiData.AlgorithmSpeedsPerDevice[dev.Uuid].Select(info => info.AlgorithmType).ToArray()); stat.PowerUsageAlgorithmSetting = algo == null ? 0d : algo.PowerUsage; } lock (_devMiningStats) { var index = _devMiningStats.IndexOf(stat); if (index < 0) { _devMiningStats.Add(stat); } else { _devMiningStats[index] = stat; } } //// TODO enable StabilityAnalyzer //// TODO not the best place but here is where we get our per device speeds //var algorithmName = string.Join("+", stat.Speeds.Select(speedPair => Enum.GetName(typeof(AlgorithmType), speedPair.type))); //var algorithmStringID = $"{algorithmName}_{minerUUID}"; //var speedID = $"{deviceUuid}-{algorithmStringID}"; //var primarySpeed = stat.Speeds.Count > 0 ? stat.Speeds[0].speed : 0d; //var secondarySpeed = stat.Speeds.Count > 1 ? stat.Speeds[1].speed : 0d; //var miningSpeed = new BenchmarkingAnalyzer.MiningSpeed //{ // PrimarySpeed = primarySpeed, // SecondarySpeed = secondarySpeed, // Timestamp = DateTime.Now //}; //BenchmarkingAnalyzer.UpdateMiningSpeeds(speedID, miningSpeed); }
//private static async Task ResumeAllMiners() //{ // foreach (var groupMiner in _runningMiners.Values) // { // await groupMiner.StartMinerTask(stopMiningManager, _username); // } // // TODO resume devices to Mining state //} private static async Task CheckGroupingAndUpdateMiners(MainCommand command) { // #1 parse the command var commandType = command.GetType().Name; Logger.Debug(Tag, $"Command type {commandType}"); if (command is NormalizedProfitsUpdateCommand normalizedProfitsUpdateCommand) { _normalizedProfits = normalizedProfitsUpdateCommand.normalizedProfits; } else if (command is UsernameChangedCommand usernameChangedCommand) { _username = usernameChangedCommand.username; } else if (command is PauseMiningWhenGamingModeSettingsChangedCommand pauseMiningWhenGamingModeSettingsChangedCommand) { _isPauseMiningWhenGamingEnabled = pauseMiningWhenGamingModeSettingsChangedCommand.isPauseMiningWhenGamingModeSettingEnabled; if (!_isPauseMiningWhenGamingEnabled) { var dev = AvailableDevices.Devices.FirstOrDefault(d => d.IsGaming == true); if (dev != null) { dev.IsGaming = false; } } } else if (command is IsSteamGameRunningChangedCommand isSteamGameRunningChangedCommand) { _isGameRunning = isSteamGameRunningChangedCommand.isSteamGameRunning; } else if (command is GPUToPauseChangedCommand gpuToPauseChangedCommand) { _deviceToPauseUuid = gpuToPauseChangedCommand.gpuUuid; // unpause device if not mining and not selected var devToUnpause = AvailableDevices.Devices.FirstOrDefault(d => d.Uuid != _deviceToPauseUuid && d.IsGaming == true); if (devToUnpause != null) { devToUnpause.IsGaming = false; } // set new selected gpu to true var newSelectedDev = AvailableDevices.GetDeviceWithUuid(_deviceToPauseUuid); if (newSelectedDev != null) { newSelectedDev.PauseMiningWhenGamingMode = true; ConfigManager.DeviceConfigFileCommit(newSelectedDev); } // set previous selected gpu to false var oldSelectedDev = AvailableDevices.Devices.FirstOrDefault(d => d.Uuid != _deviceToPauseUuid && d.PauseMiningWhenGamingMode); if (oldSelectedDev != null) { oldSelectedDev.PauseMiningWhenGamingMode = false; ConfigManager.DeviceConfigFileCommit(oldSelectedDev); } } bool isRestartMinersCommand(Command command) => command switch { UsernameChangedCommand => true, UseOptimizationProfilesChangedCommand => true, DNSQChangedCommand => true, SSLMiningChangedCommand => true, _ => false, }; // here we do the deciding // to mine we need to have the username mining location set and ofc device to mine with if (_username == null || _normalizedProfits == null) { if (_username == null) { Logger.Error(Tag, "_username is null"); } if (_normalizedProfits == null) { Logger.Error(Tag, "_normalizedProfits is null"); } await PauseAllMiners(); } else if (isRestartMinersCommand(command)) { // RESTART-STOP-START // STOP foreach (var miner in _runningMiners.Values) { await miner.StopTask(); } // START foreach (var miner in _runningMiners.Values) { await miner.StartMinerTask(_stopMiningManager, _username); } } else if (_miningDevices.Count == 0) { await StopAllMinersTask(); ApplicationStateManager.StopMining(); } else if (_isGameRunning && _isPauseMiningWhenGamingEnabled && _deviceToPauseUuid != null) { AvailableNotifications.CreateGamingStarted(); var dev = AvailableDevices.Devices.FirstOrDefault(d => d.Uuid == _deviceToPauseUuid); dev.IsGaming = true; bool skipProfitsThreshold = CheckIfShouldSkipProfitsThreshold(command); await SwichMostProfitableGroupUpMethodTask(_normalizedProfits, skipProfitsThreshold); } else if (!_isGameRunning && _isPauseMiningWhenGamingEnabled && command is IsSteamGameRunningChangedCommand) { AvailableNotifications.CreateGamingFinished(); var dev = AvailableDevices.Devices.FirstOrDefault(d => d.Uuid == _deviceToPauseUuid); dev.IsGaming = false; bool skipProfitsThreshold = CheckIfShouldSkipProfitsThreshold(command); await SwichMostProfitableGroupUpMethodTask(_normalizedProfits, skipProfitsThreshold); } else { ApplicationStateManager.StartMining(); bool skipProfitsThreshold = CheckIfShouldSkipProfitsThreshold(command); await SwichMostProfitableGroupUpMethodTask(_normalizedProfits, skipProfitsThreshold); } }