MainForm.cs 9.0 KB

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