MainForm.cs 8.7 KB

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