/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Cathx.cc                                                      */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/14/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/

#include "Cathx.h"
#include "IntegerAttribute.h"
#include "Syslog.h"
#include "System.h"
#include "TimeP.h"

Cathx::Cathx()
  : Behavior(CathxBehaviorName, Sequential), _server(NULL),
  _capture(-1), _profile(-1)
{
  Boolean debug = False;

  //  Start/stop image capture 
  attributes.add(new IntegerAttribute("capture",
      "Image capture button (1=On,0=Off)",
              &_capture, -1));

  //  Profile choice 
  attributes.add(new IntegerAttribute("profile",
      "Set the desired profile (1, 2, 3, ...)",
              &_profile, -1));
  
   if (!Behavior::_cathx)
   {
     try {
        Behavior::_cathx = new CathxIF("CathxServer");
     }
     catch (Exception e) {
       Syslog::write("Cathx Behavior - %s", e.msg);
       Behavior::_cathx = NULL;
     }
     catch (...) {
       Syslog::write("Cathx Behavior - Exception while connecting to CathxServer");
       Behavior::_cathx = NULL;
     }
   }
   _server = Behavior::_cathx;

}

Cathx::~Cathx()
{
   if( !_server) 
   {
      delete _server;
      _server = NULL;
   }
}

Boolean Cathx::shouldBehaviorStart( void ) 
{
  return bothSequence();
}

void Cathx::execute()
{
  DeviceIF::Status status;
  Boolean debug = False;

  dprintf("Cathx - Id:%d, Duration:%d, capture:%d, profile:%d",
              _id, _duration, _capture, _profile);

  if (_server == NULL)
  {
    Syslog::write("Cathx::execute() - Sorry, not connected to server");
    return;
  }

  CathxIF::Actions actions;
  actions.imageCapture  = -1;
  actions.profileNumber = -1;

  if (_capture < 0 && _profile < 0)
  {
    Syslog::write("Cathx::execute() - %d all done!", _id);
    setState(Finished);     // Nothing more to do
    return;
  }

  // Perform the actions listed in the behavior once
  //
  if (_capture >= 0)
  {
    // Request the capture to be set or reset
    //
    Syslog::write("Cathx Behavior %d: Setting capture to %d", _id, _capture);
    actions.imageCapture = _capture;

    _capture = -1;
  }

  if (_profile > 0)
  {
    // Request a different camera profile
    //
    Syslog::write("Cathx Behavior %d: Setting profile to %d", _id, _profile);
    actions.profileNumber = _profile;

    _profile = -1;
  }

  _server->actions(&actions);

  return;
}


Boolean Cathx::validInput()
{
  Boolean valid = True;

  if (_capture < -1)
  {
    Syslog::write("Cathx Behavior %d: invalid value for capture: %d",
      _id, _capture);
    valid = False;
  }

  if (_profile < -1 )
  {
    Syslog::write("Cathx Behavior %d: invalid value for profile: %d",
      _id, _profile);
    valid = False;
  }

  return valid;
}

