private async Task <ApiData> GetSummaryAsync() { var apiData = new ApiData(); if (!IsUpdatingApi) { IsUpdatingApi = true; await _apiSemaphore.WaitAsync(); try { apiData = await _miner.GetMinerStatsDataAsync(); } catch (Exception e) { Logger.Error(MinerTag(), $"GetSummaryAsync error: {e.Message}"); } finally { IsUpdatingApi = false; _apiSemaphore.Release(); if (apiData.AlgorithmSpeedsPerDevice != null) { var anyNegative = apiData.AlgorithmSpeedsPerDevice.Any(apiDev => apiDev.Value.Any(kvp => kvp.speed < 0)); if (anyNegative) { await StopAsync(); } } } } UpdateApiTimestamp(apiData); // TODO workaround plugins should return this info // create empty stub if it is null if (apiData == null) { Logger.Debug(MinerTag(), "GetSummary returned null... Will create ZERO fallback"); apiData = new ApiData(); } if (apiData.AlgorithmSpeedsPerDevice == null) { apiData = new ApiData(); var perDevicePowerDict = new Dictionary <string, int>(); var perDeviceSpeedsDict = new Dictionary <string, IReadOnlyList <(AlgorithmType type, double speed)> >(); var perDeviceSpeeds = _miningPairs.Select(pair => (pair.Device.UUID, pair.Algorithm.IDs.Select(type => (type, 0d)))); foreach (var kvp in perDeviceSpeeds) { var uuid = kvp.Item1; // kvp.UUID compiler doesn't recognize ValueTypes lib??? perDeviceSpeedsDict[uuid] = kvp.Item2.ToList(); perDevicePowerDict[uuid] = 0; } apiData.AlgorithmSpeedsPerDevice = perDeviceSpeedsDict; apiData.PowerUsagePerDevice = perDevicePowerDict; apiData.PowerUsageTotal = 0; } else if (apiData.AlgorithmSpeedsPerDevice != null && apiData.PowerUsagePerDevice.Count == 0) { var perDevicePowerDict = new Dictionary <string, int>(); foreach (var kvp in _miningPairs) { var uuid = kvp.Device.UUID; perDevicePowerDict[uuid] = 0; } apiData.PowerUsagePerDevice = perDevicePowerDict; apiData.PowerUsageTotal = 0; } // TODO temporary here move it outside later MiningDataStats.UpdateGroup(apiData, _plugin.PluginUUID, _plugin.Name); return(apiData); }
private async Task <ApiData> GetSummaryAsync() { var apiData = new ApiData(); if (!IsUpdatingApi) { IsUpdatingApi = true; await _apiSemaphore.WaitAsync(); try { apiData = await _miner.GetMinerStatsDataAsync(); } catch (Exception e) { Logger.Error(MinerTag(), $"GetSummaryAsync error: {e.Message}"); } finally { IsUpdatingApi = false; _apiSemaphore.Release(); foreach (var apiDev in apiData.AlgorithmSpeedsPerDevice) { foreach (var kvp in apiDev.Value) { if (kvp.Speed < 0) { await StopTask(); await StartMinerTask(new CancellationToken(), StratumService.Instance.SelectedServiceLocation, CredentialsSettings.Instance.BitcoinAddress); } } } } } UpdateApiTimestamp(apiData); // TODO workaround plugins should return this info // create empty stub if it is null if (apiData == null) { Logger.Debug(MinerTag(), "GetSummary returned null... Will create ZERO fallback"); apiData = new ApiData(); } if (apiData.AlgorithmSpeedsPerDevice == null) { apiData = new ApiData(); var perDevicePowerDict = new Dictionary <string, int>(); var perDeviceSpeedsDict = new Dictionary <string, IReadOnlyList <AlgorithmTypeSpeedPair> >(); var perDeviceSpeeds = MiningPairs.Select(pair => (pair.Device.UUID, pair.Algorithm.IDs.Select(type => new AlgorithmTypeSpeedPair(type, 0d)))); foreach (var kvp in perDeviceSpeeds) { var uuid = kvp.Item1; // kvp.UUID compiler doesn't recognize ValueTypes lib??? perDeviceSpeedsDict[uuid] = kvp.Item2.ToList(); perDevicePowerDict[uuid] = 0; } apiData.AlgorithmSpeedsPerDevice = perDeviceSpeedsDict; apiData.PowerUsagePerDevice = perDevicePowerDict; apiData.PowerUsageTotal = 0; apiData.AlgorithmSpeedsTotal = perDeviceSpeedsDict.First().Value; } else if (apiData.AlgorithmSpeedsPerDevice != null && apiData.PowerUsagePerDevice.Count == 0) { var perDevicePowerDict = new Dictionary <string, int>(); foreach (var kvp in MiningPairs) { var uuid = kvp.Device.UUID; perDevicePowerDict[uuid] = 0; } apiData.PowerUsagePerDevice = perDevicePowerDict; apiData.PowerUsageTotal = 0; } // TODO temporary here move it outside later MiningDataStats.UpdateGroup(apiData, _plugin.PluginUUID, _plugin.Name); return(apiData); }