MainForm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using DevExpress.XtraBars;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using DevExpress.XtraBars.Docking2010.Views;
  12. using XdCxRhDW.App.UserControl;
  13. using DevExpress.XtraBars.Ribbon;
  14. using System.Threading;
  15. using DevExpress.XtraEditors;
  16. using ExtensionsDev;
  17. using DevExpress.XtraBars.Forms;
  18. using XdCxRhDW.App.CorTools;
  19. using System.Data.Entity;
  20. using System.IO;
  21. using System.Data.Entity.Migrations;
  22. using XdCxRhDW.Dto;
  23. using XdCxRhDW.Entity;
  24. using XdCxRhDW.Repostory;
  25. using XdCxRhDW.Api;
  26. using System.Net.Http;
  27. using XdCxRhDW.App.App.Properties;
  28. using System.Windows.Documents;
  29. using XdCxRhDW.App;
  30. using System.Diagnostics;
  31. using DevExpress.Utils.Extensions;
  32. using System.Net;
  33. using XdCxRhDW.WebApi;
  34. namespace XdCxRhDW
  35. {
  36. public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
  37. {
  38. Dictionary<string, Type> ctrlTypes = new Dictionary<string, Type>();
  39. public MainForm()
  40. {
  41. InitializeComponent();
  42. ribbon.UseDefault();
  43. tabbedView1.UseDefault();
  44. ctrlTypes.Add("任务管理", typeof(CtrlHome));
  45. ctrlTypes.Add("参估结果", typeof(CtrlCgRes));
  46. ctrlTypes.Add("星历管理", typeof(CtrlXl));
  47. ctrlTypes.Add("卫星管理", typeof(CtrlSat));
  48. ctrlTypes.Add("天线管理", typeof(CtrlTx));
  49. ctrlTypes.Add("信号管理", typeof(CtrlSignal));
  50. ctrlTypes.Add("目标管理", typeof(CtrlTarget));
  51. ctrlTypes.Add("系统设置", typeof(CtrlSysSettings));
  52. ctrlTypes.Add("变采样", typeof(ResampleForm));
  53. ctrlTypes.Add("GPU参估", typeof(GpuCalcForm));
  54. ctrlTypes.Add("检测参估", typeof(DetectToolForm));
  55. ctrlTypes.Add("星历推算", typeof(XlCalculateForm));
  56. ctrlTypes.Add("服务状态", typeof(CtrlSvrs));
  57. using (RHDWContext db = new RHDWContext())
  58. {
  59. SysConfig.Config = db.SysSetings.FirstOrDefault();
  60. }
  61. if (SysConfig.Config != null)
  62. {
  63. StartWebApi();
  64. }
  65. btn_ItemClick(null, null);
  66. ServerContext.Instance.Init();
  67. }
  68. private string text;
  69. private async void MainForm_Load(object sender, EventArgs e)
  70. {
  71. this.text = this.Text;
  72. this.HtmlText = $"<size=12>{this.text}";
  73. if (SysConfig.Config == null)
  74. {
  75. var size = new Size(500, 600);
  76. XtraForm frm = new XtraForm() { StartPosition = FormStartPosition.CenterScreen, CloseBox = false, MinimizeBox = false, MaximizeBox = false };
  77. frm.MinimumSize = frm.MaximumSize = frm.Size = size;
  78. new CtrlSysSettings() { Dock = DockStyle.Fill, Parent = frm };
  79. if (frm.ShowDialog() != DialogResult.Cancel)
  80. StartWebApi();
  81. }
  82. this.HtmlText = $"<size=12>{text}(<size=9>{SysConfig.Config.TimeZoneDisplayName}</size>)";
  83. Messenger.Defalut.Sub<SysSetings>("时区改变", settings =>
  84. {
  85. this.HtmlText = $"<size=12>{text}(<size=9>{settings.TimeZoneDisplayName}</size>)";
  86. });
  87. StartProcess();
  88. _ = XlScan();
  89. _ = XlClear();
  90. _ = ClearLocalFile();
  91. await XlLonCalc();
  92. }
  93. private void StartWebApi()
  94. {
  95. try
  96. {
  97. Startup.Start(SysConfig.Config.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml", SysConfig.Config.TimeZoneUTC);
  98. }
  99. catch (System.Reflection.TargetInvocationException ex)
  100. {
  101. Serilog.Log.Error(ex, $"启动Http服务失败!");
  102. if (ex.InnerException is HttpListenerException)
  103. {
  104. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败,{ex.InnerException.Message}");
  105. }
  106. else
  107. {
  108. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. Serilog.Log.Error(ex, $"启动Http服务失败!");
  114. DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!");
  115. }
  116. }
  117. //自动导入Tle
  118. private async Task XlScan()
  119. {
  120. while (true)
  121. {
  122. await Task.Delay(10000);
  123. if (SysConfig.Config == null
  124. || string.IsNullOrWhiteSpace(SysConfig.Config.XLDirectory)
  125. || !Directory.Exists(SysConfig.Config.XLDirectory))
  126. {
  127. continue;
  128. }
  129. DirectoryInfo dir = new DirectoryInfo(SysConfig.Config.XLDirectory);
  130. var backUpDir = dir.Parent.FullName;
  131. var files = Directory.EnumerateFiles(SysConfig.Config.XLDirectory, "*", SearchOption.AllDirectories);
  132. foreach (string file in files)
  133. {
  134. try
  135. {
  136. var fileName = await HttpHelper.UploadFileAsync(file, SysConfig.GetBaseUrl());
  137. XlImportDto dto = new XlImportDto() { File = fileName };
  138. var res = await HttpHelper.PostRequestAsync<RecordRes>(SysConfig.GetUrl("Xl/ImportTleAsync"), dto);
  139. if (res.code == 200)
  140. {
  141. Serilog.Log.Information($"星历文件[{file}]自动导入成功!");
  142. //导入完成的文件放在备份目录
  143. var baseDirectory = Path.Combine(backUpDir, "TleBackUp");
  144. Directory.CreateDirectory(baseDirectory);
  145. var newFile = Path.Combine(baseDirectory, Path.GetFileName(file));
  146. if (File.Exists(newFile))
  147. File.Delete(newFile);
  148. File.Move(file, newFile);
  149. }
  150. else
  151. {
  152. Serilog.Log.Information($"星历文件[{file}]自动导入失败.{res.msg}");
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Serilog.Log.Error(ex, $"星历文件[{file}]自动导入失败");
  158. }
  159. }
  160. }
  161. }
  162. //清理180天之前导入的星历
  163. private async Task XlClear()
  164. {
  165. while (true)
  166. {
  167. try
  168. {
  169. using (RHDWContext db = new RHDWContext())
  170. {
  171. DateTime dt = DateTime.Now.AddDays(-180);
  172. var clearData = await db.XlInfos.Where(p => p.UpdateTime < dt).ToListAsync();
  173. if (clearData.Any())
  174. {
  175. db.XlInfos.RemoveRange(clearData);
  176. await db.SaveChangesAsync();
  177. }
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. Serilog.Log.Error(ex, "清理过期星历异常");
  183. }
  184. await Task.Delay(3600 * 1000);
  185. }
  186. }
  187. //计算星历的轨道经度
  188. private async Task XlLonCalc()
  189. {
  190. while (true)
  191. {
  192. try
  193. {
  194. using (RHDWContext db = new RHDWContext())
  195. {
  196. var calcItems = await db.XlInfos.Where(p => p.Lon == null).OrderByDescending(p => p.SatCode).ToListAsync();
  197. if (calcItems.Any())
  198. {
  199. var satInfo = await db.SatInfos.ToListAsync();
  200. var firstUpdateItems = calcItems.Where(p => satInfo.Any(t => t.SatCode == p.SatCode)).ToList();
  201. await Task.Run(() =>
  202. {
  203. foreach (var item in firstUpdateItems)
  204. {
  205. try
  206. {
  207. var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC);
  208. item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1);
  209. }
  210. catch (Exception ex)
  211. {
  212. item.Lon = -999;
  213. Serilog.Log.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!");
  214. }
  215. db.XlInfos.AddOrUpdate(item);
  216. db.SaveChangesAsync();
  217. calcItems.Remove(item);
  218. }
  219. foreach (var item in calcItems)
  220. {
  221. try
  222. {
  223. var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC);
  224. item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1);
  225. }
  226. catch (Exception ex)
  227. {
  228. item.Lon = -999;
  229. Serilog.Log.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!");
  230. }
  231. db.XlInfos.AddOrUpdate(item);
  232. db.SaveChangesAsync();
  233. }
  234. });
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. Serilog.Log.Error(ex, "清理过期星历异常");
  241. }
  242. await Task.Delay(60 * 1000);
  243. }
  244. }
  245. //清理10分钟之前的文件
  246. private async Task ClearLocalFile()
  247. {
  248. while (true)
  249. {
  250. try
  251. {
  252. var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  253. var files = Directory.EnumerateFiles(uploadFolder);
  254. foreach (var file in files)
  255. {
  256. FileInfo info = new FileInfo(file);
  257. if (info.CreationTime < DateTime.Now.AddMinutes(-10))
  258. {
  259. try
  260. {
  261. info.Delete();
  262. }
  263. catch
  264. {
  265. }
  266. }
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. Serilog.Log.Error(ex, "清理wwwroot历史文件异常");
  272. }
  273. await Task.Delay(60 * 1000);
  274. }
  275. }
  276. private void btn_ItemClick(object sender, ItemClickEventArgs e)
  277. {
  278. var btnTxt = e?.Item?.Caption?.Trim() ?? "任务管理";
  279. BaseDocument doc = null;
  280. doc = tabbedView1.Documents.Find(p => p.Control.GetType() == ctrlTypes[btnTxt]).FirstOrDefault();
  281. if (doc == null)
  282. {
  283. doc = tabbedView1.AddDocument((Control)Activator.CreateInstance(ctrlTypes[btnTxt]), btnTxt);
  284. if (doc.Control is Form form)
  285. {
  286. form.Text = "";
  287. doc.Caption = btnTxt;
  288. }
  289. if (btnTxt == "任务管理")
  290. doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.False;
  291. else
  292. doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.True;
  293. }
  294. tabbedView1.ActivateDocument(doc.Control);
  295. }
  296. protected override void OnFormClosing(FormClosingEventArgs e)
  297. {
  298. if (e.CloseReason == CloseReason.UserClosing)
  299. {
  300. if (!DxHelper.MsgBoxHelper.ShowConfirm("确定要退出当前系统吗?"))
  301. {
  302. e.Cancel = true;
  303. return;
  304. }
  305. }
  306. KillProcess();
  307. Application.Exit();
  308. }
  309. private void btnOpenApi_ItemClick(object sender, ItemClickEventArgs e)
  310. {
  311. using (RHDWContext db = new RHDWContext())
  312. {
  313. if (SysConfig.Config == null)
  314. {
  315. DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置基础信息");
  316. return;
  317. }
  318. string addr = $"http://{IpHelper.GetLocalIp()}:{SysConfig.Config.HttpPort}/swagger";
  319. try
  320. {
  321. System.Diagnostics.Process.Start(addr);
  322. }
  323. catch
  324. {
  325. db.Dispose();
  326. DxHelper.MsgBoxHelper.ShowError($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}");
  327. }
  328. }
  329. }
  330. private void StartProcess()
  331. {
  332. KillProcess();
  333. Process pro = new Process();
  334. pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\CPU参估服务\\CPU参估服务.exe");
  335. pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  336. pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}";
  337. pro.Start();
  338. pro = new Process();
  339. pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  340. pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\GPU参估服务\\GPU参估服务.exe");
  341. pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}";
  342. pro.Start();
  343. pro = new Process();
  344. pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  345. pro.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\信号检测服务\\信号检测服务.exe");
  346. pro.StartInfo.Arguments = $"http://127.0.0.1:{SysConfig.Config.HttpPort}";
  347. pro.Start();
  348. }
  349. private void KillProcess()
  350. {
  351. List<Process> list = new List<Process>();
  352. list.AddRange(Process.GetProcessesByName("CPU参估服务"));
  353. list.AddRange(Process.GetProcessesByName("GPU参估服务"));
  354. list.AddRange(Process.GetProcessesByName("信号检测服务"));
  355. foreach (var item in list)
  356. {
  357. try
  358. {
  359. item.Kill();
  360. }
  361. catch
  362. {
  363. }
  364. }
  365. }
  366. }
  367. }