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

#ifndef ISUS_H
#define ISUS_H

#include "ISUSIF.h"

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

class UniversalDataWriter;

/**
 *  Provides software interface to the ISUS nitrate sensor.
 *
 *  \ingroup modules_science
 */

class ISUS : public SyncSensorComponent
{
public:

    ISUS( const Module* module );

    virtual ~ISUS();

    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.
    ISUS( const ISUS& old ); // disallow copy constructor

    // Parses the header information sent back
    bool parseHeader();

    // Parses the information sent back
    bool parse( char *data );

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

    // Sends data to DataWriters
    void writeData();

    // Queries the LCBs for status
    bool logVoltageAndCurrent();

    // Calculates the crc
    unsigned short fCrc16Bit( const unsigned char* msg );

#define MAX_ISUS_RESPONSE ( 2096U )

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

    /// Holds the communications device
    UartStream uart_;

    /// Power and comms in this case
    LoadControl loadControl_;

    /// Time data request started
    Timestamp startTime_, dataTime_, isusDateString_;

    /// Timeout for getting data and powering up
    Timespan timeout_, dataTimeout_;
    Timespan poTimeout_;

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

    // For CTD data
    DataReader* salinityReader_;
    DataReader* temperatureReader_;
    DataReader* depthReader_;

    /// Outputs to slate
    DataWriter* dateStringWriter_;
    UniversalDataWriter* nitrateWriter_;


    float nitrateAccuracy_;
    float nitrate_;

    char* dateString_;

    /// Debugging outputs
    bool debug_;

};
#endif /*ISUS_H*/
