Commit bbebd3e5 authored by Alessio Igor Bogani's avatar Alessio Igor Bogani
Browse files

Move Serial2Client class into a dedicated repository

parent 427745e4
/*----- PROTECTED REGION ID(Serial2Client.cpp) ENABLED START -----*/
static const char *RcsId = "$Id: Serial2Client.cpp,v 1.5 2018-07-09 13:58:05 alessio Exp $";
//=============================================================================
//
// file : Serial2Client.cpp
//
// description : C++ source for the Serial2Client class 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
// Serial2Client are implemented in this file.
//
// project :
//
// 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/>.
//
// $Author: alessio $
//
// $Revision: 1.5 $
// $Date: 2018-07-09 13:58:05 $
//
// $HeadURL: $
//
//=============================================================================
// This file is generated by POGO
// (Program Obviously used to Generate tango Object)
//=============================================================================
#include <Serial2Client.h>
#include <Serial2ClientClass.h>
#include <iomanip>
/*----- PROTECTED REGION END -----*/ // Serial2Client.cpp
/**
* Serial2Client class description:
*
*/
//================================================================
// The following table gives the correspondence
// between command and method names.
//
// Command name | Method name
//================================================================
// State | Inherited (no method)
// Status | Inherited (no method)
//================================================================
//================================================================
// Attributes managed is:
//================================================================
//================================================================
namespace Serial2Client_ns
{
/*----- PROTECTED REGION ID(Serial2Client::namespace_starting) ENABLED START -----*/
// static initializations
/*----- PROTECTED REGION END -----*/ // Serial2Client::namespace_starting
//--------------------------------------------------------
/**
* Method : Serial2Client::Serial2Client()
* Description : Constructors for a Tango device
* implementing the classSerial2Client
*/
//--------------------------------------------------------
Serial2Client::Serial2Client(Tango::DeviceClass *cl, string &s)
: TANGO_BASE_CLASS(cl, s.c_str())
{
/*----- PROTECTED REGION ID(Serial2Client::constructor_1) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // Serial2Client::constructor_1
}
//--------------------------------------------------------
Serial2Client::Serial2Client(Tango::DeviceClass *cl, const char *s)
: TANGO_BASE_CLASS(cl, s)
{
/*----- PROTECTED REGION ID(Serial2Client::constructor_2) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // Serial2Client::constructor_2
}
//--------------------------------------------------------
Serial2Client::Serial2Client(Tango::DeviceClass *cl, const char *s, const char *d)
: TANGO_BASE_CLASS(cl, s, d)
{
/*----- PROTECTED REGION ID(Serial2Client::constructor_3) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // Serial2Client::constructor_3
}
//--------------------------------------------------------
/**
* Method : Serial2Client::delete_device()
* Description : will be called at device destruction or at init command
*/
//--------------------------------------------------------
void Serial2Client::delete_device()
{
DEBUG_STREAM << "Serial2Client::delete_device() " << device_name << endl;
/*----- PROTECTED REGION ID(Serial2Client::delete_device) ENABLED START -----*/
// Delete device allocated objects
if (device_proxy) {
delete device_proxy;
device_proxy = NULL;
}
/*----- PROTECTED REGION END -----*/ // Serial2Client::delete_device
}
//--------------------------------------------------------
/**
* Method : Serial2Client::init_device()
* Description : will be called at device initialization.
*/
//--------------------------------------------------------
void Serial2Client::init_device()
{
DEBUG_STREAM << "Serial2Client::init_device() create device " << device_name << endl;
/*----- PROTECTED REGION ID(Serial2Client::init_device_before) ENABLED START -----*/
// Initialization before get_device_property() call
device_proxy = NULL;
init_error.clear();
/*----- PROTECTED REGION END -----*/ // Serial2Client::init_device_before
// Get the device properties from database
get_device_property();
// No longer if mandatory property not set.
if (mandatoryNotDefined)
return;
/*----- PROTECTED REGION ID(Serial2Client::init_device) ENABLED START -----*/
// Initialize device
try
{
device_proxy = new Tango::DeviceProxy(deviceName);
} catch( Tango::DevFailed &e )
{
init_error = "Initialization failed: " + string(e.errors[0].desc);
}
catch( ... )
{
init_error = "Initialization failed: unknown reason";
}
/*----- PROTECTED REGION END -----*/ // Serial2Client::init_device
}
//--------------------------------------------------------
/**
* Method : Serial2Client::get_device_property()
* Description : Read database to initialize property data members.
*/
//--------------------------------------------------------
void Serial2Client::get_device_property()
{
/*----- PROTECTED REGION ID(Serial2Client::get_device_property_before) ENABLED START -----*/
// Initialize property data members
/*----- PROTECTED REGION END -----*/ // Serial2Client::get_device_property_before
mandatoryNotDefined = false;
// Read device properties from database.
Tango::DbData dev_prop;
dev_prop.push_back(Tango::DbDatum("DeviceName"));
// is there at least one property to be read ?
if (dev_prop.size()>0)
{
// Call database and extract values
if (Tango::Util::instance()->_UseDb==true)
get_db_device()->get_property(dev_prop);
// get instance on Serial2ClientClass to get class property
Tango::DbDatum def_prop, cl_prop;
Serial2ClientClass *ds_class =
(static_cast<Serial2ClientClass *>(get_device_class()));
int i = -1;
// Try to initialize DeviceName from class property
cl_prop = ds_class->get_class_property(dev_prop[++i].name);
if (cl_prop.is_empty()==false) cl_prop >> deviceName;
else {
// Try to initialize DeviceName from default device value
def_prop = ds_class->get_default_device_property(dev_prop[i].name);
if (def_prop.is_empty()==false) def_prop >> deviceName;
}
// And try to extract DeviceName value from database
if (dev_prop[i].is_empty()==false) dev_prop[i] >> deviceName;
// Property StartDsPath is mandatory, check if has been defined in database.
check_mandatory_property(cl_prop, dev_prop[i]);
}
/*----- PROTECTED REGION ID(Serial2Client::get_device_property_after) ENABLED START -----*/
// Check device property data members init
/*----- PROTECTED REGION END -----*/ // Serial2Client::get_device_property_after
}
//--------------------------------------------------------
/**
* Method : Serial2Client::check_mandatory_property()
* Description : For mandatory properties check if defined in database.
*/
//--------------------------------------------------------
void Serial2Client::check_mandatory_property(Tango::DbDatum &class_prop, Tango::DbDatum &dev_prop)
{
// Check if all properties are empty
if (class_prop.is_empty() && dev_prop.is_empty())
{
TangoSys_OMemStream tms;
tms << endl <<"Property \'" << dev_prop.name;
if (Tango::Util::instance()->_UseDb==true)
tms << "\' is mandatory but not defined in database";
else
tms << "\' is mandatory but cannot be defined without database";
string status(get_status());
status += tms.str();
set_status(status);
mandatoryNotDefined = true;
/*----- PROTECTED REGION ID(Serial2Client::check_mandatory_property) ENABLED START -----*/
cerr << tms.str() << " for " << device_name << endl;
/*----- PROTECTED REGION END -----*/ // Serial2Client::check_mandatory_property
}
}
//--------------------------------------------------------
/**
* Method : Serial2Client::always_executed_hook()
* Description : method always executed before any command is executed
*/
//--------------------------------------------------------
void Serial2Client::always_executed_hook()
{
DEBUG_STREAM << "Serial2Client::always_executed_hook() " << device_name << endl;
if (mandatoryNotDefined)
{
string status(get_status());
Tango::Except::throw_exception(
(const char *)"PROPERTY_NOT_SET",
status.c_str(),
(const char *)"Serial2Client::always_executed_hook()");
}
/*----- PROTECTED REGION ID(Serial2Client::always_executed_hook) ENABLED START -----*/
// code always executed before all requests
if (! init_error.empty()) {
set_state( Tango::FAULT );
set_status( init_error );
}
/*----- PROTECTED REGION END -----*/ // Serial2Client::always_executed_hook
}
//--------------------------------------------------------
/**
* Method : Serial2Client::read_attr_hardware()
* Description : Hardware acquisition for attributes
*/
//--------------------------------------------------------
void Serial2Client::read_attr_hardware(TANGO_UNUSED(vector<long> &attr_list))
{
DEBUG_STREAM << "Serial2Client::read_attr_hardware(vector<long> &attr_list) entering... " << endl;
/*----- PROTECTED REGION ID(Serial2Client::read_attr_hardware) ENABLED START -----*/
// Add your own code
check_init();
/*----- PROTECTED REGION END -----*/ // Serial2Client::read_attr_hardware
}
//--------------------------------------------------------
/**
* Method : Serial2Client::add_dynamic_attributes()
* Description : Create the dynamic attributes if any
* for specified device.
*/
//--------------------------------------------------------
void Serial2Client::add_dynamic_attributes()
{
/*----- PROTECTED REGION ID(Serial2Client::add_dynamic_attributes) ENABLED START -----*/
// Add your own code to create and add dynamic attributes if any
/*----- PROTECTED REGION END -----*/ // Serial2Client::add_dynamic_attributes
}
//--------------------------------------------------------
/**
* Method : Serial2Client::add_dynamic_commands()
* Description : Create the dynamic commands if any
* for specified device.
*/
//--------------------------------------------------------
void Serial2Client::add_dynamic_commands()
{
/*----- PROTECTED REGION ID(Serial2Client::add_dynamic_commands) ENABLED START -----*/
// Add your own code to create and add dynamic commands if any
/*----- PROTECTED REGION END -----*/ // Serial2Client::add_dynamic_commands
}
/*----- PROTECTED REGION ID(Serial2Client::namespace_ending) ENABLED START -----*/
// Additional Methods
void Serial2Client::check_init()
{
if (! init_error.empty())
Tango::Except::throw_exception( "",
init_error.c_str(),
"Serial2Client::check_init()");
}
void Serial2Client::write_(const vector<unsigned char> &data_to_write)
{
check_init();
vector<unsigned char> *data_to_write_
= const_cast< vector<unsigned char>* >(&data_to_write);
Tango::DeviceData dtw;
dtw << *data_to_write_;
device_proxy->command_inout("Write", dtw);
}
void Serial2Client::read_(const Tango::DevLong len, vector<unsigned char> &data_to_read)
{
check_init();
Tango::DeviceData dtr, dtr_len;
dtr_len << len;
dtr = device_proxy->command_inout("Read", dtr_len);
dtr >> data_to_read;
}
void Serial2Client::read_until_(const unsigned char delim, vector<unsigned char> &data_to_read)
{
check_init();
vector<unsigned char> delim_;
delim_.push_back(delim);
Tango::DeviceData dtr, dtr_delim;
dtr_delim << delim_;
dtr = device_proxy->command_inout("ReadUntil", dtr_delim);
dtr >> data_to_read;
}
void Serial2Client::write(const vector<unsigned char> &data_to_write)
{
write_(data_to_write);
DEBUG_STREAM << "Written " << data_to_write.size()
<< " byte(s): " << dump_hex(data_to_write) << endl;
}
void Serial2Client::read(const Tango::DevLong len, vector<unsigned char> &data_to_read)
{
read_(len, data_to_read);
DEBUG_STREAM << "Readed " << data_to_read.size()
<< " byte(s): " << dump_hex(data_to_read) << endl;
}
void Serial2Client::read_until(const unsigned char delim, vector<unsigned char> &data_to_read)
{
read_until_(delim, data_to_read);
DEBUG_STREAM << "Readed " << data_to_read.size()
<< " byte(s): " << dump_hex(data_to_read) << endl;
}
void Serial2Client::write(const string &data_to_write)
{
vector<unsigned char> data;
for (unsigned int i=0; i<data_to_write.size(); ++i)
data.push_back(data_to_write[i]);
write_(data);
DEBUG_STREAM << "Written " << data.size()
<< " byte(s): " << dump_ascii(data) << endl;
}
void Serial2Client::read(const Tango::DevLong len, string &data_readed)
{
vector<unsigned char> data;
read_(len, data);
data_readed = "";
for (unsigned int i=0; i<data.size(); ++i)
data_readed += data[i];
DEBUG_STREAM << "Readed " << data.size()
<< " byte(s): " << dump_ascii(data) << endl;
}
void Serial2Client::read_until(const char delim, string &data_readed)
{
vector<unsigned char> data;
read_until_(delim, data);
data_readed = "";
for (unsigned int i=0; i<data.size(); ++i)
data_readed += data[i];
DEBUG_STREAM << "Readed " << data.size()
<< " byte(s): " << dump_ascii(data) << endl;
}
int Serial2Client::output_length()
{
check_init();
Tango::DevLong ret;
device_proxy->read_attribute("OutputLength") >> ret;
return ret;
}
int Serial2Client::input_length()
{
check_init();
Tango::DevLong ret;
device_proxy->read_attribute("InputLength") >> ret;
return ret;
}
Tango::DevState Serial2Client::get_state2()
{
check_init();
Tango::DevState state;
device_proxy->read_attribute( "State" ) >> state;
return state;
}
string Serial2Client::get_status2()
{
check_init();
string status;
device_proxy->read_attribute( "Status" ) >> status;
return status;
}
/*
void StreamClient::read(Tango::DeviceProxy &dp, const Tango::DevLong len,
Tango::DevVarCharArray &data_readed)
{
Tango::DeviceData dtr, dtr_len;
dtr_len << len;
dtr = dp.command_inout("Read", dtr_len);
const Tango::DevVarCharArray *dvca;
dtr >> dvca;
data_readed.length(dvca->length());
for (unsigned int i=0; i<dvca->length(); ++i)
data_readed[i] = (*dvca)[i];
}
void StreamClient::read_until(Tango::DeviceProxy &dp,
const Tango::DevVarCharArray &delim,
Tango::DevVarCharArray &data_readed)
{
Tango::DeviceData dtr, d;
Tango::DevVarCharArray *temp = new Tango::DevVarCharArray();
temp->length(delim.length());
for (unsigned int i=0; i<delim.length(); ++i)
(*temp)[i] = delim[i];
d << temp;
dtr = dp.command_inout("ReadUntil", d);
const Tango::DevVarCharArray *dvca;
dtr >> dvca;
data_readed.length(dvca->length());
for (unsigned int i=0; i<dvca->length(); ++i)
data_readed[i] = (*dvca)[i];
}
*/
/*
void Serial2Client::write(const Tango::DevVarCharArray *data_to_write)
{
Tango::DeviceData dtw;
dtw << data_to_write;
device_proxy->command_inout("Write", dtw);
}
*/
/*
void StreamClient::write(const Tango::DevString *data_to_write)
{
}
void StreamClient::write_and_read(Tango::DeviceProxy &dp,
const Tango::DevVarCharArray &data_to_write,
const Tango::DevLong len, Tango::DevVarCharArray &data_readed)
{
write(dp, data_to_write);
read(dp, len, data_readed);
}
void StreamClient::write_and_read(Tango::DeviceProxy &dp,
const string &data_to_write, const Tango::DevLong len,
string &data_readed)
{
write(dp, data_to_write);
read(dp, len, data_readed);
}
void StreamClient::write_and_read_until(Tango::DeviceProxy &dp,
const Tango::DevVarCharArray &data_to_write,
const Tango::DevVarCharArray &delim,
Tango::DevVarCharArray &data_readed)
{
write(dp, data_to_write);
read_until(dp, delim, data_readed);
}
void StreamClient::write_and_read_until(Tango::DeviceProxy &dp,
const string &data_to_write, const string &delim,
string &data_readed)
{
write(dp, data_to_write);
read_until(dp, delim, data_readed);
}
*/
string Serial2Client::dump_ascii(const vector<unsigned char> &data)
{
stringstream mesg_ascii;
for (unsigned int i=0; i<data.size(); ++i) {
if (isspace((char)data[i])) {
if ((int)data[i] == 0x20)
mesg_ascii << (char)data[i];
} else
mesg_ascii << (char)data[i];
}
return mesg_ascii.str();
}
string Serial2Client::dump_hex(const vector<unsigned char> &data)
{
stringstream mesg_hex;
for (unsigned int i=0; i<data.size(); ++i) {
mesg_hex << setw(2) << setfill('0')
<< hex << (int)data[i] << " ";
}
return mesg_hex.str();
}
/*----- PROTECTED REGION END -----*/ // Serial2Client::namespace_ending
} // namespace
/*----- PROTECTED REGION ID(Serial2Client.h) ENABLED START -----*/
//=============================================================================
//
// file : Serial2Client.h
//
// description : Include file for the Serial2Client class
//
// project :
//
// 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/>.
//
// $Author: alessio $
//
// $Revision: 1.5 $
// $Date: 2018-07-09 13:58:05 $
//
// $HeadURL: $
//
//=============================================================================
// This file is generated by POGO
// (Program Obviously used to Generate tango Object)
//=============================================================================
#ifndef Serial2Client_H
#define Serial2Client_H
#include <tango.h>
/*----- PROTECTED REGION END -----*/ // Serial2Client.h
/**
* Serial2Client class description: