static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/Instrumentation/AcquireWaveformLecroy/src/AcquireWaveformLecroyClass.cpp,v 1.5 2004-10-18 13:17:49 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.5 $
//
// $Log: not supported by cvs2svn $
// 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>


namespace AcquireWaveformLecroy
{



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

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

//+----------------------------------------------------------------------------
//
// 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)
{
	
	for (long i=0 ; i < devlist_ptr->length() ; i++)
	{
		cout4 << "Device name : " << (*devlist_ptr)[i].in() << endl;
						
		// Create device 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 id database used.
		//---------------------------------------------
		if (Tango::Util::_UseDb == true)
			export_device(device_list.back());
		else
			export_device(device_list.back(), (*devlist_ptr)[i]);
	}
}
//+----------------------------------------------------------------------------
//	Method: AcquireWaveformLecroyClass::attribute_factory(vector<Tango::Attr *> &att_list)
//-----------------------------------------------------------------------------
void AcquireWaveformLecroyClass::attribute_factory(vector<Tango::Attr *> &att_list)
{
	//	Attribute : rawWaveformData
	Tango::SpectrumAttr	*raw_waveform_data = 
		new Tango::SpectrumAttr("rawWaveformData", Tango::DEV_SHORT, 150000);
	Tango::UserDefaultAttrProp	raw_waveform_data_prop;
	raw_waveform_data_prop.set_label("Raw Waveform Data");
	raw_waveform_data_prop.set_unit("data");
	raw_waveform_data_prop.set_display_unit("data");
	raw_waveform_data_prop.set_format("%d");
	raw_waveform_data_prop.set_description("Gets the raw waveform data.\nMaximum size set to 150000 data by default.");
	raw_waveform_data->set_default_properties(raw_waveform_data_prop);
	att_list.push_back(raw_waveform_data);

	//	Attribute : verticalScaledData
	Tango::SpectrumAttr	*vertical_scaled_data = 
		new Tango::SpectrumAttr("verticalScaledData", Tango::DEV_DOUBLE, 150000);
	Tango::UserDefaultAttrProp	vertical_scaled_data_prop;
	vertical_scaled_data_prop.set_label("Vertical Scaled Data");
	vertical_scaled_data_prop.set_unit("data");
	vertical_scaled_data_prop.set_display_unit("data");
	vertical_scaled_data_prop.set_format("%d");
	vertical_scaled_data_prop.set_description("Gets the scaled waveform data.\nMaximum size set to 150000 data by default.");
	vertical_scaled_data->set_default_properties(vertical_scaled_data_prop);
	att_list.push_back(vertical_scaled_data);

	//	Attribute : waveArray1
	Tango::Attr	*wave_array1 = 
		new Tango::Attr("waveArray1", Tango::DEV_LONG, Tango::READ);
	Tango::UserDefaultAttrProp	wave_array1_prop;
	wave_array1_prop.set_label("Wave Array 1");
	wave_array1_prop.set_unit("data");
	wave_array1_prop.set_display_unit("data");
	wave_array1_prop.set_format("%d");
	wave_array1_prop.set_description("Length of the first array.\nThis second array contains data of simples\nwaveforms.");
	wave_array1->set_default_properties(wave_array1_prop);
	wave_array1->set_disp_level(Tango::EXPERT);
	att_list.push_back(wave_array1);

	//	Attribute : waveArray2
	Tango::Attr	*wave_array2 = 
		new Tango::Attr("waveArray2", Tango::DEV_LONG, Tango::READ);
	Tango::UserDefaultAttrProp	wave_array2_prop;
	wave_array2_prop.set_label("Wave Array 2");
	wave_array2_prop.set_unit("data");
	wave_array2_prop.set_display_unit("data");
	wave_array2_prop.set_format("%d");
	wave_array2_prop.set_description("Length of the second array.\nThis second array contains data of complex\nwaveforms (as FFT, Extrema ....).");
	wave_array2->set_default_properties(wave_array2_prop);
	wave_array2->set_disp_level(Tango::EXPERT);
	att_list.push_back(wave_array2);

	//	Attribute : waveArrayCount
	Tango::Attr	*wave_array_count = 
		new Tango::Attr("waveArrayCount", Tango::DEV_LONG, Tango::READ);
	Tango::UserDefaultAttrProp	wave_array_count_prop;
	wave_array_count_prop.set_label("Wave Array Count");
	wave_array_count_prop.set_unit("data");
	wave_array_count_prop.set_display_unit("data");
	wave_array_count_prop.set_format("%d");
	wave_array_count_prop.set_description("Length of the two arrays.\nIf the length of wavearray1 is different of this\nlength that's means data are present in the\narray wavearray2. Which is used for complex\ndata as math operations on a waveform.");
	wave_array_count->set_default_properties(wave_array_count_prop);
	att_list.push_back(wave_array_count);

	//	Attribute : nominalBits
	Tango::Attr	*nominal_bits = 
		new Tango::Attr("nominalBits", Tango::DEV_SHORT, Tango::READ);
	Tango::UserDefaultAttrProp	nominal_bits_prop;
	nominal_bits_prop.set_label("Nominal Bits");
	nominal_bits_prop.set_display_unit("bits");
	nominal_bits_prop.set_format("%d");
	nominal_bits_prop.set_description("ADC resolution.\nFor simple data this ADC is an 8 bits resolution\nelse it's can be 10 up to 12 bits");
	nominal_bits->set_default_properties(nominal_bits_prop);
	nominal_bits->set_disp_level(Tango::EXPERT);
	att_list.push_back(nominal_bits);

	//	Attribute : horizontalInterval
	Tango::Attr	*horizontal_interval = 
		new Tango::Attr("horizontalInterval", Tango::DEV_DOUBLE, Tango::READ);
	Tango::UserDefaultAttrProp	horizontal_interval_prop;
	horizontal_interval_prop.set_label("Horizontal Interval");
	horizontal_interval_prop.set_description("Sampling interval for time domain waveforms\nNeeded to scale the waveform data stored\nin the rawWaveformData attribute.");
	horizontal_interval->set_default_properties(horizontal_interval_prop);
	horizontal_interval->set_disp_level(Tango::EXPERT);
	att_list.push_back(horizontal_interval);

	//	Attribute : horizontalOffset
	Tango::Attr	*horizontal_offset = 
		new Tango::Attr("horizontalOffset", Tango::DEV_DOUBLE, Tango::READ);
	Tango::UserDefaultAttrProp	horizontal_offset_prop;
	horizontal_offset_prop.set_label("Horizontal Offset");
	horizontal_offset_prop.set_description("Trigger offset for the first sweep of the trigger,\nseconds between the trigger and the first \ndata point.\nNeeded to scale the waveform data stored\nin the rawWaveformData attribute.");
	horizontal_offset->set_default_properties(horizontal_offset_prop);
	horizontal_offset->set_disp_level(Tango::EXPERT);
	att_list.push_back(horizontal_offset);

	//	Attribute : verticalGain
	Tango::Attr	*vertical_gain = 
		new Tango::Attr("verticalGain", Tango::DEV_DOUBLE, Tango::READ);
	Tango::UserDefaultAttrProp	vertical_gain_prop;
	vertical_gain_prop.set_label("Vertical Gain");
	vertical_gain_prop.set_description("The vertical gain.\nUsed to scale the waveform data stored in\n the rawWaveformData attribute.");
	vertical_gain->set_default_properties(vertical_gain_prop);
	vertical_gain->set_disp_level(Tango::EXPERT);
	att_list.push_back(vertical_gain);

	//	Attribute : verticalOffset
	Tango::Attr	*vertical_offset = 
		new Tango::Attr("verticalOffset", Tango::DEV_DOUBLE, Tango::READ);
	Tango::UserDefaultAttrProp	vertical_offset_prop;
	vertical_offset_prop.set_label("Vertical Offset");
	vertical_offset_prop.set_description("Needed to scale the waveform data stored\nin the rawWaveformData attribute.");
	vertical_offset->set_default_properties(vertical_offset_prop);
	vertical_offset->set_disp_level(Tango::EXPERT);
	att_list.push_back(vertical_offset);

	//	Attribute : triggerTime
	Tango::Attr	*trigger_time = 
		new Tango::Attr("triggerTime", Tango::DEV_STRING, Tango::READ);
	Tango::UserDefaultAttrProp	trigger_time_prop;
	trigger_time_prop.set_label("Trigger Time");
	trigger_time->set_default_properties(trigger_time_prop);
	att_list.push_back(trigger_time);

}
//+----------------------------------------------------------------------------
//
// 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;

