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; 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(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(CtrlSvrs)); using (RHDWContext db = new RHDWContext()) { SysConfig.Config = db.SysSetings.FirstOrDefault(); } if (SysConfig.Config != null) { StartWebApi(); } btn_ItemClick(null, null); ServerContext.Instance.Init(); } private string text; private async void MainForm_Load(object sender, EventArgs e) { this.text = this.Text; this.HtmlText = $"{this.text}"; if (SysConfig.Config == null) { var size = new Size(500, 500); 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(); } this.HtmlText = $"{text}({SysConfig.Config.TimeZoneDisplayName})"; Messenger.Defalut.Sub("时区改变", settings => { this.HtmlText = $"{text}({settings.TimeZoneDisplayName})"; }); StartProcess(); _ = XlScan(); _ = XlClear(); await XlLonCalc(); } private void StartWebApi() { try { Startup.Start(SysConfig.Config.HttpPort, "多模式融合定位平台.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($"{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; } var baseUrl = $"http://{IpHelper.GetLocalIp()}:{SysConfig.Config.HttpPort}/api/"; 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, baseUrl + "File/UploadFileAsync"); XlImportDto dto = new XlImportDto() { File = fileName }; var res = await HttpHelper.PostRequestAsync(baseUrl + "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) { 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).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; Serilog.Log.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!"); } 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; Serilog.Log.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!"); } db.XlInfos.AddOrUpdate(item); db.SaveChangesAsync(); } }); } } } 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; } } 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() { KillProcess(); Process pro = new Process(); pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\CPU参估服务\\CPU参估服务.exe"); pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}"; pro.Start(); pro = new Process(); pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\GPU参估服务\\GPU参估服务.exe"); pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}"; pro.Start(); pro = new Process(); pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\信号检测服务\\信号检测服务.exe"); pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}"; pro.Start(); } private void KillProcess() { List list = new List(); list.AddRange(Process.GetProcessesByName("CPU参估服务")); list.AddRange(Process.GetProcessesByName("GPU参估服务")); list.AddRange(Process.GetProcessesByName("信号检测服务")); foreach (var item in list) { try { item.Kill(); } catch { } } } } }