MainForm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 X1LeoTaskServer54
  15. {
  16. public partial class MainForm : Form
  17. {
  18. EnumSvrType svrType = EnumSvrType.LeoX1Task54;
  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. IpHelper.SetLocalIp(localIp);
  92. string url;
  93. if (posPlatformAddr.EndsWith("/"))
  94. url = posPlatformAddr + "api/SvrReport/Report";
  95. else
  96. url = posPlatformAddr + "/api/SvrReport/Report";
  97. try
  98. {
  99. Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
  100. await XdCxRhDW.UI.Lib.LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
  101. }
  102. catch
  103. {
  104. try
  105. {
  106. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  107. {
  108. SvrID = svrID,
  109. SvrType = svrType,
  110. ReportType = 1,
  111. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  112. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  113. ModuleType = EnumModuleType.Port,
  114. ModuleState = EnumModuleState.Error,
  115. });
  116. }
  117. catch (Exception ex)
  118. {
  119. XdCxRhDW.Framework.LogHelper.Error("端口状态上报异常", ex);
  120. }
  121. }
  122. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  123. {
  124. SvrType = svrType,
  125. SvrID = svrID,
  126. ReportType = 0,
  127. BaseHttpAddr = $"http://{localIp}:{port}",
  128. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  129. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  130. ModuleType = EnumModuleType.Soft,
  131. ModuleState = EnumModuleState.正常,
  132. });
  133. bool preSucceed = false;
  134. long id = 0;
  135. while (!this.Disposing)
  136. {
  137. try
  138. {
  139. if (!preSucceed)
  140. await XdCxRhDW.UI.Lib.LogHelper.Info($"服务状态上报成功![url={url}]");
  141. preSucceed = true;
  142. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  143. {
  144. SvrType = svrType,
  145. SvrID = svrID,
  146. ID = id++,
  147. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  148. ReportType = 0,
  149. BaseHttpAddr = $"http://{localIp}:{port}",
  150. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  151. ModuleType = EnumModuleType.Soft,
  152. ModuleState = EnumModuleState.空闲,
  153. });
  154. }
  155. catch (Exception ex)
  156. {
  157. await XdCxRhDW.UI.Lib.LogHelper.Error($"服务状态上报异常[url={url}]", ex);
  158. preSucceed = false;
  159. }
  160. var pro = Process.GetCurrentProcess();
  161. if ((pro.PrivateMemorySize64 >> 20) > 4096)//程序内存大于4096MB
  162. {
  163. try
  164. {
  165. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  166. {
  167. SvrType = svrType,
  168. SvrID = svrID,
  169. ReportType = 0,
  170. BaseHttpAddr = $"http://{localIp}:{port}",
  171. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  172. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  173. ModuleType = EnumModuleType.Memory,
  174. ModuleState = EnumModuleState.Error,
  175. });
  176. }
  177. catch (Exception ex)
  178. {
  179. XdCxRhDW.Framework.LogHelper.Error("内存状态上报异常", ex);
  180. }
  181. }
  182. try
  183. {
  184. ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
  185. ManagementObjectCollection disks = diskClass.GetInstances();
  186. foreach (ManagementObject disk in disks)
  187. {
  188. try
  189. {
  190. double totalSpace = Convert.ToDouble(disk["Size"]);
  191. if (totalSpace <= 0) continue;
  192. long freqSpace = Convert.ToInt64(disk["FreeSpace"]);
  193. if (freqSpace / totalSpace < 0.1 && id % 10 == 0)//磁盘可用空间不足10%
  194. {
  195. await XdCxRhDW.UI.Lib.LogHelper.Error($"{disk["Name"].ToString().Replace(":", "")}盘可用空间不足10%");
  196. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  197. {
  198. SvrType = svrType,
  199. SvrID = svrID,
  200. ReportType = 0,
  201. BaseHttpAddr = $"http://{localIp}:{port}",
  202. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  203. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  204. ModuleType = EnumModuleType.Disk,
  205. ModuleState = EnumModuleState.Error,
  206. });
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. XdCxRhDW.Framework.LogHelper.Error("磁盘状态上报异常", ex);
  212. }
  213. }
  214. }
  215. catch
  216. { }
  217. await Task.Delay(6000);
  218. }
  219. }
  220. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  221. {
  222. try
  223. {
  224. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  225. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  226. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  227. string url;
  228. if (posPlatformAddr.EndsWith("/"))
  229. url = posPlatformAddr + "api/SvrReport/Report";
  230. else
  231. url = posPlatformAddr + "/api/SvrReport/Report";
  232. _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  233. {
  234. SvrID = svrID,
  235. SvrType = svrType,
  236. ReportType = 1,
  237. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  238. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  239. ModuleType = EnumModuleType.Soft,
  240. ModuleState = EnumModuleState.Error,
  241. });
  242. }
  243. catch (Exception ex)
  244. {
  245. _=XdCxRhDW.UI.Lib.LogHelper.Error("服务状态上报异常", ex);
  246. }
  247. }
  248. }
  249. }