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 System.Configuration; using System.Runtime.Remoting.MetadataServices; using System.Security.Policy; using XdCxRhDW.App.Service; 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(CtrlRefCgRes)); ctrlTypes.Add("星历管理", typeof(CtrlXl)); ctrlTypes.Add("卫星管理", typeof(CtrlSat)); ctrlTypes.Add("超短波管理", typeof(CtrlCDBSat)); 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(SignalEmulation)); ctrlTypes.Add("服务状态", typeof(CtrlSvrs)); ctrlTypes.Add("服务日志", typeof(CtrlSvrLog)); using (RHDWContext db = new RHDWContext()) { SysConfig.Config = db.SysSetings.FirstOrDefault(); } if (SysConfig.Config != null) { StartWebApi(); } btn_ItemClick(null, null); ServerContext.Instance.Init(); DxHelper.WaitHelper.UpdateSplashMessage("正在检查数据库环境..."); CheckDb(); } 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, 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(); _ = Rpt(); await XlLonCalc(); } private void StartWebApi() { bool succeed = false; try { Startup.Start(SysConfig.Config.HttpPort, $"{Process.GetCurrentProcess().ProcessName}.Xml", "XdCxRhDW.Dto.xml", SysConfig.Config.TimeZoneUTC); succeed = true; } catch (System.Reflection.TargetInvocationException ex) { LogHelper.Error($"启动Http服务失败!", ex).Wait(5000); if (ex.InnerException is HttpListenerException) { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败,{ex.InnerException.Message}"); } else { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } catch (Exception ex) { LogHelper.Error($"启动Http服务失败!", ex).Wait(5000); DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } if (!succeed) { try { var dto = new SvrStateReportDto() { DevId = ConfigurationManager.AppSettings["DevID"].Trim(), SvrType = EnumSvrType.PosPlatform, ModuleType = EnumModuleType.Port, ModuleState = EnumModuleState.Error, }; AutofacUtil.GetService()?.WriteStateRes(dto); } catch (Exception ex) { LogHelper.Error("端口状态上报异常", ex).Wait(5000); } } } //自动导入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) { XdCxRhDW.Framework.LogHelper.Info($"星历文件[{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 { XdCxRhDW.Framework.LogHelper.Info($"星历文件[{file}]自动导入失败.{res.msg}"); } } catch (Exception ex) { XdCxRhDW.Framework.LogHelper.Error($"星历文件[{file}]自动导入失败", ex); await Task.Delay(10000); } finally { await Task.Delay(5000); } } } } //清理1年之前导入的星历 private async Task XlClear() { while (true) { try { List clearData = new List(); using (RHDWContext db = new RHDWContext()) { DateTime dt = DateTime.Now.AddYears(-1); clearData = await db.XlInfos.Where(p => p.UpdateTime < dt).Take(1000).ToListAsync(); } if (!clearData.Any()) { await Task.Delay(60 * 1000); } else { using (RHDWContext db = new RHDWContext()) { db.XlInfos.RemoveRange(clearData); await db.SaveChangesAsync(); } } } catch (Exception ex) { await XdCxRhDW.UI.Lib.LogHelper.Error("清理过期星历异常", ex); } finally { await Task.Delay(2000); } } } //计算星历的轨道经度 private async Task XlLonCalc() { while (true) { try { List calcItems = null; List satInfo = null; using (RHDWContext db = new RHDWContext()) { calcItems = await db.XlInfos.Where(p => p.Lon == null).OrderByDescending(p => p.SatCode).Take(100).ToListAsync(); satInfo = await db.SatInfos.ToListAsync(); } if (calcItems != null && calcItems.Any()) { var firstUpdateItems = calcItems.Where(p => satInfo.Any(t => t.SatCode == p.SatCode)).ToList(); await Task.Run(() => { List Level1 = new List(); 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); Level1.Add(item); calcItems.Remove(item); } catch (Exception ex) { item.Lon = -999; LogHelper.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } } if (Level1.Any()) { using (RHDWContext db = new RHDWContext()) { db.XlInfos.AddOrUpdate(Level1.ToArray()); db.SaveChanges(); } } List Level2 = new List(); 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); Level2.Add(item); } catch (Exception ex) { item.Lon = -999; LogHelper.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } } if (Level2.Any()) { using (RHDWContext db = new RHDWContext()) { db.XlInfos.AddOrUpdate(Level2.ToArray()); db.SaveChanges(); } } }); } else { await Task.Delay(60 * 1000); } } catch (Exception ex) { LogHelper.Error($"推算XYZ星历出错!", ex).Wait(5000); } finally { await Task.Delay(5000); } } } private async Task Rpt() { var dto = new SvrStateReportDto() { DevId = ConfigurationManager.AppSettings["DevID"].Trim(), SvrType = EnumSvrType.PosPlatform, ModuleType = EnumModuleType.Soft, ModuleState = EnumModuleState.正常, }; AutofacUtil.GetService()?.WriteStateRes(dto); await Task.Run(async () => { while (true) { try { var pro = Process.GetCurrentProcess(); if ((pro.PrivateMemorySize64 >> 20) > 8192)//程序内存大于8个GB { var dto2 = new SvrStateReportDto() { DevId = ConfigurationManager.AppSettings["DevID"].Trim(), SvrType = EnumSvrType.PosPlatform, ModuleType = EnumModuleType.Memory, ModuleState = EnumModuleState.Error, }; AutofacUtil.GetService()?.WriteStateRes(dto2); } } catch (Exception ex) { LogHelper.Error("端口状态上报异常", ex).Wait(5000); } await Task.Delay(10000); } }); } //清理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 (Exception ex) { await XdCxRhDW.UI.Lib.LogHelper.Error("清理wwwroot历史文件异常", ex); } } } } catch (Exception ex) { await XdCxRhDW.UI.Lib.LogHelper.Error("清理wwwroot历史文件异常", ex); } await Task.Delay(60 * 1000); } } private async Task ClearLog() { while (true) { 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).Take(500).ToListAsync(); db.LogRes.RemoveRange(delItems); var delItems2 = await db.LogRes.Where(p => p.LogTime < time2 && p.LogType == EnumLogType.Error).Take(500).ToListAsync(); db.LogRes.RemoveRange(delItems2); await db.SaveChangesAsync(); if (!delItems.Any() && !delItems2.Any()) { break; } } catch (Exception ex) { await XdCxRhDW.UI.Lib.LogHelper.Error("清理日志信息异常", ex); await Task.Delay(10000); } finally { await Task.Delay(5000); } } } } 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; } } AutofacUtil.GetService()?.WriteStateRes(new SvrStateReportDto() { SvrType = EnumSvrType.PosPlatform, DevId = ConfigurationManager.AppSettings["DevID"].Trim(), ModuleType = EnumModuleType.Soft, ModuleState = EnumModuleState.Error, }); 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(); string serviceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\"); Directory.CreateDirectory(serviceDir); var servicesDir = Directory.GetDirectories(serviceDir, "*", 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) { XdCxRhDW.Framework.LogHelper.Error($"启动【{item}】中的服务异常", ex); 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 { } } private void CheckDb() { try { 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()) { db.SyncDb(); } using (RHDWLogContext db = new RHDWLogContext()) { db.SyncDb(); } } catch (Exception ex) { LogHelper.Error("同步数据库结构异常", ex).Wait(5000); } } } }