NTMinerClientService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using NTMiner.Controllers;
  2. using NTMiner.Core.Daemon;
  3. using NTMiner.Core.MinerClient;
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Net.Http;
  8. using System.Threading.Tasks;
  9. namespace NTMiner.Services.Client {
  10. public class MinerClientService {
  11. public static readonly MinerClientService Instance = new MinerClientService();
  12. private readonly string _controllerName = RpcRoot.GetControllerName<IMinerClientController>();
  13. private MinerClientService() {
  14. }
  15. #region Localhost
  16. /// <summary>
  17. /// 本机网络调用
  18. /// </summary>
  19. public void ShowMainWindowAsync(int clientPort, Action<bool, Exception> callback) {
  20. RpcRoot.PostAsync("localhost", clientPort, _controllerName, nameof(IMinerClientController.ShowMainWindow), callback);
  21. }
  22. /// <summary>
  23. /// 本机同步网络调用
  24. /// </summary>
  25. public void CloseNTMiner() {
  26. string location = NTMinerRegistry.GetLocation();
  27. if (string.IsNullOrEmpty(location) || !File.Exists(location)) {
  28. return;
  29. }
  30. string processName = Path.GetFileNameWithoutExtension(location);
  31. if (Process.GetProcessesByName(processName).Length == 0) {
  32. return;
  33. }
  34. RpcRoot.PostAsync("localhost", NTKeyword.MinerClientPort, _controllerName, nameof(IMinerClientController.CloseNTMiner), new SignRequest { }, (ResponseBase response, Exception e) => {
  35. if (!response.IsSuccess()) {
  36. try {
  37. Windows.TaskKill.Kill(processName, waitForExit: true);
  38. }
  39. catch (Exception ex) {
  40. Logger.ErrorDebugLine(ex);
  41. }
  42. }
  43. }, timeountMilliseconds: 2000);
  44. }
  45. /// <summary>
  46. /// 本机网络调用
  47. /// </summary>
  48. public void RefreshAutoBootStartAsync() {
  49. Task.Factory.StartNew(() => {
  50. try {
  51. using (HttpClient client = RpcRoot.CreateHttpClient()) {
  52. client.Timeout = TimeSpan.FromSeconds(3);
  53. Task<HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.RefreshAutoBootStart)}", null);
  54. Write.DevDebug($"{nameof(RefreshAutoBootStartAsync)} {getHttpResponse.Result.ReasonPhrase}");
  55. }
  56. }
  57. catch (Exception e) {
  58. Logger.ErrorDebugLine(e);
  59. }
  60. });
  61. }
  62. #endregion
  63. #region ClientIp
  64. public void StartMineAsync(string clientIp, WorkRequest request, Action<ResponseBase, Exception> callback) {
  65. Task.Factory.StartNew(() => {
  66. try {
  67. var response = StartMine(clientIp, request);
  68. callback?.Invoke(response, null);
  69. }
  70. catch (Exception e) {
  71. callback?.Invoke(null, e);
  72. }
  73. });
  74. }
  75. public ResponseBase StartMine(string clientIp, WorkRequest request) {
  76. using (HttpClient client = RpcRoot.CreateHttpClient()) {
  77. Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.StartMine)}", request);
  78. ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
  79. return response;
  80. }
  81. }
  82. public void StopMineAsync(string clientIp, SignRequest request, Action<ResponseBase, Exception> callback) {
  83. RpcRoot.PostAsync(clientIp, NTKeyword.MinerClientPort, _controllerName, nameof(IMinerClientController.StopMine), request, callback);
  84. }
  85. public void SetMinerProfilePropertyAsync(string clientIp, SetClientMinerProfilePropertyRequest request, Action<ResponseBase, Exception> callback) {
  86. Task.Factory.StartNew(() => {
  87. try {
  88. var response = SetMinerProfileProperty(clientIp, request);
  89. callback?.Invoke(response, null);
  90. }
  91. catch (Exception e) {
  92. callback?.Invoke(null, e);
  93. }
  94. });
  95. }
  96. public ResponseBase SetMinerProfileProperty(string clientIp, SetClientMinerProfilePropertyRequest request) {
  97. using (HttpClient client = RpcRoot.CreateHttpClient()) {
  98. client.Timeout = TimeSpan.FromSeconds(3);
  99. Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.SetMinerProfileProperty)}", request);
  100. ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
  101. return response;
  102. }
  103. }
  104. public Task<SpeedData> GetSpeedAsync(string clientIp, Action<SpeedData, Exception> callback) {
  105. return Task.Factory.StartNew(() => {
  106. try {
  107. using (HttpClient client = RpcRoot.CreateHttpClient()) {
  108. client.Timeout = TimeSpan.FromSeconds(3);
  109. Task<HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.GetSpeed)}", null);
  110. SpeedData data = getHttpResponse.Result.Content.ReadAsAsync<SpeedData>().Result;
  111. callback?.Invoke(data, null);
  112. return data;
  113. }
  114. }
  115. catch (Exception e) {
  116. callback?.Invoke(null, e);
  117. return null;
  118. }
  119. });
  120. }
  121. #endregion
  122. }
  123. }