MainForm.cs 13 KB

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