/** \file
 *
 *  Contains the Aanderaa_O2 class declaration.
 *
 *  WetLabsBB2FL.h should only be included by Aanderaa_O2.cpp
 *  Other classes should include Aanderaa_O2IF.h
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef AANDERAA_O2_H
#define AANDERAA_O2_H

#include "Aanderaa_O2IF.h"

#include "component/SyncComponent.h"
#include "io/LoadControl.h"
#include "io/UartStream.h"
#include "logger/Logger.h"

class UniversalDataWriter;

/**
 *  Provides software interface to the Aanderaa O2 sensor.
 *
 *  \ingroup modules_science
 */

class Aanderaa_O2 : public SyncSensorComponent
{
public:

    Aanderaa_O2( const Module* module );

    virtual ~Aanderaa_O2();

    virtual void run();

    /// Do what needs to be done to run
    /// Similar to initialize, in old init/run/uninit sequence
    virtual RunState start();

    /// Might follow a STOP...START sequence
    virtual RunState starting();

    /// Pause for a short period (indicated by pauseTime)
    virtual RunState pause();

    /// Should eventually follow a PAUSE request: should set continueTime
    virtual RunState paused();

    /// Resume from PAUSE
    virtual RunState resume();

    /// Might follow a PAUSE...RESUME sequence
    virtual RunState resuming();

    /// Should eventually follow a START request or RESETTING
    virtual RunState runnable();

    /// Might occur in case of Error
    virtual RunState resetting()
    {
        return start();
    }

    /// Initial state -- can be later followed by START
    virtual RunState stop();

    virtual RunState stopping();

    /// Initial state -- can be later followed by START
    virtual RunState stopped();

    void uninitialize( void );

    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

private:
    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    Aanderaa_O2( const Aanderaa_O2& old ); // disallow copy constructor

    // Parses the information sent back
    bool parse();

    // returns true if data is requested from one of the readers
    bool isDataRequested();

    // Sends data to DataWriters
    void writeData();

    // Instance of load controller
    LoadControl loadControl_;

    static const unsigned int O2_MOLECULAR_MASS;
    static const unsigned int MAX_DEVICE_RESPONSE = 255;
    static const unsigned short MAX_DEVICE_MSG_QUEUE_SIZE = 5;

    // Stores the current response from the device
    char deviceResponse_[MAX_DEVICE_RESPONSE + 1];

    /// Holds the communications device
    UartStream uart_;

    /// Time data request started
    Timestamp startTime_;

    /// Timeout for getting data
    Timespan timeout_;

    /// How often data should spit out.
    Timespan period_;

    /// Outputs to slate
    DataWriter* temperatureWriter_;
    UniversalDataWriter* o2ConcentrationWriter_;
    DataWriter* airSaturationWriter_;

    // model number of this device
    Str model_;

    /// Debugging outputs
    bool debug_;

    // We have told the instrument to start sampling
    bool startCommanded_;

    // Output variable
    float temperature_;

    // Output variable
    float o2Concentration_;

    // Output variable
    float airSaturation_;

};
#endif /*AANDERAA_O2_H*/
