MainForm.cs 15 KB

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