MainForm.cs 12 KB

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