Skip to content
Snippets Groups Projects
SimulatedE2PSClass.cpp 24 KiB
Newer Older
Claudio Scafuri's avatar
Claudio Scafuri committed
/*----- PROTECTED REGION ID(SimulatedE2PSClass.cpp) ENABLED START -----*/
/* clang-format on */
//=============================================================================
//
// file :        SimulatedE2PSClass.cpp
//
// description : C++ source for the SimulatedE2PSClass.
//               A singleton class derived from DeviceClass.
//               It implements the command and attribute list
//               and all properties and methods required
//               by the SimulatedE2PS once per process.
//
// project :     SimulatedE2PS
//
// This file is part of Tango device class.
//
// Tango 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, either version 3 of the License, or
// (at your option) any later version.
//
// Tango 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 Tango.  If not, see <http://www.gnu.org/licenses/>.
//
//
//
//=============================================================================
//                This file is generated by POGO
//        (Program Obviously used to Generate tango Object)
//=============================================================================


#include "SimulatedE2PSClass.h"
/* clang-format off */
/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass.cpp

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

__declspec(dllexport)

#endif

	Tango::DeviceClass *_create_SimulatedE2PS_class(const char *name) {
		return SimulatedE2PS_ns::SimulatedE2PSClass::init(name);
	}
}

namespace SimulatedE2PS_ns
{
//===================================================================
//	Initialize pointer for singleton pattern
//===================================================================
SimulatedE2PSClass *SimulatedE2PSClass::_instance = NULL;

//===================================================================
//	Class constants
//===================================================================
//--------------------------------------------------------
/**
 * method : 		SimulatedE2PSClass::SimulatedE2PSClass(std::string &s)
 * description : 	constructor for the SimulatedE2PSClass
 *
 * @param s	The class name
 */
//--------------------------------------------------------
SimulatedE2PSClass::SimulatedE2PSClass(std::string &s):Tango::DeviceClass(s)
{
	TANGO_LOG_INFO << "Entering SimulatedE2PSClass constructor" << std::endl;
	set_default_property();
	write_class_property();

	/*----- PROTECTED REGION ID(SimulatedE2PSClass::constructor) ENABLED START -----*/
	/* clang-format on */
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::constructor

	TANGO_LOG_INFO << "Leaving SimulatedE2PSClass constructor" << std::endl;
}

//--------------------------------------------------------
/**
 * method : 		SimulatedE2PSClass::~SimulatedE2PSClass()
 * description : 	destructor for the SimulatedE2PSClass
 */
//--------------------------------------------------------
SimulatedE2PSClass::~SimulatedE2PSClass()
{
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::destructor) ENABLED START -----*/
	/* clang-format on */
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::destructor

	_instance = NULL;
}


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

//--------------------------------------------------------
/**
 * method : 		SimulatedE2PSClass::instance
 * description : 	Check if object already created,
 *                  and return a pointer to the object
 */
//--------------------------------------------------------
SimulatedE2PSClass *SimulatedE2PSClass::instance()
{
	if (_instance == NULL)
	{
		std::cerr << "Class is not initialised !!" << std::endl;
		exit(-1);
	}
	return _instance;
}



//===================================================================
//	Command execution method calls
//===================================================================
//--------------------------------------------------------
/**
 * method : 		OnClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *OnClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "OnClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->on());
	return new CORBA::Any();
}

//--------------------------------------------------------
/**
 * method : 		OffClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *OffClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "OffClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->off());
	return new CORBA::Any();
}

//--------------------------------------------------------
/**
 * method : 		ResetClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *ResetClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "ResetClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->reset());
	return new CORBA::Any();
}

//--------------------------------------------------------
/**
 * method : 		StartCyclingClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *StartCyclingClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "StartCyclingClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->start_cycling());
	return new CORBA::Any();
}

//--------------------------------------------------------
/**
 * method : 		AbortClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *AbortClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "AbortClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->abort());
	return new CORBA::Any();
}

//--------------------------------------------------------
/**
 * method : 		faultClass::execute()
 * description : 	method to trigger the execution of the command.
 *
 * @param	device	The device on which the command must be executed
 * @param	in_any	The command input data
 *
 *	returns The command output data (packed in the Any object)
 */
