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; namespace XdCxRhDW { public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm { Dictionary ctrlTypes = new Dictionary(); 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(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(CtrlSvrs)); btn_ItemClick(null, null); } private async void MainForm_Load(object sender, EventArgs e) { _ = XlScan(); _ = XlClear(); await XlLonCalc(); } //自动导入Tle private async Task XlScan() { while (true) { await Task.Delay(10000); SysSetings settings = null; using (RHDWContext db = new RHDWContext()) { settings = await db.SysSetings.FirstOrDefaultAsync(); if (settings == null || settings.XLDirectory == null || !Directory.Exists(settings.XLDirectory)) continue; } if (!Directory.Exists(settings.XLDirectory)) { continue; } var baseUrl = $"http://{IpHelper.GetLocalIp()}:{settings.HttpPort}/api/"; DirectoryInfo dir = new DirectoryInfo(settings.XLDirectory); var backUpDir = dir.Parent.FullName; var files = Directory.EnumerateFiles(settings.XLDirectory, "*", SearchOption.AllDirectories); foreach (string file in files) { try { var fileName = await HttpHelper.UploadFileAsync(file, baseUrl + "File/UploadFileAsync"); XlImportDto dto = new XlImportDto() { File = fileName }; await HttpHelper.PostRequestAsync(baseUrl + "Xl/ImportTleAsync", dto); //导入完成的文件放在备份目录 var baseDirectory = Path.Combine(backUpDir, "TleBackUp"); Directory.CreateDirectory(baseDirectory); File.Move(file, Path.Combine(baseDirectory, Path.GetFileName(file))); } catch (Exception ex) { Serilog.Log.Error(ex, "自动导入星历出错"); } } } } //清理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.TimeBJ < dt).ToListAsync(); if (clearData.Any()) { db.XlInfos.RemoveRange(clearData); await db.SaveChangesAsync(); } } } catch (Exception ex) { Serilog.Log.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).ToArrayAsync(); if (calcItems.Any()) { foreach (var item in calcItems) { await Task.Run(async () => { try { var eph = EphHelper.Calc(item.TwoLine, item.TimeBJ); item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1); } catch (Exception ex) { item.Lon = -999; Serilog.Log.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!"); } try { db.XlInfos.AddOrUpdate(item); await db.SaveChangesAsync(); } catch (Exception ex) { Serilog.Log.Error(ex, "修改星历表卫星经度出错"); } }); } } } } catch (Exception ex) { Serilog.Log.Error(ex, "清理过期星历异常"); } await Task.Delay(60 * 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; } } Application.Exit(); } private async void btnOpenApi_ItemClick(object sender, ItemClickEventArgs e) { using (RHDWContext db = new RHDWContext()) { var settings = await db.SysSetings.FirstOrDefaultAsync(); if (settings == null) { DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置Http端口"); return; } if (settings.HttpPort < 1) { DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置Http端口"); return; } string addr = $"http://{IpHelper.GetLocalIp()}:{settings.HttpPort}/swagger"; try { System.Diagnostics.Process.Start(addr); } catch { db.Dispose(); DxHelper.MsgBoxHelper.ShowError($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}"); } } } } }