CtrlPosXz.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using DevExpress.Utils.Drawing.Helpers;
  2. using DevExpress.Utils.Extensions;
  3. using DevExpress.XtraEditors;
  4. using DevExpress.XtraExport.Helpers;
  5. using DevExpress.XtraGrid.Views.Grid;
  6. using DevExpress.XtraMap;
  7. using DevExpress.XtraTreeList.Data;
  8. using DxHelper;
  9. using Extensions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Configuration;
  14. using System.Data;
  15. using System.Data.Entity;
  16. using System.Drawing;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows.Controls;
  23. using System.Windows.Documents;
  24. using System.Windows.Forms;
  25. using XdCxRhDW.App.Api.时差粗值预测;
  26. using XdCxRhDW.App.Api.时差线;
  27. using XdCxRhDW.App.Api.星历推算;
  28. using XdCxRhDW.App.DTO;
  29. using XdCxRhDW.App.UserControl;
  30. using XzXdDw.App.Api;
  31. using XzXdDw.App.Api.低轨GDOP误差椭圆;
  32. using XzXdDw.App.Api.星地GDOP误差椭圆;
  33. using XzXdDw.App.EditForms;
  34. using XzXdDw.App.EFContext;
  35. using XzXdDw.App.Model;
  36. namespace XzXdDw.App.UserControl
  37. {
  38. /// <summary>
  39. /// 星地定位
  40. /// </summary>
  41. public partial class CtrlPosXz : DevExpress.XtraEditors.XtraUserControl
  42. {
  43. /// <summary>
  44. /// 时差初值 范围计算
  45. /// </summary>
  46. DtSXLParam dtsxlctrl = null;
  47. XZGDOPParam xzGdopParam = null;
  48. XZErrEllipseParam xzErrEllipseParam = null;
  49. double MBfu;
  50. double CKfu;
  51. private TxInfo refTx;
  52. TcpServer tcpServer;
  53. public CtrlPosXz()
  54. {
  55. InitializeComponent();
  56. InitData();
  57. }
  58. private void InitData()
  59. {
  60. refTx = new TxInfo();
  61. refTx.Lon = ConfigurationManager.AppSettings["SXCKLon"].To(112.33);
  62. refTx.Lat = ConfigurationManager.AppSettings["SXCKLat"].To(16.3);
  63. MBfu = ConfigurationManager.AppSettings["MBUpFreq"].To(3.1085e8);
  64. CKfu = ConfigurationManager.AppSettings["CKUpFreq"].To(2.95e8);
  65. tcpServer = new TcpServer();
  66. }
  67. private async void CtrlXdPos_Load(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. gridPos.Init().UseMultiSelect().UseFilter().UseRowNumber().UseExportXlsx()
  72. .AddMenu("删除", SvgHelper.CreateClose(), async () =>
  73. {
  74. try
  75. {
  76. if (XtraMessageBox.Show("确认删除?(Ctrl+A可全选删除)", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  77. {
  78. return;
  79. }
  80. var ids = gridView1.GetSelectedRows();
  81. using (RHDWContext db = new RHDWContext())
  82. {
  83. foreach (var id in ids)
  84. {
  85. var item = gridView1.GetRow(id) as XZPosRes;
  86. var delItem = await db.XZPosRes.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  87. if (delItem != null)
  88. {
  89. db.XZPosRes.Remove(delItem);
  90. }
  91. }
  92. await db.SaveChangesAsync();
  93. }
  94. gridView1.DeleteSelectedRows();
  95. }
  96. catch (Exception ex)
  97. {
  98. Serilog.Log.Error("删除结果失败", ex);
  99. XtraMessageBox.Show("删除结果失败");
  100. }
  101. })
  102. .AddMenu("加载仿真数据", SvgHelper.LoadFromFile("Image\\LoadData.svg"), LoadSimulationData)
  103. .AddMenu("手动定位", SvgHelper.LoadFromFile("Image\\Pos.svg"), X2Pos)
  104. .AddMenu("绘制时差线", SvgHelper.LoadFromFile("Image\\DrawLine.svg"), DrawDtoLine)
  105. .AddMenu("绘制频差线", SvgHelper.LoadFromFile("Image\\DfoLine.svg"), DrawDfoLine)
  106. .AddMenu("GDOP分析", SvgHelper.LoadFromFile("Image\\GDOP.svg"), () =>
  107. {
  108. var ids = gridView1.GetSelectedRows();
  109. if (ids.Length <= 0)
  110. {
  111. XtraMessageBox.Show("请选择需要GDOP分析的数据信息!");
  112. return;
  113. }
  114. var item = gridView1.GetRow(ids[0]) as XZPosRes;
  115. if (xzGdopParam == null)
  116. {
  117. xzGdopParam = new XZGDOPParam(refTx, item.SigTime, MBfu, CKfu);
  118. }
  119. xzGdopParam.mapControl1 = mapControl1;
  120. PopupHelper.ShowPopup(xzGdopParam, mapControl1, mapControl1.Width / 4);
  121. })
  122. .AddMenu("绘制误差椭圆", SvgHelper.LoadFromFile("Image\\误差椭圆.svg"), () =>
  123. {
  124. var ids = gridView1.GetSelectedRows();
  125. if (ids.Length <= 0)
  126. {
  127. XtraMessageBox.Show("请选择需要误差椭圆的数据信息!");
  128. return;
  129. }
  130. var item = gridView1.GetRow(ids[0]) as XZPosRes;
  131. if (xzErrEllipseParam == null)
  132. {
  133. xzErrEllipseParam = new XZErrEllipseParam(refTx, item.SigTime, MBfu, CKfu);
  134. }
  135. xzErrEllipseParam.mapControl1 = mapControl1;
  136. xzErrEllipseParam.lon = item.PosLon;
  137. xzErrEllipseParam.lat = item.PosLat;
  138. PopupHelper.ShowPopup(xzGdopParam, mapControl1, mapControl1.Width / 4);
  139. })
  140. .AddMenu("停止加载", SvgHelper.LoadFromFile("Image\\Stop.svg"), () => stoped = true);
  141. mapControl1.UseDefalutOptions()
  142. //.UseCluster()//定位点使用内置聚合
  143. .UseClearAll()
  144. .UseDistanceLine()
  145. .UseMarkDot()
  146. //.UseExportImg()
  147. //.UseExportXlsx()
  148. //.UseExportCsv()
  149. .UseDrawRect(rect =>
  150. {
  151. if (dtsxlctrl != null)
  152. {
  153. dtsxlctrl.rect = rect;
  154. }
  155. });
  156. //MapMenu为地图瓦片上右键菜单
  157. //AddPosMenu为定位点上右键菜单
  158. //RectMenu为框选的区域上右键菜单
  159. mapControl1.AddMapMenu("加载仿真数据", SvgHelper.LoadFromFile("Image\\LoadData.svg"), (lon, lat) => LoadSimulationData())
  160. .AddMapMenu("停止加载", SvgHelper.LoadFromFile("Image\\Stop.svg"), (lon, lat) => stoped = true)
  161. .AddPosMenu<XZPosRes>("GDOP分析", SvgHelper.LoadFromFile("Image\\GDOP.svg"), item =>
  162. {
  163. if (xzGdopParam == null)
  164. {
  165. xzGdopParam = new XZGDOPParam(refTx, item.SigTime, MBfu, CKfu);
  166. }
  167. xzGdopParam.mapControl1 = mapControl1;
  168. PopupHelper.ShowPopup(xzGdopParam, mapControl1, mapControl1.Width / 4);
  169. })
  170. .AddPosMenu<XZPosRes>("误差椭圆", SvgHelper.LoadFromFile("Image\\误差椭圆.svg"), item =>
  171. {
  172. if (xzErrEllipseParam == null)
  173. {
  174. xzErrEllipseParam = new XZErrEllipseParam(refTx, item.SigTime, MBfu, CKfu);
  175. }
  176. xzErrEllipseParam.mapControl1 = mapControl1;
  177. xzErrEllipseParam.lon = item.PosLon;
  178. xzErrEllipseParam.lat = item.PosLat;
  179. PopupHelper.ShowPopup(xzGdopParam, mapControl1, mapControl1.Width / 4);
  180. })
  181. .AddPosMenu<XZPosRes>("绘制时差线", SvgHelper.LoadFromFile("Image\\DrawLine.svg"), item =>
  182. {
  183. DrawDtoLine(item);
  184. })
  185. .AddPosMenu<XZPosRes>("绘制频差线", SvgHelper.LoadFromFile("Image\\DfoLine.svg"), item =>
  186. {
  187. DrawDfoLine(item);
  188. })
  189. .AddRectMenu<XZPosRes>("时差初值预估", SvgHelper.LoadFromFile("Image\\初值预估.svg"), items =>
  190. {
  191. var rect = mapControl1.GetCurrentRect();
  192. if (dtsxlctrl == null)
  193. {
  194. dtsxlctrl = new DtSXLParam();
  195. }
  196. dtsxlctrl.rect = rect;
  197. PopupHelper.ShowPopup(dtsxlctrl, mapControl1, mapControl1.Width / 4);
  198. })
  199. .AddPosMenu<XZPosRes>("手动定位", SvgHelper.LoadFromFile("Image\\Pos.svg"), item =>
  200. {
  201. X2Pos(item);
  202. });
  203. //查询已有的定位记录
  204. using (RHDWContext db = new RHDWContext())
  205. {
  206. List<XZPosRes> list = await db.XZPosRes.ToListAsync();
  207. this.gridPos.DataSource = list;
  208. mapControl1.SetPosDataSource(list);
  209. }
  210. var view = gridPos.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
  211. view.RowClick += View_RowClick;
  212. }
  213. catch (Exception ex)
  214. {
  215. Serilog.Log.Error("查询定位结果失败", ex);
  216. XtraMessageBox.Show("查询定位结果失败");
  217. }
  218. }
  219. private void View_RowClick(object sender, RowClickEventArgs e)
  220. {
  221. if (e.RowHandle < 0) return;
  222. var item = gridView1.GetRow(e.RowHandle) as XZPosRes;
  223. item.Selected = true;
  224. mapControl1.UpdatePosItem(item);
  225. }
  226. bool stoped = false;
  227. private async void LoadSimulationData()
  228. {
  229. SimulationEditor editor = new SimulationEditor("Simulation_Data_SX20240115.dat");
  230. if (editor.ShowDialog() != DialogResult.OK)
  231. {
  232. return;
  233. }
  234. string simulationFile = editor.SimulationFile;
  235. stoped = false;
  236. //在列表控件中全选+右键可以删除所有测试结果
  237. using (RHDWContext db = new RHDWContext())
  238. {
  239. var listTx = db.TxInfos.ToList();
  240. var sysSetings = await db.SysSetings.FirstOrDefaultAsync();
  241. var lines = File.ReadAllLines(simulationFile);
  242. Random r = new Random();
  243. foreach (var item in lines)
  244. {
  245. if (stoped) break;
  246. var items = item.Split(new string[] { " ", "\"" }, StringSplitOptions.RemoveEmptyEntries);
  247. if (items.Length != 18) continue;
  248. int index = 0;
  249. string sigtimeStr = items[index++] + " " + items[index++];
  250. var SigTime = DateTime.ParseExact(sigtimeStr, "yyyy-MM-dd HH:mm:ss", null);
  251. var target_dto = Convert.ToDouble(items[index++]);
  252. var target_dfo = Convert.ToDouble(items[index++]);
  253. var ref_dto = Convert.ToDouble(items[index++]);
  254. var ref_dfo = Convert.ToDouble(items[index++]);
  255. var mainX = Convert.ToDouble(items[index++]);
  256. var mainY = Convert.ToDouble(items[index++]);
  257. var mainZ = Convert.ToDouble(items[index++]);
  258. var mainVX = Convert.ToDouble(items[index++]);
  259. var mainVY = Convert.ToDouble(items[index++]);
  260. var mainVZ = Convert.ToDouble(items[index++]);
  261. var adjaX = Convert.ToDouble(items[index++]);
  262. var adjaY = Convert.ToDouble(items[index++]);
  263. var adjaZ = Convert.ToDouble(items[index++]);
  264. var adjaVX = Convert.ToDouble(items[index++]);
  265. var adjaVY = Convert.ToDouble(items[index++]);
  266. var adjaVZ = Convert.ToDouble(items[index++]);
  267. var cgRes = new CgRes()
  268. {
  269. SigTime = SigTime,
  270. DtoSx = target_dto * 1e6,
  271. DfoSx = target_dfo,
  272. SnrSx = r.Next(18, 24) + Math.Round(r.NextDouble(), 1),
  273. DtoCdb = ref_dto * 1e6,
  274. DfoCdb = ref_dfo,
  275. SnrCdb = r.Next(22, 32) + Math.Round(r.NextDouble(), 1),
  276. MainX = mainX,
  277. MainY = mainY,
  278. MainZ = mainZ,
  279. MainVX = mainVX,
  280. MainVY = mainVY,
  281. MainVZ = mainVZ,
  282. AdjaX = adjaX,
  283. AdjaY = adjaY,
  284. AdjaZ = adjaZ,
  285. AdjaVX = adjaVX,
  286. AdjaVY = adjaVY,
  287. AdjaVZ = adjaVZ,
  288. };
  289. var cg = db.CgRes.Add(cgRes);//参估结果入库
  290. await db.SaveChangesAsync();
  291. var res = PosApi.X2_POS(cg, refTx, MBfu, CKfu);
  292. XZPosRes posRes = new XZPosRes()
  293. {
  294. SigTime = SigTime,
  295. CgResID = cg.ID,
  296. TarName = "未知目标",
  297. TsName = "tar2",
  298. PosLon = res[0],
  299. PosLat = res[1],
  300. MirrLon = res[3],
  301. MirrLat = res[4],
  302. };
  303. db.XZPosRes.Add(posRes);//定位结果入库
  304. SendPosData(posRes, sysSetings);
  305. //更新定位结果UI列表和地图
  306. var currentTask = gridView1.GetFocusedRow() as TaskInfo;
  307. var ds = this.gridPos.DataSource as List<XZPosRes>;
  308. ds.Insert(0, posRes);
  309. this.Invoke(new Action(() =>
  310. {
  311. gridView1.RefreshData();
  312. mapControl1.AddPosItem(posRes);
  313. }));
  314. await Task.Delay(200);
  315. }
  316. }
  317. }
  318. private void SendPosData(XZPosRes pos, SysSetings setings)
  319. {
  320. if (setings == null || string.IsNullOrEmpty(setings.ServerIp) || setings.Port < 0 || setings.Port > 65535)
  321. {
  322. return;
  323. }
  324. string json = Newtonsoft.Json.JsonConvert.SerializeObject(pos);
  325. tcpServer.SendData(setings.ServerIp, setings.Port, json);
  326. }
  327. private async void X2Pos()
  328. {
  329. var ids = gridView1.GetSelectedRows();
  330. if (ids.Length <= 0)
  331. {
  332. XtraMessageBox.Show("请选择需要手动定位的数据信息!");
  333. return;
  334. }
  335. var item = gridView1.GetRow(ids[0]) as XZPosRes;
  336. await Task.Run(() =>
  337. {
  338. X2Pos(item);
  339. });
  340. }
  341. private async void X2Pos(XZPosRes item)
  342. {
  343. try
  344. {
  345. List<TxInfo> listTx = new List<TxInfo>();
  346. CgRes cg;
  347. using (RHDWContext db = new RHDWContext())
  348. {
  349. listTx = db.TxInfos.ToList();
  350. cg = await db.CgRes.Where(m => m.ID == item.CgResID).FirstOrDefaultAsync();
  351. }
  352. if (cg == null)
  353. {
  354. XtraMessageBox.Show($"未找到定位相关的计算[{item.CgResID}]{item.SigTime:yyyy-MM-dd HH:mm:ss}的参估信息!");
  355. return;
  356. }
  357. X2PosParamEditor frm = new X2PosParamEditor(item, cg, refTx, MBfu, CKfu, () =>
  358. {
  359. gridView1.RefreshData();
  360. if (item.PosLon > 180 || item.PosLon < -180)
  361. {
  362. item.PosLon = 0;
  363. item.PosLat = 0;
  364. }
  365. mapControl1.UpdatePosItem(item);
  366. });
  367. frm.ShowDialog();
  368. }
  369. catch (Exception ex)
  370. {
  371. Serilog.Log.Error("手动定位失败", ex);
  372. XtraMessageBox.Show("手动定位失败");
  373. }
  374. }
  375. private async void DrawDtoLine()
  376. {
  377. var ids = gridView1.GetSelectedRows();
  378. if (ids.Length <= 0)
  379. {
  380. XtraMessageBox.Show("请选择需要绘制时差线的定位数据信息!");
  381. return;
  382. }
  383. var item = gridView1.GetRow(ids[0]) as XZPosRes;
  384. await Task.Run(() =>
  385. {
  386. DrawDtoLine(item);
  387. });
  388. }
  389. private async void DrawDtoLine(XZPosRes item)
  390. {
  391. try
  392. {
  393. List<TxInfo> listTx = new List<TxInfo>();
  394. List<Model.SatInfo> listSat = new List<Model.SatInfo>();
  395. CgRes cg;
  396. using (RHDWContext db = new RHDWContext())
  397. {
  398. listTx = db.TxInfos.ToList();
  399. listSat = db.SatInfos.ToList();
  400. cg = await db.CgRes.Where(m => m.ID == item.CgResID).FirstOrDefaultAsync();
  401. }
  402. if (cg == null)
  403. {
  404. XtraMessageBox.Show($"未找到定位相关的计算[{item.CgResID}]信息");
  405. return;
  406. }
  407. var satTx = listTx.Find(p => p.TxType == EnumTxType.MainSat);
  408. var satNTx = listTx.Find(p => p.TxType == EnumTxType.AdjaSat);
  409. DtoLineTwoStartOption twoStartOption = new DtoLineTwoStartOption();
  410. twoStartOption.MsEph = new double[6] { cg.MainX, cg.MainY, cg.MainZ, cg.MainVX, cg.MainVY, cg.MainVZ };
  411. twoStartOption.NsEph = new double[6] { cg.AdjaX, cg.AdjaY, cg.AdjaZ, cg.AdjaVX, cg.AdjaVY, cg.AdjaVZ };
  412. twoStartOption.RefGeod = new double[3] { refTx.Lon, refTx.Lat, 0 };
  413. twoStartOption.TargetDto = cg.DtoSx;
  414. twoStartOption.RefDto = cg.DtoCdb;
  415. var tsDtoLine = DrawDtoLineHelper.DtoLineXZTwoStart(twoStartOption);
  416. mapControl1.DrawDtoPonit($"双星[{listSat.FirstOrDefault(m => m.ID == satTx.ID)?.Sat},{listSat.FirstOrDefault(m => m.ID == satNTx.ID)?.Sat}]时差线", tsDtoLine);
  417. }
  418. catch (Exception ex)
  419. {
  420. Serilog.Log.Error("绘制时差线失败", ex);
  421. XtraMessageBox.Show($"绘制时差线失败,失败信息:{ex.Message}");
  422. }
  423. }
  424. private async void DrawDfoLine()
  425. {
  426. var ids = gridView1.GetSelectedRows();
  427. if (ids.Length <= 0)
  428. {
  429. XtraMessageBox.Show("请选择需要绘制频差线的定位数据信息!");
  430. return;
  431. }
  432. var item = gridView1.GetRow(ids[0]) as XZPosRes;
  433. await Task.Run(() => { DrawDfoLine(item); });
  434. }
  435. private async void DrawDfoLine(XZPosRes item)
  436. {
  437. try
  438. {
  439. List<TxInfo> listTx = new List<TxInfo>();
  440. List<Model.SatInfo> listSat = new List<Model.SatInfo>();
  441. CgRes cg;
  442. using (RHDWContext db = new RHDWContext())
  443. {
  444. listTx = db.TxInfos.ToList();
  445. listSat = db.SatInfos.ToList();
  446. cg = await db.CgRes.Where(m => m.ID == item.CgResID).FirstOrDefaultAsync();
  447. }
  448. if (cg == null)
  449. {
  450. XtraMessageBox.Show($"未找到定位相关的计算[{item.CgResID}]信息");
  451. return;
  452. }
  453. var satTx = listTx.Find(p => p.TxType == EnumTxType.MainSat);
  454. var satNTx = listTx.Find(p => p.TxType == EnumTxType.AdjaSat);
  455. DfoLineTwoStartOption twoStartOption = new DfoLineTwoStartOption();
  456. twoStartOption.MsEph = new double[6] { cg.MainX, cg.MainY, cg.MainZ, cg.MainVX, cg.MainVY, cg.MainVZ };
  457. twoStartOption.NsEph = new double[6] { cg.AdjaX, cg.AdjaY, cg.AdjaZ, cg.AdjaVX, cg.AdjaVY, cg.AdjaVZ };
  458. twoStartOption.RefGeod = new double[3] { refTx.Lon, refTx.Lat, 0 };
  459. twoStartOption.TargetDfo = cg.DfoSx;
  460. twoStartOption.RefDfo = cg.DfoCdb;
  461. twoStartOption.fu1 = MBfu;
  462. twoStartOption.fu2 = CKfu;
  463. var tsDtoLine = DrawDtoLineHelper.DfoLineXZTwoStart(twoStartOption);
  464. mapControl1.DrawDtoPonit($"双星[{listSat.FirstOrDefault(m => m.ID == satTx.ID)?.Sat},{listSat.FirstOrDefault(m => m.ID == satNTx.ID)?.Sat}]频差线", tsDtoLine);
  465. }
  466. catch (Exception ex)
  467. {
  468. Serilog.Log.Error("绘制频差线失败", ex);
  469. XtraMessageBox.Show($"绘制频差线失败,失败信息:{ex.Message}");
  470. }
  471. }
  472. }
  473. }