CtrlHome.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. using DevExpress.Mvvm.DataAnnotations;
  2. using DevExpress.Utils;
  3. using DevExpress.Utils.Design;
  4. using DevExpress.Utils.Drawing;
  5. using DevExpress.Utils.Svg;
  6. using DevExpress.XtraEditors;
  7. using DevExpress.XtraEditors.ButtonsPanelControl;
  8. using DevExpress.XtraEditors.Controls;
  9. using DevExpress.XtraEditors.Repository;
  10. using DevExpress.XtraGrid.Columns;
  11. using DevExpress.XtraGrid.Views.Grid;
  12. using DevExpress.XtraGrid.Views.Grid.ViewInfo;
  13. using DxHelper;
  14. using XdCxRhDW.App.EFContext;
  15. using XdCxRhDW.App.Model;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.ComponentModel.DataAnnotations;
  20. using System.ComponentModel.DataAnnotations.Schema;
  21. using System.Data;
  22. using System.Drawing;
  23. using System.Linq;
  24. using System.Security.Cryptography;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using System.Windows.Forms;
  28. using XdCxRhDW.App.EditForms;
  29. using System.Data.Entity;
  30. using DevExpress.XtraBars.Customization;
  31. using System.Reflection;
  32. using System.Net.Sockets;
  33. using System.Net;
  34. using DevExpress.XtraCharts.Native;
  35. using System.Threading;
  36. using XdCxRhDW.Dto;
  37. using XdCxRhDW.App.Api;
  38. using System.Windows.Controls;
  39. using XdCxRhDW.App.WebAPI;
  40. using Microsoft.Owin.Hosting;
  41. using XdCxRhDW.App.App.Properties;
  42. using System.Web.Http;
  43. using System.Data.Entity.Migrations;
  44. namespace XdCxRhDW.App.UserControl
  45. {
  46. public partial class CtrlHome : DevExpress.XtraEditors.XtraUserControl
  47. {
  48. List<TaskInfo> list = new List<TaskInfo>();
  49. public CtrlHome()
  50. {
  51. InitializeComponent();
  52. gridHomeTask.Init();
  53. gridHomePosRes.Init().UseMultiSelect().UseRowNumber();
  54. mapControl1.UseDefalutOptions()
  55. //.UseCluster()//定位点使用内置聚合
  56. .UseClearAll()
  57. .UseDistanceLine()
  58. .UseMarkDot()
  59. .UseExportImg()
  60. .UseExportXlsx()
  61. .UseExportCsv()
  62. .UseDrawRect(rect =>
  63. {
  64. (double startLon, double startLat, double centerLon, double centerLat, double endLon, double endLat, double lonRange, double latRange) = rect;
  65. });
  66. }
  67. private async void GridView1_FocusedRowObjectChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowObjectChangedEventArgs e)
  68. {
  69. var tsk = (e.Row as TaskInfo);
  70. var btnEdit = (layoutControlGroup1.CustomHeaderButtons[1] as GroupBoxButton);
  71. var btnDel = (layoutControlGroup1.CustomHeaderButtons[2] as GroupBoxButton);
  72. btnDel.Enabled = btnEdit.Enabled = tsk.TaskState != EnumTaskState.Running;
  73. List<PosRes> listPos = new List<PosRes>();
  74. using (RHDWContext db = new RHDWContext())
  75. {
  76. var items = await db.PosRes.Where(p => p.TaskID == tsk.ID).OrderByDescending(p => p.SigTime).ToListAsync();
  77. listPos.AddRange(items);
  78. }
  79. this.gridHomePosRes.DataSource = listPos;
  80. mapControl1.SetPosDataSource(listPos.Where(p => p.PosLon <= 180), false);
  81. }
  82. private async void CtrlHome_Load(object sender, EventArgs e)
  83. {
  84. gridView1.FocusedRowObjectChanged += GridView1_FocusedRowObjectChanged;
  85. try
  86. {
  87. List<SatInfo> satList = new List<SatInfo>();
  88. List<TxInfo> txList = new List<TxInfo>();
  89. using (RHDWContext db = new RHDWContext())
  90. {
  91. var qq = db.TaskInfos.OrderByDescending(p => p.CreateTime).ToList();
  92. list.AddRange(qq);
  93. var runningList = await db.TaskInfos.Where(p => p.TaskState == EnumTaskState.Running).ToListAsync();
  94. foreach (var item in runningList)
  95. {
  96. item.TaskState = EnumTaskState.Stopped;
  97. }
  98. await db.SaveChangesAsync();
  99. var satres = db.SatInfos.OrderBy(p => p.SatName).OrderBy(p => p.SatName);
  100. satList.AddRange(await satres.ToListAsync());
  101. var txres = await db.TxInfos.ToListAsync();
  102. txList.AddRange(txres);
  103. }
  104. foreach (var sat in satList)
  105. {
  106. if (!sat.SatLon.HasValue) continue;
  107. mapControl1.Invoke(new Action(() =>
  108. {
  109. mapControl1.DrawFixedImg("sat", 10, sat.SatLon.Value, DxHelper.SvgHelper.CreateSat(), new Size(24, 24), $"{sat.Sat}\r\n");
  110. }));
  111. }
  112. foreach (var txitem in txList)
  113. {
  114. if ((txitem.Lon >= 180 || txitem.Lon <= 180) && txitem.Lat >= -90 || txitem.Lat <= 90)//画天线
  115. {
  116. mapControl1.Invoke(new Action(() =>
  117. {
  118. switch (txitem.TxType)
  119. {
  120. #warning 天线图片需要邹大哥给
  121. case EnumTxType.MainSat:
  122. mapControl1.DrawFixedImg("MainSatTx", txitem.Lat, txitem.Lon, DxHelper.SvgHelper.CreatePentagram(), new Size(24, 24), $"[{txitem.Name}]\r\n主星天线经度:{txitem.Lon}°\r\n主星天线纬度:{txitem.Lat}°\r\n");
  123. break;
  124. case EnumTxType.AdjaSat:
  125. mapControl1.DrawFixedImg("AdjaSatTx", txitem.Lat, txitem.Lon, DxHelper.SvgHelper.CreatePentagram(), new Size(24, 24), $"[{txitem.Name}]\r\n邻星天线经度:{txitem.Lon}°\r\n邻星天线纬度:{txitem.Lat}°\r\n");
  126. break;
  127. case EnumTxType.Cdb:
  128. mapControl1.DrawFixedImg("CdbStation", txitem.Lat, txitem.Lon, DxHelper.SvgHelper.CreatePentagram(), new Size(24, 24), $"[{txitem.Name}]\r\n超短站经度:{txitem.Lon}°\r\n超短站纬度:{txitem.Lat}°\r\n");
  129. break;
  130. case EnumTxType.Cx:
  131. mapControl1.DrawFixedImg("CxStation", txitem.Lat, txitem.Lon, DxHelper.SvgHelper.CreatePentagram(), new Size(24, 24), $"[{txitem.Name}]\r\n测向站经度:{txitem.Lon}°\r\n测向站纬度:{txitem.Lat}°\r\n");
  132. break;
  133. case EnumTxType.Ref:
  134. mapControl1.DrawFixedImg("RefStation", txitem.Lat, txitem.Lon, DxHelper.SvgHelper.CreatePentagram(), new Size(24, 24), $"参考站经度:{txitem.Lon}°\r\n参考站纬度:{txitem.Lat}°\r\n");
  135. break;
  136. default:
  137. break;
  138. }
  139. }));
  140. }
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. Serilog.Log.Error(ex, "加载任务信息异常");
  146. DxHelper.MsgBoxHelper.ShowError("加载任务信息异常");
  147. }
  148. gridHomeTask.DataSource = list;
  149. gridView1.Columns["TaskName"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
  150. this.svgImageCollection1.Add("Stopped", SvgHelper.CreateCycle("#1E8B58"));
  151. this.svgImageCollection1.Add("Running", SvgHelper.CreateCycle("#2E8B57"));
  152. gridView1.CustomDrawCell += GridView1_CustomDrawCell;
  153. using (RHDWContext db = new RHDWContext())
  154. {
  155. var settings = await db.SysSetings.FirstOrDefaultAsync();
  156. if (settings == null)
  157. {
  158. DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置IP端口信息");
  159. return;
  160. }
  161. try
  162. {
  163. StartOptions options = new StartOptions();
  164. options.Urls.Add($"http://+:{settings.HttpPort}");
  165. httpServer = WebApp.Start<Startup>(options);
  166. }
  167. catch (Exception ex)
  168. {
  169. httpServer = null;
  170. Serilog.Log.Error(ex, "启动Http定位接口服务失败!");
  171. DxHelper.MsgBoxHelper.ShowWarning($"启动Http定位接口服务失败!");
  172. }
  173. }
  174. WaitHelper.CloseForm();
  175. }
  176. private void GridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
  177. {
  178. if (e.Column.FieldName == "TaskName" && (EnumTaskState)gridView1.GetRowCellValue(e.RowHandle, "TaskState") == EnumTaskState.Running)
  179. {
  180. var palette = SvgPaletteHelper.GetSvgPalette(this.LookAndFeel, ObjectState.Normal);
  181. e.Cache.DrawSvgImage(svgImageCollection1[1], new Rectangle(new Point(e.Bounds.X + 4, e.Bounds.Y + (e.Bounds.Height - 16) / 2), new Size(16, 16)), palette);
  182. Rectangle txtBounds = e.Bounds;
  183. txtBounds.Width -= 24;
  184. txtBounds.X += 24;
  185. e.Appearance.DrawString(e.Cache, e.DisplayText, txtBounds);
  186. e.Handled = true;
  187. }
  188. }
  189. //public void SetEnumColumnImage(GridView gv, string columnFieldName, object imageCollection)
  190. //{
  191. // var repositoryItemImageComboBoxStatus = new RepositoryItemImageEdit();
  192. // var col = gv.Columns[columnFieldName];
  193. // col.MaxWidth = 40;
  194. // repositoryItemImageComboBoxStatus.Images = imageCollection;
  195. // col.ColumnEdit = repositoryItemImageComboBoxStatus;
  196. //}
  197. private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
  198. {
  199. GridHitInfo hitInfo = gridView1.CalcHitInfo(e.ControlMousePosition);
  200. if (hitInfo.RowHandle < 0 || hitInfo.Column == null || hitInfo.HitTest != GridHitTest.RowCell)
  201. {
  202. toolTipController1.HideHint();
  203. return;
  204. }
  205. if (hitInfo.Column.FieldName == "TaskName")
  206. {
  207. var state = (EnumTaskState)gridView1.GetRowCellValue(hitInfo.RowHandle, "TaskState");
  208. if (state == EnumTaskState.Running)
  209. e.Info = new ToolTipControlInfo(hitInfo.RowHandle, "执行中");
  210. }
  211. }
  212. private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
  213. {
  214. if (gridView1.FocusedRowHandle < 0) return;
  215. var state = (EnumTaskState)gridView1.GetFocusedRowCellValue("TaskState");
  216. btnStart.Enabled = state == EnumTaskState.Stopped;
  217. btnStop.Enabled = state == EnumTaskState.Running;
  218. popupMenu1.ShowPopup(MousePosition);
  219. }
  220. private void gridView2_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
  221. {
  222. if (gridView2.FocusedRowHandle < 0) return;
  223. if (!gridView2.GetSelectedRows().Any())
  224. {
  225. btnDelPos.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
  226. }
  227. else
  228. {
  229. btnDelPos.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
  230. }
  231. popupMenu2.ShowPopup(MousePosition);
  232. }
  233. private async void layoutControlGroup1_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e)
  234. {
  235. var txt = (e.Button as GroupBoxButton).Caption;
  236. if (txt == "新建任务")
  237. {
  238. try
  239. {
  240. TaskEditor frm = new TaskEditor();
  241. if (frm.ShowDialog() != DialogResult.OK) return;
  242. var addItem = frm.info;
  243. using (RHDWContext db = new RHDWContext())
  244. {
  245. if (await db.TaskInfos.AnyAsync(p => p.TaskName == addItem.TaskName))
  246. {
  247. DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
  248. return;
  249. }
  250. db.TaskInfos.Add(addItem);
  251. await db.SaveChangesAsync();
  252. }
  253. list.Insert(0, addItem);
  254. gridView1.RefreshData();
  255. gridView1.FocusedRowHandle = 0;
  256. }
  257. catch (Exception ex)
  258. {
  259. Serilog.Log.Error(ex, "保存任务异常");
  260. DxHelper.MsgBoxHelper.ShowError("保存任务异常");
  261. }
  262. }
  263. else if (txt == "编辑任务")
  264. {
  265. try
  266. {
  267. var editItem = gridView1.GetFocusedRow() as TaskInfo;
  268. TaskEditor frm = new TaskEditor(editItem);
  269. if (frm.ShowDialog() != DialogResult.OK) return;
  270. editItem = frm.info;
  271. using (RHDWContext db = new RHDWContext())
  272. {
  273. if (await db.TaskInfos.AnyAsync(p => p.ID != editItem.ID && p.TaskName == editItem.TaskName))
  274. {
  275. DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
  276. return;
  277. }
  278. var find = await db.TaskInfos.Where(p => p.ID == editItem.ID).FirstOrDefaultAsync();
  279. find.TaskName = editItem.TaskName;
  280. find.PosType = editItem.PosType;
  281. await db.SaveChangesAsync();
  282. }
  283. gridView1.RefreshData();
  284. }
  285. catch (Exception ex)
  286. {
  287. Serilog.Log.Error(ex, "保存任务异常");
  288. DxHelper.MsgBoxHelper.ShowError("保存任务异常");
  289. }
  290. }
  291. else
  292. {
  293. try
  294. {
  295. var tsk = gridView1.GetFocusedRow() as TaskInfo;
  296. if (tsk == null) return;
  297. if (!DxHelper.MsgBoxHelper.ShowConfirm($"删除任务[{tsk.TaskName}]?"))
  298. return;
  299. using (RHDWContext db = new RHDWContext())
  300. {
  301. var item = db.TaskInfos.FirstOrDefault(p => p.ID == tsk.ID);
  302. db.TaskInfos.Remove(item);
  303. db.SaveChanges();
  304. }
  305. gridView1.DeleteSelectedRows();
  306. }
  307. catch (Exception ex)
  308. {
  309. Serilog.Log.Error(ex, "删除任务异常");
  310. DxHelper.MsgBoxHelper.ShowError("删除任务异常");
  311. }
  312. }
  313. }
  314. IDisposable httpServer;
  315. TcpServer server;
  316. private async void btn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  317. {
  318. var tsk = gridView1.GetFocusedRow() as TaskInfo;
  319. if (e.Item.Caption == "启动任务")
  320. {
  321. try
  322. {
  323. using (RHDWContext db = new RHDWContext())
  324. {
  325. var find = await db.TaskInfos.FirstOrDefaultAsync(p => p.TaskState == EnumTaskState.Running && p.PosType == tsk.PosType);
  326. if (find != null)
  327. {
  328. var field = typeof(EnumPosType).GetField(Enum.GetName(typeof(EnumPosType), find.PosType));
  329. DxHelper.MsgBoxHelper.ShowWarning($"已经有一个[{field.GetCustomAttribute<DisplayAttribute>().Name}]任务正在执行,请先停止!");
  330. return;
  331. }
  332. var settings = await db.SysSetings.FirstOrDefaultAsync();
  333. if (settings == null)
  334. {
  335. DxHelper.MsgBoxHelper.ShowWarning($"请在系统设置中配置IP端口信息");
  336. return;
  337. }
  338. if (server == null)
  339. {
  340. server = new TcpServer();
  341. server.OnDataReceived += this.Server_OnDataReceived;
  342. server.Start(settings.Port);
  343. }
  344. if (httpServer == null)
  345. {
  346. try
  347. {
  348. StartOptions options = new StartOptions();
  349. options.Urls.Add($"http://+:{settings.HttpPort}");
  350. httpServer = WebApp.Start<Startup>(options);
  351. }
  352. catch (Exception ex)
  353. {
  354. Serilog.Log.Error(ex, "启动任务异常,Http定位接口服务启动失败");
  355. DxHelper.MsgBoxHelper.ShowError($"启动任务异常,Http定位接口服务启动失败");
  356. }
  357. }
  358. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == tsk.ID);
  359. item.TaskState = EnumTaskState.Running;
  360. await db.SaveChangesAsync();
  361. tsk.TaskState = EnumTaskState.Running;
  362. Serilog.Log.Information($"用户启动了任务,ID={tsk.ID}");
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. Serilog.Log.Error(ex, "启动任务异常");
  368. DxHelper.MsgBoxHelper.ShowError($"启动任务异常.{ex.Message}");
  369. }
  370. }
  371. else
  372. {
  373. try
  374. {
  375. using (RHDWContext db = new RHDWContext())
  376. {
  377. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == tsk.ID);
  378. item.TaskState = EnumTaskState.Stopped;
  379. await db.SaveChangesAsync();
  380. if (!db.TaskInfos.Any(p => p.TaskState == EnumTaskState.Running))
  381. {
  382. server?.StopListening();
  383. server = null;
  384. }
  385. }
  386. tsk.TaskState = EnumTaskState.Stopped;
  387. Serilog.Log.Information($"用户停止了任务,ID={tsk.ID}");
  388. }
  389. catch (Exception ex)
  390. {
  391. Serilog.Log.Error(ex, "停止任务异常");
  392. DxHelper.MsgBoxHelper.ShowError($"停止任务异常.{ex.Message}");
  393. }
  394. }
  395. gridView1.RefreshData();
  396. var btnEdit = (layoutControlGroup1.CustomHeaderButtons[1] as GroupBoxButton);
  397. var btnDel = (layoutControlGroup1.CustomHeaderButtons[2] as GroupBoxButton);
  398. btnDel.Enabled = btnEdit.Enabled = tsk.TaskState != EnumTaskState.Running;
  399. }
  400. private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
  401. {
  402. if (gridView1.RowCount == 0)
  403. {
  404. string txt = "点击+创建任务";
  405. var s = e.Appearance.CalcTextSize(e.Cache, txt, e.Bounds.Width).ToSize();
  406. var x = (e.Bounds.Width - s.Width) / 2;
  407. var y = (e.Bounds.Height - s.Height) / 2;
  408. //e.Appearance.DrawString(e.Cache, txt, new Rectangle(x,y,s.Width,s.Height));
  409. e.DrawHtml(htmlTemplate1);
  410. }
  411. }
  412. readonly Random r = new Random();
  413. private async void Server_OnDataReceived(byte[] data)
  414. {
  415. try
  416. {
  417. var msg = Encoding.UTF8.GetString(data);
  418. var dto = Newtonsoft.Json.JsonConvert.DeserializeObject<SendDto>(msg);
  419. if (dto == null) return;
  420. var sigTime = DateTime.Now;
  421. using (RHDWContext db = new RHDWContext())
  422. {
  423. var cgRes = db.CgRes.Add(new CgRes()
  424. {
  425. SigTime = sigTime,
  426. DtoSx = dto.SxDto * 1e6,
  427. DfoSx = 200 + r.Next(1, 5) + Math.Round(r.NextDouble(), 3),
  428. SnrSx = r.Next(18, 24) + Math.Round(r.NextDouble(), 1),
  429. DtoCdb = dto.XdDto * 1e6,
  430. DfoCdb = 600 + r.Next(1, 5) + Math.Round(r.NextDouble(), 3),
  431. SnrCdb = r.Next(22, 32) + Math.Round(r.NextDouble(), 1),
  432. YbMain = dto.MainYbDto * 1e6,
  433. YbAdja = dto.AdjaYbDto * 1e6,
  434. MainX = dto.MainX,
  435. MainY = dto.MainY,
  436. MainZ = dto.MainZ,
  437. AdjaX = dto.AdjaX,
  438. AdjaY = dto.AdjaY,
  439. AdjaZ = dto.AdjaZ,
  440. });
  441. var listTx = await db.TxInfos.ToListAsync();
  442. var cxRes = db.CxRes.Add(new CxRes()
  443. {
  444. SigTime = sigTime,
  445. Fx = dto.CxRes,
  446. });
  447. var tsk1X1D = await db.TaskInfos.FirstOrDefaultAsync(p => p.PosType == EnumPosType.X1D1CX && p.TaskState == EnumTaskState.Running);
  448. var tsk2X1D = await db.TaskInfos.FirstOrDefaultAsync(p => p.PosType == EnumPosType.X2D1 && p.TaskState == EnumTaskState.Running);
  449. var tskRh = await db.TaskInfos.FirstOrDefaultAsync(p => p.PosType == EnumPosType.RH && p.TaskState == EnumTaskState.Running);
  450. if (tsk1X1D != null)
  451. {
  452. var res = PosApi.X1D1_POS(cgRes, listTx, cxRes);
  453. PosRes posRes = new PosRes()
  454. {
  455. SigTime = sigTime,
  456. TaskID = tsk1X1D.ID,
  457. CxResID = cxRes.ID,
  458. CgResID = cgRes.ID,
  459. TarName = "未知目标",
  460. TsName = "DAMA--56-115ms",
  461. PosLon = res[0],
  462. PosLat = res[1],
  463. MirrLon = res[3],
  464. MirrLat = res[4],
  465. };
  466. db.PosRes.Add(posRes);
  467. var currentTask = gridView1.GetFocusedRow() as TaskInfo;
  468. if (currentTask != null && currentTask.ID == tsk1X1D.ID)
  469. {
  470. var ds = this.gridHomePosRes.DataSource as List<PosRes>;
  471. ds.Insert(0, posRes);
  472. this.Invoke(new Action(() =>
  473. {
  474. gridView2.RefreshData();
  475. mapControl1.AddPosItem(posRes);
  476. }));
  477. }
  478. }
  479. if (tsk2X1D != null)
  480. {
  481. var res = PosApi.X2D1_POS(cgRes, listTx);
  482. PosRes posRes = new PosRes()
  483. {
  484. SigTime = sigTime,
  485. TaskID = tsk2X1D.ID,
  486. CxResID = cxRes.ID,
  487. CgResID = cgRes.ID,
  488. TarName = "未知目标",
  489. TsName = "DAMA-225-173ms",
  490. PosLon = res[0],
  491. PosLat = res[1],
  492. MirrLon = res[3],
  493. MirrLat = res[4],
  494. };
  495. db.PosRes.Add(posRes);
  496. var currentTask = gridView1.GetFocusedRow() as TaskInfo;
  497. if (currentTask.ID == tsk2X1D.ID)
  498. {
  499. var ds = this.gridHomePosRes.DataSource as List<PosRes>;
  500. ds.Insert(0, posRes);
  501. this.Invoke(new Action(() =>
  502. {
  503. gridView2.RefreshData();
  504. mapControl1.AddPosItem(posRes);
  505. }));
  506. }
  507. }
  508. if (tskRh != null)
  509. {
  510. var res = PosApi.RH_POS(cgRes, listTx, cxRes);
  511. PosRes posRes = new PosRes()
  512. {
  513. SigTime = sigTime,
  514. TaskID = tskRh.ID,
  515. CxResID = cxRes.ID,
  516. CgResID = cgRes.ID,
  517. TarName = "未知目标",
  518. TsName = "DAMA-738-109ms",
  519. PosLon = res[0],
  520. PosLat = res[1],
  521. MirrLon = res[3],
  522. MirrLat = res[4],
  523. };
  524. db.PosRes.Add(posRes);
  525. var currentTask = gridView1.GetFocusedRow() as TaskInfo;
  526. if (currentTask.ID == tskRh.ID)
  527. {
  528. var ds = this.gridHomePosRes.DataSource as List<PosRes>;
  529. ds.Insert(0, posRes);
  530. this.Invoke(new Action(() =>
  531. {
  532. gridView2.RefreshData();
  533. mapControl1.AddPosItem(posRes);
  534. }));
  535. }
  536. }
  537. await db.SaveChangesAsync();
  538. }
  539. }
  540. catch (Exception ex)
  541. {
  542. Serilog.Log.Error(ex, "接收到推送消息后处理异常");
  543. }
  544. }
  545. private async void btnDelPos_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  546. {
  547. try
  548. {
  549. var ids = gridView2.GetSelectedRows();
  550. using (RHDWContext db = new RHDWContext())
  551. {
  552. foreach (var id in ids)
  553. {
  554. var item = gridView2.GetRow(id) as PosRes;
  555. var delItem = await db.PosRes.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  556. if (delItem != null)
  557. {
  558. db.PosRes.Remove(delItem);
  559. mapControl1.DelPosItem<PosRes>(item);
  560. }
  561. }
  562. await db.SaveChangesAsync();
  563. }
  564. gridView2.DeleteSelectedRows();
  565. }
  566. catch (Exception ex)
  567. {
  568. Serilog.Log.Error(ex, "删除定位结果异常");
  569. DxHelper.MsgBoxHelper.ShowError("删除定位结果异常");
  570. }
  571. }
  572. private void gridView2_RowClick(object sender, RowClickEventArgs e)
  573. {
  574. var p = gridView2.GetFocusedRow() as PosRes;
  575. p.Selected = true;
  576. mapControl1.UpdatePosItem(p);
  577. }
  578. private async void btnTar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  579. {
  580. try
  581. {
  582. var ids = gridView2.GetSelectedRows();
  583. if (ids.Length <= 0) return;
  584. var fisrtitem = gridView2.GetRow(ids[0]) as PosRes;
  585. EditorTar frm = new EditorTar(new TargetInfo() { ID = fisrtitem.TargetID.HasValue ? fisrtitem.TargetID.Value : 0, TargetName = fisrtitem.TarName });
  586. if (frm.ShowDialog() != DialogResult.OK) return;
  587. var tar = frm.info;
  588. using (RHDWContext db = new RHDWContext())
  589. {
  590. foreach (var id in ids)
  591. {
  592. var item = gridView2.GetRow(id) as PosRes;
  593. var tarItem = await db.PosRes.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  594. if (tarItem != null)
  595. {
  596. tarItem.TargetID = tar.ID;
  597. tarItem.TarName = tar.TargetName;
  598. }
  599. }
  600. await db.SaveChangesAsync();
  601. }
  602. this.Invoke(new Action(() =>
  603. {
  604. gridView2.RefreshData();
  605. }));
  606. }
  607. catch (Exception ex)
  608. {
  609. Serilog.Log.Error(ex, "编辑定位目标异常");
  610. DxHelper.MsgBoxHelper.ShowError("编辑定位目标异常");
  611. }
  612. }
  613. }
  614. }