Monitor
DestijlCommandManager.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace monitor
4 {
5  public class DestijlCommandList
6  {
7  public const string HeaderMtsComDmb = "COM";
8  public const string HeaderMtsDmbOrder = "DMB";
9  public const string HeaderMtsCamera = "CAM";
10  public const string HeaderMtsMessage = "MSG";
11 
12  public const string DataComOpen = "o";
13  public const string DataComClose = "C";
14 
15  public const string DataCamOpen = "A";
16  public const string DataCamClose = "I";
17  public const string DataCamAskArena = "y";
18  public const string DataCamArenaConfirm = "x";
19  public const string DataCamInfirm = "z";
20  public const string DataCamComputePosition = "p";
21  public const string DataCamStopComputePosition = "s";
22 
23  public const string HeaderStmAck = "ACK";
24  public const string HeaderStmNoAck = "NAK";
25  public const string HeaderStmLostDmb = "LCD";
26  public const string HeaderStmImage = "IMG";
27  public const string HeaderStmPos = "POS";
28  public const string HeaderStmMes = "MSG";
29  public const string HeaderStmBat = "BAT";
30  }
31 
32  public class RobotCommandList
33  {
34  public const string RobotPing = "p";
35  public const string RobotReset = "r";
36  public const string RobotStartWithoutWatchdog = "u";
37  public const string RobotStartWithWatchdog = "W";
38  public const string RobotGetBattery = "v";
39  public const string RobotGetBusyState = "b";
40  public const string RobotMove = "M";
41  public const string RobotTurn = "T";
42  public const string RobotGetVersion = "V";
43  public const string RobotPowerOff = "z";
44  }
45 
46  public class DestijlCommandManager
47  {
48  private CommandManager commandManager = null;
49 
50  private string receivedHeader = null;
51  private string receivedData = null;
52 
53  public delegate void CommandReceivedEvent(string header, string data, byte[] buffer);
54  public CommandReceivedEvent commandReceivedEvent = null;
55 
56  public double timeout = 100; // timeout pour les commandes avec acquitement
57 
58  public enum CommandStatus
59  {
60  Success,
61  Rejected,
62  InvalidAnswer,
63  Busy,
64  CommunicationLostWithRobot,
65  CommunicationLostWithServer
66  }
67 
68  public DestijlCommandManager(CommandReceivedEvent callback)
69  {
70  commandManager = new CommandManager(OnCommandReceived);
71  this.commandReceivedEvent += callback;
72  }
73 
75  {
76  if (commandManager != null) commandManager.Close();
77  }
78 
79  private void OnCommandReceived(string msg, byte[] buffer)
80  {
81  string[] msgs = msg.Split(':');
82 
83  if (msgs.Length >= 1) receivedHeader = msgs[0];
84  else receivedHeader = null;
85 
86  if (msgs.Length >= 2) receivedData = msgs[1];
87  else receivedData = null;
88 
89  this.commandReceivedEvent?.Invoke(receivedHeader, receivedData, buffer);
90  }
91 
92  public bool Open(string hostname)
93  {
94  return this.Open(hostname, Client.defaultPort);
95  }
96 
97  public bool Open(string hostname, int port)
98  {
99  if (commandManager != null) return commandManager.Open(hostname, port);
100  else return false;
101  }
102 
103  public void Close()
104  {
105  if (commandManager != null) commandManager.Close();
106  }
107 
108  private string CreateCommand(string header, string data)
109  {
110  return header + ":" + data;
111  }
112 
113  private void SplitCommand(string cmd, out string header, out string data)
114  {
115  string[] cmdParts = cmd.Split(':');
116 
117  if (cmdParts.Length > 0) header = cmdParts[0];
118  else header = null;
119 
120  if (cmdParts.Length > 1) data = cmdParts[1];
121  else data = null;
122  }
123 
124  private CommandStatus DecodeStatus(CommandManager.CommandManagerStatus localStatus, string answer)
125  {
126  CommandStatus status = CommandStatus.Success;
127 
128  if (localStatus == CommandManager.CommandManagerStatus.Timeout) status = CommandStatus.CommunicationLostWithServer;
129  else if (localStatus == CommandManager.CommandManagerStatus.Busy) status = CommandStatus.Busy;
130  else
131  {
132  if (answer != null)
133  {
134  if (answer.ToUpper().Contains(DestijlCommandList.HeaderStmNoAck)) status = CommandStatus.Rejected;
135  else if (answer.ToUpper().Contains(DestijlCommandList.HeaderStmLostDmb)) status = CommandStatus.CommunicationLostWithRobot;
136  else if (answer.Length == 0) status = CommandStatus.CommunicationLostWithServer;
137  //else status = CommandStatus.InvalidAnswer;
138  }
139  }
140 
141  return status;
142  }
143 
145  {
147  string answer;
148 
149  localStatus = commandManager.SendCommand(
151  out answer,
152  this.timeout);
153 
154  return DecodeStatus(localStatus, answer);
155  }
156 
158  {
160  string answer;
161 
162  localStatus = commandManager.SendCommand(
164  out answer,
165  this.timeout);
166 
167  return DecodeStatus(localStatus, answer);
168  }
169 
171  {
173  string answer;
174 
175  localStatus = commandManager.SendCommand(
177  out answer,
178  this.timeout);
179 
180  return DecodeStatus(localStatus, answer);
181  }
182 
184  {
186  string answer;
187 
188  localStatus = commandManager.SendCommand(
190  out answer,
191  0);
192 
193  return DecodeStatus(localStatus, answer);
194  }
195 
197  {
199  string answer;
200 
201  localStatus = commandManager.SendCommand(
203  out answer,
204  this.timeout);
205 
206  return DecodeStatus(localStatus, answer);
207  }
208 
210  {
212  string answer;
213 
214  localStatus = commandManager.SendCommand(
216  out answer,
217  this.timeout);
218 
219  return DecodeStatus(localStatus, answer);
220  }
221 
222  public CommandStatus RobotMove(int distance)
223  {
225  string answer;
226 
227  localStatus = commandManager.SendCommand(
228  CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotMove + "=" + distance),
229  out answer,
230  0);
231 
232  return DecodeStatus(localStatus, answer);
233  }
234 
235  public CommandStatus RobotTurn(int angle)
236  {
238  string answer;
239 
240  localStatus = commandManager.SendCommand(
241  CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotTurn + "=" + angle),
242  out answer,
243  0);
244 
245  return DecodeStatus(localStatus, answer);
246  }
247 
248  //public CommandStatus RobotGetBattery(out int battery)
250  {
252  //CommandStatus status = CommandStatus.Success;
253 
254  //battery = -1;
255 
256  string answer;
257 
258  localStatus = commandManager.SendCommand(
260  out answer,
261  0);
262 
263  //if (localStatus == CommandManager.CommandManagerStatus.AnswerReceived) {
264  // string[] msg = answer.Split(':');
265 
266  // if (msg.Length > 1)
267  // {
268  // try
269  // {
270  // battery = Convert.ToInt32(msg[1]);
271  // }
272  // catch (Exception) { }
273  // }
274  //}
275  //else if (localStatus == CommandManager.CommandManagerStatus.Timeout)
276  //{
277  // status = CommandStatus.CommunicationLostWithServer;
278  //}
279 
280  //return status;
281  return DecodeStatus(localStatus, answer);
282  }
283 
284  public CommandStatus RobotGetVersion(out string version)
285  {
287  CommandStatus status = CommandStatus.Success;
288 
289  version = "";
290 
291  string answer;
292 
293  localStatus = commandManager.SendCommand(
295  out answer,
296  this.timeout);
297 
298  if (localStatus == CommandManager.CommandManagerStatus.AnswerReceived)
299  {
300  string[] msg = answer.Split(':');
301 
302  if (msg.Length > 1)
303  {
304  version = msg[1];
305  }
306  }
307  else if (localStatus == CommandManager.CommandManagerStatus.Timeout)
308  {
309  status = CommandStatus.CommunicationLostWithServer;
310  }
311 
312  return status;
313  }
314 
316  {
318  string answer;
319 
320  localStatus = commandManager.SendCommand(
322  out answer,
323  0);
324 
325  return DecodeStatus(localStatus, answer);
326  }
327 
329  {
331  string answer;
332 
333  localStatus = commandManager.SendCommand(
335  out answer,
336  this.timeout);
337 
338  return DecodeStatus(localStatus, answer);
339  }
340 
342  {
344  string answer;
345 
346  localStatus = commandManager.SendCommand(
348  out answer,
349  0);
350 
351  return DecodeStatus(localStatus, answer);
352  }
353 
355  {
357  string answer;
358 
359  localStatus = commandManager.SendCommand(
361  out answer,
362  0);
363 
364  return DecodeStatus(localStatus, answer);
365  }
366 
368  {
370  string answer;
371 
372  localStatus = commandManager.SendCommand(
374  out answer,
375  0);
376 
377  return DecodeStatus(localStatus, answer);
378  }
379 
381  {
383  string answer;
384 
385  localStatus = commandManager.SendCommand(
387  out answer,
388  0);
389 
390  return DecodeStatus(localStatus, answer);
391  }
392 
394  {
396  string answer;
397 
398  localStatus = commandManager.SendCommand(
400  out answer,
401  0);
402 
403  return DecodeStatus(localStatus, answer);
404  }
405 
407  {
409  string answer;
410 
411  localStatus = commandManager.SendCommand(
413  out answer,
414  0);
415 
416  return DecodeStatus(localStatus, answer);
417  }
418  }
419 }
CommandStatus RobotGetVersion(out string version)
const int defaultPort
Default server port number
Definition: Client.cs:20
bool Open(string hostname)
Open the specified hostname server, using default port number.
Command Manager. Use for timeout managment during reception of data Used as intermediate layer betwee...
void OnCommandReceived(string msg, byte[] buffer)
CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)
Sends a command to TCP server
bool Open(string hostname, int port)
void SplitCommand(string cmd, out string header, out string data)
DestijlCommandManager(CommandReceivedEvent callback)
CommandStatus RobotTurn(int angle)
string CreateCommand(string header, string data)
Definition: Client.cs:5
Static class for TCP client
Definition: Client.cs:10
CommandStatus RobotMove(int distance)
CommandManagerStatus
Available status when sending command
CommandStatus DecodeStatus(CommandManager.CommandManagerStatus localStatus, string answer)
void Close()
Close connection to server