static const char *RcsId = "$Id:  $";
//+=============================================================================
//
// file :         Keithley428.cpp
//
// description :  C++ source for the Keithley428 and its commands. 
//                The class is derived from Device. It represents the
//                CORBA servant object which will be accessed from the
//                network. All commands which can be executed on the
//                Keithley428 are implemented in this file.
//
// project :      TANGO Device Server
//
// $Author:  $
//
// $Revision:  $
//
// $Revision:  $
// $Date:  $
//
// SVN only:
// $HeadURL: $
//
// CVS only:
// $Source:  $
// $Log:  $
//
// copyleft :     European Synchrotron Radiation Facility
//                BP 220, Grenoble 38043
//                FRANCE
//
//-=============================================================================
//
//  		This file is generated by POGO
//	(Program Obviously used to Generate tango Object)
//
//         (c) - Software Engineering Group - ESRF
//=============================================================================



//===================================================================
//
//	The following table gives the correspondence
//	between commands and method name.
//
//  Command name        |  Method name
//	----------------------------------------
//  State               |  dev_state()
//  Status              |  dev_status()
//  PerformZeroCorrect  |  perform_zero_correct()
//  FilterOn            |  filter_on()
//  FilterOff           |  filter_off()
//  AutoFilterOn        |  auto_filter_on()
//  AutoFilterOff       |  auto_filter_off()
//
//===================================================================


#include <tango.h>
#ifndef USE_HELPERS_DIR
#include <PogoHelper.h>
#else
#include <helpers/PogoHelper.h>
#endif
#include <Keithley428.h>
#include <Keithley428Class.h>

