MainForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.UI.Lib;
  13. namespace CheckServer
  14. {
  15. public partial class MainForm : Form
  16. {
  17. EnumSvrType svrType = EnumSvrType.CgDbScan;
  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 svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  60. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  61. LogHelper.BaseUrl = posPlatformAddr + "/api/";
  62. this.Text = svrType.GetEnumDisplayName() + "-" + svrID;
  63. string localIp;
  64. string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
  65. while (true)
  66. {
  67. try
  68. {
  69. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl);
  70. if (rsp.code == 0)
  71. {
  72. await XdCxRhDW.UI.Lib.LogHelper.Error(rsp.msg);
  73. return;
  74. }
  75. else
  76. {
  77. localIp = rsp.data;
  78. }
  79. break;
  80. }
  81. catch (Exception ex)
  82. {
  83. await XdCxRhDW.UI.Lib.LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  84. await Task.Delay(5000);
  85. }
  86. }
  87. await XdCxRhDW.UI.Lib.LogHelper.Info($"本机IP={localIp}");
  88. IpHelper.SetLocalIp(localIp);
  89. string url;
  90. if (posPlatformAddr.EndsWith("/"))
  91. url = posPlatformAddr + "api/SvrReport/Report";
  92. else
  93. url = posPlatformAddr + "/api/SvrReport/Report";
  94. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  95. {
  96. SvrType = svrType,
  97. SvrID = svrID,
  98. ReportType = 0,
  99. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  100. ModuleType = EnumModuleType.Soft,
  101. ModuleState = EnumModuleState.正常,
  102. });
  103. Do();
  104. bool preSucceed = false;
  105. long id = 0;
  106. while (!this.Disposing)
  107. {
  108. try
  109. {
  110. if (!preSucceed)
  111. await XdCxRhDW.UI.Lib.LogHelper.Info($"服务状态上报成功![url={url}]");
  112. preSucceed = true;
  113. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  114. {
  115. SvrType = svrType,
  116. SvrID = svrID,
  117. ID = id++,
  118. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  119. ReportType = 0,
  120. ModuleType = EnumModuleType.Soft,
  121. ModuleState = EnumModuleState.空闲,
  122. });
  123. }
  124. catch (Exception ex)
  125. {
  126. XdCxRhDW.Framework.LogHelper.Error($"服务状态上报异常[url={url}]", ex);
  127. preSucceed = false;
  128. }
  129. var pro = Process.GetCurrentProcess();
  130. if ((pro.PrivateMemorySize64 >> 20) > 4096)//程序内存大于4096MB
  131. {
  132. try
  133. {
  134. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  135. {
  136. SvrType = svrType,
  137. SvrID = svrID,
  138. ReportType = 0,
  139. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  140. ModuleType = EnumModuleType.Memory,
  141. ModuleState = EnumModuleState.Error,
  142. });
  143. }
  144. catch (Exception ex)
  145. {
  146. XdCxRhDW.Framework.LogHelper.Error($"内存状态上报异常[url={url}]", ex);
  147. }
  148. }
  149. try
  150. {
  151. ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
  152. ManagementObjectCollection disks = diskClass.GetInstances();
  153. foreach (ManagementObject disk in disks)
  154. {
  155. try
  156. {
  157. double totalSpace = Convert.ToDouble(disk["Size"]);
  158. if (totalSpace <= 0) continue;
  159. long freqSpace = Convert.ToInt64(disk["FreeSpace"]);
  160. if (freqSpace / totalSpace < 0.1 && id % 10 == 0)//磁盘可用空间不足10%
  161. {
  162. await XdCxRhDW.UI.Lib.LogHelper.Error($"{disk["Name"].ToString().Replace(":", "")}盘可用空间不足10%");
  163. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  164. {
  165. SvrType = svrType,
  166. SvrID = svrID,
  167. ReportType = 0,
  168. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  169. ModuleType = EnumModuleType.Disk,
  170. ModuleState = EnumModuleState.Error,
  171. });
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. XdCxRhDW.Framework.LogHelper.Error($"磁盘状态上报异常[url={url}]", ex);
  177. }
  178. }
  179. }
  180. catch
  181. { }
  182. await Task.Delay(6000);
  183. }
  184. }
  185. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  186. {
  187. try
  188. {
  189. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  190. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  191. string url;
  192. if (posPlatformAddr.EndsWith("/"))
  193. url = posPlatformAddr + "api/SvrReport/Report";
  194. else
  195. url = posPlatformAddr + "/api/SvrReport/Report";
  196. _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  197. {
  198. SvrID = svrID,
  199. SvrType = svrType,
  200. ReportType = 1,
  201. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  202. ModuleType = EnumModuleType.Soft,
  203. ModuleState = EnumModuleState.Error,
  204. });
  205. }
  206. catch (Exception ex)
  207. {
  208. _=XdCxRhDW.UI.Lib.LogHelper.Error("服务状态上报异常", ex);
  209. }
  210. }
  211. private void Do()
  212. {
  213. Task.Run(async () =>
  214. {
  215. while (true)
  216. {
  217. try
  218. {
  219. string baseurl = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim() + "/api/";
  220. var taskRsp = await HttpHelper.PostRequestAsync<List<TaskQueryResDto>>(baseurl + "Result/GetRunningTasks", null);
  221. if (taskRsp.code != 200)
  222. {
  223. await XdCxRhDW.UI.Lib.LogHelper.Error($"查询执行中的任务失败.{taskRsp.msg}");
  224. await Task.Delay(10000);
  225. continue;
  226. }
  227. var taskIds = taskRsp.data.Select(p => p.TaskID).ToList();
  228. if (!taskIds.Any())
  229. {
  230. await Task.Delay(10000);
  231. continue;
  232. }
  233. foreach (var taskId in taskIds)
  234. {
  235. var taskFreqsRsp = await HttpHelper.PostRequestAsync<List<TaskFreqResDto>>(baseurl + "Result/GetTaskFreqs", new TaskFreqQueryDto()
  236. {
  237. TaskInfoID = taskId
  238. });
  239. if (taskFreqsRsp.code != 200)
  240. {
  241. await XdCxRhDW.UI.Lib.LogHelper.Error($"查询任务频点信息失败.{taskFreqsRsp.msg},任务ID={taskId}");
  242. continue;
  243. }
  244. foreach (var item in taskFreqsRsp.data)
  245. {
  246. var cgResRsp = await HttpHelper.PostRequestAsync<List<CgResDto>>(baseurl + "Result/GetNoneDbScanCgRes", new DbsacnCgResQueryDto()
  247. {
  248. TaskInfoID = taskId,
  249. TarFrequpHz = item.FreqUpHz,
  250. });
  251. if (cgResRsp.code != 200)
  252. {
  253. await XdCxRhDW.UI.Lib.LogHelper.Error($"查询未编批的参估结果信息失败.{taskFreqsRsp.msg},任务ID={taskId},上行频点={item.FreqUpHz}");
  254. continue;
  255. }
  256. var cgRes = cgResRsp.data;
  257. var groupByData = cgRes.GroupBy(p => p.SigTime);
  258. //重复时间的数据只取一条
  259. List<CgResDto> listDto = new List<CgResDto>();
  260. foreach (var groupByItem in groupByData)
  261. {
  262. listDto.Add(groupByItem.First());
  263. }
  264. if (listDto.Count < 10) continue;//至少10条记录时开始编批
  265. var firstTime = listDto.First().SigTime;
  266. double t1 = (firstTime - new DateTime(2020, 1, 1, 0, 0, 0)).TotalSeconds;
  267. //将参估结果组装为编批结构
  268. List<Dbscan.Data> list = new List<Dbscan.Data>();
  269. foreach (var cgItem in listDto)
  270. {
  271. Dbscan.Data data = new Dbscan.Data()
  272. {
  273. ID = cgItem.ID,
  274. Point = new Dbscan.Point(t1 + (cgItem.SigTime - firstTime).TotalSeconds, cgItem.Dto1.Value * 100 + cgItem.Dto2.Value)
  275. };
  276. list.Add(data);
  277. }
  278. var clusters = Dbscan.Dbscan.CalculateClusters(
  279. list,
  280. epsilon: 100,//邻域半径(需要根据实际情况调整)
  281. minimumPointsPerCluster: 1);//每个聚合最少个数
  282. }
  283. }
  284. await Task.Delay(5000);
  285. }
  286. catch (Exception ex)
  287. {
  288. await XdCxRhDW.UI.Lib.LogHelper.Error($"参估编批处理异常", ex);
  289. await Task.Delay(30000);
  290. continue;
  291. }
  292. }
  293. });
  294. }
  295. }
  296. }