static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/Instrumentation/AcquireWaveformLecroy/src/AcquireWaveformLecroyClass.cpp,v 1.7 2008-02-28 09:02:05 xavela Exp $";

static const char *TagName   = "$Name: not supported by cvs2svn $";

static const char *FileName= "$Source: /users/chaize/newsvn/cvsroot/Instrumentation/AcquireWaveformLecroy/src/AcquireWaveformLecroyClass.cpp,v $"; 

static const char *HttpServer= "http://controle/DeviceServer/doc/";

static const char *RCSfile = "$RCSfile: AcquireWaveformLecroyClass.cpp,v $"; 
//+=============================================================================
//
// file :        AcquireWaveformLecroyClass.cpp
//
// description : C++ source for the AcquireWaveformLecroyClass. A singleton
//               class derived from DeviceClass. It implements the
//               command list and all properties and methods required
//               by the AcquireWaveformLecroy once per process.
//
// project :     TANGO Device Server
//
// $Author: xavela $
//
// $Revision: 1.7 $
//
// $Log: not supported by cvs2svn $
// Revision 1.5  2004/10/18 13:17:49  xavela
// xavier : add an attr waveArray2.
// Sorts attr in expert and operator view with Pogo.
// Inconsistencies removed.
//
// Revision 1.4  2004/10/07 09:21:18  xavela
// xavier : no cout now.
//
// Revision 1.3  2004/10/05 13:11:11  xavela
// Xavier : previous commit failed.
//
// Revision 1.2  2004/10/05 13:09:10  xavela
// Xavier : memory allocation in read_attr_hardware() method.
// calculation of offset of the begining of the struct, which contains the context of the waveform acquisition, in the received block data.
//
//
// copyleft :     Synchrotron SOLEIL
//                L'Orme des Merisiers
//                Saint-Aubin - BP 48/
//-=============================================================================
//
//  		This file is generated by POGO
//	(Program Obviously used to Generate tango Object)
//
//         (c) - Software Engineering Group - ESRF
//=============================================================================


#include <tango.h>

#include <AcquireWaveformLecroy.h>
#include <AcquireWaveformLecroyClass.h>


//+----------------------------------------------------------------------------
/**
 *	Create AcquireWaveformLecroyClass singleton and return it in a C function for Python usage
 */
//+----------------------------------------------------------------------------
extern "C" {
#ifdef WIN32

__declspec(dllexport)

#endif

	Tango::DeviceClass *_create_AcquireWaveformLecroy_class(const char *name) {
		return AcquireWaveformLecroy_ns::AcquireWaveformLecroyClass::init(name);
	}
}


