MainForm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 DW5S.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 DW5S.App.CorTools;
  19. using System.IO;
  20. using DW5S.DTO;
  21. using DW5S.Entity;
  22. using DW5S.Repostory;
  23. using DW5S.KxcApi;
  24. using System.Net.Http;
  25. using DW5S.App.Properties;
  26. using System.Windows.Documents;
  27. using DW5S.App;
  28. using System.Diagnostics;
  29. using DevExpress.Utils.Extensions;
  30. using System.Net;
  31. using DW5S.WebApi;
  32. using DW5S.DataEmulation;
  33. using DxHelper;
  34. using System.Runtime.InteropServices;
  35. using System.Runtime.InteropServices.ComTypes;
  36. using DevExpress.LookAndFeel.Design;
  37. using System.Configuration;
  38. using Serilog;
  39. using DW5S.Service;
  40. using System.Runtime.CompilerServices;
  41. namespace DW5S
  42. {
  43. public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
  44. {
  45. Dictionary<string, Type> ctrlTypes = new Dictionary<string, Type>();
  46. public MainForm()
  47. {
  48. WebApiHelper.Start(AppConfigHelper.Get("HttpPort", 8090),
  49. dtoXmlName: "02.DW5S.DTO.xml",
  50. controllerXmlName: "08.DW5S.Controller.xml",
  51. dllKey: "DW5S");
  52. InitializeComponent();
  53. ribbon.UseDefault();
  54. tabbedView1.UseDefault();
  55. ctrlTypes.Add("任务管理", typeof(CtrlHome));
  56. ctrlTypes.Add("参考任务", typeof(CtrlRefTask));
  57. ctrlTypes.Add("参估结果", typeof(CtrlCgRes));
  58. ctrlTypes.Add("参考参估", typeof(CtrlRefCgRes));
  59. ctrlTypes.Add("绘图管理", typeof(CtrlDraw));
  60. ctrlTypes.Add("星历管理", typeof(CtrlXl));
  61. ctrlTypes.Add("卫星管理", typeof(CtrlSat));
  62. ctrlTypes.Add("接收站管理", typeof(CtrlRecTx));
  63. ctrlTypes.Add("参考站管理", typeof(CtrlRefTx));
  64. ctrlTypes.Add("超短站管理", typeof(CtrlCdbTx));
  65. ctrlTypes.Add("测向站管理", typeof(CtrlCxTx));
  66. ctrlTypes.Add("固定站管理", typeof(CtrlFixedStation));
  67. ctrlTypes.Add("信号管理", typeof(CtrlSignal));
  68. ctrlTypes.Add("样本管理", typeof(CtrlSample));
  69. ctrlTypes.Add("目标管理", typeof(CtrlTarget));
  70. ctrlTypes.Add("系统设置", typeof(CtrlSysSettings));
  71. ctrlTypes.Add("变采样", typeof(ResampleForm));
  72. ctrlTypes.Add("GPU参估", typeof(GpuCalcForm));
  73. ctrlTypes.Add("检测参估", typeof(DetectToolForm));
  74. ctrlTypes.Add("星历推算", typeof(XlCalculateForm));
  75. ctrlTypes.Add("信号仿真", typeof(SignalEmulation));
  76. ctrlTypes.Add("服务状态", typeof(CtrlSvrs));
  77. ctrlTypes.Add("服务日志", typeof(CtrlSvrLog));
  78. DxHelper.WaitHelper.UpdateSplashMessage("正在检查数据库环境...");
  79. }
  80. private string text;
  81. private async void MainForm_Load(object sender, EventArgs e)
  82. {
  83. //this.text = this.Text;
  84. //this.HtmlText = $"<size=12>{this.text}";
  85. //var repsSys = unitOfWork.Of<SysSetings>();
  86. //var settings = await repsSys.FirstOrDefaultAsync();
  87. //btn_ItemClick(null, null);
  88. //ServerContext.Instance.Init();
  89. //this.HtmlText = $"<size=12>{text}(<size=9>{settings.TimeZoneDisplayName}</size>)";
  90. //Messenger.Defalut.Sub<SysSetings>("时区改变", settings =>
  91. //{
  92. // this.HtmlText = $"<size=12>{text}(<size=9>{settings.TimeZoneDisplayName}</size>)";
  93. //});
  94. //StartProcess();
  95. //_ = XlScan();
  96. //_ = XlClear();
  97. //_ = ClearLocalFile();
  98. //_ = ClearLog();
  99. //_ = MonitorCpuAndMemory();
  100. //await XlLonCalc();
  101. var unitOfWork = IocContainer.UnitOfWork;
  102. var repsXl = unitOfWork.Of<XlInfo>() as XlRepository;
  103. await repsXl.GetAllSat();
  104. DxHelper.WaitHelper.CloseForm();
  105. }
  106. private Task MonitorCpuAndMemory()
  107. {
  108. if (!AppConfigHelper.Get("UseGatherDevState", true)) return Task.CompletedTask;
  109. return Task.Run(() =>
  110. {
  111. try
  112. {
  113. int interval = 30000;
  114. CpuMonitor sys = new CpuMonitor();
  115. const int GB_DIV = 1024 * 1024 * 1024;
  116. while (true)
  117. {
  118. //第二章方法获取系统CPU和内存使用情况
  119. IocContainer.Logger.Information($"设备CPU:{sys.CpuLoad:f1}%,设备内存:{(sys.PhysicalMemory - sys.MemoryAvailable) / GB_DIV:f2}GB/{sys.PhysicalMemory / (double)GB_DIV:f2}GB,平台内存:{sys.ProcessMemory() / (double)GB_DIV:f2}GB,平台线程:{sys.ProcessThread()}");
  120. Thread.Sleep(interval);
  121. }
  122. }
  123. catch
  124. { }
  125. });
  126. }
  127. //自动导入Tle
  128. private async Task XlScan()
  129. {
  130. while (true)
  131. {
  132. await Task.Delay(10000);
  133. var unitOfWork = IocContainer.UnitOfWork;
  134. var repsSys = unitOfWork.Of<SysSetings>();
  135. var settings = await repsSys.FirstOrDefaultAsync();
  136. if (settings == null
  137. || string.IsNullOrWhiteSpace(settings.XLDirectory)
  138. || !Directory.Exists(settings.XLDirectory))
  139. {
  140. continue;
  141. }
  142. DirectoryInfo dir = new DirectoryInfo(settings.XLDirectory);
  143. var backupSucceed = Path.Combine(dir.FullName, "Backup", "Succeed");
  144. var backupFailed = Path.Combine(dir.FullName, "Backup", "Failed");
  145. Directory.CreateDirectory(backupSucceed);
  146. Directory.CreateDirectory(backupFailed);
  147. var files = Directory.EnumerateFiles(settings.XLDirectory, "*", SearchOption.TopDirectoryOnly);
  148. foreach (string file in files)
  149. {
  150. bool succeed = false;
  151. try
  152. {
  153. var fileName = await HttpHelper.UploadFileAsync(file);
  154. XlImportDto dto = new XlImportDto() { File = fileName };
  155. var res = await HttpHelper.PostRequestAsync<RecordRes>(SysConfig.GetUrl("Xl/ImportTleAsync"), dto);
  156. if (res.code == 200)
  157. {
  158. IocContainer.Logger.Information($"星历文件[{file}]自动导入成功!");
  159. try
  160. {
  161. //导入完成的文件放在备份目录
  162. var newFile = Path.Combine(backupSucceed, Path.GetFileName(file));
  163. if (File.Exists(newFile))
  164. File.Delete(newFile);
  165. File.Move(file, newFile);
  166. }
  167. catch
  168. {
  169. }
  170. }
  171. else
  172. {
  173. IocContainer.Logger.Error($"星历文件[{file}]自动导入失败.{res.msg}");
  174. try
  175. {
  176. //导入完成的文件放在备份目录
  177. var newFile = Path.Combine(backupFailed, Path.GetFileName(file));
  178. if (File.Exists(newFile))
  179. File.Delete(newFile);
  180. File.Move(file, newFile);
  181. }
  182. catch
  183. {
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. IocContainer.Logger.Error(ex, $"星历文件[{file}]自动导入失败");
  190. try
  191. {
  192. //导入完成的文件放在备份目录
  193. var newFile = Path.Combine(backupFailed, Path.GetFileName(file));
  194. if (File.Exists(newFile))
  195. File.Delete(newFile);
  196. File.Move(file, newFile);
  197. }
  198. catch
  199. {
  200. }
  201. await Task.Delay(5000);
  202. }
  203. finally
  204. {
  205. await Task.Delay(2000);
  206. }
  207. }
  208. }
  209. }
  210. //清理1年之前导入的星历
  211. private async Task XlClear()
  212. {
  213. while (true)
  214. {
  215. try
  216. {
  217. var unitOfWork = IocContainer.UnitOfWork;
  218. var repsXl = unitOfWork.Of<XlInfo>();
  219. DateTime dt = DateTime.Now.AddYears(-1);
  220. await repsXl.DeleteAsync(p => p.UpdateTime < dt);
  221. await unitOfWork.SaveAsync();
  222. await Task.Delay(60 * 1000);
  223. }
  224. catch (Exception ex)
  225. {
  226. IocContainer.Logger.Error(ex, "清理过期星历异常");
  227. }
  228. finally
  229. {
  230. await Task.Delay(2000);
  231. }
  232. }
  233. }
  234. //计算星历的轨道经度
  235. private async Task XlLonCalc()
  236. {
  237. while (true)
  238. {
  239. try
  240. {
  241. List<XlInfo> calcItems = null;
  242. List<SatInfo> satInfo = null;
  243. var unitOfWork = IocContainer.UnitOfWork;
  244. var repsXl = unitOfWork.Of<XlInfo>();
  245. var repsSat = unitOfWork.Of<SatInfo>();
  246. calcItems = (await repsXl.FindAsync(p => p.Lon == null, p => p.SatCode, false)).Take(100).ToList();
  247. satInfo = (await repsSat.GetAllAsync()).ToList();
  248. if (calcItems != null && calcItems.Any())
  249. {
  250. var firstUpdateItems = calcItems.Where(p => satInfo.Any(t => t.SatCode == p.SatCode)).ToList();
  251. await Task.Run(async () =>
  252. {
  253. List<XlInfo> Level1 = new List<XlInfo>();
  254. foreach (var item in firstUpdateItems)
  255. {
  256. try
  257. {
  258. var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC);
  259. item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1);
  260. Level1.Add(item);
  261. calcItems.Remove(item);
  262. }
  263. catch (Exception ex)
  264. {
  265. item.Lon = -999;
  266. IocContainer.Logger.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!");
  267. }
  268. }
  269. if (Level1.Any())
  270. {
  271. await repsXl.AddOrUpdateAsync(Level1);
  272. }
  273. List<XlInfo> Level2 = new List<XlInfo>();
  274. foreach (var item in calcItems)
  275. {
  276. try
  277. {
  278. var eph = EphHelper.Calc(item.TwoLine, item.TimeUTC);
  279. item.Lon = Math.Round(PhysicsHelper.EcefToGeo((eph.X, eph.Y, eph.Z)).lon, 1);
  280. Level2.Add(item);
  281. }
  282. catch (Exception ex)
  283. {
  284. item.Lon = -999;
  285. IocContainer.Logger.Error(ex, $"[{item.TwoLine}]推算XYZ星历出错!");
  286. }
  287. }
  288. if (Level2.Any())
  289. {
  290. await repsXl.AddOrUpdateAsync(Level2);
  291. }
  292. });
  293. await unitOfWork.SaveAsync();
  294. }
  295. else
  296. {
  297. await Task.Delay(60 * 1000);
  298. }
  299. }
  300. catch (Exception ex)
  301. {
  302. IocContainer.Logger.Error(ex, $"推算XYZ星历出错!");
  303. }
  304. finally
  305. {
  306. await Task.Delay(5000);
  307. }
  308. }
  309. }
  310. //清理10分钟之前的文件
  311. private async Task ClearLocalFile()
  312. {
  313. while (true)
  314. {
  315. try
  316. {
  317. var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  318. var files = Directory.EnumerateFiles(uploadFolder);
  319. foreach (var file in files)
  320. {
  321. FileInfo info = new FileInfo(file);
  322. if (info.CreationTime < DateTime.Now.AddMinutes(-10))
  323. {
  324. try
  325. {
  326. info.Delete();
  327. }
  328. catch
  329. {
  330. }
  331. }
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. IocContainer.Logger.Error(ex, "清理wwwroot历史文件异常");
  337. }
  338. await Task.Delay(60 * 1000);
  339. }
  340. }
  341. private async Task ClearLog()
  342. {
  343. while (true)
  344. {
  345. var unitOfWork = IocContainer.UnitOfWork;
  346. var repsLogRes = unitOfWork.Of<LogRes>();
  347. try
  348. {
  349. var time1 = DateTime.Today.AddDays(-7);
  350. var time2 = DateTime.Today.AddDays(-30);
  351. //var delItems = (await repsLogRes.FindAsync(p => p.LogTime < time1 && p.LogType != Entity.EnumLogType.Error)).Take(500).ToList();
  352. var deleteTime1 = await repsLogRes.DeleteAsync(p => p.LogTime < time1 && p.LogType != Entity.EnumLogType.Error);
  353. //var delItems2 = (await repsLogRes.FindAsync(p => p.LogTime < time2 && p.LogType == Entity.EnumLogType.Error)).Take(500).ToList();
  354. var deleteTime2 = await repsLogRes.DeleteAsync(p => p.LogTime < time2 && p.LogType == Entity.EnumLogType.Error);
  355. await unitOfWork.SaveAsync();
  356. if (deleteTime1 > 0 && deleteTime2 > 0)
  357. {
  358. break;
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. IocContainer.Logger.Error(ex, "清理日志信息异常");
  364. await Task.Delay(10000);
  365. }
  366. finally
  367. {
  368. await Task.Delay(5000);
  369. }
  370. }
  371. }
  372. private void btn_ItemClick(object sender, ItemClickEventArgs e)
  373. {
  374. var btnTxt = e?.Item?.Caption?.Trim() ?? "任务管理";
  375. BaseDocument doc = null;
  376. doc = tabbedView1.Documents.Find(p => p.Control.GetType() == ctrlTypes[btnTxt]).FirstOrDefault();
  377. if (doc == null)
  378. {
  379. doc = tabbedView1.AddDocument((Control)Activator.CreateInstance(ctrlTypes[btnTxt]), btnTxt);
  380. if (doc.Control is Form form)
  381. {
  382. form.Text = "";
  383. doc.Caption = btnTxt;
  384. }
  385. if (btnTxt == "任务管理")
  386. doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.False;
  387. else
  388. doc.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.True;
  389. }
  390. tabbedView1.ActivateDocument(doc.Control);
  391. }
  392. protected override void OnFormClosing(FormClosingEventArgs e)
  393. {
  394. if (e.CloseReason == CloseReason.UserClosing)
  395. {
  396. if (!DxHelper.MsgBoxHelper.ShowConfirm("确定要退出当前系统吗?"))
  397. {
  398. e.Cancel = true;
  399. return;
  400. }
  401. }
  402. KillProcess();
  403. Application.Exit();
  404. }
  405. private async void btnOpenApi_ItemClick(object sender, ItemClickEventArgs e)
  406. {
  407. string addr = $"http://{IpHelper.GetLocalIp()}:{AppConfigHelper.Get("HttpPort", 8090)}/swagger";
  408. try
  409. {
  410. //System.Diagnostics.Process.Start(addr);//NetFramework可用,NetCore使用CMD启动
  411. ProcessStartInfo info = new ProcessStartInfo();
  412. info.Arguments = $"/C start {addr}";
  413. info.FileName = "cmd";
  414. info.UseShellExecute = false;
  415. info.CreateNoWindow = true;
  416. Process.Start(info);
  417. }
  418. catch (Exception ex)
  419. {
  420. DxHelper.MsgBoxHelper.ShowError($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}");
  421. }
  422. }
  423. private void StartProcess()
  424. {
  425. Task.Run(async () =>
  426. {
  427. KillProcess();
  428. Process pro = new Process();
  429. var servicesDir = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\"), "*", SearchOption.TopDirectoryOnly);
  430. var unitOfWork = IocContainer.UnitOfWork;
  431. var repsSys = unitOfWork.Of<SysSetings>();
  432. var settings = await repsSys.FirstOrDefaultAsync();
  433. foreach (var item in servicesDir)
  434. {
  435. try
  436. {
  437. string exeName = Path.GetFileNameWithoutExtension(item);
  438. string exeFile = $"{item}\\{exeName}.exe";
  439. pro.StartInfo.FileName = exeFile;
  440. pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  441. pro.StartInfo.Arguments = $"http://127.0.0.1:{settings.HttpPort}";
  442. pro.Start();
  443. }
  444. catch (Exception ex)
  445. {
  446. IocContainer.Logger.Error(ex, $"启动【{item}】中的服务异常");
  447. DxHelper.MsgBoxHelper.ShowError($"启动【{item}】中的服务异常");
  448. }
  449. }
  450. });
  451. }
  452. private void KillProcess()
  453. {
  454. try
  455. {
  456. List<Process> list = new List<Process>();
  457. var servicesDir = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Service\\"), "*", SearchOption.TopDirectoryOnly);
  458. foreach (var item in servicesDir)
  459. {
  460. string exeName = Path.GetFileNameWithoutExtension(item);
  461. list.AddRange(Process.GetProcessesByName(exeName));
  462. }
  463. foreach (var item in list)
  464. {
  465. try
  466. {
  467. item.Kill();
  468. }
  469. catch
  470. {
  471. }
  472. }
  473. }
  474. catch
  475. { }
  476. }
  477. }
  478. }