MainForm.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 X2D1NoRefTaskServer54
  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. try
  51. {
  52. if (Directory.Exists("tmp"))
  53. Directory.Delete("tmp");
  54. }
  55. catch
  56. { }
  57. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  58. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  59. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  60. LogHelper.BaseUrl = posPlatformAddr + "/api/";
  61. this.Text = EnumSvrType.X2D1NoRefTask54.GetEnumDisplayName() + "-" + svrID;
  62. string localIp;
  63. string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
  64. while (true)
  65. {
  66. try
  67. {
  68. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl, 10);
  69. if (rsp.code == 0)
  70. {
  71. await LogHelper.Error(rsp.msg);
  72. return;
  73. }
  74. else
  75. {
  76. localIp = rsp.data;
  77. }
  78. break;
  79. }
  80. catch (Exception ex)
  81. {
  82. await LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  83. await Task.Delay(5000);
  84. }
  85. }
  86. await LogHelper.Info($"本机IP={localIp}");
  87. Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
  88. await LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
  89. string url;
  90. if (posPlatformAddr.EndsWith("/"))
  91. url = posPlatformAddr + "api/SvrReport/Report";
  92. else
  93. url = posPlatformAddr + "/api/SvrReport/Report";
  94. bool preSucceed = false;
  95. while (!this.Disposing)
  96. {
  97. try
  98. {
  99. var res = await HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  100. {
  101. SvrType = EnumSvrType.X2D1NoRefTask54,
  102. SvrID = svrID,
  103. ReportType = 0,
  104. BaseHttpAddr = $"http://{localIp}:{port}",
  105. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  106. });
  107. if (res.code != 200)
  108. {
  109. await LogHelper.Error($"状态上报异常[url={url}].{res.msg}");
  110. preSucceed = false;
  111. }
  112. else
  113. {
  114. if (!preSucceed)
  115. await LogHelper.Info($"状态上报成功![url={url}]");
  116. preSucceed = true;
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. await LogHelper.Error($"状态上报异常[url={url}]", ex);
  122. preSucceed = false;
  123. }
  124. await Task.Delay(6000);
  125. }
  126. }
  127. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  128. {
  129. try
  130. {
  131. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  132. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  133. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  134. string url;
  135. if (posPlatformAddr.EndsWith("/"))
  136. url = posPlatformAddr + "api/SvrReport/Report";
  137. else
  138. url = posPlatformAddr + "/api/SvrReport/Report";
  139. var localIp = IpHelper.GetLocalIp();
  140. _ = HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  141. {
  142. SvrID = svrID,
  143. SvrType = EnumSvrType.X2D1NoRefTask54,
  144. ReportType = 1,
  145. BaseHttpAddr = $"http://{localIp}:{port}",
  146. });
  147. }
  148. catch (Exception ex)
  149. {
  150. _ = LogHelper.Error("状态上报异常", ex);
  151. }
  152. }
  153. }
  154. }