//--------------------------------------------------------
CORBA::Any *faultClass::execute(Tango::DeviceImpl *device, TANGO_UNUSED(const CORBA::Any &in_any))
{
	TANGO_LOG_INFO << "faultClass::execute(): arrived" << std::endl;
	((static_cast<SimulatedE2PS *>(device))->fault());
	return new CORBA::Any();
}


//===================================================================
//	Properties management
//===================================================================
//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::get_class_property()
 * Description:  Get the class property for specified name.
 */
//--------------------------------------------------------
Tango::DbDatum SimulatedE2PSClass::get_class_property(std::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, returns  an empty DbDatum
	return Tango::DbDatum(prop_name);
}

//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::get_default_device_property()
 * Description:  Return the default value for device property.
 */
//--------------------------------------------------------
Tango::DbDatum SimulatedE2PSClass::get_default_device_property(std::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      : SimulatedE2PSClass::get_default_class_property()
 * Description:  Return the default value for class property.
 */
//--------------------------------------------------------
Tango::DbDatum SimulatedE2PSClass::get_default_class_property(std::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      : SimulatedE2PSClass::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 SimulatedE2PSClass::set_default_property()
{
	std::string	prop_name;
	std::string	prop_desc;
	std::string	prop_def;
	std::vector<std::string>	vect_data;

	//	Set Default Class Properties

	//	Set Default device Properties
	prop_name = "CurrentRipple";
	prop_desc = "Residual current oscillation [A]\n\nThe simulator adds noise of CurrenRipple amplitude to the reading";
	prop_def  = "0.0025";
	vect_data.clear();
	vect_data.push_back("0.0025");
	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);
Claudio Scafuri's avatar
Claudio Scafuri committed
}

//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::write_class_property()
 * Description:  Set class description fields as property in database
 */
//--------------------------------------------------------
void SimulatedE2PSClass::write_class_property()
{
//	First time, check if database used
if (Tango::Util::_UseDb == false)
	return;

Tango::DbData	data;
std::string	classname = get_name();
std::string	header;

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

//	Put Description
Tango::DbDatum	description("Description");
std::vector<std::string>	str_desc;
str_desc.push_back("Simulated power supply for Elettra 2.0 tests (digiltal twin).");
str_desc.push_back("Tango interface Loosely based on NGPS tango device");
description << str_desc;
data.push_back(description);

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

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

//===================================================================
//	Factory methods
//===================================================================

//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::device_factory()
 * Description:  Create the device object(s)
 *                and store them in the device list
 */
//--------------------------------------------------------
void SimulatedE2PSClass::device_factory(const Tango::DevVarStringArray *devlist_ptr)
{
/*----- PROTECTED REGION ID(SimulatedE2PSClass::device_factory_before) ENABLED START -----*/
/* clang-format on */
//	Add your own code
/* clang-format off */
/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::device_factory_before

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

//	Manage dynamic attributes if any
erase_dynamic_attributes(devlist_ptr, get_class_attr()->get_attr_list());

//	Export devices to the outside world
for (unsigned long i=1 ; i<=devlist_ptr->length() ; i++)
{
	//	Add dynamic attributes if any
	SimulatedE2PS *dev = static_cast<SimulatedE2PS *>(device_list[device_list.size()-i]);
	dev->add_dynamic_attributes();

	//	Check before if database used.
	if ((Tango::Util::_UseDb == true) && (Tango::Util::_FileDb == false))
		export_device(dev);
	else
		export_device(dev, dev->get_name().c_str());
}

/*----- PROTECTED REGION ID(SimulatedE2PSClass::device_factory_after) ENABLED START -----*/
/* clang-format on */
//	Add your own code
/* clang-format off */
/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::device_factory_after
}
//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::attribute_factory()
 * Description:  Create the attribute object(s)
 *                and store them in the attribute list
 */
//--------------------------------------------------------
void SimulatedE2PSClass::attribute_factory(std::vector<Tango::Attr *> &att_list)
{
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::attribute_factory_before) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::attribute_factory_before
	//	Attribute : current
	currentAttrib	*current = new currentAttrib();
	Tango::UserDefaultAttrProp	current_prop;
	current_prop.set_description("The powersupply current setting in amps");
	current_prop.set_label("current");
	current_prop.set_unit("A");
	current_prop.set_standard_unit("1");
	current_prop.set_display_unit("A");
	current_prop.set_format("%6.4f");
	//	max_value	not set for current
	//	min_value	not set for current
	//	max_alarm	not set for current
	//	min_alarm	not set for current
	//	max_warning	not set for current
	//	min_warning	not set for current
	//	delta_t	not set for current
	//	delta_val	not set for current
	current->set_default_properties(current_prop);
	//	Not Polled
	current->set_disp_level(Tango::OPERATOR);
	//	Not Memorized
	att_list.push_back(current);

	//	Attribute : voltage
	voltageAttrib	*voltage = new voltageAttrib();
	Tango::UserDefaultAttrProp	voltage_prop;
	voltage_prop.set_description("The powersupply voltage in volts.");
	//	label	not set for voltage
	voltage_prop.set_unit("V");
	voltage_prop.set_standard_unit("1");
	voltage_prop.set_display_unit("V");
	voltage_prop.set_format("%6.4f");
	//	max_value	not set for voltage
	//	min_value	not set for voltage
	//	max_alarm	not set for voltage
	//	min_alarm	not set for voltage
	//	max_warning	not set for voltage
	//	min_warning	not set for voltage
	//	delta_t	not set for voltage
	//	delta_val	not set for voltage
	voltage->set_default_properties(voltage_prop);
	//	Not Polled
	voltage->set_disp_level(Tango::OPERATOR);
	//	Not Memorized
	att_list.push_back(voltage);

	//	Attribute : currentSet
	currentSetAttrib	*currentset = new currentSetAttrib();
	Tango::UserDefaultAttrProp	currentset_prop;
	currentset_prop.set_description("The current set value as stored in the powersupply.");
	//	label	not set for currentSet
	currentset_prop.set_unit("A");
	currentset_prop.set_standard_unit("1");
	currentset_prop.set_display_unit("A");
	currentset_prop.set_format("%6.4f");
	//	max_value	not set for currentSet
	//	min_value	not set for currentSet
	//	max_alarm	not set for currentSet
	//	min_alarm	not set for currentSet
	//	max_warning	not set for currentSet
	//	min_warning	not set for currentSet
	//	delta_t	not set for currentSet
	//	delta_val	not set for currentSet
	currentset->set_default_properties(currentset_prop);
	//	Not Polled
	currentset->set_disp_level(Tango::OPERATOR);
	//	Not Memorized
	att_list.push_back(currentset);


	//	Create a list of static attributes
	create_static_attribute_list(get_class_attr()->get_attr_list());
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::attribute_factory_after) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::attribute_factory_after
}
//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::pipe_factory()
 * Description:  Create the pipe object(s)
 *                and store them in the pipe list
 */
