MainForm.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using XdCxRhDW.Dto;
  9. using XdCxRhDW.Framework;
  10. using XdCxRhDW.UI.Lib;
  11. using XdCxRhDW.WebApi;
  12. namespace X2D1TaskServer
  13. {
  14. public partial class MainForm : Form
  15. {
  16. private EnumSvrType svrType = EnumSvrType.X2D1NoRefTask;
  17. public MainForm()
  18. {
  19. InitializeComponent();
  20. LogUI.Logger = info =>
  21. {
  22. try
  23. {
  24. this.Invoke(new Action(() =>
  25. {
  26. this.listBox1.Items.Insert(0, $"{info.LogTime:yyyyMMddHHmmss.fff}--{info.Msg}");
  27. if (this.listBox1.Items.Count > 1000)
  28. this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
  29. }));
  30. }
  31. catch
  32. { }
  33. };
  34. }
  35. private async void MainForm_LoadAsync(object sender, EventArgs e)
  36. {
  37. if (Debugger.IsAttached)//结束已启动的进程,方便调试
  38. {
  39. var pros = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).OrderBy(p => p.StartTime).ToList();
  40. pros.RemoveAt(pros.Count - 1);
  41. foreach (var item in pros)
  42. {
  43. try
  44. {
  45. item.Kill();
  46. }
  47. catch
  48. { }
  49. }
  50. }
  51. var port = AppConfigHelper.Get<int>("LocalHttpPort");
  52. var svrID = AppConfigHelper.Get("SvrID");
  53. var posPlatformAddr = AppConfigHelper.Get("PosPlatformAddr").AppendUrlSuffix("api/");
  54. LogUI.BaseUrl = posPlatformAddr;
  55. this.Text = svrType.GetEnumDisplayName() + "-" + svrID;
  56. string localIp;
  57. string getIpUrl = posPlatformAddr.AppendUrlSuffix("task/getclientip");
  58. while (true)
  59. {
  60. try
  61. {
  62. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl);
  63. if (rsp.code == 0)
  64. {
  65. await LogUI.Error(rsp.msg);
  66. return;
  67. }
  68. else
  69. {
  70. localIp = rsp.data;
  71. }
  72. break;
  73. }
  74. catch (Exception ex)
  75. {
  76. await LogUI.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  77. await Task.Delay(5000);
  78. }
  79. }
  80. await LogUI.Info($"本机IP={localIp}");
  81. Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
  82. await LogUI.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
  83. string url = posPlatformAddr.AppendUrlSuffix("SvrReport/Report");
  84. _ = ClearLocalFile();
  85. bool preSucceed = false;
  86. while (!this.Disposing)
  87. {
  88. try
  89. {
  90. var res = await HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  91. {
  92. SvrType = svrType,
  93. SvrID = svrID,
  94. ReportType = 0,
  95. BaseHttpAddr = $"http://{localIp}:{port}",
  96. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  97. });
  98. if (res.code != 200)
  99. {
  100. await LogUI.Error($"状态上报异常[url={url}].{res.msg}");
  101. preSucceed = false;
  102. }
  103. else
  104. {
  105. if (!preSucceed)
  106. await LogUI.Info($"状态上报成功![url={url}]");
  107. preSucceed = true;
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. await LogUI.Error($"状态上报异常[url={url}]", ex);
  113. preSucceed = false;
  114. }
  115. await Task.Delay(10000);
  116. }
  117. }
  118. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  119. {
  120. try
  121. {
  122. var port = AppConfigHelper.Get<int>("LocalHttpPort");
  123. var svrID = AppConfigHelper.Get("SvrID");
  124. var posPlatformAddr = AppConfigHelper.Get("PosPlatformAddr").AppendUrlSuffix("api/");
  125. string url = posPlatformAddr.AppendUrlSuffix("SvrReport/Report");
  126. var localIp = IpHelper.GetLocalIp();
  127. _ = HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  128. {
  129. SvrID = svrID,
  130. SvrType = svrType,
  131. ReportType = 1,
  132. BaseHttpAddr = $"http://{localIp}:{port}",
  133. });
  134. }
  135. catch (Exception ex)
  136. {
  137. _ = LogUI.Error("状态上报异常", ex);
  138. }
  139. }
  140. //清理10分钟之前的文件
  141. private async Task ClearLocalFile()
  142. {
  143. while (true)
  144. {
  145. try
  146. {
  147. var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  148. var files = Directory.EnumerateFiles(uploadFolder);
  149. foreach (var file in files)
  150. {
  151. FileInfo info = new FileInfo(file);
  152. if (info.CreationTime < DateTime.Now.AddMinutes(-10))
  153. {
  154. try
  155. {
  156. info.Delete();
  157. }
  158. catch
  159. {
  160. }
  161. }
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. await LogUI.Error("清理wwwroot历史文件异常", ex);
  167. }
  168. await Task.Delay(60 * 1000);
  169. }
  170. }
  171. }
  172. }