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; using DevExpress.LookAndFeel.Design; using System.Configuration; using XdCxRhDW.Framework; using XdCxRhDW.UI.Lib; using DevExpress.XtraBars.Docking2010.Views.Tabbed; using MySql.Data.EntityFramework; using MySql.Data.MySqlClient; 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(CtrlDraw)); 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("正在检查数据库环境..."); CheckDb(); } private string text; private async void MainForm_Load(object sender, EventArgs e) { this.text = this.Text; this.HtmlText = $"{this.text}"; this.ribbonPageGroup1.Enabled = false; this.ribbonPageGroup2.Enabled = false; btnXlCalculate.Enabled = false; txtSvrLog.Enabled = false; string con = AppConfigHelper.GetConnectionString("MySql"); string conWithTimeout = con; if (!con.Contains("ConnectionTimeout")) { conWithTimeout = $"{con}ConnectionTimeout=5;"; } tabbedView1.DocumentActivated += (s, arg) => this.tablePanel1.Visible = false; tabbedView1.DocumentRemoved += (s, arg) => this.tablePanel1.Visible = tabbedView1.ActiveDocument == null; bool dbEnable = false; using (MySqlConnection myCon = new MySqlConnection(conWithTimeout)) { txtMsg.Text = $"正在连接MySql..."; await Task.Run(() => { try { myCon.Open(); dbEnable = true; } catch { } }); } if (!dbEnable) { txtMsg.Text = $"无法连接到MySql=>{con}"; return; } //using (MySqlSync ctx = new MySqlSync()) //{ // ctx.SyncDb(); //} using (MySqlContext ctx = new MySqlContext()) { SysConfig.Config = await ctx.SysSetings.FirstOrDefaultAsync(); } this.ribbonPageGroup1.Enabled = true; this.ribbonPageGroup2.Enabled = true; btnXlCalculate.Enabled = true; txtSvrLog.Enabled = true; 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() { if (!AppConfigHelper.Get("UseGatherDevState", true)) return Task.CompletedTask; return Task.Run(() => { try { int interval = 30000; CpuMonitor sys = new CpuMonitor(); const int GB_DIV = 1024 * 1024 * 1024; while (true) { //第二章方法获取系统CPU和内存使用情况 LogUI.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 void CheckDb() { try { //using (MySqlContext db = new MySqlContext()) //{ // db.SyncDb(); //} } catch (Exception ex) { LogUI.Error("同步数据库结构异常", ex).Wait(5000); } } private void StartWebApi() { try { Startup.Start(SysConfig.Config.HttpPort, $"{Process.GetCurrentProcess().ProcessName}.Xml", "XdCxRhDW.Dto.xml", SysConfig.Config.TimeZoneUTC); } catch (System.Reflection.TargetInvocationException ex) { XdCxRhDW.Framework.LogHelper.Error($"启动Http服务失败!", ex); if (ex.InnerException is HttpListenerException) { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败,{ex.InnerException.Message}"); } else { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } catch (Exception ex) { XdCxRhDW.Framework.LogHelper.Error($"启动Http服务失败!", ex); 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 backupSucceed = Path.Combine(dir.FullName, "Backup", "Succeed"); var backupFailed = Path.Combine(dir.FullName, "Backup", "Failed"); Directory.CreateDirectory(backupSucceed); Directory.CreateDirectory(backupFailed); var files = Directory.EnumerateFiles(SysConfig.Config.XLDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string file in files) { bool succeed = false; 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) { LogUI.Info($"星历文件[{file}]自动导入成功!").Wait(5000); try { //导入完成的文件放在备份目录 var newFile = Path.Combine(backupSucceed, Path.GetFileName(file)); if (File.Exists(newFile)) File.Delete(newFile); File.Move(file, newFile); } catch (Exception ex) { await App.LogHelper.Error($"{file}导入完成后移动到备份目录失败",ex); } } else { LogUI.Error($"星历文件[{file}]自动导入失败.{res.msg}").Wait(5000); try { //导入完成的文件放在备份目录 var newFile = Path.Combine(backupFailed, Path.GetFileName(file)); if (File.Exists(newFile)) File.Delete(newFile); File.Move(file, newFile); } catch(Exception ex) { await App.LogHelper.Error($"{file}导入失败后移动到备份目录失败",ex); } } } catch (Exception ex) { LogUI.Error($"星历文件[{file}]自动导入失败", ex).Wait(5000); try { //导入完成的文件放在备份目录 var newFile = Path.Combine(backupFailed, Path.GetFileName(file)); if (File.Exists(newFile)) File.Delete(newFile); File.Move(file, newFile); } catch(Exception ex2) { await App.LogHelper.Error($"{file}导入失败后移动到备份目录失败", ex2); } await Task.Delay(5000); } finally { await Task.Delay(2000); } } } } //清理1年之前导入的星历 private async Task XlClear() { while (true) { try { List clearData = new List(); using (MySqlContext db = new MySqlContext()) { DateTime dt = DateTime.Now.AddYears(-1); clearData = await db.XlInfos.Where(p => p.UpdateTime < dt).Take(1000).ToListAsync(); if (clearData.Any()) { db.XlInfos.RemoveRange(clearData); await db.SaveChangesAsync(); } } if (!clearData.Any()) { await Task.Delay(60 * 1000); } } catch (Exception ex) { await LogUI.Error("清理过期星历异常", ex); } finally { await Task.Delay(2000); } } } //计算星历的轨道经度 private async Task XlLonCalc() { while (true) { try { List calcItems = null; List satInfo = null; using (MySqlContext db = new MySqlContext()) { 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; LogUI.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } } if (Level1.Any()) { using (MySqlContext db = new MySqlContext()) { 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; LogUI.Error($"[{item.TwoLine}]推算XYZ星历出错!", ex).Wait(5000); } } if (Level2.Any()) { using (MySqlContext db = new MySqlContext()) { db.XlInfos.AddOrUpdate(Level2.ToArray()); db.SaveChanges(); } } }); await Task.Delay(2000); } else { await Task.Delay(60 * 1000); } } catch (Exception ex) { LogUI.Error($"推算XYZ星历出错!", ex).Wait(5000); } finally { await Task.Delay(5000); } } } //清理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 LogUI.Error("清理wwwroot历史文件异常", ex); } await Task.Delay(60 * 1000); } } private async Task ClearLog() { while (true) { using (MySqlContext db = new MySqlContext()) { 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 != Entity.EnumLogType.Error).Take(500).ToListAsync(); db.LogRes.RemoveRange(delItems); var delItems2 = await db.LogRes.Where(p => p.LogTime < time2 && p.LogType == Entity.EnumLogType.Error).Take(500).ToListAsync(); db.LogRes.RemoveRange(delItems2); await db.SaveChangesAsync(); if (!delItems.Any() && !delItems2.Any()) { break; } } catch (Exception ex) { await LogUI.Error("清理日志信息异常", ex); await Task.Delay(10000); } finally { await Task.Delay(5000); } } } } private void btn_ItemClick(object sender, ItemClickEventArgs e) { this.tablePanel1.Visible = false; 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) { if (SysConfig.Config == null) { DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置基础信息"); return; } string addr = $"http://{IpHelper.GetLocalIp()}:{SysConfig.Config.HttpPort}/swagger"; try { System.Diagnostics.Process.Start(addr); } catch { 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) { 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 { } } } }