MainForm.cs 6.4 KB

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