DirectXForm1.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Ips.Library.Basic;
  2. using Ips.Library.DxpLib;
  3. using Ips.Library.Entity;
  4. using Ips.Library.WebApi;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. namespace Ips.Service.GpuServer
  18. {
  19. public partial class DirectXForm1 : DevExpress.XtraEditors.DirectXForm
  20. {
  21. private bool _inited = false;
  22. private string _localPort;//本地端口
  23. private string _svrNo;//服务编号
  24. private string _rptAddr;//服务注册上报http地址
  25. public DirectXForm1()
  26. {
  27. InitializeComponent();
  28. this.Size = new Size(1024, 768);
  29. this.Text = "GPU服务";
  30. }
  31. protected async override void OnLoad(EventArgs e)
  32. {
  33. base.OnLoad(e);
  34. new Library.DxpLib.LogCtrl() { Parent = groupControl1 };
  35. LoadParams();
  36. if (int.TryParse(_localPort, out int intPort))
  37. {
  38. WebApiHelper.Start(intPort, "Ips.Library.Entity.xml");
  39. this.FormClosing += (_, _) => WebApiHelper.Stop();
  40. _inited = true;
  41. await StateRpt();
  42. }
  43. _inited = true;
  44. }
  45. private async Task StateRpt()
  46. {
  47. string localIp;
  48. while (true)
  49. {
  50. string getIpUrl = null;
  51. try
  52. {
  53. if (_rptAddr == null)
  54. {
  55. await Task.Delay(5000);
  56. continue;
  57. }
  58. if (!_rptAddr.ToLower().StartsWith("http://"))
  59. {
  60. IpsLogger.Error($"注册地址必须以http://开头");
  61. await Task.Delay(5000);
  62. continue;
  63. }
  64. getIpUrl = _rptAddr.AppendUrlSuffix("api/getclientip");
  65. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl, 5);
  66. if (rsp.code == 0)
  67. {
  68. IpsLogger.Error(rsp.msg);
  69. return;
  70. }
  71. else
  72. {
  73. localIp = rsp.data;
  74. }
  75. break;
  76. }
  77. catch (Exception ex)
  78. {
  79. IpsLogger.Error($"无法连接到{getIpUrl},请检测地址是否正确并确保远程设备设备防火墙入栈规则允许TCP端口{getIpUrl.GetUrlPort()}通过", ex);
  80. await Task.Delay(5000);
  81. }
  82. }
  83. AppConst.LocalIp = localIp;
  84. IpsLogger.Info($"服务启动成功.接口地址http://{localIp}:{_localPort}/swagger");
  85. bool preSucceed = false;
  86. while (!this.Disposing)
  87. {
  88. string url = _rptAddr.AppendUrlSuffix("api/SvrStateRpt/Report");
  89. try
  90. {
  91. var dto = new ModelSvrRpt()
  92. {
  93. SvrType = EnumSvrType.GpuSvr,
  94. SvrNo = _svrNo,
  95. SvrRptType = EnumSvrRptType.Online,
  96. BaseHttpAddr = $"http://{localIp}:{_localPort}",
  97. SwaggerAddr = $"http://{localIp}:{_localPort}/Swagger",
  98. };
  99. List<string> list = new List<string>();
  100. if (AppConst.UseCg)
  101. {
  102. list.Add("参数估计");
  103. }
  104. if (AppConst.UseTpdx)
  105. {
  106. list.Add("同频对消");
  107. }
  108. dto.Features = string.Join(" ", list);
  109. var res = await HttpHelper.PostRequestAsync<object>(url, dto);
  110. if (res.code != 200)
  111. {
  112. IpsLogger.Error($"状态上报异常[url={url}].{res.msg}");
  113. preSucceed = false;
  114. }
  115. else
  116. {
  117. if (!preSucceed)
  118. IpsLogger.Info($"状态上报成功![url={url}]");
  119. preSucceed = true;
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. IpsLogger.Error($"状态上报异常[url={url}]", ex);
  125. preSucceed = false;
  126. }
  127. await Task.Delay(10000);
  128. }
  129. }
  130. private void Parms_TextChanged(object sender, EventArgs e)
  131. {
  132. if (!_inited) return;
  133. SaveParams();
  134. }
  135. void SaveParams()
  136. {
  137. try
  138. {
  139. Directory.CreateDirectory("Params");
  140. StringBuilder sb = new StringBuilder();
  141. sb.AppendLine(txtLocalPort.Text);
  142. sb.AppendLine(txtSvrNo.Text);
  143. sb.AppendLine(txtRegistryAddr.Text);
  144. sb.AppendLine(txtUseTpdx.Checked.ToString());
  145. sb.AppendLine(txtUseCg.Checked.ToString());
  146. File.WriteAllText("Params\\Params.txt", sb.ToString());
  147. IpsLogger.Info("用户修改了UI参数!");
  148. }
  149. catch (Exception ex)
  150. {
  151. IpsLogger.Error("保存Params.txt文件异常", ex);
  152. }
  153. _localPort = txtLocalPort.Text.Trim();
  154. _svrNo = txtSvrNo.Text.Trim();
  155. _rptAddr = txtRegistryAddr.Text.Trim();
  156. AppConst.UseTpdx = txtUseTpdx.Checked;
  157. AppConst.UseCg = txtUseCg.Checked;
  158. AppConst.SvrNo = _svrNo;
  159. }
  160. void LoadParams()
  161. {
  162. if (File.Exists("Params\\Params.txt"))
  163. {
  164. try
  165. {
  166. var lines = File.ReadAllLines("Params\\Params.txt");
  167. txtLocalPort.Text = lines[0];
  168. txtSvrNo.Text = lines[1];
  169. txtRegistryAddr.Text = lines[2];
  170. txtUseTpdx.Checked = lines[3].ToLower() == "true";
  171. txtUseCg.Checked = lines[4].ToLower() == "true";
  172. }
  173. catch (Exception ex)
  174. {
  175. IpsLogger.Error("加载Params.txt文件异常", ex);
  176. }
  177. }
  178. _localPort = txtLocalPort.Text.Trim();
  179. _svrNo = txtSvrNo.Text.Trim();
  180. _rptAddr = txtRegistryAddr.Text.Trim();
  181. AppConst.UseTpdx = txtUseCg.Checked;
  182. AppConst.UseCg = txtUseCg.Checked;
  183. AppConst.SvrNo = _svrNo;
  184. }
  185. private void BtnShowApi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  186. {
  187. string addr = $"http://127.0.0.1:{_localPort}/swagger";
  188. try
  189. {
  190. //System.Diagnostics.Process.Start(addr);//NetFramework可用,NetCore使用CMD启动
  191. ProcessStartInfo info = new ProcessStartInfo();
  192. info.Arguments = $"/C start {addr}";
  193. info.FileName = "cmd";
  194. info.UseShellExecute = false;
  195. info.CreateNoWindow = true;
  196. Process.Start(info);
  197. }
  198. catch (Exception ex)
  199. {
  200. IpsLogger.Error($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}", ex);
  201. }
  202. }
  203. }
  204. }