DestijlCommandManager.cs
Go to the documentation of this file.
//
// DestijlCommandManager.cs
//
// Author:
// Di MERCURIO Sébastien <dimercur@insa-toulouse.fr>
//
// Copyright (c) 2018 INSA - DGEI
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace monitor
{
public static class DestijlCommandList
{
public const string HeaderMtsComDmb = "COM";
public const string HeaderMtsDmbOrder = "DMB";
public const string HeaderMtsCamera = "CAM";
public const string HeaderMtsMessage = "MSG";
public const string DataComOpen = "o";
public const string DataComClose = "C";
public const string DataCamOpen = "A";
public const string DataCamClose = "I";
public const string DataCamAskArena = "y";
public const string DataCamArenaConfirm = "x";
public const string DataCamInfirm = "z";
public const string DataCamComputePosition = "p";
public const string DataCamStopComputePosition = "s";
public const string HeaderStmAck = "ACK";
public const string HeaderStmNoAck = "NAK";
public const string HeaderStmLostDmb = "LCD";
public const string HeaderStmImage = "IMG";
public const string HeaderStmPos = "POS";
public const string HeaderStmMes = "MSG";
public const string HeaderStmBat = "BAT";
}
public static class RobotCommandList
{
public const string RobotPing = "p";
public const string RobotReset = "r";
public const string RobotStartWithoutWatchdog = "u";
public const string RobotStartWithWatchdog = "W";
public const string RobotGetBattery = "v";
public const string RobotGetBusyState = "b";
public const string RobotMove = "M";
public const string RobotTurn = "T";
public const string RobotGetVersion = "V";
public const string RobotPowerOff = "z";
}
public class DestijlCommandManager
{
private CommandManager commandManager = null;
private string receivedHeader = null;
private string receivedData = null;
public delegate void CommandReceivedEvent(string header, string data, byte[] buffer);
public CommandReceivedEvent commandReceivedEvent = null;
public double timeout = 100;
public enum CommandStatus
{
Success,
Rejected,
InvalidAnswer,
Busy,
CommunicationLostWithRobot,
CommunicationLostWithServer
}
public DestijlCommandManager(CommandReceivedEvent callback)
{
commandManager = new CommandManager(OnCommandReceived);
this.commandReceivedEvent += callback;
}
~DestijlCommandManager()
{
if (commandManager != null) commandManager.Close();
}
private void OnCommandReceived(string msg, byte[] buffer)
{
// Firstly, split message in (at least) two part : header, and data
string[] msgs = msg.Split(':');
// If it exist at least on element in string array, it should be command header
if (msgs.Length >= 1) receivedHeader = msgs[0];
else receivedHeader = null;
// if msgs array contains at least two elements, second element is normally data
if (msgs.Length >= 2) receivedData = msgs[1];
else receivedData = null;
// when split is done, provide data to application
this.commandReceivedEvent?.Invoke(receivedHeader, receivedData, buffer);
}
public bool Open(string hostname)
{
return this.Open(hostname, Client.defaultPort);
}
public bool Open(string hostname, int port)
{
if (commandManager != null) return commandManager.Open(hostname, port);
else return false;
}
public void Close()
{
if (commandManager != null) commandManager.Close();
}
private string CreateCommand(string header, string data)
{
return header + ":" + data;
}
private CommandStatus DecodeStatus(CommandManager.CommandManagerStatus localStatus, string answer)
{
CommandStatus status = CommandStatus.Success;
// if timeout occures, return CommandStatus.CommunicationLostWithServer
if (localStatus == CommandManager.CommandManagerStatus.Timeout) status = CommandStatus.CommunicationLostWithServer;
// if a command is currently processed, return Busy
else if (localStatus == CommandManager.CommandManagerStatus.Busy) status = CommandStatus.Busy;
else
{
if (answer != null)
{
// if command is not acknowledged, return Rejected
if (answer.ToUpper().Contains(DestijlCommandList.HeaderStmNoAck)) status = CommandStatus.Rejected;
// if communication is lost with robot, return CommunicationLostWithRobot
else if (answer.ToUpper().Contains(DestijlCommandList.HeaderStmLostDmb)) status = CommandStatus.CommunicationLostWithRobot;
// if answer is empty, communication with robot is lost
else if (answer.Length == 0) status = CommandStatus.CommunicationLostWithServer;
//else status = CommandStatus.InvalidAnswer;
}
}
return status;
}
public CommandStatus RobotOpenCom()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsComDmb, DestijlCommandList.DataComOpen),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotCloseCom()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsComDmb, DestijlCommandList.DataComClose),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotPing()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotPing),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotReset()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotReset),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotStartWithWatchdog()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotStartWithWatchdog),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotStartWithoutWatchdog()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotStartWithoutWatchdog),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotMove(int distance)
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotMove + "=" + distance),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotTurn(int angle)
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotTurn + "=" + angle),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotGetBattery()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotGetBattery),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus RobotGetVersion(out string version)
{
CommandManager.CommandManagerStatus localStatus;
CommandStatus status = CommandStatus.Success;
version = "";
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotGetVersion),
out answer,
this.timeout);
if (localStatus == CommandManager.CommandManagerStatus.AnswerReceived)
{
string[] msg = answer.Split(':');
if (msg.Length > 1)
{
version = msg[1];
}
}
else if (localStatus == CommandManager.CommandManagerStatus.Timeout)
{
status = CommandStatus.CommunicationLostWithServer;
}
return status;
}
public CommandStatus RobotPowerOff()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsDmbOrder, RobotCommandList.RobotPowerOff),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraOpen()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamOpen),
out answer,
this.timeout);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraClose()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamClose),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraAskArena()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamAskArena),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraArenaConfirm()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamArenaConfirm),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraArenaInfirm()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamInfirm),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraComputePosition()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamComputePosition),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
public CommandStatus CameraStopComputePosition()
{
CommandManager.CommandManagerStatus localStatus;
string answer;
localStatus = commandManager.SendCommand(
CreateCommand(DestijlCommandList.HeaderMtsCamera, DestijlCommandList.DataCamStopComputePosition),
out answer,
0);
return DecodeStatus(localStatus, answer);
}
}
}
monitor.DestijlCommandList.HeaderMtsDmbOrder
const string HeaderMtsDmbOrder
Definition: DestijlCommandManager.cs:32
monitor.DestijlCommandList.HeaderStmImage
const string HeaderStmImage
Definition: DestijlCommandManager.cs:50
monitor.DestijlCommandManager.RobotGetVersion
CommandStatus RobotGetVersion(out string version)
Request robot firmware version
Definition: DestijlCommandManager.cs:390
monitor.DestijlCommandManager.RobotStartWithoutWatchdog
CommandStatus RobotStartWithoutWatchdog()
Start robot, without enabling watchdog
Definition: DestijlCommandManager.cs:319
monitor.Client.defaultPort
const int defaultPort
Default server port number
Definition: Client.cs:41
monitor.DestijlCommandManager.CameraArenaInfirm
CommandStatus CameraArenaInfirm()
Reject arena detected (after requesting image of detected arena, using CameraAskArena ...
Definition: DestijlCommandManager.cs:509
monitor.CommandManager.Open
bool Open(string hostname)
Open the specified hostname server, using default port number.
Definition: CommandManager.cs:96
monitor.RobotCommandList.RobotGetBattery
const string RobotGetBattery
Definition: DestijlCommandManager.cs:65
monitor.CommandManager
Command Manager. Use for timeout managment during reception of data Used as intermediate layer betwee...
Definition: CommandManager.cs:31
monitor.DestijlCommandManager.Close
void Close()
Close connection to server
Definition: DestijlCommandManager.cs:183
monitor.DestijlCommandList.HeaderStmBat
const string HeaderStmBat
Definition: DestijlCommandManager.cs:53
System
monitor.RobotCommandList.RobotGetVersion
const string RobotGetVersion
Definition: DestijlCommandManager.cs:69
monitor.DestijlCommandList.DataCamComputePosition
const string DataCamComputePosition
Definition: DestijlCommandManager.cs:44
monitor.DestijlCommandManager.OnCommandReceived
void OnCommandReceived(string msg, byte[] buffer)
Callback used for receiving data from lower layer (CommandManager class)
Definition: DestijlCommandManager.cs:141
monitor.RobotCommandList.RobotPowerOff
const string RobotPowerOff
Definition: DestijlCommandManager.cs:70
monitor.DestijlCommandManager.RobotGetBattery
CommandStatus RobotGetBattery()
Request robot battery level
Definition: DestijlCommandManager.cs:372
monitor.DestijlCommandManager.RobotStartWithWatchdog
CommandStatus RobotStartWithWatchdog()
Start robot, enabling watchdog
Definition: DestijlCommandManager.cs:302
monitor.CommandManager.SendCommand
CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)
Sends a command to TCP server
Definition: CommandManager.cs:171
monitor.RobotCommandList.RobotTurn
const string RobotTurn
Definition: DestijlCommandManager.cs:68
monitor.DestijlCommandList.DataCamOpen
const string DataCamOpen
Definition: DestijlCommandManager.cs:39
monitor.DestijlCommandManager.Open
bool Open(string hostname, int port)
Open connection to server "host", with port number "port"
Definition: DestijlCommandManager.cs:174
monitor.DestijlCommandList.HeaderStmAck
const string HeaderStmAck
Definition: DestijlCommandManager.cs:47
monitor.RobotCommandList.RobotReset
const string RobotReset
Definition: DestijlCommandManager.cs:62
monitor.RobotCommandList
Commands used for robot messages
Definition: DestijlCommandManager.cs:59
monitor.RobotCommandList.RobotStartWithoutWatchdog
const string RobotStartWithoutWatchdog
Definition: DestijlCommandManager.cs:63
monitor.DestijlCommandManager
Specialization class for command manager, which implemnent destijl protocol between monitor and serve...
Definition: DestijlCommandManager.cs:76
monitor.DestijlCommandList.HeaderStmMes
const string HeaderStmMes
Definition: DestijlCommandManager.cs:52
monitor.DestijlCommandManager.RobotPing
CommandStatus RobotPing()
Ping the robot.
Definition: DestijlCommandManager.cs:268
monitor.DestijlCommandList.DataCamInfirm
const string DataCamInfirm
Definition: DestijlCommandManager.cs:43
monitor.DestijlCommandManager.CameraComputePosition
CommandStatus CameraComputePosition()
Request robot position computing
Definition: DestijlCommandManager.cs:526
monitor.DestijlCommandManager.CameraStopComputePosition
CommandStatus CameraStopComputePosition()
Stop robot position computing
Definition: DestijlCommandManager.cs:543
monitor.DestijlCommandManager.CameraArenaConfirm
CommandStatus CameraArenaConfirm()
Confirm arena detection (after requesting image of detected arena, using CameraAskArena ...
Definition: DestijlCommandManager.cs:492
monitor.DestijlCommandManager.CommandStatus
CommandStatus
List of available return status
Definition: DestijlCommandManager.cs:107
monitor.DestijlCommandList.DataComOpen
const string DataComOpen
Definition: DestijlCommandManager.cs:36
monitor.DestijlCommandList.DataComClose
const string DataComClose
Definition: DestijlCommandManager.cs:37
monitor.DestijlCommandManager.CameraAskArena
CommandStatus CameraAskArena()
Request still image of detected arena
Definition: DestijlCommandManager.cs:475
monitor.RobotCommandList.RobotPing
const string RobotPing
Definition: DestijlCommandManager.cs:61
monitor.DestijlCommandList.HeaderStmNoAck
const string HeaderStmNoAck
Definition: DestijlCommandManager.cs:48
monitor.DestijlCommandManager.RobotReset
CommandStatus RobotReset()
Reset robot and let it in idle mode
Definition: DestijlCommandManager.cs:285
monitor.DestijlCommandManager.CameraClose
CommandStatus CameraClose()
Close camera on remote device
Definition: DestijlCommandManager.cs:458
monitor.DestijlCommandList.HeaderStmPos
const string HeaderStmPos
Definition: DestijlCommandManager.cs:51
monitor.DestijlCommandManager.DestijlCommandManager
DestijlCommandManager(CommandReceivedEvent callback)
Initializes a new instance of the monitor.DestijlCommandManager class.
Definition: DestijlCommandManager.cs:121
monitor.DestijlCommandList.DataCamArenaConfirm
const string DataCamArenaConfirm
Definition: DestijlCommandManager.cs:42
monitor.DestijlCommandManager.RobotTurn
CommandStatus RobotTurn(int angle)
Make robot turn left or right, for a given angle
Definition: DestijlCommandManager.cs:355
monitor.DestijlCommandManager.CreateCommand
string CreateCommand(string header, string data)
Creates the command to send to server, based on header and data provided
Definition: DestijlCommandManager.cs:194
monitor.DestijlCommandManager.CameraOpen
CommandStatus CameraOpen()
Open camera on remote device
Definition: DestijlCommandManager.cs:441
monitor.DestijlCommandList.HeaderMtsCamera
const string HeaderMtsCamera
Definition: DestijlCommandManager.cs:33
monitor.DestijlCommandManager.RobotPowerOff
CommandStatus RobotPowerOff()
Power off robot
Definition: DestijlCommandManager.cs:424
monitor.DestijlCommandList.DataCamClose
const string DataCamClose
Definition: DestijlCommandManager.cs:40
monitor
Definition: Client.cs:26
monitor.DestijlCommandList.DataCamAskArena
const string DataCamAskArena
Definition: DestijlCommandManager.cs:41
monitor.DestijlCommandList.HeaderMtsMessage
const string HeaderMtsMessage
Definition: DestijlCommandManager.cs:34
monitor.DestijlCommandList.HeaderMtsComDmb
const string HeaderMtsComDmb
Definition: DestijlCommandManager.cs:31
monitor.Client
Static class for TCP client
Definition: Client.cs:31
monitor.DestijlCommandManager.~DestijlCommandManager
~DestijlCommandManager()
Releases unmanaged resources and performs other cleanup operations before the monitor.DestijlCommandManager is reclaimed by garbage collection.
Definition: DestijlCommandManager.cs:131
monitor.DestijlCommandManager.RobotCloseCom
CommandStatus RobotCloseCom()
Close communication with robot and wait acknowledge
Definition: DestijlCommandManager.cs:251
monitor.DestijlCommandManager.RobotMove
CommandStatus RobotMove(int distance)
Move robot forward or backward, for a distance expressed in millimeter
Definition: DestijlCommandManager.cs:337
monitor.CommandManager.CommandManagerStatus
CommandManagerStatus
Available status when sending command
Definition: CommandManager.cs:63
monitor.DestijlCommandList.DataCamStopComputePosition
const string DataCamStopComputePosition
Definition: DestijlCommandManager.cs:45
monitor.DestijlCommandManager.RobotOpenCom
CommandStatus RobotOpenCom()
Open communication with robot and wait acknowledge
Definition: DestijlCommandManager.cs:234
monitor.RobotCommandList.RobotMove
const string RobotMove
Definition: DestijlCommandManager.cs:67
monitor.DestijlCommandManager.DecodeStatus
CommandStatus DecodeStatus(CommandManager.CommandManagerStatus localStatus, string answer)
Provide DestijlCommandManager.CommandStatus based on status received by CommandManager.SendCommand and answer string
Definition: DestijlCommandManager.cs:205
monitor.DestijlCommandList
Commands and options parameters used in Destijl project when communicating with server ...
Definition: DestijlCommandManager.cs:29
monitor.DestijlCommandManager.Open
bool Open(string hostname)
Open the specified hostname server, using default port number.
Definition: DestijlCommandManager.cs:163
monitor.DestijlCommandList.HeaderStmLostDmb
const string HeaderStmLostDmb
Definition: DestijlCommandManager.cs:49
monitor.RobotCommandList.RobotStartWithWatchdog
const string RobotStartWithWatchdog
Definition: DestijlCommandManager.cs:64
monitor.CommandManager.Close
void Close()
Close connection to server
Definition: CommandManager.cs:115