MainForm.cs 10 KB

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