/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Trn.cc                                                        */
/* Author   : henthorn                                                      */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 12/04/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/

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

Trn::Trn()
  : Behavior(TrnBehaviorName, Sequential), _updates(-1)
{
  Boolean debug = False;

  //  Start/stop image comms 
  attributes.add(new IntegerAttribute("updates",
      "Send TRN updates? (1=Yes,0=No (default))",
              &_updates, -1));

   if (!Behavior::_trn)
   {
     try {
        Behavior::_trn = new TerrainAidIF("TerrainAidServer");
     }
     catch (Exception e) {
       Syslog::write("Trn Behavior - %s", e.msg);
       Behavior::_trn = NULL;
     }
     catch (...) {
       Syslog::write("Trn Behavior - Exception while connecting to TerrainAidServer");
       Behavior::_trn = NULL;
     }
   }
   _server = Behavior::_trn;

}

Trn::~Trn()
{
}

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

void Trn::execute()
{
  Boolean debug = False;

  dprintf("Trn - Id:%d, Duration:%d, comms:%d", _id, _updates);


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

  if (_server == NULL) return;

  // Perform the actions listed in the behavior once
  //
  if (_updates >= 0)
  {
    // Request the comms to be set or reset
    //
    Syslog::write("Trn Behavior %d: Setting updates to %d", _id, _updates);

    // Trigger NewOutput event to subscribers
    //
    _server->trnSwitch(_updates);

    _updates = -1;
  }

  return;
}


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

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

  return valid;
}

