/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _simulatedfuelCellTest.cc                                     */
/* Author   : John Rieffel                                                  */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <process.h>
#include <signal.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

#include "mvc_fcc_api.h"
#include "EventService.h"
#include "SimulatedFuelCellIF.h"
#include "SimulatedFuelCell.h"
#include "Syslog.h"
#include "Task.h"
#include "System.h"

extern "C"{
Boolean debug_flag=FALSE;       // enable debug statements 
nid_t target_node;   
char *ProgramName;    
};

class SimulatedFuelCellTest : public Task {

public:
  SimulatedFuelCellTest(int argc, char **argv);
  virtual ~SimulatedFuelCellTest();
  virtual void run();
  //  void event_catch(TaskInterface *taskInterface, EventCode code);

protected:
  SimulatedFuelCellIF::FCC_data fcc_data;
  SimulatedFuelCellIF::FCC_status fcc_status;
  SimulatedFuelCellIF *simulatedfuelcell;
  //  pid_t _fuelCellServer;
  // EventService *_eventService;
  

};

SimulatedFuelCellTest::SimulatedFuelCellTest(int argc, char **argv)
  :Task("SimulatedFuelCellTest")
{
  // do nothing for now
  try
    {
      simulatedfuelcell = new SimulatedFuelCellIF("simulatedfuelcell");
    }
  
  catch (Exception e) 
    {
    Syslog::write("simulatedfuelCellTest - caught Exception: %s", e.msg);
    exit(1);
    }
  catch (...) 
    {
    Syslog::write("simulatedfuelCellTest - caught unknown exception");
    exit(1);
    }
  
}

SimulatedFuelCellTest::~SimulatedFuelCellTest()
{
  delete simulatedfuelcell;

}


void SimulatedFuelCellTest::run()
{
  char buf[256];
  
  while (True)
    {

      int reply_code;


  //get status
      reply_code = simulatedfuelcell->get_simFCC_status(&fcc_status);
      
      if(reply_code != FCC_ERR_OK)
	fprintf(stdout, "Error from simulatedfuelcell->getstatus\n");
      else
	{
	  //print some status
	  printf("control status: %d, health: %10x, fc remaining kwh: %d, bat remaining wh: %d, bit status: %d, bit_result: %d \n", 
		 fcc_status.fc_control_state,
		 fcc_status.health,
		 fcc_status.FC_remaining_kwh,
		 fcc_status.RB_remaining_wh,
		 fcc_status.BIT_status,
		 fcc_status.BIT_result);
	  
	}
      
      printf("Simulated Fuel Cell Test Program\n enter a command:\n");
      printf("'h' -- toggle HEALTHY \n");
      printf("'vy' -- toggle VOLTAGE_YELLOW\n");
      printf("'vr' -- toggle VOLTAGE_RED\n");
      printf("'aty' -- toggle ANOLYTE_TEMP_YELLOW\n");
      printf("'atr'   -- toggle ANOLYTE_TEMP_RED\n");
      printf("'cty' -- toggle CATHOLYTE_TEMP_YELLOW\n");
      printf("'ctr' -- toggle CATHOLYTE_TEMP_RED\n");
      printf("'pty' -- toggle PEROXIDE_TEMP_YELLOW\n");
      printf("'ptr' -- toggle PEROXIDE_TEMP_RED\n");
      printf("'aay' -- toggle ANOLYTE_A_CURRENT_YELLOW\n");
      printf("'aar' -- toggle ANOLYTE_A_CURRENT_RED\n");
      printf("'aby' -- toggle ANOLYTE_B_CURRENT_YELLOW\n");
      printf("'abr' -- toggle ANOLYTE_B_CURRENT_RED\n");
      printf("'ccy' -- toggle CATHOLYTE_CURRENT_YELLOW\n");
      printf("'ccr' -- toggle CATHOLYTE_CURRENT_RED\n");
      printf("'pcy' -- toggle PEROXIDE_CURRENT_YELLOW\n");
      printf("'pcr' -- toggle PEROXIDE_CURRENT_RED\n");
      printf("'rvy' -- toggle RB_VOLTAGE_YELLOW\n");
      printf("'rvr' -- toggle RB_VOLTAGE_RED\n");
            
      gets(buf);
      char *token = strtok(buf, " \t");
      if (!token)
	continue;
      if (!strcmp(token, "h"))
	{
	  // healthy, 
	  fcc_status.health = FCC_HEALTHY;
	}
      else if (!strcmp(token, "vy"))
	{
	  //do a bitwise OR
	  fcc_status.health = (fcc_status.health | FCC_ERR_FC_VOLTAGE_YELLOW);
	}

      else if (!strcmp(token, "vr"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_FC_VOLTAGE_RED);
	}

      else if (!strcmp(token, "aty"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_ANOLYTE_TEMP_YELLOW);
	}

      else if (!strcmp(token, "atr"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_ANOLYTE_TEMP_RED);
	}

      else if (!strcmp(token, "cty"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_CATHOLYTE_TEMP_YELLOW );
	}

      else if (!strcmp(token, "ctr"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_CATHOLYTE_TEMP_RED);
	}

      else if (!strcmp(token, "pty"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_PEROXIDE_TEMP_YELLOW );
	}

      else if (!strcmp(token, "ptr"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_PEROXIDE_TEMP_RED );
	}

      else if (!strcmp(token, "aay"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_ANOLYTE_A_CURRENT_YELLOW);
	}

      else if (!strcmp(token, "aar"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_ANOLYTE_A_CURRENT_RED );
	}

      else if (!strcmp(token, "aby"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_ANOLYTE_B_CURRENT_YELLOW );
	}
      else if (!strcmp(token, "abr"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_ANOLYTE_B_CURRENT_RED );
	}
      else if (!strcmp(token, "ccy"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_CATHOLYTE_CURRENT_YELLOW);
	}
      else if (!strcmp(token, "ccr"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_CATHOLYTE_CURRENT_RED );
	}
      else if (!strcmp(token, "pcy"))
	{
	  fcc_status.health = (fcc_status.health |FCC_ERR_PEROXIDE_CURRENT_YELLOW );
	}
      else if (!strcmp(token, "pcr"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_PEROXIDE_CURRENT_RED );
	}
      else if (!strcmp(token, "rvy"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_RB_VOLTAGE_YELLOW );
	}
      else if (!strcmp(token, "rvr"))
	{
	  fcc_status.health = (fcc_status.health | FCC_ERR_RB_VOLTAGE_RED);
	}

      
      simulatedfuelcell->set_simFCC_status(&fcc_status);
      
    }
  
}



int main (int argc, char **argv)
{
  Boolean error = False;
  Boolean debug = True;
  SimulatedFuelCellTest *test = 0;
  
  target_node = 2;
  
  ProgramName = argv[0];
  
  
  try{
    test = new SimulatedFuelCellTest(argc, argv);
    test->run();
  }
  catch (Exception e) {
    fprintf(stderr, "Caught exception: %s\n", e.msg);
    error = True;
  }
  catch (...) {
    fprintf(stderr, "Caught some exception...");
    error = True;
  }
  dprintf("deleting test\n");
  delete test;
  
  if (error)
    return 1;
  else
    return 0;
}

