36 #include "DTrackSDK.hpp" 45 static const unsigned int VERSION_MAJOR = 1;
46 static const unsigned int VERSION_MINOR = 1;
47 static const unsigned int VERSION_PATCH = 0;
50 ERR_WRONG_INPUT_PARAMETER = -101 ,
51 ERR_WRONG_USAGE = -102 ,
52 ERR_DTRACKSDK_INIT = -103 ,
53 ERR_DTRACK2_CMD_SPELLING = -104 ,
54 ERR_OPEN_FILE = -105 ,
61 static void dtrack2_get_and_print_all_event_messages()
82 static int dtrack2_error_to_console()
88 std::cerr <<
"error " << dtrack2Error <<
": " << errorMessage << std::endl;
89 dtrack2_get_and_print_all_event_messages();
98 static void show_help(
const std::string& programName )
100 std::cout <<
"DTrackCLI v" << VERSION_MAJOR <<
"." << VERSION_MINOR <<
"." << VERSION_PATCH << std::endl;
101 std::cout <<
"Usage: " << programName <<
" <ATC hostname or ip> [<action> ...]" << std::endl;
102 std::cout <<
"Apply an action to the ART Controller (ATC) specified by ACTION(s)" << std::endl;
103 std::cout <<
"with <ATC hostname or ip> being either the IP address or the" << std::endl;
104 std::cout <<
"hostname of the ART Controller e.g.: atc-123456 or 12.34.56.78" << std::endl;
105 std::cout <<
"Available actions:" << std::endl;
106 std::cout <<
" -meastart start measurement" << std::endl;
107 std::cout <<
" -meastop stop measurement" << std::endl;
108 std::cout <<
" -shutdown shut down the ART Controller" << std::endl;
109 std::cout <<
" -get <parameter> read and display the value of a DTrack2/DTRACK3 parameter" << std::endl;
110 std::cout <<
" -set <parameter> <value> change the value of a DTrack2/DTRACK3 parameter" << std::endl;
111 std::cout <<
" -cmd <dtrack2 command> send DTrack2/DTRACK3 command directly" << std::endl;
112 std::cout <<
" -f <filename> read and execute DTrack2/DTRACK3 commands from a file" << std::endl;
113 std::cout <<
" -h, --help, /? display this help" << std::endl;
120 static int checkInput(
int argc,
char** argv )
125 bool errorOccured =
false;
128 while ( ( argc >= iArgument ) )
131 if ( iArgument == 2 )
135 std::string command( argv [ iArgument - 1 ] );
139 if ( ( command.compare(
"-h" ) == 0 ) ||
140 ( command.compare(
"--help" ) == 0 ) ||
141 ( command.compare(
"/?" ) == 0 ) ||
142 ( command.compare(
"-meastart" ) == 0 ) ||
143 ( command.compare(
"-meastop" ) == 0 ) ||
144 ( command.compare(
"-shutdown" ) == 0 ) )
149 else if ( ( command.compare(
"-get" ) == 0 ) ||
150 ( command.compare(
"-f" ) == 0 ) ||
151 ( command.compare(
"-cmd" ) == 0 ) )
153 bool haveEnoughCommands = ( argc > iArgument );
154 if ( haveEnoughCommands )
160 else if ( ( command.compare(
"-set" ) == 0 ) )
162 bool haveEnoughCommands = ( argc > iArgument + 1 );
163 if ( haveEnoughCommands )
174 std::cerr <<
"Please check input parameters! (See help)" << std::endl;
176 return ERR_WRONG_INPUT_PARAMETER;
186 static int start_measurement()
188 std::string trackingStatus;
191 bool success = dt->
getParam(
"status active", trackingStatus );
195 if ( trackingStatus.compare(
"mea" ) != 0 && trackingStatus.compare(
"wait" ) != 0 )
199 int errorOccured = dtrack2_error_to_console();
207 int errorOccured = dtrack2_error_to_console();
218 static int stop_measurement()
220 std::string trackingStatus;
223 bool success = dt->
getParam(
"status active", trackingStatus );
227 if ( trackingStatus.compare(
"none" ) != 0 && trackingStatus.compare(
"err" ) != 0 )
231 int errorOccured = dtrack2_error_to_console();
239 int errorOccured = dtrack2_error_to_console();
250 static int get_dtrack2_parameter(
const std::string& someParameter )
252 std::string receivedParameter;
254 bool success = dt->
getParam( someParameter, receivedParameter );
256 std::cout << receivedParameter << std::endl;
259 int errorOccured = dtrack2_error_to_console();
270 static int set_dtrack2_parameter(
const std::string& someParameter )
272 if ( !dt->
setParam( someParameter ) )
274 int errorOccured = dtrack2_error_to_console();
285 static int send_dtrack2_command(
const std::string& rawDtrack2Command )
287 std::string dtrack2Response;
290 if ( isSuccessful == 0 )
291 std::cout << dtrack2Response << std::endl;
294 int errorOccured = dtrack2_error_to_console();
305 static int process_command( std::string someCommand )
308 if ( someCommand.compare( 0, 4,
"get " ) == 0 )
310 someCommand.erase( 0, 4 );
311 int errorOccured = get_dtrack2_parameter( someCommand );
316 else if ( someCommand.compare( 0, 12,
"dtrack2 get " ) == 0 )
318 someCommand.erase( 0, 12 );
319 int errorOccured = get_dtrack2_parameter( someCommand );
324 else if ( someCommand.compare( 0, 4,
"set " ) == 0 )
326 someCommand.erase( 0, 4 );
327 int errorOccured = set_dtrack2_parameter( someCommand );
332 else if ( someCommand.compare( 0, 12,
"dtrack2 set " ) == 0 )
334 someCommand.erase( 0, 12 );
335 int errorOccured = set_dtrack2_parameter( someCommand );
342 if ( someCommand.compare( 0, 8,
"dtrack2 " ) != 0 )
345 someCommand =
"dtrack2 " + someCommand;
347 int errorOccured = send_dtrack2_command( someCommand );
358 static int open_file(
const std::string& file_to_open )
360 std::string readLine;
361 std::ifstream readFile( file_to_open.c_str() );
364 int firstErrorInFile = 0;
367 if ( readFile.is_open() )
370 while ( std::getline( readFile, readLine ) )
372 if ( readLine.empty() )
376 int errorOccured = process_command( readLine );
377 if ( ( firstErrorInFile == 0 ) && errorOccured )
378 firstErrorInFile = errorOccured;
383 int errorOccured = dtrack2_error_to_console();
390 std::cerr <<
"Unable to open file" << std::endl;
391 return ERR_OPEN_FILE;
394 if ( firstErrorInFile == 0 )
397 return firstErrorInFile;
404 static int process_cmd_line_input(
const int& argc,
char** argv )
410 while ( argc >= nCommands )
413 if ( nCommands == 2 )
417 std::string command( argv[ nCommands - 1 ] );
420 if ( ( command.compare(
"-h" ) == 0 ) ||
421 ( command.compare(
"--help" ) == 0 ) ||
422 ( command.compare(
"/?" ) == 0 ) )
425 show_help( argv[ 0 ] );
428 else if ( command.compare(
"-meastart" ) == 0 )
431 int errorOccured = start_measurement();
436 else if ( command.compare(
"-meastop" ) == 0 )
439 int errorOccured = stop_measurement();
444 else if ( command.compare(
"-shutdown" ) == 0 )
447 if ( send_dtrack2_command(
"dtrack2 system shutdown" ) )
449 int errorOccured = dtrack2_error_to_console();
456 else if ( command.compare(
"-get" ) == 0 )
458 std::string dtrack2Get;
460 dtrack2Get = argv[ nCommands ];
461 nCommands = nCommands + 2;
463 int errorOccured = get_dtrack2_parameter( dtrack2Get );
468 else if ( command.compare(
"-set" ) == 0 )
470 std::string dtrack2SetParam;
471 std::string dtrack2SetValue;
472 std::string dtrack2Set;
474 dtrack2SetParam = argv[ nCommands ];
475 dtrack2SetValue = argv[ nCommands + 1 ];
476 nCommands = nCommands + 3;
477 dtrack2Set = dtrack2SetParam +
" " + dtrack2SetValue;
479 int errorOccured = set_dtrack2_parameter( dtrack2Set );
484 else if ( command.compare(
"-cmd" ) == 0 )
486 std::string dtrack2Cmd;
488 dtrack2Cmd = argv[ nCommands ];
489 nCommands = nCommands + 2;
491 if ( dtrack2Cmd.compare( 0, 8,
"dtrack2 " ) != 0 )
494 dtrack2Cmd =
"dtrack2 " + dtrack2Cmd;
497 int errorOccured = send_dtrack2_command( dtrack2Cmd );
502 else if ( command.compare(
"-f" ) == 0 )
504 std::string filename;
506 filename = argv[ nCommands ];
507 nCommands = nCommands + 2;
509 int errorOccured = open_file( filename );
515 std::cout <<
"unknown error occured" << std::endl;
526 int main(
int argc,
char** argv )
531 show_help( argv[ 0 ] );
532 return ERR_WRONG_USAGE;
536 std::string command( argv[ 1 ] );
537 if ( ( command.compare(
"-h" ) == 0 ) ||
538 ( command.compare(
"--help" ) == 0 ) ||
539 ( command.compare(
"/?" ) == 0 ) )
541 show_help( argv[ 0 ] );
546 dt =
new DTrackSDK( (
const char * )argv [ 1 ], 0 );
550 std::cerr <<
"No connection to ART controller! Is \"" << argv[ 1 ] <<
551 "\" a valid controller hostname of ip address?" << std::endl;
553 return ERR_DTRACKSDK_INIT;
560 std::string pipeCommand;
563 int firstErrorInPipe = 0;
566 while ( std::getline( std::cin, pipeCommand ) )
569 int errorOccured = process_command( pipeCommand );
570 if ( ( firstErrorInPipe == 0 ) && errorOccured )
571 firstErrorInPipe = errorOccured;
574 if ( firstErrorInPipe != 0 )
577 return firstErrorInPipe;
584 int paramInputError = checkInput( argc, argv );
585 if ( paramInputError )
588 return paramInputError;
592 int someError = process_cmd_line_input( argc, argv );
bool setParam(const std::string &category, const std::string &name, const std::string &value)
Set DTrack2/DTRACK3 parameter.
std::string getMessageOrigin() const
Get origin of last DTrack2/DTRACK3 event message.
std::string getLastDTrackErrorDescription() const
Get last DTrack2/DTRACK3 command error description.
unsigned int getMessageErrorId() const
Get error id of last DTrack2/DTRACK3 event message.
bool getParam(const std::string &category, const std::string &name, std::string &value)
Get DTrack2/DTRACK3 parameter.
unsigned int getMessageFrameNr() const
Get frame counter of last DTrack2/DTRACK3 event message.
DTrack SDK main class derived from DTrackParser.
std::string getMessageStatus() const
Get status of last DTrack2/DTRACK3 event message.
bool isCommandInterfaceValid() const
Returns if TCP connection for DTrack2/DTRACK3 commands is active.
std::string getMessageMsg() const
Get message text of last DTrack2/DTRACK3 event message.
bool getMessage()
Get DTrack2/DTRACK3 event message from the Controller.
bool stopMeasurement()
Stop measurement.
bool startMeasurement()
Start measurement.
int getLastDTrackError() const
Get last DTrack2/DTRACK3 command error code.
int sendDTrack2Command(const std::string &command, std::string *answer=NULL)
Send DTrack2/DTRACK3 command to DTrack and receive answer (TCP command interface).