|
1 |
| -using System.IO; |
| 1 | +using BitPoolMiner.Models; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | +using System.IO; |
| 8 | +using System.Text; |
| 9 | +using System.Globalization; |
| 10 | +using System.Linq; |
2 | 11 | using BitPoolMiner.Enums;
|
| 12 | +using BitPoolMiner.Utils; |
3 | 13 |
|
4 | 14 | namespace BitPoolMiner.Miners
|
5 | 15 | {
|
6 | 16 | /// <summary>
|
7 |
| - /// This class is for CryptoDredge which is ccminer fork derived class. |
| 17 | + /// This class is for CryptoDredge derived class. |
8 | 18 | /// </summary>
|
9 |
| - public class CryptoDredge : Ccminer |
| 19 | + public class CryptoDredge : Miner |
10 | 20 | {
|
11 |
| - public CryptoDredge(HardwareType hardwareType, MinerBaseType minerBaseType) : base(hardwareType, minerBaseType, false) |
| 21 | + public CryptoDredge(HardwareType hardwareType, MinerBaseType minerBaseType) : base("Ccminer", hardwareType, minerBaseType, true) |
12 | 22 | {
|
13 | 23 | string versionedDirectory = "";
|
14 | 24 | MinerFileName = "CryptoDredge.exe";
|
15 | 25 | versionedDirectory = "CryptoDredge_0.11.0_win_x64";
|
| 26 | + |
16 | 27 | MinerWorkingDirectory = Path.Combine(Utils.Core.GetBaseMinersDir(), versionedDirectory);
|
17 | 28 |
|
18 | 29 | ApiPort = 4068;
|
19 | 30 | HostName = "127.0.0.1";
|
20 | 31 | }
|
21 | 32 |
|
| 33 | + public override void Start() |
| 34 | + { |
| 35 | + MinerProcess = StartProcess(); |
| 36 | + } |
| 37 | + |
| 38 | + public override void Stop() |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + StopProcess(); |
| 43 | + } |
| 44 | + catch (Exception e) |
| 45 | + { |
| 46 | + throw new ApplicationException(string.Format("There was an error killing the miner process {0} with PID {1}", MinerProcess.MinerProcess.ProcessName, MinerProcess.MinerProcess.Handle), e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #region Monitoring Statistics |
| 51 | + |
| 52 | + public async override void ReportStatsAsyc() |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + var minerMonitorStat = await GetRPCResponse(); |
| 57 | + |
| 58 | + if (minerMonitorStat == null) |
| 59 | + return; |
| 60 | + |
| 61 | + System.Threading.Thread.Sleep(4000); |
| 62 | + PostMinerMonitorStat(minerMonitorStat); |
| 63 | + } |
| 64 | + catch (Exception e) |
| 65 | + { |
| 66 | + NLogProcessing.LogError(e, "Error reporting stats for Ccminer"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private async Task<MinerMonitorStat> GetRPCResponse() |
| 71 | + { |
| 72 | + MinerMonitorStat minerMonitorStat = new MinerMonitorStat(); |
| 73 | + try |
| 74 | + { |
| 75 | + int gpuNumber = 0; |
| 76 | + var dictHW = CryptoDredgeApi.GetHwInfo(HostName, ApiPort); |
| 77 | + var dictHist = CryptoDredgeApi.GetHistory(HostName, ApiPort); |
| 78 | + var dictSummary = CryptoDredgeApi.GetSummary(HostName, ApiPort); |
| 79 | + |
| 80 | + minerMonitorStat.AccountGuid = (Guid)Application.Current.Properties["AccountID"]; |
| 81 | + minerMonitorStat.WorkerName = Application.Current.Properties["WorkerName"].ToString(); |
| 82 | + minerMonitorStat.CoinType = CoinType; |
| 83 | + minerMonitorStat.MinerBaseType = MinerBaseType; |
| 84 | + |
| 85 | + var gpuList = dictHW.ToList(); |
| 86 | + |
| 87 | + // Remove the last element in the list |
| 88 | + gpuList.RemoveAt(gpuList.Count - 1); |
| 89 | + |
| 90 | + List<GPUMonitorStat> gpuMonitorStatList = new List<GPUMonitorStat>(); |
| 91 | + |
| 92 | + foreach (var gpu in gpuList) |
| 93 | + { |
| 94 | + //var gpuHash = (from element in dictHist |
| 95 | + // orderby element["GPU"] ascending, element["TS"] descending |
| 96 | + // where element["GPU"] == gpuNumber |
| 97 | + // select element).FirstOrDefault(); |
| 98 | + |
| 99 | + var gpuHw = (from hw in dictHW |
| 100 | + where hw["GPU"].ToString() == gpuNumber.ToString() |
| 101 | + select hw).FirstOrDefault(); |
| 102 | + |
| 103 | + // Create new GPU monitor stats object and map values |
| 104 | + GPUMonitorStat gpuMonitorStat = new GPUMonitorStat(); |
| 105 | + |
| 106 | + gpuMonitorStat.AccountGuid = (Guid)Application.Current.Properties["AccountID"]; |
| 107 | + gpuMonitorStat.WorkerName = Application.Current.Properties["WorkerName"].ToString(); |
| 108 | + gpuMonitorStat.CoinType = CoinType; |
| 109 | + gpuMonitorStat.GPUID = Convert.ToInt32(gpuNumber); |
| 110 | + //gpuMonitorStat.HashRate = (Convert.ToDecimal(gpuHash["KHS"])); |
| 111 | + gpuMonitorStat.FanSpeed = Convert.ToInt16(gpuHw["FAN"]); |
| 112 | + gpuMonitorStat.Temp = (short)Convert.ToDecimal(gpuHw["TEMP"]); |
| 113 | + gpuMonitorStat.Power = (short)(Convert.ToDecimal(gpuHw["POWER"]) / 1000); |
| 114 | + gpuMonitorStat.HardwareType = Hardware; |
| 115 | + |
| 116 | + // Sum up power and hashrate |
| 117 | + minerMonitorStat.Power += (short)gpuMonitorStat.Power; |
| 118 | + minerMonitorStat.HashRate += gpuMonitorStat.HashRate; |
| 119 | + |
| 120 | + // Add GPU stats to list |
| 121 | + gpuMonitorStatList.Add(gpuMonitorStat); |
| 122 | + gpuNumber++; |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + // Set list of GPU monitor stats |
| 127 | + minerMonitorStat.GPUMonitorStatList = gpuMonitorStatList; |
| 128 | + |
| 129 | + return await Task.Run(() => { return minerMonitorStat; }); |
| 130 | + |
| 131 | + } |
| 132 | + catch (Exception ex) |
| 133 | + { |
| 134 | + NLogProcessing.LogError(ex, "Error calling GetRPCResponse from miner."); |
| 135 | + |
| 136 | + // Return null object; |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
22 | 140 | }
|
23 |
| -} |
24 | 141 |
|
| 142 | + internal static class CryptoDredgeApi |
| 143 | + { |
| 144 | + // Converts the results from Pruvots api to an array of dictionaries to get more JSON like results |
| 145 | + public static Dictionary<string, string>[] ConvertPruvotToDictArray(string apiResult) |
| 146 | + { |
| 147 | + if (apiResult == null) return null; |
| 148 | + |
| 149 | + // Will return a Dict per GPU |
| 150 | + string[] splitGroups = apiResult.Split('|'); |
| 151 | + |
| 152 | + Dictionary<string, string>[] totalDict = new Dictionary<string, string>[splitGroups.Length - 1]; |
| 153 | + |
| 154 | + for (int index = 0; index < splitGroups.Length - 1; index++) |
| 155 | + { |
| 156 | + Dictionary<string, string> separateDict = new Dictionary<string, string>(); |
| 157 | + string[] keyValues = splitGroups[index].Split(';'); |
| 158 | + for (int i = 0; i < keyValues.Length; i++) |
| 159 | + { |
| 160 | + string[] elements = keyValues[i].Split('='); |
| 161 | + if (elements.Length > 1) separateDict.Add(elements[0], elements[1]); |
| 162 | + } |
| 163 | + totalDict[index] = separateDict; |
| 164 | + } |
| 165 | + |
| 166 | + return totalDict; |
| 167 | + } |
| 168 | + |
| 169 | + public static T GetDictValue<T>(Dictionary<string, string> dictionary, string key) |
| 170 | + { |
| 171 | + string value; |
| 172 | + |
| 173 | + if (dictionary.TryGetValue(key, out value)) |
| 174 | + { |
| 175 | + return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture); |
| 176 | + } |
| 177 | + |
| 178 | + // Unsigneds can't be negative |
| 179 | + if (typeof(T) == typeof(uint)) return (T)Convert.ChangeType(9001, typeof(T), CultureInfo.InvariantCulture); |
| 180 | + return (T)Convert.ChangeType(-1, typeof(T), CultureInfo.InvariantCulture); |
| 181 | + } |
| 182 | + |
| 183 | + // Overload that just returns the string without type conversion |
| 184 | + public static string GetDictValue(Dictionary<string, string> dictionary, string key) |
| 185 | + { |
| 186 | + string value; |
| 187 | + |
| 188 | + if (dictionary.TryGetValue(key, out value)) |
| 189 | + { |
| 190 | + return value; |
| 191 | + } |
| 192 | + |
| 193 | + return "-1"; |
| 194 | + } |
| 195 | + |
| 196 | + public static Dictionary<string, string>[] GetSummary(string ip = "127.0.0.1", int port = 4068) |
| 197 | + { |
| 198 | + return Request(ip, port, "summary"); |
| 199 | + } |
| 200 | + |
| 201 | + public static Dictionary<string, string>[] GetHwInfo(string ip = "127.0.0.1", int port = 4068) |
| 202 | + { |
| 203 | + return Request(ip, port, "hwinfo"); |
| 204 | + } |
| 205 | + |
| 206 | + public static Dictionary<string, string>[] GetMemInfo(string ip = "127.0.0.1", int port = 4068) |
| 207 | + { |
| 208 | + return Request(ip, port, "meminfo"); |
| 209 | + } |
| 210 | + |
| 211 | + public static Dictionary<string, string>[] GetThreads(string ip = "127.0.0.1", int port = 4068) |
| 212 | + { |
| 213 | + return Request(ip, port, "threads"); |
| 214 | + } |
| 215 | + |
| 216 | + public static Dictionary<string, string>[] GetHistory(string ip = "127.0.0.1", int port = 4068, int minerMap = -1) |
| 217 | + { |
| 218 | + Dictionary<string, string>[] histo = Request(ip, port, "histo"); |
| 219 | + |
| 220 | + if (histo == null) return null; |
| 221 | + |
| 222 | + bool existsInHisto = false; |
| 223 | + foreach (Dictionary<string, string> log in histo) |
| 224 | + { |
| 225 | + if (GetDictValue<int>(log, "GPU") == minerMap) |
| 226 | + { |
| 227 | + existsInHisto = true; |
| 228 | + break; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + if (existsInHisto || minerMap == -1) return minerMap == -1 ? histo : Request(ip, port, "histo|" + minerMap); |
| 233 | + |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + public static Dictionary<string, string>[] GetPoolInfo(string ip = "127.0.0.1", int port = 4068) |
| 238 | + { |
| 239 | + return Request(ip, port, "pool"); |
| 240 | + } |
| 241 | + |
| 242 | + public static Dictionary<string, string>[] GetScanLog(string ip = "127.0.0.1", int port = 4068) |
| 243 | + { |
| 244 | + return Request(ip, port, "scanlog"); |
| 245 | + } |
| 246 | + |
| 247 | + public static Dictionary<string, string>[] Request(string ip, int port, string message) |
| 248 | + { |
| 249 | + string responseData = ""; |
| 250 | + |
| 251 | + try |
| 252 | + { |
| 253 | + using (TcpClient client = new TcpClient(ip, port)) |
| 254 | + { |
| 255 | + using (NetworkStream stream = client.GetStream()) |
| 256 | + { |
| 257 | + byte[] data = Encoding.ASCII.GetBytes(message); |
| 258 | + stream.Write(data, 0, data.Length); |
| 259 | + stream.Flush(); |
| 260 | + |
| 261 | + data = new Byte[4096]; |
| 262 | + |
| 263 | + int bytes = stream.Read(data, 0, data.Length); |
| 264 | + responseData = Encoding.ASCII.GetString(data, 0, bytes); |
| 265 | + } |
| 266 | + |
| 267 | + } |
| 268 | + } |
| 269 | + catch (Exception e) |
| 270 | + { |
| 271 | + NLogProcessing.LogError(e, $"Error getting Request from ccminer on port {port}"); |
| 272 | + return null; |
| 273 | + } |
| 274 | + |
| 275 | + |
| 276 | + return ConvertPruvotToDictArray(responseData); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + #endregion |
| 281 | +} |
0 commit comments