MainForm.cs 10 KB

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