MainForm.cs 9.7 KB

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