MainForm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Management;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using XdCxRhDW.Dto;
  12. using XdCxRhDW.WebApi;
  13. namespace X3TaskServer54
  14. {
  15. public partial class MainForm : Form
  16. {
  17. EnumSvrType svrType = EnumSvrType.X3NoRefTask54;
  18. public MainForm()
  19. {
  20. InitializeComponent();
  21. LogHelper.Logger = info =>
  22. {
  23. try
  24. {
  25. this.Invoke(new Action(() =>
  26. {
  27. this.listBox1.Items.Insert(0, $"{info.LogTime:yyyyMMddHHmmss.fff}--{info.Msg}");
  28. if (this.listBox1.Items.Count > 1000)
  29. this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
  30. }));
  31. }
  32. catch
  33. { }
  34. };
  35. }
  36. private async void MainForm_LoadAsync(object sender, EventArgs e)
  37. {
  38. if (Debugger.IsAttached)//结束已启动的进程,方便调试
  39. {
  40. var pros = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).OrderBy(p => p.StartTime).ToList();
  41. pros.RemoveAt(pros.Count - 1);
  42. foreach (var item in pros)
  43. {
  44. try
  45. {
  46. item.Kill();
  47. }
  48. catch
  49. { }
  50. }
  51. }
  52. try
  53. {
  54. if (Directory.Exists("tmp"))
  55. Directory.Delete("tmp");
  56. }
  57. catch
  58. { }
  59. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  60. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  61. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  62. LogHelper.BaseUrl = posPlatformAddr + "/api/";
  63. this.Text = svrType.GetEnumDisplayName() + "-" + svrID;
  64. string localIp;
  65. string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
  66. while (true)
  67. {
  68. try
  69. {
  70. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl, 10);
  71. if (rsp.code == 0)
  72. {
  73. await LogHelper.Error(rsp.msg);
  74. return;
  75. }
  76. else
  77. {
  78. localIp = rsp.data;
  79. }
  80. break;
  81. }
  82. catch (Exception ex)
  83. {
  84. await LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  85. await Task.Delay(5000);
  86. }
  87. }
  88. await LogHelper.Info($"本机IP={localIp}");
  89. IpHelper.SetLocalIp(localIp);
  90. string url;
  91. if (posPlatformAddr.EndsWith("/"))
  92. url = posPlatformAddr + "api/SvrReport/Report";
  93. else
  94. url = posPlatformAddr + "/api/SvrReport/Report";
  95. try
  96. {
  97. Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
  98. await LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
  99. }
  100. catch
  101. {
  102. try
  103. {
  104. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  105. {
  106. SvrID = svrID,
  107. SvrType = svrType,
  108. ReportType = 1,
  109. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  110. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  111. ModuleType = EnumModuleType.Port,
  112. ModuleState = EnumModuleState.Error,
  113. });
  114. }
  115. catch (Exception ex)
  116. {
  117. Serilog.Log.Error(ex, "端口状态上报异常");
  118. }
  119. }
  120. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  121. {
  122. SvrType = svrType,
  123. SvrID = svrID,
  124. ReportType = 0,
  125. BaseHttpAddr = $"http://{localIp}:{port}",
  126. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  127. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  128. ModuleType = EnumModuleType.Soft,
  129. ModuleState = EnumModuleState.正常,
  130. });
  131. bool preSucceed = false;
  132. long id = 0;
  133. while (!this.Disposing)
  134. {
  135. try
  136. {
  137. if (!preSucceed)
  138. await LogHelper.Info($"服务状态上报成功![url={url}]");
  139. preSucceed = true;
  140. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  141. {
  142. SvrType = svrType,
  143. SvrID = svrID,
  144. ID = id++,
  145. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  146. ReportType = 0,
  147. BaseHttpAddr = $"http://{localIp}:{port}",
  148. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  149. ModuleType = EnumModuleType.Soft,
  150. ModuleState = EnumModuleState.空闲,
  151. });
  152. }
  153. catch (Exception ex)
  154. {
  155. await LogHelper.Error($"服务状态上报异常[url={url}]", ex);
  156. preSucceed = false;
  157. }
  158. var pro = Process.GetCurrentProcess();
  159. if ((pro.PrivateMemorySize64 >> 20) > 4096)//程序内存大于4096MB
  160. {
  161. try
  162. {
  163. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  164. {
  165. SvrType = svrType,
  166. SvrID = svrID,
  167. ReportType = 0,
  168. BaseHttpAddr = $"http://{localIp}:{port}",
  169. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  170. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  171. ModuleType = EnumModuleType.Memory,
  172. ModuleState = EnumModuleState.Error,
  173. });
  174. }
  175. catch (Exception ex)
  176. {
  177. Serilog.Log.Error(ex, "内存状态上报异常");
  178. }
  179. }
  180. try
  181. {
  182. ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
  183. ManagementObjectCollection disks = diskClass.GetInstances();
  184. foreach (ManagementObject disk in disks)
  185. {
  186. try
  187. {
  188. double totalSpace = Convert.ToDouble(disk["Size"]);
  189. if (totalSpace <= 0) continue;
  190. long freqSpace = Convert.ToInt64(disk["FreeSpace"]);
  191. if (freqSpace / totalSpace < 0.1 && id % 10 == 0)//磁盘可用空间不足10%
  192. {
  193. await LogHelper.Error($"{disk["Name"].ToString().Replace(":", "")}盘可用空间不足10%");
  194. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  195. {
  196. SvrType = svrType,
  197. SvrID = svrID,
  198. ReportType = 0,
  199. BaseHttpAddr = $"http://{localIp}:{port}",
  200. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  201. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  202. ModuleType = EnumModuleType.Disk,
  203. ModuleState = EnumModuleState.Error,
  204. });
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. Serilog.Log.Error(ex, "磁盘状态上报异常");
  210. }
  211. }
  212. }
  213. catch
  214. { }
  215. await Task.Delay(6000);
  216. }
  217. }
  218. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  219. {
  220. try
  221. {
  222. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  223. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  224. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  225. string url;
  226. if (posPlatformAddr.EndsWith("/"))
  227. url = posPlatformAddr + "api/SvrReport/Report";
  228. else
  229. url = posPlatformAddr + "/api/SvrReport/Report";
  230. _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  231. {
  232. SvrID = svrID,
  233. SvrType = svrType,
  234. ReportType = 1,
  235. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  236. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  237. ModuleType = EnumModuleType.Soft,
  238. ModuleState = EnumModuleState.Error,
  239. });
  240. }
  241. catch (Exception ex)
  242. {
  243. _ = LogHelper.Error("服务状态上报异常", ex);
  244. }
  245. }
  246. }
  247. }