123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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.WebApi;
- using XdCxRhDW.UI.Lib;
- namespace X1LeoTaskServer54
- {
- public partial class MainForm : Form
- {
- EnumSvrType svrType = EnumSvrType.LeoX1Task54;
- 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 port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
- 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);
- IpHelper.SetLocalIp(localIp);
- string url;
- if (posPlatformAddr.EndsWith("/"))
- url = posPlatformAddr + "api/SvrReport/Report";
- else
- url = posPlatformAddr + "/api/SvrReport/Report";
- try
- {
- Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
- await XdCxRhDW.UI.Lib.LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
- }
- catch
- {
- try
- {
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrID = svrID,
- SvrType = svrType,
- ReportType = 1,
- BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Port,
- ModuleState = EnumModuleState.Error,
- });
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error("端口状态上报异常", ex);
- }
- }
- await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
- {
- SvrType = svrType,
- SvrID = svrID,
- ReportType = 0,
- BaseHttpAddr = $"http://{localIp}:{port}",
- SwaggerAddr = $"http://{localIp}:{port}/Swagger",
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.正常,
- });
- 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,
- BaseHttpAddr = $"http://{localIp}:{port}",
- SwaggerAddr = $"http://{localIp}:{port}/Swagger",
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.空闲,
- });
- }
- catch (Exception ex)
- {
- await XdCxRhDW.UI.Lib.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,
- BaseHttpAddr = $"http://{localIp}:{port}",
- SwaggerAddr = $"http://{localIp}:{port}/Swagger",
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Memory,
- ModuleState = EnumModuleState.Error,
- });
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error("内存状态上报异常", 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,
- BaseHttpAddr = $"http://{localIp}:{port}",
- SwaggerAddr = $"http://{localIp}:{port}/Swagger",
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Disk,
- ModuleState = EnumModuleState.Error,
- });
- }
- }
- catch (Exception ex)
- {
- XdCxRhDW.Framework.LogHelper.Error("磁盘状态上报异常", ex);
- }
- }
- }
- catch
- { }
- await Task.Delay(6000);
- }
- }
- private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- try
- {
- var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
- 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,
- BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
- DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
- ModuleType = EnumModuleType.Soft,
- ModuleState = EnumModuleState.Error,
- });
- }
- catch (Exception ex)
- {
- _=XdCxRhDW.UI.Lib.LogHelper.Error("服务状态上报异常", ex);
- }
- }
- }
- }
|