/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWSENS_PROCSENSOR_H
#define __IWSENS_PROCSENSOR_H

#ifndef __IWSENS_DATASENSOR_H
#include <iwsens/datasensor.h>
#endif

#include <fstream>
#include <vector>
#include <iwear/triple.h>
using std::ifstream;

namespace iwear
{
    namespace sensor
    {
class SensorHandler;
/**
 * A ProcSensor DataSensor. This will give the seconds since epoch as a double.
 */
class ProcSensor: public DataSensor {
private:
    ProcSensor();
protected:
    string path;

    string fmtstr;

    std::vector<DataSensor*> subsens;

    std::vector<triple<double, double, units> > modvec;

    mutable std::vector<double> values;

    datasensor_type senstype;

    mutable timeval lastvaltime;

    bool keep_open;

    mutable ifstream procfile;

    void read_values( void ) const;

    string fline;

    static void read_sscanf(const char*, const char*, std::vector<double>& );
public:

    /**
     * Dummy feed a line to use this sensor for a multiline /proc file from
     * externals
     */
    void feed_line( const string& fl ) { fline = fl; }

    ProcSensor( SensorHandler*, const string& path,
	    const string& fmt,
	    const std::vector<triple<double, double, units> >& mods,
	    bool keepopen = false,
	    datasensor_type dst = proc_data );

    virtual ~ProcSensor();
    /**
     * Returns the actual luminous flux in lux of this sensor
     */
    virtual double get_value( void ) const { return get_subvalue(0); }

    virtual double get_subvalue( uint32_t sb) const; 
    /**
     * ProcSensor is measured in luminous flux = lux
     */
    virtual units get_unit( void ) const { return get_subunit(0); }

    virtual units get_subunit( uint32_t sb ) const;

    /**
     * We know about the time so we tell it. 
     */
    virtual datasensor_type get_datasensor_type( void ) const { return senstype; }

    /**
     * Module requires us to implement this. But we dont have one so just
     * return succes.
     */
    virtual uint32_t activate_power_state( power_state ) { return 0; }

    virtual DataSensor* get_subsensor( uint32_t );

    virtual const DataSensor* get_subsensor( uint32_t ) const;

};

} // namespace sensor
} // namespace iwear
#endif

