LocalMinerStudioService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using NTMiner.Controllers;
  2. using NTMiner.Core;
  3. using NTMiner.Core.Daemon;
  4. using NTMiner.Core.Impl;
  5. using NTMiner.Core.MinerClient;
  6. using NTMiner.Core.MinerServer;
  7. using NTMiner.Core.MinerStudio;
  8. using NTMiner.JsonDb;
  9. using NTMiner.VirtualMemory;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Net.Http;
  14. namespace NTMiner.MinerStudio.Impl {
  15. public class LocalMinerStudioService : ILocalMinerStudioService {
  16. private readonly string _daemonControllerName = RpcRoot.GetControllerName<INTMinerDaemonController>();
  17. private readonly IClientDataSet _clientDataSet;
  18. private readonly ICoinSnapshotSet _coinSnapshotSet;
  19. public LocalMinerStudioService() {
  20. _clientDataSet = new ClientDataSet();
  21. _coinSnapshotSet = new CoinSnapshotSet(_clientDataSet);
  22. }
  23. #region AddClientsAsync
  24. public void AddClientsAsync(List<string> clientIps, Action<ResponseBase, Exception> callback) {
  25. try {
  26. foreach (var clientIp in clientIps) {
  27. ClientData clientData = _clientDataSet.AsEnumerable().FirstOrDefault(a => a.MinerIp == clientIp);
  28. if (clientData != null) {
  29. continue;
  30. }
  31. _clientDataSet.AddClient(clientIp);
  32. }
  33. callback?.Invoke(ResponseBase.Ok(), null);
  34. }
  35. catch (Exception e) {
  36. callback?.Invoke(ResponseBase.ServerError(e.Message), e);
  37. }
  38. }
  39. #endregion
  40. #region RemoveClientsAsync
  41. public void RemoveClientsAsync(List<string> objectIds, Action<ResponseBase, Exception> callback) {
  42. try {
  43. foreach (var objectId in objectIds) {
  44. _clientDataSet.RemoveByObjectId(objectId);
  45. }
  46. callback?.Invoke(ResponseBase.Ok(), null);
  47. }
  48. catch (Exception e) {
  49. callback?.Invoke(ResponseBase.ServerError(e.Message), e);
  50. }
  51. }
  52. #endregion
  53. #region QueryClientsAsync
  54. public void QueryClientsAsync(QueryClientsRequest query, Action<QueryClientsResponse, Exception> callback) {
  55. try {
  56. var data = _clientDataSet.QueryClients(
  57. user: null,
  58. query,
  59. out int total,
  60. out List<CoinSnapshotData> latestSnapshots,
  61. out int totalOnlineCount,
  62. out int totalMiningCount);
  63. callback?.Invoke(QueryClientsResponse.Ok(data, total, latestSnapshots, totalMiningCount, totalOnlineCount), null);
  64. }
  65. catch (Exception e) {
  66. callback?.Invoke(ResponseBase.ServerError<QueryClientsResponse>(e.Message), e);
  67. }
  68. }
  69. #endregion
  70. #region UpdateClientAsync
  71. public void UpdateClientAsync(string objectId, string propertyName, object value, Action<ResponseBase, Exception> callback) {
  72. try {
  73. _clientDataSet.UpdateClient(objectId, propertyName, value);
  74. callback?.Invoke(ResponseBase.Ok(), null);
  75. }
  76. catch (Exception e) {
  77. callback?.Invoke(ResponseBase.ServerError(e.Message), e);
  78. }
  79. }
  80. #endregion
  81. #region UpdateClientsAsync
  82. public void UpdateClientsAsync(string propertyName, Dictionary<string, object> values, Action<ResponseBase, Exception> callback) {
  83. try {
  84. _clientDataSet.UpdateClients(propertyName, values);
  85. callback?.Invoke(ResponseBase.Ok(), null);
  86. }
  87. catch (Exception e) {
  88. callback?.Invoke(ResponseBase.ServerError(e.Message), e);
  89. }
  90. }
  91. #endregion
  92. #region GetLatestSnapshotsAsync
  93. public void GetLatestSnapshotsAsync(int limit, Action<GetCoinSnapshotsResponse, Exception> callback) {
  94. try {
  95. List<CoinSnapshotData> data = _coinSnapshotSet.GetLatestSnapshots(
  96. limit,
  97. out int totalMiningCount,
  98. out int totalOnlineCount) ?? new List<CoinSnapshotData>();
  99. callback?.Invoke(GetCoinSnapshotsResponse.Ok(data, totalMiningCount, totalOnlineCount), null);
  100. }
  101. catch (Exception e) {
  102. callback?.Invoke(ResponseBase.ServerError<GetCoinSnapshotsResponse>(e.Message), e);
  103. }
  104. }
  105. #endregion
  106. #region EnableRemoteDesktopAsync
  107. public void EnableRemoteDesktopAsync(IMinerData client) {
  108. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.EnableRemoteDesktop), null, null, timeountMilliseconds: 3000);
  109. }
  110. #endregion
  111. #region GetConsoleOutLinesAsync
  112. public void GetConsoleOutLinesAsync(IMinerData client, long afterTime) {
  113. RpcRoot.Client.MinerClientService.GetConsoleOutLinesAsync(client.GetLocalIp(), afterTime, (data, e) => {
  114. if (data != null && data.Count > 0) {
  115. VirtualRoot.RaiseEvent(new ClientConsoleOutLinesEvent(client.ClientId, data));
  116. }
  117. });
  118. }
  119. #endregion
  120. #region BlockWAUAsync
  121. public void BlockWAUAsync(IMinerData client) {
  122. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.BlockWAU), null, null, timeountMilliseconds: 3000);
  123. }
  124. #endregion
  125. #region AtikmdagPatcherAsync
  126. public void AtikmdagPatcherAsync(IMinerData client) {
  127. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.AtikmdagPatcher), null, null, timeountMilliseconds: 3000);
  128. }
  129. #endregion
  130. #region SwitchRadeonGpuAsync
  131. public void SwitchRadeonGpuAsync(IMinerData client, bool on) {
  132. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SwitchRadeonGpu), new Dictionary<string, string> {
  133. {"on", on.ToString() }
  134. }, null, null, timeountMilliseconds: 3000);
  135. }
  136. #endregion
  137. #region RestartWindowsAsync
  138. public void RestartWindowsAsync(IMinerData client) {
  139. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.RestartWindows), new object(), null, timeountMilliseconds: 3000);
  140. }
  141. #endregion
  142. #region ShutdownWindowsAsync
  143. public void ShutdownWindowsAsync(IMinerData client) {
  144. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.ShutdownWindows), new object(), null, timeountMilliseconds: 3000);
  145. }
  146. #endregion
  147. #region UpgradeNTMinerAsync
  148. // ReSharper disable once InconsistentNaming
  149. public void UpgradeNTMinerAsync(IMinerData client, string ntminerFileName) {
  150. UpgradeNTMinerRequest request = new UpgradeNTMinerRequest {
  151. NTMinerFileName = ntminerFileName
  152. };
  153. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.UpgradeNTMiner), request, null, timeountMilliseconds: 3000);
  154. }
  155. #endregion
  156. #region SetAutoBootStartAsync
  157. public void SetAutoBootStartAsync(IMinerData client, SetAutoBootStartRequest request) {
  158. RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetAutoBootStart), new Dictionary<string, string> {
  159. {"autoBoot", request.AutoBoot.ToString() },
  160. {"autoStart", request.AutoStart.ToString() }
  161. }, null, callback: null, timeountMilliseconds: 3000);
  162. }
  163. #endregion
  164. #region StartMineAsync
  165. public void StartMineAsync(IMinerData client, Guid workId) {
  166. string localJson = string.Empty, serverJson = string.Empty;
  167. if (workId != Guid.Empty) {
  168. localJson = MinerStudioPath.ReadMineWorkLocalJsonFile(workId).Replace(NTKeyword.MinerNameParameterName, client.WorkerName);
  169. serverJson = MinerStudioPath.ReadMineWorkServerJsonFile(workId);
  170. }
  171. WorkRequest request = new WorkRequest {
  172. WorkId = workId,
  173. WorkerName = client.WorkerName,
  174. LocalJson = localJson,
  175. ServerJson = serverJson
  176. };
  177. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.StartMine), request, null, timeountMilliseconds: 3000);
  178. }
  179. #endregion
  180. #region StopMineAsync
  181. public void StopMineAsync(IMinerData client) {
  182. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.StopMine), new object(), null, timeountMilliseconds: 3000);
  183. }
  184. #endregion
  185. #region GetDrivesAsync
  186. public void GetDrivesAsync(IMinerData client) {
  187. RpcRoot.PostAsync<List<DriveDto>>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetDrives), null, (data, e) => {
  188. VirtualRoot.RaiseEvent(new GetDrivesResponsedEvent(client.ClientId, data));
  189. }, timeountMilliseconds: 3000);
  190. }
  191. #endregion
  192. #region SetVirtualMemoryAsync
  193. public void SetVirtualMemoryAsync(IMinerData client, Dictionary<string, int> data) {
  194. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetVirtualMemory), new DataRequest<Dictionary<string, int>> {
  195. Data = data
  196. }, null, timeountMilliseconds: 3000);
  197. }
  198. #endregion
  199. #region GetLocalIpsAsync
  200. public void GetLocalIpsAsync(IMinerData client) {
  201. RpcRoot.PostAsync<List<LocalIpDto>>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetLocalIps), null, (data, e) => {
  202. VirtualRoot.RaiseEvent(new GetLocalIpsResponsedEvent(client.ClientId, data));
  203. }, timeountMilliseconds: 3000);
  204. }
  205. #endregion
  206. #region SetLocalIpsAsync
  207. public void SetLocalIpsAsync(IMinerData client, List<LocalIpInput> data) {
  208. RpcRoot.PostAsync<ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetLocalIps), new DataRequest<List<LocalIpInput>> {
  209. Data = data
  210. }, null, timeountMilliseconds: 3000);
  211. }
  212. #endregion
  213. #region GetOperationResultsAsync
  214. public void GetOperationResultsAsync(IMinerData client, long afterTime) {
  215. RpcRoot.GetAsync<List<OperationResultData>>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetOperationResults), new Dictionary<string, string> {
  216. {"afterTime",afterTime.ToString() }
  217. }, (data, e) => {
  218. if (data != null && data.Count > 0) {
  219. VirtualRoot.RaiseEvent(new ClientOperationResultsEvent(client.ClientId, data));
  220. }
  221. }, timeountMilliseconds: 3000);
  222. }
  223. #endregion
  224. #region GetSelfWorkLocalJsonAsync
  225. public void GetSelfWorkLocalJsonAsync(IMinerData client) {
  226. RpcRoot.PostAsync<string>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetSelfWorkLocalJson), null, (json, e) => {
  227. VirtualRoot.RaiseEvent(new GetSelfWorkLocalJsonResponsedEvent(client.ClientId, json));
  228. }, timeountMilliseconds: 3000);
  229. }
  230. #endregion
  231. #region SaveSelfWorkLocalJsonAsync
  232. public void SaveSelfWorkLocalJsonAsync(IMinerData client, string localJson, string serverJson) {
  233. if (string.IsNullOrEmpty(localJson) || string.IsNullOrEmpty(serverJson)) {
  234. return;
  235. }
  236. WorkRequest request = new WorkRequest {
  237. WorkId = MineWorkData.SelfMineWorkId,
  238. WorkerName = client.WorkerName,
  239. LocalJson = localJson.Replace(NTKeyword.MinerNameParameterName, client.WorkerName),
  240. ServerJson = serverJson
  241. };
  242. RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SaveSelfWorkLocalJson), null, request, timeountMilliseconds: 3000);
  243. }
  244. #endregion
  245. #region GetGpuProfilesJsonAsync
  246. public void GetGpuProfilesJsonAsync(IMinerData client) {
  247. RpcRoot.PostAsync<string>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetGpuProfilesJson), null, (json, e) => {
  248. GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>(json) ?? new GpuProfilesJsonDb();
  249. VirtualRoot.RaiseEvent(new GetGpuProfilesResponsedEvent(client.ClientId, data));
  250. }, timeountMilliseconds: 3000);
  251. }
  252. #endregion
  253. #region SaveGpuProfilesJsonAsync
  254. public void SaveGpuProfilesJsonAsync(IMinerData client, string json) {
  255. HttpContent content = new StringContent(json);
  256. RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SaveGpuProfilesJson), null, content, null, timeountMilliseconds: 3000);
  257. }
  258. #endregion
  259. }
  260. }