Beispiel #1
0
        public async override Task <ApiData> GetMinerStatsDataAsync()
        {
            var api = new ApiData();

            try
            {
                JsonApiResponse resp = null;
                using (var client = new TcpClient("127.0.0.1", _apiPort))
                    using (var nwStream = client.GetStream())
                    {
                        var bytesToSend = Encoding.ASCII.GetBytes("{\"method\":\"getstat\"}\n");
                        await nwStream.WriteAsync(bytesToSend, 0, bytesToSend.Length);

                        var bytesToRead = new byte[client.ReceiveBufferSize];
                        var bytesRead   = await nwStream.ReadAsync(bytesToRead, 0, client.ReceiveBufferSize);

                        var respStr = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
                        resp = JsonConvert.DeserializeObject <JsonApiResponse>(respStr);
                        client.Close();
                    }

                // return if we got nothing
                var respOK = resp != null && resp.error == null;
                if (respOK == false)
                {
                    return(api);
                }

                var results = resp.result;

                var gpus = _miningPairs.Select(pair => pair.Device);
                var perDeviceSpeedInfo = new Dictionary <string, IReadOnlyList <AlgorithmTypeSpeedPair> >();
                var perDevicePowerInfo = new Dictionary <string, int>();
                var totalSpeed         = 0d;
                var totalPowerUsage    = 0;
                foreach (var gpu in gpus)
                {
                    var currentDevStats = results.Where(r => r.cudaid == gpu.ID).FirstOrDefault();
                    if (currentDevStats == null)
                    {
                        continue;
                    }
                    totalSpeed += currentDevStats.speed_sps;
                    perDeviceSpeedInfo.Add(gpu.UUID, new List <AlgorithmTypeSpeedPair>()
                    {
                        new AlgorithmTypeSpeedPair(_algorithmType, currentDevStats.speed_sps * (1 - DevFee * 0.01))
                    });
                    totalPowerUsage += (int)currentDevStats.gpu_power_usage;
                    perDevicePowerInfo.Add(gpu.UUID, (int)currentDevStats.gpu_power_usage);
                }
                api.AlgorithmSpeedsTotal = new List <AlgorithmTypeSpeedPair> {
                    new AlgorithmTypeSpeedPair(_algorithmType, totalSpeed * (1 - DevFee * 0.01))
                };
                api.PowerUsageTotal          = totalPowerUsage;
                api.AlgorithmSpeedsPerDevice = perDeviceSpeedInfo;
                api.PowerUsagePerDevice      = perDevicePowerInfo;
            }
            catch (Exception e)
            {
                Logger.Error(_logGroup, $"Error occured while getting API stats: {e.Message}");
            }

            return(api);
        }
Beispiel #2
0
        public async override Task <ApiData> GetMinerStatsDataAsync()
        {
            var ad = new ApiData();

            try
            {
                JsonApiResponse resp = null;
                using (var client = new TcpClient("127.0.0.1", _apiPort))
                    using (var nwStream = client.GetStream())
                    {
                        var bytesToSend = Encoding.ASCII.GetBytes("{\"method\":\"getstat\"}\n");
                        await nwStream.WriteAsync(bytesToSend, 0, bytesToSend.Length);

                        var bytesToRead = new byte[client.ReceiveBufferSize];
                        var bytesRead   = await nwStream.ReadAsync(bytesToRead, 0, client.ReceiveBufferSize);

                        var respStr = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
                        resp = JsonConvert.DeserializeObject <JsonApiResponse>(respStr);
                        client.Close();
                    }

                // return if we got nothing
                var respOK = resp != null && resp.error == null;
                if (respOK == false)
                {
                    return(ad);
                }

                var results = resp.result;

                var gpus = _miningPairs.Select(pair => pair.device);
                var perDeviceSpeedInfo = new List <(string uuid, IReadOnlyList <(AlgorithmType, double)>)>();
                var perDevicePowerInfo = new List <(string, int)>();
                var totalSpeed         = 0d;
                var totalPowerUsage    = 0;
                foreach (var gpu in gpus)
                {
                    var currentDevStats = results.Where(r => r.cudaid == gpu.ID).FirstOrDefault();
                    if (currentDevStats == null)
                    {
                        continue;
                    }
                    totalSpeed += currentDevStats.speed_sps;
                    perDeviceSpeedInfo.Add((gpu.UUID, new List <(AlgorithmType, double)>()
                    {
                        (_algorithmType, currentDevStats.speed_sps)
                    }));
                    totalPowerUsage += (int)currentDevStats.gpu_power_usage;
                    perDevicePowerInfo.Add((gpu.UUID, (int)currentDevStats.gpu_power_usage));
                }
                ad.AlgorithmSpeedsTotal = new List <(AlgorithmType, double)> {
                    (_algorithmType, totalSpeed)
                };
                ad.PowerUsageTotal = totalPowerUsage;
            }
            catch (Exception ex)
            {
                //Helpers.ConsolePrint(MinerTag(), ex.Message);
            }

            return(ad);
        }