namespace AcquireWaveformLecroy_ns
{
//+----------------------------------------------------------------------------
//
// method : 		WriteReadCmd::execute()
// 
// description : 	method to trigger the execution of the command.
//                PLEASE DO NOT MODIFY this method core without pogo   
//
// in : - device : The device on which the command must be excuted
//		- in_any : The command input data
//
// returns : The command output data (packed in the Any object)
//
//-----------------------------------------------------------------------------
CORBA::Any *WriteReadCmd::execute(Tango::DeviceImpl *device,const CORBA::Any &in_any)
{

	cout2 << "WriteReadCmd::execute(): arrived" << endl;

	Tango::DevString	argin;
	extract(in_any, argin);

	return insert((static_cast<AcquireWaveformLecroy *>(device))->write_read(argin));
}



//
//----------------------------------------------------------------
//	Initialize pointer for singleton pattern
//----------------------------------------------------------------
//
AcquireWaveformLecroyClass *AcquireWaveformLecroyClass::_instance = NULL;

//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::AcquireWaveformLecroyClass(string &s)
// 
// description : 	constructor for the AcquireWaveformLecroyClass
//
// in : - s : The class name
//
//-----------------------------------------------------------------------------
AcquireWaveformLecroyClass::AcquireWaveformLecroyClass(string &s):DeviceClass(s)
{

	cout2 << "Entering AcquireWaveformLecroyClass constructor" << endl;
	set_default_property();
	get_class_property();
	write_class_property();
	
	cout2 << "Leaving AcquireWaveformLecroyClass constructor" << endl;

}
//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::~AcquireWaveformLecroyClass()
// 
// description : 	destructor for the AcquireWaveformLecroyClass
//
//-----------------------------------------------------------------------------
AcquireWaveformLecroyClass::~AcquireWaveformLecroyClass()
{
	_instance = NULL;
}

//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::instance
// 
// description : 	Create the object if not already done. Otherwise, just
//			return a pointer to the object
//
// in : - name : The class name
//
//-----------------------------------------------------------------------------
AcquireWaveformLecroyClass *AcquireWaveformLecroyClass::init(const char *name)
{
	if (_instance == NULL)
	{
		try
		{
			string s(name);
			_instance = new AcquireWaveformLecroyClass(s);
		}
		catch (bad_alloc)
		{
			throw;
		}		
	}		
	return _instance;
}

AcquireWaveformLecroyClass *AcquireWaveformLecroyClass::instance()
{
	if (_instance == NULL)
	{
		cerr << "Class is not initialised !!" << endl;
		exit(-1);
	}
	return _instance;
}

//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::command_factory
// 
// description : 	Create the command object(s) and store them in the 
//			command list
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::command_factory()
{
	command_list.push_back(new WriteReadCmd("WriteRead",
		Tango::DEV_STRING, Tango::DEV_STRING,
		"command to send",
		"device response (if any)",
		Tango::EXPERT));

	//	add polling if any
	for (unsigned int i=0 ; i<command_list.size(); i++)
	{
	}
}

//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::get_class_property
// 
// description : 	Get the class property for specified name.
//
// in :		string	name : The property name
//
//+----------------------------------------------------------------------------
Tango::DbDatum AcquireWaveformLecroyClass::get_class_property(string &prop_name)
{
	for (unsigned int i=0 ; i<cl_prop.size() ; i++)
		if (cl_prop[i].name == prop_name)
			return cl_prop[i];
	//	if not found, return  an empty DbDatum
	return Tango::DbDatum(prop_name);
}
//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::get_default_device_property()
// 
// description : 	Return the default value for device property.
//
//-----------------------------------------------------------------------------
Tango::DbDatum AcquireWaveformLecroyClass::get_default_device_property(string &prop_name)
{
	for (unsigned int i=0 ; i<dev_def_prop.size() ; i++)
		if (dev_def_prop[i].name == prop_name)
			return dev_def_prop[i];
	//	if not found, return  an empty DbDatum
	return Tango::DbDatum(prop_name);
}

//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::get_default_class_property()
// 
// description : 	Return the default value for class property.
//
//-----------------------------------------------------------------------------
Tango::DbDatum AcquireWaveformLecroyClass::get_default_class_property(string &prop_name)
{
	for (unsigned int i=0 ; i<cl_def_prop.size() ; i++)
		if (cl_def_prop[i].name == prop_name)
			return cl_def_prop[i];
	//	if not found, return  an empty DbDatum
	return Tango::DbDatum(prop_name);
}
//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::device_factory
// 
// description : 	Create the device object(s) and store them in the 
//			device list
//
// in :		Tango::DevVarStringArray *devlist_ptr : The device name list
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::device_factory(const Tango::DevVarStringArray *devlist_ptr)
{

	//	Create all devices.(Automatic code generation)
	//-------------------------------------------------------------
	for (unsigned long i=0 ; i < devlist_ptr->length() ; i++)
	{
		cout4 << "Device name : " << (*devlist_ptr)[i].in() << endl;
						
		// Create devices and add it into the device list
		//----------------------------------------------------
		device_list.push_back(new AcquireWaveformLecroy(this, (*devlist_ptr)[i]));							 

		// Export device to the outside world
		// Check before if database used.
		//---------------------------------------------
		if ((Tango::Util::_UseDb == true) && (Tango::Util::_FileDb == false))
			export_device(device_list.back());
		else
			export_device(device_list.back(), (*devlist_ptr)[i]);
	}
	//	End of Automatic code generation
	//-------------------------------------------------------------

}
//+----------------------------------------------------------------------------
//	Method: AcquireWaveformLecroyClass::attribute_factory(vector<Tango::Attr *> &att_list)
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::attribute_factory(vector<Tango::Attr *> &att_list)
{
	//	Attribute : triggerTime
	triggerTimeAttrib	*trigger_time = new triggerTimeAttrib();
	att_list.push_back(trigger_time);

	//	Attribute : verticalOffset
	verticalOffsetAttrib	*vertical_offset = new verticalOffsetAttrib();
	att_list.push_back(vertical_offset);

	//	Attribute : verticalGain
	verticalGainAttrib	*vertical_gain = new verticalGainAttrib();
	att_list.push_back(vertical_gain);

	//	Attribute : horizontalOffset
	horizontalOffsetAttrib	*horizontal_offset = new horizontalOffsetAttrib();
	att_list.push_back(horizontal_offset);

	//	Attribute : horizontalInterval
	horizontalIntervalAttrib	*horizontal_interval = new horizontalIntervalAttrib();
	att_list.push_back(horizontal_interval);

	//	Attribute : nominalBits
	nominalBitsAttrib	*nominal_bits = new nominalBitsAttrib();
	att_list.push_back(nominal_bits);

	//	Attribute : waveArrayCount
	waveArrayCountAttrib	*wave_array_count = new waveArrayCountAttrib();
	att_list.push_back(wave_array_count);

	//	Attribute : waveArray2
	waveArray2Attrib	*wave_array2 = new waveArray2Attrib();
	att_list.push_back(wave_array2);

	//	Attribute : waveArray1
	waveArray1Attrib	*wave_array1 = new waveArray1Attrib();
	att_list.push_back(wave_array1);

	//	Attribute : verticalScaledData
	verticalScaledDataAttrib	*vertical_scaled_data = new verticalScaledDataAttrib();
	att_list.push_back(vertical_scaled_data);

	//	Attribute : rawWaveformData
	rawWaveformDataAttrib	*raw_waveform_data = new rawWaveformDataAttrib();
	att_list.push_back(raw_waveform_data);

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

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

	//	Read class properties from database.(Automatic code generation)
	//------------------------------------------------------------------

	//	Call database and extract values
	//--------------------------------------------
	if (Tango::Util::instance()->_UseDb==true)
		get_db_class()->get_property(cl_prop);
	Tango::DbDatum	def_prop;
	int	i = -1;


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

}

//+----------------------------------------------------------------------------
//
// method : 	AcquireWaveformLecroyClass::set_default_property
// 
// description: Set default property (class and device) for wizard.
//              For each property, add to wizard property name and description
//              If default value has been set, add it to wizard property and
//              store it in a DbDatum.
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::set_default_property()
{
	string	prop_name;
	string	prop_desc;
	string	prop_def;

	vector<string>	vect_data;
	//	Set Default Class Properties
	//	Set Default Device Properties
	prop_name = "IPaddress";
	prop_desc = "The IP address of the Lecroy scope to build a connection with.";
	prop_def  = "";
	if (prop_def.length()>0)
	{
		Tango::DbDatum	data(prop_name);
		data << vect_data ;
		dev_def_prop.push_back(data);
		add_wiz_dev_prop(prop_name, prop_desc,  prop_def);
	}
	else
		add_wiz_dev_prop(prop_name, prop_desc);

	prop_name = "ChannelName";
	prop_desc = "The name of channel on which the acquisition will be done.\nThis name must be composed with two caracters : (a letter followed by a number)\nC for a physical channel\nF for a calculated waveform\nM for a memorised waveform\n...\nExample : C2 (is the channel 2)\nDefault : C1 (channel 1";
	prop_def  = "";
	if (prop_def.length()>0)
	{
		Tango::DbDatum	data(prop_name);
		data << vect_data ;
		dev_def_prop.push_back(data);
		add_wiz_dev_prop(prop_name, prop_desc,  prop_def);
	}
	else
		add_wiz_dev_prop(prop_name, prop_desc);

}
//+----------------------------------------------------------------------------
//
// method : 		AcquireWaveformLecroyClass::write_class_property
// 
// description : 	Set class description as property in database
//
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::write_class_property()
{
	//	First time, check if database used
	//--------------------------------------------
	if (Tango::Util::_UseDb == false)
		return;

	Tango::DbData	data;
	string	classname = get_name();
	string	header;
	string::size_type	start, end;

	//	Put title
	Tango::DbDatum	title("ProjectTitle");
	string	str_title("Acquire_Waveform_Lecroy_Prj");
	title << str_title;
	data.push_back(title);

	//	Put Description
	Tango::DbDatum	description("Description");
	vector<string>	str_desc;
	str_desc.push_back("This class allows the acquisition of a waveform (the description and the data),");
	str_desc.push_back("from a specific channel and from any Lecroy scope series.");
	str_desc.push_back("The description is a structure which contains in particular :");
	str_desc.push_back("the length of the array 1 (the raw data)");
	str_desc.push_back("the length of the array 2 (2nd part of the calculated waveform data, if any)");
	str_desc.push_back("the timebase");
	str_desc.push_back("the total length of the acquired data (lgth array1 + lgth array2)");
	str_desc.push_back(".... its represents the context of the acquisition.");
	str_desc.push_back("");
	str_desc.push_back("NOTE :");
	str_desc.push_back("for all Lecroy scope, the port number is the same : 1861");
	description << str_desc;
	data.push_back(description);
		
	//	put cvs location
	string	rcsId(RcsId);
	string	filename(classname);
	start = rcsId.find("/");
	if (start!=string::npos)
	{
		filename += "Class.cpp";
		end   = rcsId.find(filename);
		if (end>start)
		{
			string	strloc = rcsId.substr(start, end-start);
			//	Check if specific repository
			start = strloc.find("/cvsroot/");
			if (start!=string::npos && start>0)
			{
				string	repository = strloc.substr(0, start);
				if (repository.find("/segfs/")!=string::npos)
					strloc = "ESRF:" + strloc.substr(start, strloc.length()-start);
			}
			Tango::DbDatum	cvs_loc("cvs_location");
			cvs_loc << strloc;
			data.push_back(cvs_loc);
		}
	}

	//	Get CVS tag revision
	string	tagname(TagName);
	header = "$Name: ";
	start = header.length();
	string	endstr(" $");
	end   = tagname.find(endstr);
	if (end!=string::npos && end>start)
	{
		string	strtag = tagname.substr(start, end-start);
		Tango::DbDatum	cvs_tag("cvs_tag");
		cvs_tag << strtag;
		data.push_back(cvs_tag);
	}

	//	Get URL location
	string	httpServ(HttpServer);
	if (httpServ.length()>0)
	{
		Tango::DbDatum	db_doc_url("doc_url");
		db_doc_url << httpServ;
		data.push_back(db_doc_url);
	}

	//  Put inheritance
	Tango::DbDatum	inher_datum("InheritedFrom");
	vector<string> inheritance;
	inheritance.push_back("Device_3Impl");
	inher_datum << inheritance;
	data.push_back(inher_datum);

	//	Call database and and values
	//--------------------------------------------
	get_db_class()->put_property(data);
}

}	// namespace