namespace Keithley428_ns
{

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::Keithley428(string &s)
// 
// description : 	constructor for simulated Keithley428
//
// in : - cl : Pointer to the DeviceClass object
//      - s : Device name 
//
//-----------------------------------------------------------------------------
Keithley428::Keithley428(Tango::DeviceClass *cl,string &s)
    :Tango::Device_4Impl(cl,s.c_str())
{
    init_device();
}

Keithley428::Keithley428(Tango::DeviceClass *cl,const char *s)
    :Tango::Device_4Impl(cl,s)
{
    init_device();
}

Keithley428::Keithley428(Tango::DeviceClass *cl,const char *s,const char *d)
    :Tango::Device_4Impl(cl,s,d)
{
    init_device();
}
//+----------------------------------------------------------------------------
//
// method : 		Keithley428::delete_device()
// 
// description : 	will be called at device destruction or at init command.
//
//-----------------------------------------------------------------------------
void Keithley428::delete_device()
{
    //	Delete device allocated objects

    DELETE_DEVSTRING_ATTRIBUTE(attr_GainStr_read);
    DELETE_SCALAR_ATTRIBUTE(attr_Overloaded_read);
    DELETE_DEVSTRING_ATTRIBUTE(attr_filterState_read);
    DELETE_DEVSTRING_ATTRIBUTE(attr_autoFilterState_read);

    if (gpib_device != NULL)
        delete gpib_device; 

    if (buffer != NULL)
        delete buffer; 
}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::init_device()
// 
// description : 	will be called at device initialization.
//
//-----------------------------------------------------------------------------
void Keithley428::init_device()
{
    INFO_STREAM << "Keithley428::Keithley428() create device " << device_name << endl;

    // Initialise variables to default values
    //--------------------------------------------
    get_device_property();

    CREATE_DEVSTRING_ATTRIBUTE(attr_GainStr_read,MAX_STRING_LENGTH);
    CREATE_SCALAR_ATTRIBUTE(attr_Overloaded_read);
    CREATE_DEVSTRING_ATTRIBUTE(attr_filterState_read,MAX_STRING_LENGTH);
    CREATE_DEVSTRING_ATTRIBUTE(attr_autoFilterState_read,MAX_STRING_LENGTH);

    gpib_device = NULL;
    try
    {
        gpib_device = new Tango::DeviceProxy(gpibDevice);
        gpib_device->ping();
        set_state(Tango::ON);
        set_status("Keithley428 is ON");
    } 
    catch (Tango::DevFailed &e)
    {
        set_state(Tango::FAULT);
        set_status("Not able to connect to Gpib Device");
    }

    buffer = new char[180]; 
    memset(buffer,0, 180);

    if(gpibDeviceType == 1)
    {
        m_write_read    = "GPIBWriteRead";
        m_write         = "GPIBWrite";
    }
    else
    {
        m_write_read    = "WriteRead";
        m_write         = "Write";
    }
}


//+----------------------------------------------------------------------------
//
// method : 		Keithley428::get_device_property()
// 
// description : 	Read the device properties from database.
//
//-----------------------------------------------------------------------------
void Keithley428::get_device_property()
{
    //	Initialize your default values here (if not done with  POGO).
    //------------------------------------------------------------------

    //	Read device properties from database.(Automatic code generation)
    //------------------------------------------------------------------
	Tango::DbData	dev_prop;
	dev_prop.push_back(Tango::DbDatum("GpibDevice"));
	dev_prop.push_back(Tango::DbDatum("GpibDeviceType"));

	//	Call database and extract values
	//--------------------------------------------
	if (Tango::Util::instance()->_UseDb==true)
		get_db_device()->get_property(dev_prop);
	Tango::DbDatum	def_prop, cl_prop;
	Keithley428Class	*ds_class =
		(static_cast<Keithley428Class *>(get_device_class()));
	int	i = -1;

	//	Try to initialize GpibDevice from class property
	cl_prop = ds_class->get_class_property(dev_prop[++i].name);
	if (cl_prop.is_empty()==false)	cl_prop  >>  gpibDevice;
	else {
		//	Try to initialize GpibDevice from default device value
		def_prop = ds_class->get_default_device_property(dev_prop[i].name);
		if (def_prop.is_empty()==false)	def_prop  >>  gpibDevice;
	}
	//	And try to extract GpibDevice value from database
	if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  gpibDevice;

	//	Try to initialize GpibDeviceType from class property
	cl_prop = ds_class->get_class_property(dev_prop[++i].name);
	if (cl_prop.is_empty()==false)	cl_prop  >>  gpibDeviceType;
	else {
		//	Try to initialize GpibDeviceType from default device value
		def_prop = ds_class->get_default_device_property(dev_prop[i].name);
		if (def_prop.is_empty()==false)	def_prop  >>  gpibDeviceType;
	}
	//	And try to extract GpibDeviceType value from database
	if (dev_prop[i].is_empty()==false)	dev_prop[i]  >>  gpibDeviceType;



    //	End of Automatic code generation
    //------------------------------------------------------------------

}
//+----------------------------------------------------------------------------
//
// method : 		Keithley428::always_executed_hook()
// 
// description : 	method always executed before any command is executed
//
//-----------------------------------------------------------------------------
void Keithley428::always_executed_hook()
{

}
//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_attr_hardware
// 
// description : 	Hardware acquisition for attributes.
//
//-----------------------------------------------------------------------------
void Keithley428::read_attr_hardware(vector<long> &attr_list)
{
    DEBUG_STREAM << "Keithley428::read_attr_hardware(vector<long> &attr_list) entering... "<< endl;
    //	Add your own code here
}
//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_filterState
// 
// description : 	Extract real attribute values for filterState acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_filterState(Tango::Attribute &attr)
{
	DEBUG_STREAM << "Keithley428::read_filterState(Tango::Attribute &attr) entering... "<< endl;

    DeviceData din;
    DeviceData dout;

    string tmp_string;
    string selected_str;
    string attr_str;
    size_t pos;

    int r_v;
    int w_v;

    sprintf(tmp_buffer,"U0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    pos = tmp_string.find("P");
    pos = pos + 1;
    selected_str = tmp_string.substr(pos);
    r_v = atoi(selected_str.c_str());

    switch(r_v)
    {
    case 0:
        attr_str = "OFF";
        break;
    case 1:
        attr_str = "ON";
        break;
    default:
        attr_str = "BAD_VALUE";
        break;
    }

    strcpy(*attr_filterState_read,attr_str.c_str());
    attr.set_value( attr_filterState_read);
}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_autoFilterState
// 
// description : 	Extract real attribute values for autoFilterState acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_autoFilterState(Tango::Attribute &attr)
{
	DEBUG_STREAM << "Keithley428::read_autoFilterState(Tango::Attribute &attr) entering... "<< endl;

    DeviceData din;
    DeviceData dout;

    string tmp_string;
    string selected_str;
    string attr_str;
    size_t pos;

    int r_v;
    int w_v;

    sprintf(tmp_buffer,"U0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    pos = tmp_string.find("Z");
    pos = pos + 1;
    selected_str = tmp_string.substr(pos);
    r_v = atoi(selected_str.c_str());

    switch(r_v)
    {
    case 0:
        attr_str = "OFF";
        break;
    case 1:
        attr_str = "ON";
        break;
    default:
        attr_str = "BAD_VALUE";
        break;
    }

    strcpy(*attr_autoFilterState_read,attr_str.c_str());
    attr.set_value( attr_autoFilterState_read);
}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_Bypass
// 
// description : 	Extract real attribute values for Bypass acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_Bypass(Tango::Attribute &attr)
{
    DEBUG_STREAM << "Keithley428::read_Bypass(Tango::Attribute &attr) entering... "<< endl; 

    DeviceData din;
    DeviceData dout;

    string tmp_string;
    string selected_str;
    size_t pos;

    int c_v;

    sprintf(tmp_buffer,"U0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    pos = tmp_string.find("C");
    pos = pos + 1;
    selected_str = tmp_string.substr(pos);
    c_v = atoi(selected_str.c_str());

    attr_Bypass_write = c_v;

    attr.set_value( &attr_Bypass_write); 
}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::write_Bypass
// 
// description : 	Write Bypass attribute values to hardware.
//
//-----------------------------------------------------------------------------
void Keithley428::write_Bypass(Tango::WAttribute &attr)
{
    DEBUG_STREAM << "Keithley428::write_Bypass(Tango::WAttribute &attr) entering... "<< endl;

    DeviceData din;

    attr.get_write_value( attr_Bypass_write);

    if( (attr_Bypass_write < 0) || (attr_Bypass_write > 1) ){
        Tango::Except::throw_exception ((const char *)"Keithley428 error",
            (const char *)"Value out of range (off -> 0, on -> 1)",
            (const char *)"write_Bypass");
    }

    sprintf(tmp_buffer,"C%dX", attr_Bypass_write);
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_Overloaded
// 
// description : 	Extract real attribute values for Overloaded acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_Overloaded(Tango::Attribute &attr)
{
    DEBUG_STREAM << "Keithley428::read_Overloaded(Tango::Attribute &attr) entering... "<< endl;  

    DeviceData din;
    DeviceData dout;

    string tmp_string;
    string selected_str;
    size_t pos;

    int overload_flag;

    sprintf(tmp_buffer,"U1X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    pos = 12;
    selected_str = tmp_string.substr(pos,1);
    overload_flag = atoi(selected_str.c_str());

    *attr_Overloaded_read = overload_flag;

    attr.set_value(attr_Overloaded_read); 
}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_GainStr
// 
// description : 	Extract real attribute values for GainStr acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_GainStr(Tango::Attribute &attr)
{
    DEBUG_STREAM << "Keithley428::read_GainStr(Tango::Attribute &attr) entering... "<< endl;

    DeviceData din;
    DeviceData dout;

    string tmp_string;


    sprintf(tmp_buffer,"U3X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    strcpy(*attr_GainStr_read,tmp_string.c_str());

    attr.set_value( attr_GainStr_read);
}


//+----------------------------------------------------------------------------
//
// method : 		Keithley428::read_Gain
// 
// description : 	Extract real attribute values for Gain acquisition result.
//
//-----------------------------------------------------------------------------
void Keithley428::read_Gain(Tango::Attribute &attr)
{
    DEBUG_STREAM << "Keithley428::read_Gain(Tango::Attribute &attr) entering... "<< endl;

    DeviceData din;
    DeviceData dout;

    string tmp_string;
    string selected_str;
    size_t pos;

    int r_v;
    int w_v;

    sprintf(tmp_buffer,"U0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    dout = gpib_device->command_inout(m_write_read.c_str(),din);  

    dout >> tmp_string;

    pos = tmp_string.find("R");
    pos = pos + 1;
    selected_str = tmp_string.substr(pos);
    r_v = atoi(selected_str.c_str());

    pos = tmp_string.find("W");
    pos = pos + 1;
    selected_str = tmp_string.substr(pos);
    w_v = atoi(selected_str.c_str());

    if(w_v == 0){
        attr_Gain_write = r_v;
    } else {
        attr_Gain_write = r_v + 1;
    }

    attr.set_value( &attr_Gain_write); 

}

//+----------------------------------------------------------------------------
//
// method : 		Keithley428::write_Gain
// 
// description : 	Write Gain attribute values to hardware.
//
//-----------------------------------------------------------------------------
void Keithley428::write_Gain(Tango::WAttribute &attr)
{
    DEBUG_STREAM << "Keithley428::write_Gain(Tango::WAttribute &attr) entering... "<< endl;

    DeviceData din;

    attr.get_write_value( attr_Gain_write);

    if( (attr_Gain_write < 0) || (attr_Gain_write > 11) ){
        Tango::Except::throw_exception ((const char *)"Keithley428 error",
            (const char *)"Value out of range (0-11)",
            (const char *)"write_Gain");
    }


    if(attr_Gain_write < 11){
        sprintf(tmp_buffer,"W0XR%dX", (int)attr_Gain_write);
        strcpy(buffer,tmp_buffer);
        din << buffer;
    } else {
        sprintf(tmp_buffer,"W1XR10X");
        strcpy(buffer,tmp_buffer);
        din << buffer;
    }


    gpib_device->command_inout(m_write.c_str(),din);

}



//+------------------------------------------------------------------
/**
 *	method:	Keithley428::perform_zero_correct
 *
 *	description:	method to execute "PerformZeroCorrect"
 *	Perform Zero correction.
 *
 *
 */
//+------------------------------------------------------------------
void Keithley428::perform_zero_correct()
{
    DEBUG_STREAM << "Keithley428::perform_zero_correct(): entering... !" << endl;

    //	Add your own code to control device here

    DeviceData din;

    sprintf(tmp_buffer,"C2X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}

//+------------------------------------------------------------------
/**
 *	method:	Keithley428::filter_on
 *
 *	description:	method to execute "FilterOn"
 *	Turn the filter ON
 *
 *
 */
//+------------------------------------------------------------------
void Keithley428::filter_on()
{
	DEBUG_STREAM << "Keithley428::filter_on(): entering... !" << endl;

	//	Add your own code to control device here

    DeviceData din;

    sprintf(tmp_buffer,"P1X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}

//+------------------------------------------------------------------
/**
 *	method:	Keithley428::filter_off
 *
 *	description:	method to execute "FilterOff"
 *	Turn the filter OFF
 *
 *
 */
//+------------------------------------------------------------------
void Keithley428::filter_off()
{
	DEBUG_STREAM << "Keithley428::filter_off(): entering... !" << endl;

	//	Add your own code to control device here

    DeviceData din;

    sprintf(tmp_buffer,"P0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}

//+------------------------------------------------------------------
/**
 *	method:	Keithley428::auto_filter_on
 *
 *	description:	method to execute "AutoFilterOn"
 *	Enable Auto Filter
 *
 *
 */
//+------------------------------------------------------------------
void Keithley428::auto_filter_on()
{
	DEBUG_STREAM << "Keithley428::auto_filter_on(): entering... !" << endl;

	//	Add your own code to control device here

    //- read the current filterState
	Tango::Attribute &filterState = dev_attr->get_attr_by_name("filterState");
	this->read_filterState(filterState);

    string filter_state = *attr_filterState_read;

    if(filter_state == "ON")
    {
        Tango::Except::throw_exception(
				(const char *)"OPERATION_NOT_ALLOWED",
				(const char *)"Auto filter ON is not allowed when filter is already ON",
				(const char *)"Keithley428::auto_filter_on");
    }

    DeviceData din;

    sprintf(tmp_buffer,"Z1X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}

//+------------------------------------------------------------------
/**
 *	method:	Keithley428::auto_filter_off
 *
 *	description:	method to execute "AutoFilterOff"
 *	Disable Auto Filter
 *
 *
 */
//+------------------------------------------------------------------
void Keithley428::auto_filter_off()
{
	DEBUG_STREAM << "Keithley428::auto_filter_off(): entering... !" << endl;

	//	Add your own code to control device here

    DeviceData din;

    sprintf(tmp_buffer,"Z0X");
    strcpy(buffer,tmp_buffer);
    din << buffer;

    gpib_device->command_inout(m_write.c_str(),din);

}


}	//	namespace