//--------------------------------------------------------
void SimulatedE2PSClass::pipe_factory()
{
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::pipe_factory_before) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::pipe_factory_before
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::pipe_factory_after) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::pipe_factory_after
}
//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::command_factory()
 * Description:  Create the command object(s)
 *                and store them in the command list
 */
//--------------------------------------------------------
void SimulatedE2PSClass::command_factory()
{
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::command_factory_before) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::command_factory_before


	//	Command On
	OnClass	*pOnCmd =
		new OnClass("On",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::OPERATOR);
	command_list.push_back(pOnCmd);

	//	Command Off
	OffClass	*pOffCmd =
		new OffClass("Off",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::OPERATOR);
	command_list.push_back(pOffCmd);

	//	Command Reset
	ResetClass	*pResetCmd =
		new ResetClass("Reset",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::OPERATOR);
	command_list.push_back(pResetCmd);

	//	Command StartCycling
	StartCyclingClass	*pStartCyclingCmd =
		new StartCyclingClass("StartCycling",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::OPERATOR);
	command_list.push_back(pStartCyclingCmd);

	//	Command Abort
	AbortClass	*pAbortCmd =
		new AbortClass("Abort",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::OPERATOR);
	command_list.push_back(pAbortCmd);

	//	Command fault
	faultClass	*pfaultCmd =
		new faultClass("fault",
			Tango::DEV_VOID, Tango::DEV_VOID,
			"",
			"",
			Tango::EXPERT);
	command_list.push_back(pfaultCmd);

	/*----- PROTECTED REGION ID(SimulatedE2PSClass::command_factory_after) ENABLED START -----*/
	/* clang-format on */
	//	Add your own code
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::command_factory_after
}

