FormMonitor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using DevExpress.XtraEditors;
  6. using DevExpress.Utils;
  7. using SIASUN.Autopilot.GNSS;
  8. using SIASUN.Autopilot.Model;
  9. using ProjectBase.Data.BaseDAL;
  10. using SIASUN.Autopilot.BLL;
  11. using ProjectBase.Data.Redis;
  12. using ProjectBase.Util;
  13. using ProjectBase.Data.Logs;
  14. namespace SIASUN.Autopilot.View
  15. {
  16. public partial class FormMonitor : DevExpress.XtraEditors.XtraForm
  17. {
  18. #region 私有变量
  19. /// <summary>
  20. /// 鼠标位置
  21. /// </summary>
  22. private Point mousePoint = new Point(); //鼠标位置
  23. ///// <summary>
  24. ///// 图片宽度
  25. ///// </summary>
  26. //private int ImageWidth;
  27. ///// <summary>
  28. ///// 图片高度
  29. ///// </summary>
  30. //private int ImageHeight;
  31. /// <summary>
  32. /// 坐标与经纬度转换对象
  33. /// </summary>
  34. private CoordinateConversion conversion;
  35. /// <summary>
  36. /// 行驶过程中的经纬度
  37. /// </summary>
  38. private List<PointF> pointDriving = new List<PointF>();
  39. /// <summary>
  40. /// 行驶过程中航向角度
  41. /// </summary>
  42. private List<float> angleDriving = new List<float>();
  43. /// <summary>
  44. /// 线的宽度
  45. /// </summary>
  46. private const int lineWidth = 3; //线的宽度
  47. /// <summary>
  48. /// 下拉框选择路径的所有点
  49. /// </summary>
  50. private List<MoLocationRuleInfo> locationList = new List<MoLocationRuleInfo>(); //下拉框选择路径
  51. private Status status = Status.Stop;
  52. private delegate void startPointMesgDelegate();
  53. BlDecisionControler control = new BlDecisionControler();
  54. ///// <summary>
  55. ///// 状态
  56. ///// </summary>
  57. private enum Status
  58. {
  59. Play,
  60. Stop,
  61. }
  62. RedisHelper redis = new RedisHelper();
  63. #endregion
  64. public FormMonitor()
  65. {
  66. InitializeComponent();
  67. //BeckhoffOpcHelper bfOpc = new BeckhoffOpcHelper();
  68. //bfOpc.Opc_Init();
  69. }
  70. private void FormDeviceManagement_Load(object sender, EventArgs e)
  71. {
  72. InitControls();
  73. MoLocationMap map = BLLFactory<BlLocationMap>.Instance.FindSingle(string.Format("map_id = '{0}'", System.Configuration.ConfigurationManager.AppSettings["MapId"]));
  74. WGS84Points point0 = new WGS84Points { x = map.LocationLon1,y = map.LocationLat1,z = 0};
  75. WGS84Points point1 = new WGS84Points { x = map.LocationLon2,y = map.LocationLat2,z = 0};
  76. PointF imagePoint0 = new PointF { X = map.LocationX1, Y = map.LocationY1};
  77. PointF imagePoint1 = new PointF { X = map.LocationX2, Y = map.LocationY2};
  78. conversion = new CoordinateConversion(imagePoint0, imagePoint1, point0, point1);
  79. BlGNSSPerception.Instance.Init();
  80. BlGNSSPerception.Instance.GNSSHandler += GetDrivingInfo;
  81. control.StartPointHandler += StartPointMessage;
  82. redis.SetString(SysEnvironment.Start,"1");
  83. }
  84. /// <summary>
  85. /// 初始化控件
  86. /// </summary>
  87. private void InitControls()
  88. {
  89. //测试状态使用,正式使用时需注释掉
  90. this.layoutControlGroup2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  91. this.layoutControlGroup3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  92. this.layoutControlGroup4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  93. //地图控件
  94. this.picExControl1.SetImage(global::SIASUN.Autopilot.View.Properties.Resources.地图1, true);
  95. //ImageWidth = global::SIASUN.Autopilot.View.Properties.Resources.地图1.Width;
  96. //ImageHeight = global::SIASUN.Autopilot.View.Properties.Resources.地图1.Height;
  97. this.picExControl1.MouseMoveTooltipEvent += new PicExControl.MouseMoveTooltip(picExControl1_MouseMove);
  98. this.picExControl1.DrawOriginalPathEvent += new PicExControl.DrawPath(DrawOriginalPath);
  99. this.picExControl1.DrawDrivingPathEvent += new PicExControl.DrawPath(DrawDrivingPath);
  100. //下拉框
  101. this.lookUp_Path.Properties.DataSource = BLLFactory<BlLocationRuleInfo>.Instance.SqlTable("SELECT rule_name FROM location_rule_info GROUP BY rule_name");
  102. this.lookUp_Path.Properties.ValueMember = "RULE_NAME";
  103. this.lookUp_Path.Properties.DisplayMember = "RULE_NAME";
  104. this.lookUp_Path.Properties.NullText = null;
  105. this.btn_Stop.Enabled = false;
  106. }
  107. /// <summary>
  108. /// 鼠标移动显示经纬度
  109. /// </summary>
  110. /// <param name="e"></param>
  111. private void picExControl1_MouseMove(MouseEventArgs e)
  112. {
  113. if (mousePoint.X != e.X || mousePoint.Y != e.Y)
  114. {
  115. ToolTipControllerShowEventArgs args = this.toolTip_Message.CreateShowArgs();
  116. //this.toolTip_Message.SetToolTip(this.picExControl1, string.Format("横坐标:{0}\n纵坐标:{1}", e.X.ToString(), e.Y.ToString()));
  117. //this.toolTip_Message.SetTitle(this.picExControl1,"提示");
  118. //this.toolTip_Message.SetToolTipIconType(this.picExControl1,ToolTipIconType.Exclamation);
  119. double lon;
  120. double lat;
  121. conversion.GetGaussToWGS84((e.X - this.picExControl1.StartPoint.X) / this.picExControl1.Wrate, (e.Y - this.picExControl1.StartPoint.Y) / this.picExControl1.Hrate, out lon,out lat);
  122. args.ToolTip = string.Format("经度:{0}\n纬度:{1}", lon.ToString(), lat.ToString());
  123. args.SelectedControl = this.picExControl1;
  124. //args.Title = "提示";
  125. this.toolTip_Message.ShowHint(args, System.Windows.Forms.Control.MousePosition);
  126. mousePoint.X = e.X;
  127. mousePoint.Y = e.Y;
  128. }
  129. else
  130. {
  131. //this.toolTip1.Hide(this.pictureBox1);
  132. mousePoint.X = e.X;
  133. mousePoint.Y = e.Y;
  134. }
  135. }
  136. /// <summary>
  137. /// 指令信息表格
  138. /// </summary>
  139. private void InitGridDirective()
  140. {
  141. if (this.lookUp_Path.EditValue != null)
  142. {
  143. this.gridControl__Directive.DataSource = BLLFactory<BlActionDirective>.Instance.Find(string.Format("rule_name = '{0}'",SysEnvironment.CurrentRuleName));
  144. this.gridControl__Directive.RefreshDataSource();
  145. }
  146. }
  147. /// <summary>
  148. /// 指令信息表格——单元格样式
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void gridView__Directive_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
  153. {
  154. if (e.Column.FieldName == "DirectiveResult")
  155. {
  156. Color color = Color.White;
  157. if (Convert.ToInt32(e.CellValue) == 1)
  158. {
  159. e.Appearance.BackColor = Color.Green;
  160. e.Appearance.BackColor2 = Color.LightCyan;
  161. }
  162. else if (Convert.ToInt32(e.CellValue) == 0)
  163. {
  164. e.Appearance.BackColor = Color.Red;
  165. e.Appearance.BackColor2 = Color.LightCyan;
  166. }
  167. }
  168. }
  169. private void picExControl1_MouseLeave(object sender, EventArgs e)
  170. {
  171. this.toolTip_Message.HideHint();
  172. }
  173. /// <summary>
  174. /// 画行驶路径
  175. /// </summary>
  176. /// <param name="g"></param>
  177. private void DrawDrivingPath(Graphics g)
  178. {
  179. if (pointDriving.Count < 1 || angleDriving.Count < 1)
  180. {
  181. return;
  182. }
  183. double x;
  184. double y;
  185. if (redis.GetString(SysEnvironment.Start).Equals("1")) //车辆未到达起始点
  186. {
  187. conversion.GetWGS84ToGauss(pointDriving[pointDriving.Count -1].X, pointDriving[pointDriving.Count - 1].Y, out x, out y);
  188. g.TranslateTransform((float)x, (float)y);
  189. g.RotateTransform(angleDriving[angleDriving.Count -1]);
  190. g.DrawImage(global::SIASUN.Autopilot.View.Properties.Resources.汽车2, -7, -15, 14, 30);
  191. return;
  192. }
  193. //车辆到达起始点
  194. if (pointDriving.Count < 2)
  195. {
  196. conversion.GetWGS84ToGauss(pointDriving[0].X, pointDriving[0].Y, out x, out y);
  197. g.TranslateTransform((float)x, (float)y);
  198. g.RotateTransform(angleDriving[0]);
  199. g.DrawImage(global::SIASUN.Autopilot.View.Properties.Resources.汽车2, -7, -15, 14, 30);
  200. }
  201. else
  202. {
  203. List<PointF> list = new List<PointF>();
  204. for (int i = 0; i < pointDriving.Count; i++)
  205. {
  206. conversion.GetWGS84ToGauss(pointDriving[i].X, pointDriving[i].Y, out x, out y);
  207. list.Add(new PointF((float)(x * this.picExControl1.Wrate) + this.picExControl1.StartPoint.X, (float)(y * this.picExControl1.Hrate) + this.picExControl1.StartPoint.Y));
  208. }
  209. g.DrawCurve(new Pen(Color.Blue, lineWidth), list.ToArray());
  210. g.TranslateTransform(list[list.Count - 1].X, list[list.Count - 1].Y);
  211. g.RotateTransform(angleDriving[angleDriving.Count - 1]);
  212. g.DrawImage(global::SIASUN.Autopilot.View.Properties.Resources.汽车2, -7, -15, 14, 30);
  213. }
  214. }
  215. /// <summary>
  216. /// 获取行驶中经纬度和角度值
  217. /// </summary>
  218. private void GetDrivingInfo(object sender,GNSSInfoEventArgs gnss)
  219. {
  220. pointDriving.Add(new PointF { X = (float)gnss.Nmea.gprmc.Longitude, Y = (float)gnss.Nmea.gprmc.Latitude });
  221. angleDriving.Add((float)gnss.Nmea.gprmc.Deviated);
  222. this.picExControl1.Invalidate();
  223. }
  224. /// <summary>
  225. /// 画原始路径
  226. /// </summary>
  227. /// <param name="g"></param>
  228. private void DrawOriginalPath(Graphics g)
  229. {
  230. try
  231. {
  232. if (locationList.Count <= 0)
  233. {
  234. return;
  235. }
  236. List<PointF> list = new List<PointF>();
  237. double x;
  238. double y;
  239. foreach (MoLocationRuleInfo item in locationList)
  240. {
  241. conversion.GetWGS84ToGauss(item.LocationLon, item.LocationLat, out x, out y);
  242. list.Add(new PointF((float)(x * this.picExControl1.Wrate) + this.picExControl1.StartPoint.X, (float)(y * this.picExControl1.Hrate) + this.picExControl1.StartPoint.Y));
  243. }
  244. g.DrawCurve(new Pen(Color.Red, lineWidth), list.ToArray());
  245. g.DrawImage(global::SIASUN.Autopilot.View.Properties.Resources.绿色, list[0].X, list[0].Y, 23, 31);
  246. g.DrawImage(global::SIASUN.Autopilot.View.Properties.Resources.红色, list[list.Count - 1].X, list[list.Count - 1].Y, 23, 31);
  247. }
  248. catch (Exception ex)
  249. {
  250. DevExpress.XtraEditors.XtraMessageBox.Show("绘制路径失败。" + ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  251. LogHelper.log.Error(string.Format("绘制路径失败:{0}", ex.ToString()));
  252. }
  253. }
  254. /// <summary>
  255. /// 下拉框选中项改变事件
  256. /// </summary>
  257. /// <param name="sender"></param>
  258. /// <param name="e"></param>
  259. private void lookUp_Path_EditValueChanged(object sender, EventArgs e)
  260. {
  261. try
  262. {
  263. if (this.lookUp_Path.EditValue == null)
  264. {
  265. locationList = new List<MoLocationRuleInfo>();
  266. }
  267. else
  268. {
  269. SysEnvironment.CurrentRuleName = this.lookUp_Path.EditValue.ToString().Trim();
  270. string condition = string.Format("rule_name = '{0}'", this.lookUp_Path.EditValue.ToString());
  271. locationList = BLLFactory<BlLocationRuleInfo>.Instance.Find(condition);
  272. control.InitPlanRoute(); ;
  273. }
  274. this.picExControl1.Invalidate();
  275. InitGridDirective();
  276. }
  277. catch (Exception ex)
  278. {
  279. DevExpress.XtraEditors.XtraMessageBox.Show("获取路径失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  280. LogHelper.log.Error(string.Format("获取路径失败:{0}", ex.ToString()));
  281. }
  282. }
  283. /// <summary>
  284. /// 下拉框按钮点击事件
  285. /// </summary>
  286. /// <param name="sender"></param>
  287. /// <param name="e"></param>
  288. private void lookUp_Path_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  289. {
  290. if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
  291. {
  292. LookUpEdit lookUp = sender as LookUpEdit;
  293. lookUp.EditValue = null;
  294. }
  295. }
  296. /// <summary>
  297. /// 停止按钮事件
  298. /// </summary>
  299. /// <param name="sender"></param>
  300. /// <param name="e"></param>
  301. private void btn_Stop_Click(object sender, EventArgs e)
  302. {
  303. if (status != Status.Play)
  304. {
  305. return;
  306. }
  307. //redis.SetString(SysEnvironment.Start, "0");
  308. this.btn_Stop.Enabled = false;
  309. this.btn_Action.Enabled = true;
  310. status = Status.Stop;
  311. pointDriving.Clear();
  312. angleDriving.Clear();
  313. }
  314. /// <summary>
  315. /// 窗口关闭事件
  316. /// </summary>
  317. /// <param name="sender"></param>
  318. /// <param name="e"></param>
  319. private void FormDeviceManagement_FormClosing(object sender, FormClosingEventArgs e)
  320. {
  321. BlGNSSPerception.Instance.Close();
  322. }
  323. /// <summary>
  324. /// 开始按钮事件
  325. /// </summary>
  326. /// <param name="sender"></param>
  327. /// <param name="e"></param>
  328. private void btn_Action_Click(object sender, EventArgs e)
  329. {
  330. if (status != Status.Stop)
  331. {
  332. return;
  333. }
  334. if (this.lookUp_Path.EditValue == null)
  335. {
  336. DevExpress.XtraEditors.XtraMessageBox.Show("请选择路径之后再开始测试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  337. return;
  338. }
  339. //redis.SetString(SysEnvironment.Start,"1");
  340. this.btn_Action.Enabled = false;
  341. status = Status.Play;
  342. this.btn_Stop.Enabled = true;
  343. pointDriving.Clear();
  344. angleDriving.Clear();
  345. }
  346. /// <summary>
  347. /// 指令信息表格——显示文字
  348. /// </summary>
  349. /// <param name="sender"></param>
  350. /// <param name="e"></param>
  351. private void gridView__Directive_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  352. {
  353. if (e.Column.FieldName == "DirectiveResult")
  354. {
  355. if (Convert.ToInt32(e.Value) == 0)
  356. {
  357. e.DisplayText = "失败";
  358. }
  359. else if (Convert.ToInt32(e.Value) == 1)
  360. {
  361. e.DisplayText = "成功";
  362. }
  363. else if (Convert.ToInt32(e.Value) == 2)
  364. {
  365. e.DisplayText = "未执行";
  366. }
  367. }
  368. }
  369. //private void InitGridControl()
  370. //{
  371. // //OpcFactory.Instance.opcHelper.GetOpcRecv();
  372. // EventMessages.Instance.newMessages += Instance_newMessages;
  373. //}
  374. //private void Instance_newMessages(object sender, EventMessage.MessageEventArgs e)
  375. //{
  376. // throw new NotImplementedException();
  377. //}
  378. /// <summary>
  379. /// 响应起始点正确事件的函数
  380. /// </summary>
  381. /// <param name="e"></param>
  382. private void StartPointMessage(object sender, StartPointEventArgs e)
  383. {
  384. DevExpress.XtraEditors.XtraMessageBox.Show(e.Mesg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  385. //control.StartPointHandler -= StartPointMessage;
  386. ////BlGNSSPerception.Instance.Close();
  387. //if (this.btn_Stop.InvokeRequired)
  388. //{
  389. // startPointMesgDelegate mesgDelegate = new startPointMesgDelegate(StopCommunicate);
  390. // this.btn_Stop.BeginInvoke(mesgDelegate, null);
  391. //}
  392. }
  393. /// <summary>
  394. /// 放大按钮
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. private void btn_Big_Click(object sender, EventArgs e)
  399. {
  400. float rate = 1;
  401. if (Math.Max(this.picExControl1.Wrate, this.picExControl1.Hrate) <= 10)
  402. {
  403. rate = 1.15F;
  404. }
  405. Zoom(rate);
  406. }
  407. /// <summary>
  408. /// 缩小按钮
  409. /// </summary>
  410. /// <param name="sender"></param>
  411. /// <param name="e"></param>
  412. private void btn_Small_Click(object sender, EventArgs e)
  413. {
  414. float rate = 1;
  415. if (Math.Min(this.picExControl1.Wrate, this.picExControl1.Hrate) > Math.Max(0.75687, 0.75687))
  416. {
  417. rate = 0.85F;
  418. }
  419. Zoom(rate);
  420. }
  421. /// <summary>
  422. /// 地图缩放
  423. /// </summary>
  424. /// <param name="rate"></param>
  425. private void Zoom(float rate)
  426. {
  427. if (rate == 1) return;
  428. float imageX = (356F - this.picExControl1.StartPoint.X) / this.picExControl1.Wrate;
  429. float imageY = (279F - this.picExControl1.StartPoint.Y) / this.picExControl1.Hrate;
  430. this.picExControl1.Hrate *= rate;
  431. this.picExControl1.Wrate *= rate;
  432. this.picExControl1.StartPoint = new PointF(356F - imageX * this.picExControl1.Wrate, 279F - imageY * this.picExControl1.Hrate);
  433. this.picExControl1.Invalidate();
  434. }
  435. }
  436. }