MainForm.cs 6.8 KB

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