//******************************
//
// Filename = DumbSurrogate.cpp
// Author = Michael Wales
//
// Version History:
//  1.0   Initial version.  GOTO_XY and FOLLOW_PATH not supported.
//
//******************************

#include"RobotAPI.h"
#include<stdio.h>
#include<string.h>

int main (void)
   {
   TesterClass Test;
   Test.runMe();
   }
   
//************************************//
// CLASS IMPLEMENTATION - TesterClass //
//************************************//

RobotAPIRobot APIR;

int TesterClass::runMe (void)
   {
   // Load configuration
   ConfigSys Configuration;
   Configuration.loadConfiguration();
   printf("Server IP = %s\n", Configuration.ServerIP);
   
   printf("Press RETURN to connect to controller...\n");
   getchar();
   
   APIR.setReceiver( this );
   APIR.setPort( 7000 );
   APIR.establishConnection( Configuration.ServerIP );
   
   printf("Press RETURN to disconnect and exit...\n");
   getchar();

   return 0;
   }
   

void TesterClass::receiveCommand(char InstructionCode, RobotCommand* NewCommand)
   {
   static int BotSpeed = 0;
   static int BotOrientation = 0;
   static int BotX = 0;
   static int BotY = 0;
   
   switch(InstructionCode)
      {
      case 'A':
         // CHANGE ORIENTATION
         printf("Received:  CHANGE_ORIENTATION (%i)\n", NewCommand->Value);
         BotOrientation = NewCommand->Value;
         
         delete NewCommand;
         break;
         
      case 'B':
         // CHANGE_SPEED
         printf("Received:  CHANGE_SPEED (%i)\n", NewCommand->Value);
         BotSpeed = NewCommand->Value;
         
         delete NewCommand;         
         break;
         
      case 'C':
         // GOTO_XY
         printf("Received:  GOTO_XY\n");
         delete NewCommand;
         
         break;
         
      case 'D':
         // FOLLOW_PATH
         printf("Received:  FOLLOW_PATH\n");
         delete NewCommand;
         
         break;
         
      case 'E':
         // STATUS_CHECK
         printf("Received:  STATUS_CHECK\n");
         
         RobotCommand ReturnCommand;
         strcpy( ReturnCommand.CommandName, "CURRENT_STATUS");
         ReturnCommand.Value = BotOrientation;
         ReturnCommand.ValueB = BotSpeed;
         ReturnCommand.XCoordinate = BotX;
         ReturnCommand.YCoordinate = BotY;
         
         printf("Sending:  CURRENT_STATUS(0=%i, S=%i, (%i,%i) )\n", BotOrientation, BotSpeed, BotX, BotY);
         APIR.sendCommand( ReturnCommand );         
         
         delete NewCommand;         
         break;
         
      default:
         printf("Invalid Instruction Code\n");
         
         delete NewCommand;
         return;      
      }
   }
