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

#ifndef RDI_PATHFINDERUP_H
#define RDI_PATHFINDERUP_H

#include "RDI_PathfinderUpIF.h"

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

#include "data/Point3D.h"
#include "data/Point6D.h"
#include "data/Matrix3x3.h"

class UniversalDataReader;
class UniversalDataWriter;


#define UART_BUFSIZE 10240L

class RDI_PathfinderUp : public SyncSensorComponent
{
public:

    RDI_PathfinderUp( const Module* module );

    virtual ~RDI_PathfinderUp();

    virtual void run();
    virtual RunState start();
    virtual RunState starting();
    virtual RunState pause();
    virtual RunState paused();
    virtual RunState resume();
    virtual RunState resuming();
    virtual RunState runnable();

    virtual RunState resetting()
    {
        return stop();
    }

    virtual RunState stop();
    virtual RunState stopping();
    virtual RunState stopped();
    void uninitialize();
    void logVoltageAndCurrent();
    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

private:
    RDI_PathfinderUp( const RDI_PathfinderUp& old );

    // scan configuraiton settings
    void readConfig();

    bool checkTimeouts( void );
    bool simPD6( void );
    bool simPD13( void );
    bool readPD6( void );
    bool readPD13( void );
    bool parsePD6( void );
    bool parsePD13( void );
    void processPD6( void );
    void processPD13( void );
    void writeData( void );
    void setAcousticWritersInvalid( bool invalid );

    bool hasPD13_;

    Timespan dvlTimeout_;
    Timespan dvlFailTimeout_; // How long we can go without valid data
    Timespan powerOnTimeout_; // How long it takes for the DVL to begin sending data
    Timestamp startTime_, dvlFailTime_;

    DataReader* power24vConverterDataReader_;

    //UniversalDataWriter* altitudeWriter_;
    DataWriter* altitudeWriter_;
    DataWriter* xVelWrtGroundWriter_;
    DataWriter* yVelWrtGroundWriter_;
    DataWriter* zVelWrtGroundWriter_;

    DataWriter* veloInstFlagWriter_;
    DataWriter* beam1RangeWriter_;
    DataWriter* beam2RangeWriter_;
    DataWriter* beam3RangeWriter_;
    DataWriter* beam4RangeWriter_;

    bool isDataRequested();

    bool debug_;
    bool dvlTimeoutArmed_;
    char deviceResponse_[UART_BUFSIZE];
    UartStream uart_;
    LoadControl loadControl_;

    int status_;

    Point3D veloInst_;
    double veloInstX_;
    double veloInstY_;
    double veloInstZ_;
    double veloError_;
    int veloInstFlag_;

    double altEstimate_;
    double beam1Range_, beam2Range_, beam3Range_, beam4Range_;

    int bitError_;
};
#endif /*RDI_PATHFINDERUP_H*/
