/****************************************************************************/
/* Copyright (c) 2012 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : delta_tInteractiveTest.cc                                     */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/17/2012                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <unix.h>
#include <errno.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include <signal.h>
#include <stdlib.h>
#include "Delta_T.h"    // Behavior
#include "Exception.h"
#include "StringConverter.h"
#include "MathP.h"
#include "System.h"
#include "Task.h"
#include "Syslog.h"
#include "DeltaTIF.h"

char cmdbuf[32];
char* prompt()
{
  printf("Enter one of the following (c)ommands:\n");
  printf("\t(G)et data\n");
  printf("\t(S)tart Logging\n");
  printf("\tS(t)op Logging\n");
  return gets(cmdbuf);
}

char sonarbuf[32];
DeltaTIF::Sonar sonar()
{
  printf("Which sonar?:\n");
  printf("\t(S)ide sonar\n");
  printf("\t(F)wd sonar\n");
  char *s = gets(sonarbuf);

  if (!strcasecmp(sonarbuf, "f"))
    return DeltaTIF::Fwd;
  else
    return DeltaTIF::Side;
}

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

  printf("Starting delta_tServer...\n");

  char *program = "delta_tServer";

  char errorBuf[256];
  char buf[256];
 
  pid_t pid;
 
  //Delta_T *dt_behavior = new Delta_T();

  switch ((pid = fork())) {
     
  case 0:
    // In child
    // Execute driver program
    execlp(program, program, (char *)0);

    sprintf(errorBuf, 
	    "Task::spawnServer() - exec() of \"%s\" failed", program);

    perror(errorBuf);
 
    break;

  case -1:
    perror("Task::spawnServer() - fork() failed");
    return -1;
  }

  DeltaTIF *mbs;
  
  try {
    mbs = new DeltaTIF("Delta_TServer");
  }
  catch (Exception e) {
    fprintf(stderr, "Caught exception: %s\n", e.msg);
    error = True;
  }
  catch (...) {
    fprintf(stderr, "Caught some exception...");
    error = True;
  }

  char* cmd;
  DeltaTIF::Sonar idt = DeltaTIF::Side;
  do {
    cmd = prompt();
    idt = sonar();

    if (!strcasecmp(cmd, "g")) {
      printf("\nOk, getting data from %s sonar...\n", sonarbuf);
      DeltaTIF::Data data;
      mbs->get(idt, &data);
      printf("%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    }
    else if (!strcasecmp(cmd, "s")) {
      printf("\nOk, starting logging on %s sonar...\n", sonarbuf);
      mbs->set_idt_logging(idt, True);
    }
    else if (!strcasecmp(cmd, "t")) {
      printf("\nOk, stopping logging on %s sonar...\n", sonarbuf);
      mbs->set_idt_logging(idt, False);
    }

  } while (strcasecmp(cmd, "q"));

  kill(0, SIGTERM);
  exit(0);

  printf("Started: executing 10 second delay...\n");
  sleep(10);

  printf("Start logging on Fwd::IDT and locally\n");
  mbs->start(DeltaTIF::Fwd);
  mbs->start(DeltaTIF::Side);
  mbs->set_idt_logging(DeltaTIF::Fwd, True);

  for (int i = 0; i < 5; i++)
  {
    sleep(1);
    DeltaTIF::Data data;
    mbs->get(DeltaTIF::Fwd, &data);
    printf("Fwd:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    mbs->get(DeltaTIF::Side, &data);
    printf("Side:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
  }

  printf("Stop logging on Fwd::IDT and locally, start on Side::IDT\n");
  mbs->stop(DeltaTIF::Fwd);
  mbs->stop(DeltaTIF::Side);
  mbs->set_idt_logging(DeltaTIF::Fwd, False);
  mbs->set_idt_logging(DeltaTIF::Side, True);

  for (i = 0; i < 3; i++)
  {
    sleep(1);
    DeltaTIF::Data data;
    mbs->get(DeltaTIF::Fwd, &data);
    printf("Fwd:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    mbs->get(DeltaTIF::Side, &data);
    printf("Side:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
  }

  printf("Set nbeams to 480/240 and 120\n");
  mbs->set_nbeams(DeltaTIF::Fwd, 480);
  mbs->set_nbeams(DeltaTIF::Side, 240);
  sleep(5);
  mbs->set_nbeams(DeltaTIF::Fwd, 120);
  mbs->set_nbeams(DeltaTIF::Side, 120);
  sleep(5);

  printf("Set sector size to 30/60 and 120\n");
  mbs->set_sector_size(DeltaTIF::Fwd, 30);
  mbs->set_sector_size(DeltaTIF::Side, 60);
  sleep(5);
  mbs->set_sector_size(DeltaTIF::Fwd, 120);
  mbs->set_sector_size(DeltaTIF::Side, 120);
  sleep(5);

  printf("Start logging on Fwd::IDT and locally\n");
  mbs->start(DeltaTIF::Fwd);
  mbs->set_idt_logging(DeltaTIF::Fwd, True);

  for (i = 0; i < 3; i++)
  {
    sleep(1);
    DeltaTIF::Data data;
    mbs->get(DeltaTIF::Fwd, &data);
    printf("Fwd:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    mbs->get(DeltaTIF::Side, &data);
    printf("Side:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
  }

  printf("Set beam width to Wide and Narrow-Mixed\n");
  mbs->set_beam_width(DeltaTIF::Fwd, 0);
  mbs->set_beam_width(DeltaTIF::Side, 1);
  sleep(5);
  mbs->set_beam_width(DeltaTIF::Fwd, 3);
  mbs->set_beam_width(DeltaTIF::Side, 3);
  sleep(5);

  printf("Set averaging to Off and 5\n");
  mbs->set_averaging(DeltaTIF::Fwd, 1);
  mbs->set_averaging(DeltaTIF::Side, 1);
  sleep(5);
  mbs->set_averaging(DeltaTIF::Fwd, 5);
  mbs->set_averaging(DeltaTIF::Side, 3);
  sleep(5);

  printf("Set range to 30/10 and 100\n");
  mbs->set_range(DeltaTIF::Fwd, 30);
  mbs->set_range(DeltaTIF::Side, 10);
  sleep(5);
  mbs->set_range(DeltaTIF::Fwd, 100);
  mbs->set_range(DeltaTIF::Side, 100);

  for (i = 0; i < 3; i++)
  {
    sleep(1);
    DeltaTIF::Data data;
    mbs->get(DeltaTIF::Fwd, &data);
    printf("Fwd:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    mbs->get(DeltaTIF::Side, &data);
    printf("Side:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
  }

  printf("Stop logging on IDT and locally\n");
  mbs->stop(DeltaTIF::Side);
  mbs->set_idt_logging(DeltaTIF::Side, False);
  mbs->stop(DeltaTIF::Fwd);
  mbs->set_idt_logging(DeltaTIF::Fwd, False);

  printf("Set gain to 19and 10\n");
  mbs->set_gain(DeltaTIF::Fwd, 19);
  mbs->set_gain(DeltaTIF::Side, 19);
  sleep(5);
  mbs->set_gain(DeltaTIF::Fwd, 10);
  mbs->set_gain(DeltaTIF::Side, 10);
  sleep(5);

  printf("Set gain_eq true then false\n");
  mbs->set_gain_eq(DeltaTIF::Fwd, True);
  mbs->set_gain_eq(DeltaTIF::Side, True);
  sleep(5);
  mbs->set_gain_eq(DeltaTIF::Fwd, False);
  mbs->set_gain_eq(DeltaTIF::Side, False);
  sleep(5);

  printf("Set sos to 1455 then 1492\n");
  mbs->set_sos(DeltaTIF::Fwd, 1455);
  sleep(5);
  mbs->set_sos(DeltaTIF::Fwd, 1492);
  mbs->set_sos(DeltaTIF::Side, 1492);
  sleep(5);

  printf("Start logging locally\n");
  mbs->start(DeltaTIF::Fwd);
  mbs->start(DeltaTIF::Side);

  for (i = 0; i < 3; i++)
  {
    sleep(1);
    DeltaTIF::Data data;
    mbs->get(DeltaTIF::Fwd, &data);
    printf("Fwd:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
    mbs->get(DeltaTIF::Side, &data);
    printf("Side:%f %d %d %d\n", data.range, data.update_time.seconds, data.enabled, data.interval);
  }

  // Signal all children
  kill(0, SIGTERM);
  Syslog::write("OK, LEAVING!!!\n");

  printf("Ending delta_t...\n");

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

