MainForm.cs 20 KB

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