MainForm.cs 10 KB

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