MainForm.cs 20 KB

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