/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _cathxm12.cc                                                  */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/03/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "CathxM12.h"
#include "Syslog.h"
#include "Exception.h"
#include "System.h"

#define DEFAULT_PERIOD 1000  // milliseconds

int main( int argc, char **argv )
{

  Syslog::write("cathxM12 driver app ...");

  // Get ip and port options from argv
  //
  unsigned long port = 6000;
  unsigned long period = DEFAULT_PERIOD; // Sample period in milliseconds
  char *host = "134.89.32.15";
  int c;
  while ( (c = getopt(argc, argv, "h:p:m:")) != EOF )
  {
     switch (c) {
        case 'h':
        host = strdup(optarg);
        Syslog::write("cathxM12 - Host ip is now %s...", host);
        break;

        case 'p':
        port = atol(optarg);
        Syslog::write("cathxM12 - Host port is now %ld...", port);
        break;

        case 'm':
        period = atol(optarg);
        // Limits are 10Hz and once every minute
        if (period < 100 || period > 60000) period = DEFAULT_PERIOD;
        Syslog::write("cathxM12 - Period is %ld...", period);
        break;

        default:
        Syslog::write("cathxM12 - Unknown option %d...", c);
        break;
     }
  }

  Syslog::write("cathxM12 host ip is %s...", host);
  Syslog::write("cathxM12 host port is %ld...", port);
  Syslog::write("cathxM12 host port is %ld...", period);
  CathxM12 *m12 = 0;

  try {
    m12  = new CathxM12("cathxM12", host, port, period);
    //m12->initialize();
    Syslog::write("cathxM12 running...");
    m12->run();
  }
  catch (Exception e) {
    Syslog::write("cathxM12 - caught exception: %s\n", e.msg);
  }
  catch (...) {
    Syslog::write("cathxM12 - caught exception!\n");
  }

  delete m12;
  kill(0, SIGTERM);
  return 0;
}
