123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Management;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using XdCxRhDW.Dto;
- using XdCxRhDW.UI.Lib;
- namespace CheckServer
- {
- public partial class MainForm : Form
- {
- EnumSvrType svrType = EnumSvrType.CgDbScan;
- public MainForm()
- {
- InitializeComponent();
- LogHelper.Logger = info =>
- {
- try
- {
- this.Invoke(new Action(() =>
- {
- this.listBox1.Items.Insert(0, $"{info.LogTime:yyyyMMddHHmmss.fff}--{info.Msg}");
- if (this.listBox1.Items.Count > 1000)
- this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
- }));
- }
- catch
- { }
- };
- }
- private async void MainForm_LoadAsync(object sender, EventArgs e)
- {
- if (Debugger.IsAttached)//结束已启动的进程,方便调试
- {
- var pros = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).OrderBy(p => p.StartTime).ToList();
- pros.RemoveAt(pros.Count - 1);
- foreach (var item in pros)
- {
- try
- {
- item.Kill();
- }
- catch
- { }
- }
- }
- try
- {
- if (Directory.Exists("tmp"))
- Directory.Delete("tmp");
- }
- catch
- { }
- var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
- var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
- LogHelper.BaseUrl = posPlatformAddr + "/api/";
- this.Text = svrType.GetEnumDisplayName() + "-" + svrID;
- string localIp;
- string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
- while (true)
- {
- try
- {
- var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl);
- if (rsp.code == 0)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error(rsp.msg);
- return;
- }
- else
- {
- localIp = rsp.data;
- }
- break;
- }
- catch (Exception ex)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
- await Task.Delay(5000);
- }
- }
- await XdCxRhDW.UI.Lib.LogHelper.Info($"本机IP={localIp}");
- IpHelper.SetLocalIp(localIp);
- string url;
- if (posPlatformAddr.EndsWith("/"))
- url = posPlatformAddr + "api/SvrReport/Report";
- else
- url = posPlatformAddr + "/api/SvrReport/Report";
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrType = svrType,
- SvrID = svrID,
- ReportType = 0,
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.正常,
- });
- Do();
- bool preSucceed = false;
- long id = 0;
- while (!this.Disposing)
- {
- try
- {
- if (!preSucceed)
- await XdCxRhDW.UI.Lib.LogHelper.Info($"服务状态上报成功![url={url}]");
- preSucceed = true;
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrType = svrType,
- SvrID = svrID,
- ID = id++,
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ReportType = 0,
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.空闲,
- });
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error($"服务状态上报异常[url={url}]", ex);
- preSucceed = false;
- }
- var pro = Process.GetCurrentProcess();
- if ((pro.PrivateMemorySize64 >> 20) > 4096)//程序内存大于4096MB
- {
- try
- {
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrType = svrType,
- SvrID = svrID,
- ReportType = 0,
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Memory,
- ModuleState = EnumModuleState.Error,
- });
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error($"内存状态上报异常[url={url}]", ex);
- }
- }
- try
- {
- ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
- ManagementObjectCollection disks = diskClass.GetInstances();
- foreach (ManagementObject disk in disks)
- {
- try
- {
- double totalSpace = Convert.ToDouble(disk["Size"]);
- if (totalSpace <= 0) continue;
- long freqSpace = Convert.ToInt64(disk["FreeSpace"]);
- if (freqSpace / totalSpace < 0.1 && id % 10 == 0)//磁盘可用空间不足10%
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"{disk["Name"].ToString().Replace(":", "")}盘可用空间不足10%");
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrType = svrType,
- SvrID = svrID,
- ReportType = 0,
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Disk,
- ModuleState = EnumModuleState.Error,
- });
- }
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error($"磁盘状态上报异常[url={url}]", ex);
- }
- }
- }
- catch
- { }
- await Task.Delay(6000);
- }
- }
- private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- try
- {
- var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
- var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
- string url;
- if (posPlatformAddr.EndsWith("/"))
- url = posPlatformAddr + "api/SvrReport/Report";
- else
- url = posPlatformAddr + "/api/SvrReport/Report";
- _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrID = svrID,
- SvrType = svrType,
- ReportType = 1,
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.Error,
- });
- }
- catch (Exception ex)
- {
- _=XdCxRhDW.UI.Lib.LogHelper.Error("服务状态上报异常", ex);
- }
- }
- private void Do()
- {
- Task.Run(async () =>
- {
- while (true)
- {
- try
- {
- string baseurl = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim() + "/api/";
- var taskRsp = await HttpHelper.PostRequestAsync<List<TaskQueryResDto>>(baseurl + "Result/GetRunningTasks", null);
- if (taskRsp.code != 200)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"查询执行中的任务失败.{taskRsp.msg}");
- await Task.Delay(10000);
- continue;
- }
- var taskIds = taskRsp.data.Select(p => p.TaskID).ToList();
- if (!taskIds.Any())
- {
- await Task.Delay(10000);
- continue;
- }
- foreach (var taskId in taskIds)
- {
- var taskFreqsRsp = await HttpHelper.PostRequestAsync<List<TaskFreqResDto>>(baseurl + "Result/GetTaskFreqs", new TaskFreqQueryDto()
- {
- TaskInfoID = taskId
- });
- if (taskFreqsRsp.code != 200)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"查询任务频点信息失败.{taskFreqsRsp.msg},任务ID={taskId}");
- continue;
- }
- foreach (var item in taskFreqsRsp.data)
- {
- var cgResRsp = await HttpHelper.PostRequestAsync<List<CgResDto>>(baseurl + "Result/GetNoneDbScanCgRes", new DbsacnCgResQueryDto()
- {
- TaskInfoID = taskId,
- TarFrequpHz = item.FreqUpHz,
- });
- if (cgResRsp.code != 200)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"查询未编批的参估结果信息失败.{taskFreqsRsp.msg},任务ID={taskId},上行频点={item.FreqUpHz}");
- continue;
- }
- var cgRes = cgResRsp.data;
- var groupByData = cgRes.GroupBy(p => p.SigTime);
- //重复时间的数据只取一条
- List<CgResDto> listDto = new List<CgResDto>();
- foreach (var groupByItem in groupByData)
- {
- listDto.Add(groupByItem.First());
- }
- if (listDto.Count < 10) continue;//至少10条记录时开始编批
- var firstTime = listDto.First().SigTime;
- double t1 = (firstTime - new DateTime(2020, 1, 1, 0, 0, 0)).TotalSeconds;
- //将参估结果组装为编批结构
- List<Dbscan.Data> list = new List<Dbscan.Data>();
- foreach (var cgItem in listDto)
- {
- Dbscan.Data data = new Dbscan.Data()
- {
- ID = cgItem.ID,
- Point = new Dbscan.Point(t1 + (cgItem.SigTime - firstTime).TotalSeconds, cgItem.Dto1.Value * 100 + cgItem.Dto2.Value)
- };
- list.Add(data);
- }
- var clusters = Dbscan.Dbscan.CalculateClusters(
- list,
- epsilon: 100,//邻域半径(需要根据实际情况调整)
- minimumPointsPerCluster: 1);//每个聚合最少个数
- }
- }
- await Task.Delay(5000);
- }
- catch (Exception ex)
- {
- await XdCxRhDW.UI.Lib.LogHelper.Error($"参估编批处理异常", ex);
- await Task.Delay(30000);
- continue;
- }
- }
- });
- }
- }
- }
|