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(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); } } } }