using DevExpress.Utils; using DxHelper; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; using XdCxRhDW.Dto; using XdCxRhDW.WebApi; namespace CpuCgServer { public partial class MainForm : DevExpress.XtraEditors.XtraForm { List list = new List(); public string cmdPlatformAddr; public MainForm() { InitializeComponent(); gridLog.UseDefault(list).UseExportCsv().UseClear().SetLogImageColumn(nameof(LogInfo.LogType), typeof(EnumLogType)); gridView1.Columns[nameof(LogInfo.LogType)].MaxWidth = 100; gridView1.Columns[nameof(LogInfo.LogTime)].MaxWidth = 160; gridView1.Columns[nameof(LogInfo.Msg)].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near; this.IconOptions.SvgImage = DxHelper.SvgHelper.LoadFromFile("Service.svg"); LogHelper.Logger = info => { list.Insert(0, info); gridView1.RefreshData(); }; } 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 { } } } var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim()); var svrID = ConfigurationManager.AppSettings["SvrID"].Trim(); string posPlatformAddr; if (!string.IsNullOrWhiteSpace(cmdPlatformAddr)) posPlatformAddr = cmdPlatformAddr; else { posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim(); } this.Text = EnumSvrType.CpuCgSvr.GetEnumDisplayName() + "-" + svrID; var localIp = IpHelper.GetLocalIp(); Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml"); LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger"); string url; if (posPlatformAddr.EndsWith("/")) url = posPlatformAddr + "api/SvrReport/Report"; else url = posPlatformAddr + "/api/SvrReport/Report"; _ = ClearLocalFile(); bool preSucceed = false; while (!this.Disposing) { try { var res = await HttpHelper.PostRequestAsync(url, new SvrStateReportDto() { SvrType = EnumSvrType.CpuCgSvr, SvrID = svrID, ReportType = 0, BaseHttpAddr = $"http://{localIp}:{port}", SwaggerAddr = $"http://{localIp}:{port}/Swagger", }); if (res.code != 200) { LogHelper.Error($"状态上报异常[url={url}].{res.msg}"); preSucceed = false; } else { if (!preSucceed) LogHelper.Info($"状态上报成功![url={url}]"); preSucceed = true; } } catch (Exception ex) { LogHelper.Error($"状态上报异常[url={url}]", ex); preSucceed = false; } await System.Threading.Tasks.Task.Delay(10000); } } 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"; var localIp = IpHelper.GetLocalIp(); _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto() { SvrID = svrID, SvrType = EnumSvrType.CpuCgSvr, ReportType = 1, BaseHttpAddr = $"http://{localIp}:{port}", }); } catch (Exception ex) { LogHelper.Error("状态上报异常", ex); } } //清理10分钟之前的文件 private async Task ClearLocalFile() { while (true) { try { var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot"); var files = Directory.EnumerateFiles(uploadFolder); foreach (var file in files) { FileInfo info = new FileInfo(file); if (info.CreationTime < DateTime.Now.AddMinutes(-10)) { try { info.Delete(); } catch { } } } } catch (Exception ex) { Serilog.Log.Error(ex, "清理wwwroot历史文件异常"); } await Task.Delay(60 * 1000); } } } }