MainForm.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace CheckServer
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. LogHelper.Logger = info =>
  19. {
  20. try
  21. {
  22. this.Invoke(new Action(() =>
  23. {
  24. this.listBox1.Items.Insert(0, $"{info.LogTime:yyyyMMddHHmmss.fff}--{info.Msg}");
  25. if (this.listBox1.Items.Count > 1000)
  26. this.listBox1.Items.RemoveAt(this.listBox1.Items.Count-1);
  27. }));
  28. }
  29. catch
  30. { }
  31. };
  32. }
  33. private async void MainForm_LoadAsync(object sender, EventArgs e)
  34. {
  35. if (Debugger.IsAttached)//结束已启动的进程,方便调试
  36. {
  37. var pros = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).OrderBy(p => p.StartTime).ToList();
  38. pros.RemoveAt(pros.Count - 1);
  39. foreach (var item in pros)
  40. {
  41. try
  42. {
  43. item.Kill();
  44. }
  45. catch
  46. { }
  47. }
  48. }
  49. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  50. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  51. LogHelper.BaseUrl = posPlatformAddr + "/api/";
  52. this.Text = EnumSvrType.CheckSvr.GetEnumDisplayName() + "-" + svrID;
  53. string localIp;
  54. string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
  55. while (true)
  56. {
  57. try
  58. {
  59. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl, 10);
  60. if (rsp.code == 0)
  61. {
  62. await LogHelper.Error(rsp.msg);
  63. return;
  64. }
  65. else
  66. {
  67. localIp = rsp.data;
  68. }
  69. break;
  70. }
  71. catch (Exception ex)
  72. {
  73. await LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  74. await Task.Delay(5000);
  75. }
  76. }
  77. string url;
  78. if (posPlatformAddr.EndsWith("/"))
  79. url = posPlatformAddr + "api/SvrReport/Report";
  80. else
  81. url = posPlatformAddr + "/api/SvrReport/Report";
  82. bool preSucceed = false;
  83. while (!this.Disposing)
  84. {
  85. try
  86. {
  87. var res = await HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  88. {
  89. SvrType = EnumSvrType.CgDbScan,
  90. SvrID = svrID,
  91. ReportType = 0,
  92. });
  93. if (res.code != 200)
  94. {
  95. await LogHelper.Error($"状态上报异常[url={url}].{res.msg}");
  96. preSucceed = false;
  97. }
  98. else
  99. {
  100. if (!preSucceed)
  101. await LogHelper.Info($"状态上报成功![url={url}]");
  102. preSucceed = true;
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. await LogHelper.Error($"状态上报异常[url={url}]", ex);
  108. preSucceed = false;
  109. }
  110. await Task.Delay(10000);
  111. }
  112. }
  113. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  114. {
  115. try
  116. {
  117. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  118. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  119. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  120. string url;
  121. if (posPlatformAddr.EndsWith("/"))
  122. url = posPlatformAddr + "api/SvrReport/Report";
  123. else
  124. url = posPlatformAddr + "/api/SvrReport/Report";
  125. var localIp = IpHelper.GetLocalIp();
  126. _ = HttpHelper.PostRequestAsync<object>(url, new SvrStateReportDto()
  127. {
  128. SvrID = svrID,
  129. SvrType = EnumSvrType.CgDbScan,
  130. ReportType = 1,
  131. BaseHttpAddr = $"http://{localIp}:{port}",
  132. });
  133. }
  134. catch (Exception ex)
  135. {
  136. _ = LogHelper.Error("状态上报异常", ex);
  137. }
  138. }
  139. }
  140. }