No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MonitorUI.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. //
  2. // MonitorUI.cs
  3. //
  4. // Author:
  5. // Di MERCURIO Sébastien <dimercur@insa-toulouse.fr>
  6. //
  7. // Copyright (c) 2018 INSA - DGEI
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. // 15/01/2019 dimercur
  22. // Demande #41: Modifier les messages envoyés par les flèches de direction
  23. // 15/10/2019 dimercur
  24. // Demande #43: Migrer le code lié à la gestion des images dans sa propre classe widget
  25. // 11/04/2019 dimercur
  26. // Suppression du timer battery
  27. // suppression de la case à cocher getbattery
  28. // Prise en charge des messages ANSWER_TIMEOUT et ANSWER_COM_ERROR dans OnCommandReceivedEvent
  29. using System;
  30. using Gtk;
  31. using Gdk;
  32. using Cairo;
  33. using monitor;
  34. using System.Timers;
  35. /// <summary>
  36. /// Main part of the program, behavior of main window
  37. /// </summary>
  38. public partial class MainWindow : Gtk.Window
  39. {
  40. /// <summary>
  41. /// Destijl command manager reference
  42. /// </summary>
  43. private DestijlCommandManager cmdManager;
  44. /// <summary>
  45. /// Position used for displaying position
  46. /// </summary>
  47. private DestijlCommandManager.Position position=new DestijlCommandManager.Position();
  48. /// <summary>
  49. /// List of availble state for the application
  50. /// </summary>
  51. enum SystemState
  52. {
  53. NotConnected,
  54. ServerConnected,
  55. RobotConnected
  56. };
  57. /// <summary>
  58. /// The state of the system. Can take a value from SystemState
  59. /// </summary>
  60. private SystemState systemState = SystemState.NotConnected;
  61. /// <summary>
  62. /// Object used for displaying image on a valid DrawingArea widget
  63. /// </summary>
  64. private ImageWidget imageWidget;
  65. /// <summary>
  66. /// Counter for image reception and detecting bad picture ratio
  67. /// </summary>
  68. private int imageReceivedCounter = 0;
  69. private int badImageReceivedCounter = 0;
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="MainWindow"/> class.
  72. /// </summary>
  73. public MainWindow() : base(Gtk.WindowType.Toplevel)
  74. {
  75. Build();
  76. cmdManager = new DestijlCommandManager(OnCommandReceivedEvent);
  77. // Init of image widget
  78. imageWidget = new ImageWidget(drawingAreaCamera);
  79. // Customize controls
  80. AdjustControls();
  81. }
  82. /// <summary>
  83. /// Make some adjustement to controls, like disabling some controls
  84. /// </summary>
  85. public void AdjustControls()
  86. {
  87. // Change state of system, and grey every controls not needed
  88. ChangeState(SystemState.NotConnected);
  89. // Load "no picture" image from disque
  90. imageWidget.ShowImage("monitor.ressources.missing_picture.png");
  91. // setup server controls
  92. entryServerName.Text = Client.defaultIP;
  93. entryServerPort.Text = Client.defaultPort.ToString();
  94. entryTimeout.Text = "1000";
  95. }
  96. /// <summary>
  97. /// Method used to change controls visibility (greyed or not) depending on current state
  98. /// </summary>
  99. /// <param name="newState">New state</param>
  100. private void ChangeState(SystemState newState)
  101. {
  102. switch (newState)
  103. {
  104. case SystemState.NotConnected:
  105. labelRobot.Sensitive = false;
  106. gtkAlignmentRobot.Sensitive = false;
  107. labelRobotControl.Sensitive = false;
  108. gtkAlignmentRobotControl.Sensitive = false;
  109. boxCamera.Sensitive = false;
  110. buttonServerConnection.Label = "Connect";
  111. buttonRobotActivation.Label = "Activate";
  112. labelBatteryLevel.Text = "Unknown";
  113. checkButtonCameraOn.Active = false;
  114. checkButtonRobotPosition.Active = false;
  115. checkButtonFPS.Active = false;
  116. imageWidget.ShowFPS = false;
  117. imageWidget.showPosition = false;
  118. if (cmdManager != null) cmdManager.Close();
  119. break;
  120. case SystemState.ServerConnected:
  121. buttonServerConnection.Label = "Disconnect";
  122. buttonRobotActivation.Label = "Activate";
  123. labelBatteryLevel.Text = "Unknown";
  124. labelRobot.Sensitive = true;
  125. gtkAlignmentRobot.Sensitive = true;
  126. boxCamera.Sensitive = true;
  127. labelRobotControl.Sensitive = false;
  128. gtkAlignmentRobotControl.Sensitive = false;
  129. break;
  130. case SystemState.RobotConnected:
  131. buttonRobotActivation.Label = "Reset";
  132. labelRobotControl.Sensitive = true;
  133. gtkAlignmentRobotControl.Sensitive = true;
  134. break;
  135. default:
  136. labelRobot.Sensitive = false;
  137. gtkAlignmentRobot.Sensitive = false;
  138. labelRobotControl.Sensitive = false;
  139. gtkAlignmentRobotControl.Sensitive = false;
  140. boxCamera.Sensitive = false;
  141. buttonServerConnection.Label = "Connect";
  142. buttonRobotActivation.Label = "Activate";
  143. labelBatteryLevel.Text = "Unknown";
  144. checkButtonCameraOn.Active = false;
  145. checkButtonRobotPosition.Active = false;
  146. checkButtonFPS.Active = false;
  147. imageWidget.ShowFPS = false;
  148. imageWidget.showPosition = false;
  149. systemState = SystemState.NotConnected;
  150. return;
  151. }
  152. systemState = newState;
  153. }
  154. /// <summary>
  155. /// Display a popup message window
  156. /// </summary>
  157. /// <param name="type">Type of popup window (question, error, information,...)</param>
  158. /// <param name="buttons">Buttons available on popup window</param>
  159. /// <param name="title">Title of window</param>
  160. /// <param name="message">Message</param>
  161. private void MessagePopup(MessageType type, ButtonsType buttons, string title, string message)
  162. {
  163. MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, type, buttons, message);
  164. md.Title = title;
  165. md.Run();
  166. md.Destroy();
  167. }
  168. /// <summary>
  169. /// Callback called when delete event is sent by window
  170. /// </summary>
  171. /// <param name="sender">Sender object</param>
  172. /// <param name="a">Not really sure of what it is...</param>
  173. protected void OnDeleteEvent(object sender, DeleteEventArgs a)
  174. {
  175. Console.WriteLine("Bye bye");
  176. if (cmdManager != null) cmdManager.Close();
  177. Application.Quit();
  178. a.RetVal = true;
  179. }
  180. /// <summary>
  181. /// Callback called when new message is received from server
  182. /// </summary>
  183. /// <param name="header">Header of message</param>
  184. /// <param name="data">Data of message</param>
  185. public void OnCommandReceivedEvent(string header, string data)
  186. {
  187. if (header == null)
  188. {
  189. // we have lost server
  190. ChangeState(SystemState.NotConnected);
  191. Gtk.Application.Invoke(delegate
  192. {
  193. MessagePopup(MessageType.Error,
  194. ButtonsType.Ok, "Server lost",
  195. "Server is down: disconnecting");
  196. cmdManager.Close();
  197. });
  198. }
  199. // if we have received a valid message
  200. if (header != null)
  201. {
  202. #if DEBUG
  203. // print message content
  204. if (header.Length > 4)
  205. {
  206. Console.WriteLine("Bad header(" + header.Length + ")");
  207. }
  208. #endif
  209. // Depending on message received (based on header), launch correponding action
  210. header = header.ToUpper();
  211. switch (header)
  212. {
  213. case DestijlCommandList.ANSWER_TIMEOUT:
  214. case DestijlCommandList.ANSWER_COM_ERROR:
  215. Console.WriteLine("Communication lost with robot");
  216. Gtk.Application.Invoke(delegate
  217. {
  218. MessagePopup(MessageType.Error, ButtonsType.Ok, "Robot lost", "Communication with robot lost !");
  219. });
  220. ChangeState(SystemState.ServerConnected);
  221. break;
  222. case DestijlCommandList.ROBOT_BATTERY_LEVEL:
  223. string batLevel = "";
  224. switch (data[0])
  225. {
  226. case '2':
  227. batLevel = "High";
  228. break;
  229. case '1':
  230. batLevel = "Low";
  231. break;
  232. case '0':
  233. batLevel = "Empty";
  234. break;
  235. default:
  236. batLevel = "Invalid value";
  237. break;
  238. }
  239. Gtk.Application.Invoke(delegate
  240. {
  241. labelBatteryLevel.Text = batLevel;
  242. });
  243. break;
  244. case DestijlCommandList.CAMERA_IMAGE:
  245. imageReceivedCounter++;
  246. byte[] image = new byte[2];
  247. try
  248. {
  249. image = Convert.FromBase64String(data);
  250. }
  251. catch (FormatException)
  252. {
  253. badImageReceivedCounter++;
  254. Console.WriteLine("Unable to convert from base64 ");
  255. }
  256. try
  257. {
  258. imageWidget.ShowImage(image);
  259. }
  260. catch (GLib.GException)
  261. {
  262. badImageReceivedCounter++;
  263. #if DEBUG
  264. Console.WriteLine("Bad Image: " + badImageReceivedCounter +
  265. " / " + imageReceivedCounter +
  266. " (" + badImageReceivedCounter * 100 / imageReceivedCounter + "%)");
  267. #endif
  268. }
  269. break;
  270. case DestijlCommandList.CAMERA_POSITION:
  271. imageWidget.Position = DestijlCommandManager.DecodePosition(data);
  272. break;
  273. default:
  274. Console.WriteLine("Untreated message from supervisor: " + header + ": " + data);
  275. break;
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// Callback called by "quit" menu
  281. /// </summary>
  282. /// <param name="sender">Sender object</param>
  283. /// <param name="e">Event</param>
  284. protected void OnQuitActionActivated(object sender, EventArgs e)
  285. {
  286. Console.WriteLine("Bye bye 2");
  287. if (cmdManager != null) cmdManager.Close();
  288. this.Destroy();
  289. Application.Quit();
  290. }
  291. /// <summary>
  292. /// Callback called by "show log" menu
  293. /// </summary>
  294. /// <param name="sender">Sender object</param>
  295. /// <param name="e">Event</param>
  296. protected void OnShowLogWindowActionActivated(object sender, EventArgs e)
  297. {
  298. MessagePopup(MessageType.Info,
  299. ButtonsType.Ok, "Info",
  300. "Logger not yet implemented");
  301. }
  302. /// <summary>
  303. /// Callback called by "buttonServerConnection" button
  304. /// </summary>
  305. /// <param name="sender">Sender object</param>
  306. /// <param name="e">Event</param>
  307. protected void OnButtonServerConnectionClicked(object sender, EventArgs e)
  308. {
  309. DestijlCommandManager.CommandStatus statusCmd;
  310. // if we are currently connected
  311. if (buttonServerConnection.Label == "Disconnect")
  312. {
  313. // Change state to disconnect and close connection
  314. ChangeState(SystemState.NotConnected);
  315. }
  316. else // we are not currently connected to server
  317. {
  318. // if information about hostname or port are invalid, show a popup error
  319. if ((entryServerName.Text == "") || (entryServerPort.Text == ""))
  320. {
  321. MessagePopup(MessageType.Error,
  322. ButtonsType.Ok, "Error",
  323. "Server name or port is invalid");
  324. }
  325. else
  326. {
  327. Console.WriteLine("Connecting to " + entryServerName.Text + ":" + entryServerPort.Text);
  328. bool status = false;
  329. // try to convert timout string value to double. If that failed, default to 100 ms
  330. try
  331. {
  332. cmdManager.timeout = Convert.ToDouble(entryTimeout.Text);
  333. }
  334. catch (Exception)
  335. {
  336. cmdManager.timeout = 100;
  337. entryTimeout.Text = cmdManager.timeout.ToString();
  338. }
  339. // try to connect to givn server.
  340. try
  341. {
  342. status = cmdManager.Open(entryServerName.Text, Convert.ToInt32(entryServerPort.Text));
  343. }
  344. catch (Exception)
  345. {
  346. Console.WriteLine("Something went wrong during connection");
  347. return;
  348. }
  349. //if connection status is not ok, show an error popup
  350. if (status != true)
  351. {
  352. MessagePopup(MessageType.Error,
  353. ButtonsType.Ok, "Error",
  354. "Unable to connect to server " + entryServerName.Text + ":" + Convert.ToInt32(entryServerPort.Text));
  355. }
  356. else // if we succed in connecting, open communication with robot
  357. {
  358. Console.Write("Send command RobotOpenCom: ");
  359. statusCmd = cmdManager.RobotOpenCom();
  360. Console.WriteLine(statusCmd.ToString());
  361. if (statusCmd == DestijlCommandManager.CommandStatus.Success)
  362. {
  363. ChangeState(SystemState.ServerConnected);
  364. }
  365. else // if communication with robot is not possible, show error
  366. {
  367. MessagePopup(MessageType.Error,
  368. ButtonsType.Ok, "Error",
  369. "Unable to open communication with robot.\nCheck that supervisor is accepting OPEN_COM_DMB command");
  370. cmdManager.Close();
  371. }
  372. }
  373. }
  374. }
  375. }
  376. /// <summary>
  377. /// Callback called when "buttonRobotactivation" is clicked
  378. /// </summary>
  379. /// <param name="sender">Sender object</param>
  380. /// <param name="e">Event</param>
  381. protected void OnButtonRobotActivationClicked(object sender, EventArgs e)
  382. {
  383. DestijlCommandManager.CommandStatus status;
  384. //if robot is not activated
  385. if (buttonRobotActivation.Label == "Activate")
  386. {
  387. // if a startup with watchdog is requested
  388. if (radioButtonWithWatchdog.Active)
  389. {
  390. status = cmdManager.RobotStartWithWatchdog();
  391. }
  392. else // startup without watchdog
  393. {
  394. status = cmdManager.RobotStartWithoutWatchdog();
  395. }
  396. // if status of command is ok, change state of system, enabling robot control
  397. if (status == DestijlCommandManager.CommandStatus.Success)
  398. {
  399. ChangeState(SystemState.RobotConnected);
  400. }
  401. else // if status is not ok, depending of error, show appropriate error
  402. {
  403. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  404. {
  405. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  406. ChangeState(SystemState.NotConnected);
  407. }
  408. else
  409. {
  410. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Command rejected\nCheck that supervisor accept \nDMB_START_WITH_WD and/or DMB_START_WITHOUT_WD");
  411. }
  412. }
  413. }
  414. else // If robot is already activated, request reset of robot
  415. {
  416. status = cmdManager.RobotReset();
  417. // if status of command is ok, change state of system, disabling robot control
  418. if (status == DestijlCommandManager.CommandStatus.Success)
  419. {
  420. ChangeState(SystemState.ServerConnected);
  421. }
  422. else // if status is not ok, depending of error, show appropriate error
  423. {
  424. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  425. {
  426. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  427. ChangeState(SystemState.NotConnected);
  428. }
  429. else
  430. {
  431. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Unknown error");
  432. }
  433. }
  434. }
  435. }
  436. /// <summary>
  437. /// Callback called when user click on direction button
  438. /// </summary>
  439. /// <param name="sender">Sender button</param>
  440. /// <param name="e">Event</param>
  441. protected void OnButtonMouvClicked(object sender, EventArgs e)
  442. {
  443. // depending on button clicked, launch appropriate action
  444. if (sender == buttonRight)
  445. {
  446. cmdManager.RobotGoRight();
  447. }
  448. else if (sender == buttonLeft)
  449. {
  450. cmdManager.RobotGoLeft();
  451. }
  452. else if (sender == buttonForward)
  453. {
  454. cmdManager.RobotGoForward();
  455. }
  456. else if (sender == buttonDown)
  457. {
  458. cmdManager.RobotGoBackward();
  459. }
  460. else if (sender == buttonStop)
  461. {
  462. cmdManager.RobotStop();
  463. }
  464. else
  465. {
  466. MessagePopup(MessageType.Warning, ButtonsType.Ok, "Abnormal behavior", "Callback OnButtonMouvClicked called by unknown sender");
  467. }
  468. }
  469. /// <summary>
  470. /// Callback called when checkbutton for camera is clicked
  471. /// </summary>
  472. /// <param name="sender">Sender object</param>
  473. /// <param name="e">Event</param>
  474. protected void OnCheckButtonCameraOnClicked(object sender, EventArgs e)
  475. {
  476. // if camera is already active, switch it off
  477. if (!checkButtonCameraOn.Active)
  478. {
  479. if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
  480. {
  481. Console.WriteLine("Error when closing camera: bad answer for supervisor or timeout");
  482. }
  483. }
  484. else // camera is not active, switch it on
  485. {
  486. badImageReceivedCounter = 0;
  487. imageReceivedCounter = 0;
  488. if (cmdManager.CameraOpen() != DestijlCommandManager.CommandStatus.Success)
  489. {
  490. Console.WriteLine("Error when opening camera: bad answer for supervisor or timeout");
  491. //checkButtonCameraOn.Active = false;
  492. }
  493. }
  494. }
  495. /// <summary>
  496. /// Callback called when checkbutton robot position is clicked
  497. /// </summary>
  498. /// <param name="sender">Sender object</param>
  499. /// <param name="e">Event</param>
  500. protected void OnCheckButtonRobotPositionClicked(object sender, EventArgs e)
  501. {
  502. // if server already send robot position, stop it
  503. if (!checkButtonRobotPosition.Active)
  504. {
  505. if (cmdManager.CameraStopComputePosition() != DestijlCommandManager.CommandStatus.Success)
  506. {
  507. Console.WriteLine("Error when stopping position reception: bad answer for supervisor or timeout");
  508. }
  509. }
  510. else // start reception of robot position
  511. {
  512. if (cmdManager.CameraComputePosition() != DestijlCommandManager.CommandStatus.Success)
  513. {
  514. Console.WriteLine("Error when starting getting robot position: bad answer for supervisor or timeout");
  515. //checkButtonRobotPosition.Active = false;
  516. }
  517. }
  518. imageWidget.showPosition = checkButtonRobotPosition.Active;
  519. }
  520. /// <summary>
  521. /// Show a popup asking user to tell if arena is correct or not
  522. /// </summary>
  523. protected void DetectArena()
  524. {
  525. DestijlCommandManager.CommandStatus status;
  526. MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent,
  527. MessageType.Question, ButtonsType.YesNo, "Arena is correct ?");
  528. md.Title = "Check arena";
  529. ResponseType result = (ResponseType)md.Run();
  530. md.Destroy();
  531. if (result == ResponseType.Yes)
  532. {
  533. status = cmdManager.CameraArenaConfirm();
  534. }
  535. else
  536. {
  537. status = cmdManager.CameraArenaInfirm();
  538. }
  539. if (status != DestijlCommandManager.CommandStatus.Success)
  540. {
  541. MessagePopup(MessageType.Error,
  542. ButtonsType.Ok, "Error",
  543. "Unable to send Confirm or Infirm arena command to supervisor");
  544. }
  545. }
  546. /// <summary>
  547. /// Callback called when "Detect Arena" button is clicked
  548. /// </summary>
  549. /// <param name="sender">Sender object</param>
  550. /// <param name="e">Event</param>
  551. protected void OnButtonAskArenaClicked(object sender, EventArgs e)
  552. {
  553. // Send command to server for arean rendering
  554. if (cmdManager.CameraAskArena() != DestijlCommandManager.CommandStatus.Success)
  555. {
  556. MessagePopup(MessageType.Error,
  557. ButtonsType.Ok, "Error",
  558. "Error when asking for arena rendering");
  559. return;
  560. }
  561. // show popup and wait for user to say if arena is ok or not
  562. DetectArena();
  563. }
  564. /// <summary>
  565. /// Callback function for FPS checkbutton
  566. /// </summary>
  567. /// <param name="sender">Sender object</param>
  568. /// <param name="e">unused paramter</param>
  569. protected void OnCheckButtonFPSToggled(object sender, EventArgs e)
  570. {
  571. imageWidget.ShowFPS = checkButtonFPS.Active;
  572. }
  573. }