	//	Prepeare DbDatum
	//--------------------------------------------
	Tango::DbDatum	title("ProjectTitle");
	string	str_title("Acquire_Waveform_Lecroy_Prj");
	title << str_title;

	Tango::DbDatum	description("Description");
	string	str_desc("This class allows the acquisition of a waveform (the description and the data),\n\
from a specific channel and from any Lecroy scope series.\n\
The description is a structure which contains in particular :\n\
the length of the array 1 (the raw data)\n\
th");
	description << str_desc;
	
		// Use the doc_url field to store all information 
		// on the server version and CVS 
		string::size_type pos, len; 
		
		// 1) Manage module name  
		//  get rid of the $RCSfile:  prefix and of Class.cpp suffix 
		string classname = RCSfile;
		
		pos = classname.find("$RCSfile: ");
		len = classname.length();
		
		if (pos != string::npos) 
			classname= classname.substr(pos+10, len- pos-10); 

		pos = classname.find ("Class.cpp",0);
		if (pos != string::npos) 
			classname=classname.substr(0,pos);
		
		// 2)  Manage version number with SOLEIL CVS rules 
		// tag name is in the form : release_1_0 ==> transform it to 1.0
		// 
		string version ; 
		string str_TagName=string(TagName); 
		
		pos = str_TagName.find_first_of("_",0); 
		if (pos != string::npos) 
			version= str_TagName.substr(pos+1, 3);

		pos = version.find_first_of("_",0); 
		if (pos != string::npos) 
			version[pos] = '.';

		
	//  Store all info in the str_url property		
		
		string	str_url=  "Documentation URL = " + string(HttpServer) + classname +"-" + version + "/index.html" + "\n";
		str_url= str_url + " Version CVS Tag = " + string(TagName)+ "\n"; 
		str_url= str_url + " CVS location = " + string(FileName)+ "\n"; 
		
		Tango::DbDatum	doc_url("doc_url");
		
		doc_url << str_url;

		// Push everything in DataBase

		Tango::DbData	data;
		data.push_back(title);
		data.push_back(description);
		data.push_back(doc_url);
		//	Call database and and values
		//--------------------------------------------
		get_db_class()->put_property(data);
}

}	// namespace