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

#ifndef SONARDYNE_NANO_H_
#define SONARDYNE_NANO_H_

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

class UniversalDataReader;
class UniversalDataWriter;

/**
 *  Provides software interface to the Keller depth sensor
 *  Can also use SimSlate to provide simulated depth values when
 *  they are available.
 *
 *  Sonardyne_Nano.h should only be included by Sonardyne_Nano.cpp
 *  Other classes should include Sonardyne_NanoIF.h
 *
 *  \ingroup modules_sensor
 */
class Sonardyne_Nano: public SyncSensorComponent
{
public:
    Sonardyne_Nano( const Module* module );

    virtual ~Sonardyne_Nano();

    /// 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 stop();
    }

    /// 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();

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

    /// Instance of load controller
    LoadControl loadControl_;

    /// Holds the communications device
    UartStream uart_;

    // Max lengths of device request and response
    static const unsigned int MAX_DEVICE_RESP = 128;
    static const unsigned int MAX_DEVICE_REQUEST = 15;

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

    // Stores the next request for the device
    char nextRequest_[MAX_DEVICE_REQUEST + 1];

    /// Max number of packets queued up in the buffer
    static const unsigned int MAX_DEVICE_MSG_QUEUE_SIZE = 5;

    /// Reads in component configuration
    bool readConfig();

    // Read and parse device response, returns true if status was read
    bool readAndParseData();

    // Get next command to send to device, true if command indicated
    bool getNextRequest();

    // Parse volatile status response
    void parseVolatile();

    /// Logs voltage and current and check for any faults
    void logVoltageAndCurrent();

    // Slate data writer
    DataWriter* chargeWriter_;

    /// Slate configuration readers
    ConfigReader* chargeMaxCfgReader_;
    ConfigReader* chargeMinCfgReader_;

    /// Holds the power-on timeout
    Timespan poTimeout_;

    /// Holds the component's data timeout
    Timespan requestTimeout_, dataTimeout_;

    /// Hold the component's start and data times
    Timestamp startTime_, statusTimestamp_;

    /// Holds the max charge bound config setting
    int chargeMax_;

    /// Holds the min charge bound config setting
    int chargeMin_;

    /// Holds the latest charge measurement
    int lastCharge_;

    /// Holds the device ID reading
    int uuid_;

    // False self-test status
    bool selfTestPassed_;

    /// Holds last device charging status
    bool charging_;

    /// Enables debug syslog output
    bool debug_;
};

#endif /* SONARDYNE_NANOX33_H_ */
