/** \file
 *
 *  Contains the AbortSample class implementation.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#include "AbortSample.h"
#include "AbortSampleIF.h"

#include "units/Units.h"
#include "scienceModule/ESPComponentIF.h"

AbortSample::AbortSample( const Str& prefix,  const Module* module )
    : Behavior( prefix + AbortSampleIF::NAME, module, true, true ),
      initialized_( false )
{
    logger_.syslog( "Construct." );

    stopSamplingWriter_ = newDataWriter( ESPComponentIF::STOP_SAMPLING );
}

AbortSample::~AbortSample()
{}

/// Initialize function
void AbortSample::initialize( void )
{
    logger_.syslog( "Initializing AbortSample.", Syslog::INFO );

    stopSamplingWriter_->write( Units::BOOL, 0 );
    initialized_ = true;
}

/// Uninit function
void AbortSample::uninitialize()
{
    logger_.syslog( "Uninitializing AbortSample.", Syslog::INFO );

    stopSamplingWriter_->write( Units::BOOL, 1 );
    initialized_ = false;
}

/// The actual "payload" of the component
void AbortSample::run()
{
    runIfUnsatisfied();
}

/// Runs and returns true if acoustic timer has expired, otherwise returns false
bool AbortSample::runIfUnsatisfied()
{
    if( !initialized_ )
    {
        initialize();
    }

    return false;
}

/// isSatisfied will return false to prevent inserting multipule stop commands
bool AbortSample::isSatisfied()
{
    if( !initialized_ )
    {
        initialize();
    }

    return false;
}

/// Mission Component factory interface
Behavior* AbortSample::CreateBehavior( const Str& prefix, const Module* module )
{
    return new AbortSample( prefix, module );
}
