/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _watchdogTest.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 "EventService.h"
#include "WatchdogIF.h"
#include "WatchdogServer.h"
#include "Syslog.h"
#include "Task.h"
#include "System.h"

class WatchdogTest : public Task {

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

protected:
  WatchdogIF *watchdog;
  pid_t _watchdogServer;

};

WatchdogTest::WatchdogTest(int argc, char **argv)
  :Task("WatchdogTest")
{
  if ((_watchdogServer = System::spawn("watchdogServer","")) == -1)
    {
      Syslog::write("System::spawn(watchdogSErver) failed\n");
    }
  try
    {
      watchdog = new WatchdogIF("watchdog");
    }
  
  catch (Exception e) {
    Syslog::write("watchdogTest - caught Exception: %s", e.msg);
    exit(1);
  }
  catch (...) {
    Syslog::write("watchdogTest - caught unknown exception");
    exit(1);
  }
  
}

WatchdogTest::~WatchdogTest()
{
  delete watchdog;
  kill (_watchdogServer, SIGTERM);
}

void WatchdogTest::run()
{
  char buf[256];

  
  while (True)
    {

  //get status
      //print some status

      printf("Watchdog Test Program\n enter a command:\n");
      printf("O -- turn deadman (o)n (default 5 minute kill timer)\n");
      printf("F -- turn deadman of(f)\n");
      printf("r -- (r)aise antenna \n");
      printf("l -- (l)ower antenna\n");
      
      gets(buf);
      char *token = strtok(buf, " \t");
      if (!token)
	continue;
      if (!strcmp(token, "O"))
	{
	  watchdog->Deadman_On();
	}
      else if (!strcmp(token, "F"))
	{
	  watchdog->Deadman_Off();
	}
      else if (!strcmp(token, "r"))
	{
	  watchdog->raiseAntenna();
	}
      else if (!strcmp(token, "l"))
	{
	  watchdog->lowerAntenna();
	}
    }
}


int main (int argc, char **argv)
{
  Boolean error = False;
  Boolean debug = True;
  WatchdogTest *test = 0;

  try{
    test = new WatchdogTest(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;
  dprintf("Kill child processes....");
  kill(0, SIGTERM);

  if (error)
       return 1;
  else
    return 0;
}