//===================================================================
//	Dynamic attributes related methods
//===================================================================

//--------------------------------------------------------
/**
 * method : 		SimulatedE2PSClass::create_static_attribute_list
 * description : 	Create the a list of static attributes
 *
 * @param	att_list	the created attribute list
 */
//--------------------------------------------------------
void SimulatedE2PSClass::create_static_attribute_list(std::vector<Tango::Attr *> &att_list)
{
	for (unsigned long i=0 ; i<att_list.size() ; i++)
	{
		std::string att_name(att_list[i]->get_name());
		transform(att_name.begin(), att_name.end(), att_name.begin(), ::tolower);
		defaultAttList.push_back(att_name);
	}

	TANGO_LOG_INFO << defaultAttList.size() << " attributes in default list" << std::endl;

	/*----- PROTECTED REGION ID(SimulatedE2PSClass::create_static_att_list) ENABLED START -----*/
	/* clang-format on */
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::create_static_att_list
}


//--------------------------------------------------------
/**
 * method : 		SimulatedE2PSClass::erase_dynamic_attributes
 * description : 	delete the dynamic attributes if any.
 *
 * @param	devlist_ptr	the device list pointer
 * @param	list of all attributes
 */
//--------------------------------------------------------
void SimulatedE2PSClass::erase_dynamic_attributes(const Tango::DevVarStringArray *devlist_ptr, std::vector<Tango::Attr *> &att_list)
{
	Tango::Util *tg = Tango::Util::instance();

	for (unsigned long i=0 ; i<devlist_ptr->length() ; i++)
	{
		Tango::DeviceImpl *dev_impl = tg->get_device_by_name(((std::string)(*devlist_ptr)[i]).c_str());
		SimulatedE2PS *dev = static_cast<SimulatedE2PS *> (dev_impl);

		std::vector<Tango::Attribute *> &dev_att_list = dev->get_device_attr()->get_attribute_list();
		std::vector<Tango::Attribute *>::iterator ite_att;
		for (ite_att=dev_att_list.begin() ; ite_att != dev_att_list.end() ; ++ite_att)
		{
			std::string att_name((*ite_att)->get_name_lower());
			if ((att_name == "state") || (att_name == "status"))
				continue;
			std::vector<std::string>::iterator ite_str = find(defaultAttList.begin(), defaultAttList.end(), att_name);
			if (ite_str == defaultAttList.end())
			{
				TANGO_LOG_INFO << att_name << " is a UNWANTED dynamic attribute for device " << (*devlist_ptr)[i] << std::endl;
				Tango::Attribute &att = dev->get_device_attr()->get_attr_by_name(att_name.c_str());
				dev->remove_attribute(att_list[att.get_attr_idx()], true, false);
				--ite_att;
			}
		}
	}
	/*----- PROTECTED REGION ID(SimulatedE2PSClass::erase_dynamic_attributes) ENABLED START -----*/
	/* clang-format on */
	/* clang-format off */
	/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::erase_dynamic_attributes
}

//--------------------------------------------------------
/**
 *	Method      : SimulatedE2PSClass::get_attr_object_by_name()
 * Description:  returns Tango::Attr * object found by name
 */
//--------------------------------------------------------
Tango::Attr *SimulatedE2PSClass::get_attr_object_by_name(std::vector<Tango::Attr *> &att_list, std::string attname)
{
	std::vector<Tango::Attr *>::iterator it;
	for (it=att_list.begin() ; it<att_list.end() ; ++it)
		if ((*it)->get_name()==attname)
			return (*it);
	//	Attr does not exist
	return NULL;
}


/*----- PROTECTED REGION ID(SimulatedE2PSClass::Additional Methods) ENABLED START -----*/
/* clang-format on */
/* clang-format off */
/*----- PROTECTED REGION END -----*/	//	SimulatedE2PSClass::Additional Methods
} //	namespace