Monitor
MonitorUI.cs
Go to the documentation of this file.
1 using System;
2 using Gtk;
3 using Gdk;
4 
5 using monitor;
6 
10 public partial class MainWindow : Gtk.Window
11 {
13  private Pixbuf drawingareaCameraPixbuf;
14 
16  {
17  NotConnected,
18  ServerConnected,
19  RobotConnected
20  };
21 
22  private SystemState systemState = SystemState.NotConnected;
23  private System.Timers.Timer batteryTimer;
24 
25  public MainWindow() : base(Gtk.WindowType.Toplevel)
26  {
27  Build();
28 
30 
31  batteryTimer = new System.Timers.Timer(10000.0);
33 
35  PixbufFormat[] format = Gdk.Pixbuf.Formats;
36  foreach (PixbufFormat f in format)
37  {
38  Console.WriteLine("Format: " + f.Name);
39  }
40  }
41 
42  public void AdjustControls()
43  {
44  ChangeState(SystemState.NotConnected);
45 
46  drawingareaCameraPixbuf = new Pixbuf((string)null);
47  drawingareaCameraPixbuf = Pixbuf.LoadFromResource("monitor.ressources.missing_picture.png");
48 
49  entryServerName.Text = Client.defaultIP;
50  entryServerPort.Text = Client.defaultPort.ToString();
51  entryTimeout.Text = "10000";
52  }
53 
54  private void ChangeState(SystemState newState)
55  {
56  switch (newState)
57  {
58  case SystemState.NotConnected:
59  labelRobot.Sensitive = false;
60  gtkAlignmentRobot.Sensitive = false;
61 
62  labelRobotControl.Sensitive = false;
63  gtkAlignmentRobotControl.Sensitive = false;
64  boxCamera.Sensitive = false;
65 
66  buttonServerConnection.Label = "Connect";
67  buttonRobotActivation.Label = "Activate";
68  labelBatteryLevel.Text = "Unknown";
69 
70  checkButtonCameraOn.Active = false;
71  checkButtonRobotPosition.Active = false;
72  if (cmdManager != null) cmdManager.Close();
73 
74  batteryTimer.Stop();
75  break;
76  case SystemState.ServerConnected:
77  buttonServerConnection.Label = "Disconnect";
78  buttonRobotActivation.Label = "Activate";
79  labelBatteryLevel.Text = "Unknown";
80 
81  labelRobot.Sensitive = true;
82  gtkAlignmentRobot.Sensitive = true;
83  boxCamera.Sensitive = true;
84 
85  labelRobotControl.Sensitive = false;
86  gtkAlignmentRobotControl.Sensitive = false;
87 
88  batteryTimer.Stop();
89  break;
90  case SystemState.RobotConnected:
91  buttonRobotActivation.Label = "Reset";
92  labelRobotControl.Sensitive = true;
93  gtkAlignmentRobotControl.Sensitive = true;
94 
95  batteryTimer.Start();
96  break;
97  default:
98  labelRobot.Sensitive = false;
99  gtkAlignmentRobot.Sensitive = false;
100 
101  labelRobotControl.Sensitive = false;
102  gtkAlignmentRobotControl.Sensitive = false;
103  boxCamera.Sensitive = false;
104 
105  buttonServerConnection.Label = "Connect";
106  buttonRobotActivation.Label = "Activate";
107  labelBatteryLevel.Text = "Unknown";
108 
109  checkButtonCameraOn.Active = false;
110  checkButtonRobotPosition.Active = false;
111 
112  systemState = SystemState.NotConnected;
113 
114  return;
115  }
116 
117  systemState = newState;
118  }
119 
120  private void MessagePopup(MessageType type, ButtonsType buttons, string title, string message)
121  {
122  MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, type, buttons, message)
123  {
124  Title = title
125  };
126 
127  md.Run();
128  md.Destroy();
129  }
130 
131  protected void OnDeleteEvent(object sender, DeleteEventArgs a)
132  {
133  Console.WriteLine("Bye bye");
134 
135  if (cmdManager != null) cmdManager.Close();
136  Application.Quit();
137  a.RetVal = true;
138  }
139 
140  public void OnCommandReceivedEvent(string header, string data, byte[] buffer)
141  {
142  if (header != null)
143  {
144  Console.WriteLine("Received header (" + header.Length + "): " + header);
145  if (header.ToUpper() != DestijlCommandList.HeaderStmImage)
146  {
147  if (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data);
148  }
149 
150  if (header.ToUpper() == DestijlCommandList.HeaderStmBat)
151  {
152  switch (data[0])
153  {
154  case '2':
155  labelBatteryLevel.Text = "High";
156  break;
157  case '1':
158  labelBatteryLevel.Text = "Low";
159  break;
160  case '0':
161  labelBatteryLevel.Text = "Empty";
162  break;
163  default:
164  labelBatteryLevel.Text = "Invalid value";
165  break;
166  }
167  }
168  else if (header.ToUpper() == DestijlCommandList.HeaderStmImage)
169  {
170  byte[] image = new byte[buffer.Length - 4];
171  System.Buffer.BlockCopy(buffer, 4, image, 0, image.Length);
172 
173  drawingareaCameraPixbuf = new Pixbuf(image);
174  drawingAreaCamera.QueueDraw();
175  }
176  }
177  }
178 
179  protected void OnQuitActionActivated(object sender, EventArgs e)
180  {
181  Console.WriteLine("Bye bye 2");
182  if (cmdManager != null) cmdManager.Close();
183  this.Destroy();
184  Application.Quit();
185  }
186 
187  protected void OnShowLogWindowActionActivated(object sender, EventArgs e)
188  {
189  MessagePopup(MessageType.Info,
190  ButtonsType.Ok, "Info",
191  "Logger not yet implemented");
192  }
193 
194  protected void OnButtonServerConnectionClicked(object sender, EventArgs e)
195  {
197 
198  if (buttonServerConnection.Label == "Disconnect")
199  {
200  ChangeState(SystemState.NotConnected);
201  }
202  else
203  {
204  if ((entryServerName.Text == "") || (entryServerPort.Text == ""))
205  {
206  MessagePopup(MessageType.Error,
207  ButtonsType.Ok, "Error",
208  "Server name or port is invalid");
209  }
210  else
211  {
212  Console.WriteLine("Connecting to " + entryServerName.Text + ":" + entryServerPort.Text);
213  bool status = false;
214 
215  try
216  {
217  cmdManager.timeout = Convert.ToDouble(entryTimeout.Text);
218  }
219  catch (Exception)
220  {
221  cmdManager.timeout = 100;
222  entryTimeout.Text = cmdManager.timeout.ToString();
223  }
224 
225  try
226  {
227  status = cmdManager.Open(entryServerName.Text, Convert.ToInt32(entryServerPort.Text));
228  }
229  catch (Exception)
230  {
231  Console.WriteLine("Something went wrong during connection");
232  return;
233  }
234 
235  if (status != true)
236  {
237  MessagePopup(MessageType.Error,
238  ButtonsType.Ok, "Error",
239  "Unable to connect to server " + entryServerName.Text + ":" + Convert.ToInt32(entryServerPort.Text));
240  }
241  else
242  {
243  Console.Write("Send command RobotOpenCom: ");
244  statusCmd = cmdManager.RobotOpenCom();
245  Console.WriteLine(statusCmd.ToString());
246 
247  if (statusCmd == DestijlCommandManager.CommandStatus.Success)
248  {
249  ChangeState(SystemState.ServerConnected);
250  }
251  else
252  {
253  MessagePopup(MessageType.Error,
254  ButtonsType.Ok, "Error",
255  "Unable to open communication with robot.\nCheck that supervisor is accepting OPEN_COM_DMB command");
256 
257  cmdManager.Close();
258  }
259  }
260  }
261  }
262  }
263 
264  protected void OnButtonRobotActivationClicked(object sender, EventArgs e)
265  {
267 
268  if (buttonRobotActivation.Label == "Activate") // activation du robot
269  {
270  if (radioButtonWithWatchdog.Active) // Demarrage avec watchdog
271  {
272  status = cmdManager.RobotStartWithWatchdog();
273  }
274  else // Demarrage sans watchdog
275  {
276  status = cmdManager.RobotStartWithoutWatchdog();
277  }
278 
279  if (status == DestijlCommandManager.CommandStatus.Success)
280  {
281  ChangeState(SystemState.RobotConnected);
282  }
283  else
284  {
285  if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
286  {
287  MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
288  ChangeState(SystemState.NotConnected);
289  }
290  else
291  {
292  MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Command rejected\nCheck that supervisor accept \nDMB_START_WITH_WD and/or DMB_START_WITHOUT_WD");
293  }
294  }
295  }
296  else // Reset du robot
297  {
298  status = cmdManager.RobotReset();
299 
300  if (status == DestijlCommandManager.CommandStatus.Success)
301  {
302  ChangeState(SystemState.ServerConnected);
303  }
304  else
305  {
306  if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
307  {
308  MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
309  ChangeState(SystemState.NotConnected);
310  }
311  else
312  {
313  MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Unknown error");
314  }
315  }
316  }
317  }
318 
319  protected void OnButtonMouvClicked(object sender, EventArgs e)
320  {
321  if (sender == buttonRight)
322  {
323  cmdManager.RobotTurn(90);
324  }
325  else if (sender == buttonLeft)
326  {
327  cmdManager.RobotTurn(-90);
328  }
329  else if (sender == buttonForward)
330  {
331  cmdManager.RobotMove(100);
332  }
333  else if (sender == buttonDown)
334  {
335  cmdManager.RobotMove(-100);
336  }
337  else
338  {
339  MessagePopup(MessageType.Warning, ButtonsType.Ok, "Abnormal behavior", "Callback OnButtonMouvClicked called by unknown sender");
340  }
341  }
342 
343  void OnBatteryTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
344  {
346  batteryTimer.Stop();
347 
348  if (checkButtonGetBattery.Active)
349  {
350  status = cmdManager.RobotGetBattery();
351  switch (status)
352  {
354  batteryTimer.Start();
355  break;
356  case DestijlCommandManager.CommandStatus.CommunicationLostWithServer:
357  Console.WriteLine("Error: Connection lost with server");
358  batteryTimer.Stop();
359  labelBatteryLevel.Text = "Unknown";
360 
361  ChangeState(SystemState.NotConnected);
362  break;
363  case DestijlCommandManager.CommandStatus.CommunicationLostWithRobot:
364  Console.WriteLine("Error: Connection lost with robot");
365  batteryTimer.Stop();
366  labelBatteryLevel.Text = "Unknown";
367 
368  ChangeState(SystemState.ServerConnected);
369  break;
370  default:
371  labelBatteryLevel.Text = "Unknown";
372  batteryTimer.Start();
373  break;
374  }
375  }
376  else batteryTimer.Start();
377  }
378 
379  protected void OnCheckButtonCameraOnClicked(object sender, EventArgs e)
380  {
381  if (!checkButtonCameraOn.Active)
382  {
383  if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
384  {
385  MessagePopup(MessageType.Error,
386  ButtonsType.Ok, "Error",
387  "Error when closing camera: bad answer for supervisor or timeout");
388  }
389  }
390  else
391  {
392  if (cmdManager.CameraOpen() != DestijlCommandManager.CommandStatus.Success)
393  {
394  MessagePopup(MessageType.Error,
395  ButtonsType.Ok, "Error",
396  "Error when opening camera: bad answer for supervisor or timeout");
397  checkButtonCameraOn.Active = false;
398  }
399  }
400  }
401 
402  protected void OnCheckButtonRobotPositionClicked(object sender, EventArgs e)
403  {
404  if (!checkButtonRobotPosition.Active)
405  {
407  {
408  MessagePopup(MessageType.Error,
409  ButtonsType.Ok, "Error",
410  "Error when stopping position reception: bad answer for supervisor or timeout");
411  }
412  }
413  else
414  {
415  if (cmdManager.CameraComputePosition() != DestijlCommandManager.CommandStatus.Success)
416  {
417  MessagePopup(MessageType.Error,
418  ButtonsType.Ok, "Error",
419  "Error when starting getting robot position: bad answer for supervisor or timeout");
420 
421  checkButtonRobotPosition.Active = false;
422  }
423  }
424  }
425 
426  protected void OnDrawingAreaCameraExposeEvent(object o, ExposeEventArgs args)
427  {
428  //Console.WriteLine("Event expose. Args = " + args.ToString());
429 
430  DrawingArea area = (DrawingArea)o;
431  Gdk.Pixbuf displayPixbuf;
432  int areaWidth, areaHeight;
433 
434  Gdk.GC gc = area.Style.BackgroundGC(Gtk.StateType.Normal);
435 
436  area.GdkWindow.GetSize(out areaWidth, out areaHeight);
437  int width = drawingareaCameraPixbuf.Width;
438  int height = drawingareaCameraPixbuf.Height;
439  float ratio = (float)width / (float)height;
440 
441  if (areaWidth <= width)
442  {
443  width = areaWidth;
444  height = (int)(width / ratio);
445  }
446 
447  if (width > areaWidth)
448  {
449  width = areaWidth;
450  }
451 
452  if (height > areaHeight)
453  {
454  height = areaHeight;
455  }
456 
457  displayPixbuf = drawingareaCameraPixbuf.ScaleSimple(width, height, InterpType.Bilinear);
458 
459  area.GdkWindow.DrawPixbuf(gc, displayPixbuf,
460  0, 0,
461  (areaWidth - displayPixbuf.Width) / 2,
462  (areaHeight - displayPixbuf.Height) / 2,
463  displayPixbuf.Width, displayPixbuf.Height,
464  RgbDither.Normal, 0, 0);
465  }
466 
467  protected void DetectArena()
468  {
470  MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent,
471  MessageType.Question, ButtonsType.YesNo, "Arena is correct ?");
472  {
473  Title = "Check arena";
474  };
475 
476  ResponseType result = (ResponseType)md.Run();
477  md.Destroy();
478 
479  if (result == ResponseType.Yes)
480  {
481  status = cmdManager.CameraArenaConfirm();
482  }
483  else
484  {
485  status = cmdManager.CameraArenaInfirm();
486  }
487 
488  if (status != DestijlCommandManager.CommandStatus.Success)
489  {
490  MessagePopup(MessageType.Error,
491  ButtonsType.Ok, "Error",
492  "Unable to send Confirm or Infirm arena command to supervisor");
493  }
494  }
495 
496  protected void OnButtonAskArenaClicked(object sender, EventArgs e)
497  {
498  if (cmdManager.CameraAskArena() != DestijlCommandManager.CommandStatus.Success)
499  {
500  MessagePopup(MessageType.Error,
501  ButtonsType.Ok, "Error",
502  "Error when asking for arena rendering");
503  return;
504  }
505 
506  DetectArena();
507  }
508 }
const int defaultPort
Default server port number
Definition: Client.cs:20
void OnButtonServerConnectionClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:194
void OnButtonRobotActivationClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:264
DestijlCommandManager cmdManager
Definition: MonitorUI.cs:12
void OnCheckButtonCameraOnClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:379
void DetectArena()
Definition: MonitorUI.cs:467
Pixbuf drawingareaCameraPixbuf
Definition: MonitorUI.cs:13
void OnDeleteEvent(object sender, DeleteEventArgs a)
Definition: MonitorUI.cs:131
const string defaultIP
Default server name
Definition: Client.cs:15
void OnBatteryTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
Definition: MonitorUI.cs:343
void OnButtonMouvClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:319
void OnShowLogWindowActionActivated(object sender, EventArgs e)
Definition: MonitorUI.cs:187
void OnCheckButtonRobotPositionClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:402
void ChangeState(SystemState newState)
Definition: MonitorUI.cs:54
void MessagePopup(MessageType type, ButtonsType buttons, string title, string message)
Definition: MonitorUI.cs:120
void OnQuitActionActivated(object sender, EventArgs e)
Definition: MonitorUI.cs:179
MainWindow()
Definition: MonitorUI.cs:25
CommandStatus RobotTurn(int angle)
void OnDrawingAreaCameraExposeEvent(object o, ExposeEventArgs args)
Definition: MonitorUI.cs:426
Definition: Client.cs:5
void OnButtonAskArenaClicked(object sender, EventArgs e)
Definition: MonitorUI.cs:496
Static class for TCP client
Definition: Client.cs:10
System.Timers.Timer batteryTimer
Definition: MonitorUI.cs:23
void AdjustControls()
Definition: MonitorUI.cs:42
CommandStatus RobotMove(int distance)
SystemState systemState
Definition: MonitorUI.cs:22
Main window.
Definition: MonitorUI.cs:10
void OnCommandReceivedEvent(string header, string data, byte[] buffer)
Definition: MonitorUI.cs:140