using DevExpress.XtraBars; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraBars.Docking2010.Views; using XdCxRhDW.App.UserControl; using DevExpress.XtraBars.Ribbon; using System.Threading; using DevExpress.XtraEditors; using ExtensionsDev; using DevExpress.XtraBars.Forms; using XdCxRhDW.App.CorTools; using System.Data.Entity; using System.IO; using System.Data.Entity.Migrations; using XdCxRhDW.Dto; using XdCxRhDW.Entity; using XdCxRhDW.Repostory; using XdCxRhDW.Api; using System.Net.Http; using XdCxRhDW.App.App.Properties; using System.Windows.Documents; using XdCxRhDW.App; using System.Diagnostics; using DevExpress.Utils.Extensions; using System.Net; using XdCxRhDW.WebApi; using XdCxRhDW.DataEmulation; using DxHelper; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; namespace XdCxRhDW { public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm { Dictionary ctrlTypes = new Dictionary(); string errmsg; public MainForm() { InitializeComponent(); ribbon.UseDefault(); tabbedView1.UseDefault(); ctrlTypes.Add("任务管理", typeof(CtrlHome)); ctrlTypes.Add("参估结果", typeof(CtrlCgRes)); ctrlTypes.Add("星历管理", typeof(CtrlXl)); ctrlTypes.Add("卫星管理", typeof(CtrlSat)); ctrlTypes.Add("天线管理", typeof(CtrlTx)); ctrlTypes.Add("固定站管理", typeof(CtrlFixedStation)); ctrlTypes.Add("信号管理", typeof(CtrlSignal)); ctrlTypes.Add("目标管理", typeof(CtrlTarget)); ctrlTypes.Add("系统设置", typeof(CtrlSysSettings)); ctrlTypes.Add("变采样", typeof(ResampleForm)); ctrlTypes.Add("GPU参估", typeof(GpuCalcForm)); ctrlTypes.Add("检测参估", typeof(DetectToolForm)); ctrlTypes.Add("星历推算", typeof(XlCalculateForm)); ctrlTypes.Add("信号仿真", typeof(SignalEmulation)); ctrlTypes.Add("服务状态", typeof(CtrlSvrs)); ctrlTypes.Add("服务日志", typeof(CtrlSvrLog)); DxHelper.WaitHelper.UpdateSplashMessage("正在检查数据库环境..."); errmsg = CheckDb(); } private string text; private async void MainForm_Load(object sender, EventArgs e) { this.text = this.Text; this.HtmlText = $"{this.text}"; if (!string.IsNullOrWhiteSpace(errmsg)) { MsgBoxHelper.ShowError(errmsg); Application.Exit(); return; } using (RHDWContext db = new RHDWContext()) { SysConfig.Config = db.SysSetings.FirstOrDefault(); } if (SysConfig.Config != null) { StartWebApi(); } btn_ItemClick(null, null); ServerContext.Instance.Init(); if (SysConfig.Config == null) { var size = new Size(500, 600); XtraForm frm = new XtraForm() { StartPosition = FormStartPosition.CenterScreen, CloseBox = false, MinimizeBox = false, MaximizeBox = false }; frm.MinimumSize = frm.MaximumSize = frm.Size = size; new CtrlSysSettings() { Dock = DockStyle.Fill, Parent = frm }; if (frm.ShowDialog() != DialogResult.Cancel) StartWebApi(); else { this.Close(); return; } } this.HtmlText = $"{text}({SysConfig.Config.TimeZoneDisplayName})"; Messenger.Defalut.Sub("时区改变", settings => { this.HtmlText = $"{text}({settings.TimeZoneDisplayName})"; }); StartProcess(); _ = XlScan(); _ = XlClear(); _ = ClearLocalFile(); _ = ClearLog(); _ = MonitorCpuAndMemory(); await XlLonCalc(); } private Task MonitorCpuAndMemory() { return Task.Run(() => { try { int interval = 30000; CpuMonitor sys = new CpuMonitor(); const int GB_DIV = 1024 * 1024 * 1024; while (true) { //第二章方法获取系统CPU和内存使用情况 LogHelper.Info($"设备CPU:{sys.CpuLoad:f1}%,设备内存:{(sys.PhysicalMemory - sys.MemoryAvailable) / GB_DIV:f2}GB/{sys.PhysicalMemory / (double)GB_DIV:f2}GB,平台内存:{sys.ProcessMemory() / (double)GB_DIV:f2}GB,平台线程:{sys.ProcessThread()}").Wait(); Thread.Sleep(interval); } } catch { } }); } private string CheckDb() { using (RHDWContext db = new RHDWContext()) { if (File.Exists(db.DbFile)) { FileInfo fInfo = new FileInfo(db.DbFile); if (fInfo.IsReadOnly) { fInfo.IsReadOnly = false; } } } using (RHDWLogContext db = new RHDWLogContext()) { if (File.Exists(db.DbFile)) { FileInfo fInfo = new FileInfo(db.DbFile); if (fInfo.IsReadOnly) { fInfo.IsReadOnly = false; } } } using (RHDWContext db = new RHDWContext()) { string notExistTableName = db.CheckTableExist(); if (!string.IsNullOrWhiteSpace(notExistTableName)) { db.Dispose(); return $"Database.db数据库中不存在表{notExistTableName}"; } return db.CheckTableField(); } } private void StartWebApi() { try { Startup.Start(SysConfig.Config.HttpPort, $"{Process.GetCurrentProcess().ProcessName}.Xml", "XdCxRhDW.Dto.xml", SysConfig.Config.TimeZoneUTC); } catch (System.Reflection.TargetInvocationException ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); if (ex.InnerException is HttpListenerException) { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败,{ex.InnerException.Message}"); } else { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } catch (Exception ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } //自动导入Tle private async Task XlScan() { while (true) { await Task.Delay(10000); if (SysConfig.Config == null || string.IsNullOrWhiteSpace(SysConfig.Config.XLDirectory) || !Directory.Exists(SysConfig.Config.XLDirectory)) { continue; } DirectoryInfo dir = new DirectoryInfo(SysConfig.Config.XLDirectory); var backUpDir = dir.Parent.FullName; var files = Directory.EnumerateFiles(SysConfig.Config.XLDirectory, "*", SearchOption.AllDirectories); foreach (string file in files) { try { var fileName = await HttpHelper.UploadFileAsync(file, SysConfig.GetBaseUrl()); XlImportDto dto = new XlImportDto() { File = fileName }; var res = await HttpHelper.PostRequestAsync(SysConfig.GetUrl("Xl/ImportTleAsync"), dto); if (res.code == 200) { Serilog.Log.Information($"星历文件[{file}]自动导入成功!"); //导入完成的文件放在备份目录 var baseDirectory = Path.Combine(backUpDir, "TleBackUp"); Directory.CreateDirectory(baseDirectory); var newFile = Path.Combine(baseDirectory, Path.GetFileName(file)); if (File.Exists(newFile)) File.Delete(newFile); File.Move(file, newFile); } else { Serilog.Log.Information($"星历文件[{file}]自动导入失败.{res.msg}"); } } catch (Exception ex) { Serilog.Log.Error(ex, $"星历文件[{file}]自动导入失败"); } } } } //清理180天之前导入的星历 private async Task XlClear() { while (true) { try { using (RHDWContext db = new RHDWContext()) { DateTime dt = DateTime.Now.AddDays(-180); var clearData = await db.XlInfos.Where(p => p.UpdateTime < dt).ToListAsync(); if (clearData.Any()) { db.XlInfos.RemoveRange(clearData); await db.SaveChangesAsync(); } } } catch (Exception ex) { await LogHelper.Error("清理过期星历异常", ex); } await Task.Delay(3600 * 1000); } } //计算星历的轨道经度 private async Task XlLonCalc() { while (true) { try { using (RHDWContext db = new RHDWContext()) { var calcItems = await db.XlInfos.Where(p => p.Lon == null).OrderByDescending(p => p.SatCode).ToListAsync(); if (calcItems.Any()) { var satInfo = await db.SatInfos.ToListAsync(); var firstUpdateItems = calcItems.Where(p => satInfo.Any(t => t.SatCode == p.SatCode)).ToList(); await Task.Run(() => { foreach (var item in firstUpdateItems) { try { var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC); item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1); } catch (Exception ex) { item.Lon = -999; LogHelper.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } db.XlInfos.AddOrUpdate(item); db.SaveChangesAsync(); calcItems.Remove(item); } foreach (var item in calcItems) { try { var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC); item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1); } catch (Exception ex) { item.Lon = -999; LogHelper.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } db.XlInfos.AddOrUpdate(item); db.SaveChangesAsync(); } }); } } } catch (Exception ex) { LogHelper.Error($"推算XYZ星历出错!", ex).Wait(5000); } await Task.Delay(60 * 1000); } } //清理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) { await LogHelper.Error("清理wwwroot历史文件异常", ex); } await Task.Delay(60 * 1000); } } private async Task ClearLog() { using (RHDWLogContext db = new RHDWLogContext()) { try { var time1 = DateTime.Today.AddDays(-7); var time2 = DateTime.Today.AddDays(-30); var delItems = await db.LogRes.Where(p => p.LogTime < time1 && p.LogType != EnumLogType.Error).ToListAsync(); db.LogRes.RemoveRange(delItems); var delItems2 = await db.LogRes.Where(p => p.LogTime < time2 && p.LogType == EnumLogType.Error).ToListAsync(); db.LogRes.RemoveRange(delItems2); await db.SaveChangesAsync(); } catch (Exception ex) { Serilog.Log.Error(ex, "清理日志信息异常!"); } } await Task.Delay(3600 * 1000); } private void btn_ItemClick(object sender, ItemClickEventArgs e) { var btnTxt = e?.Item?.Caption?.Trim() ?? "任务管理"; BaseDocument doc = null; doc = tabbedView1.Documents.Find(p => p.Control.GetType() == ctrlTypes[btnTxt]).FirstOrDefault(); if (doc == null) { doc = tabbedView1.AddDocument((Control)Activator.CreateInstance(ctrlTypes[btnTxt]), btnTxt); if (doc.Control is Form form) { form.Text = ""; doc.Caption = btnTxt; } if (btnTxt == "任务管理") doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.False; else doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.True; } tabbedView1.ActivateDocument(doc.Control); } protected override void OnFormClosing(FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { if (!DxHelper.MsgBoxHelper.ShowConfirm("确定要退出当前系统吗?")) { e.Cancel = true; return; } } KillProcess(); Application.Exit(); } private void btnOpenApi_ItemClick(object sender, ItemClickEventArgs e) { using (RHDWContext db = new RHDWContext()) { if (SysConfig.Config == null) { DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置基础信息"); return; } string addr = $"http://{IpHelper.GetLocalIp()}:{SysConfig.Config.HttpPort}/swagger"; try { System.Diagnostics.Process.Start(addr); } catch { db.Dispose(); DxHelper.MsgBoxHelper.ShowError($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}"); } } } private void StartProcess() { Task.Run(() => { KillProcess(); Process pro = new Process(); var servicesDir = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\"), "*", SearchOption.TopDirectoryOnly); foreach (var item in servicesDir) { try { string exeName = Path.GetFileNameWithoutExtension(item); string exeFile = $"{item}\\{exeName}.exe"; pro.StartInfo.FileName = exeFile; pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}"; pro.Start(); } catch (Exception ex) { Serilog.Log.Error(ex, $"启动【{item}】中的服务异常"); DxHelper.MsgBoxHelper.ShowError($"启动【{item}】中的服务异常"); } } }); } private void KillProcess() { try { List list = new List(); var servicesDir = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\"), "*", SearchOption.TopDirectoryOnly); foreach (var item in servicesDir) { string exeName = Path.GetFileNameWithoutExtension(item); list.AddRange(Process.GetProcessesByName(exeName)); } foreach (var item in list) { try { item.Kill(); } catch { } } } catch { } } } }