Commit 5a3ce391 authored by Pavel Vainerman's avatar Pavel Vainerman

(admin): небольшой рефакторинг,

и мелкие исправления по другим утилитам
parent 90ec6540
// --------------------------------------------------------------------------
#include <memory>
#include <vector>
#include <string>
#include <sstream>
......@@ -57,7 +58,7 @@ static struct option longopts[] =
string conffile("configure.xml");
// --------------------------------------------------------------------------
static bool commandToAll( const string& section, ObjectRepository* rep, Command cmd );
static bool commandToAll( const string& section, std::shared_ptr<ObjectRepository> rep, Command cmd );
static void createSections( const std::shared_ptr<UniSetTypes::Configuration> c );
// --------------------------------------------------------------------------
int omap();
......@@ -212,12 +213,10 @@ int main(int argc, char** argv)
verb = true;
Command cmd = Exist;
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
// cout<<"(exist): done"<<endl;
}
return 0;
......@@ -229,11 +228,10 @@ int main(int argc, char** argv)
UInterface ui(conf);
Command cmd = StartUp;
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
}
return 0;
......@@ -253,11 +251,10 @@ int main(int argc, char** argv)
UInterface ui(conf);
Command cmd = Finish;
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
if( verb )
cout << "(finish): done" << endl;
......@@ -289,11 +286,10 @@ int main(int argc, char** argv)
UInterface ui(conf);
Command cmd = FoldUp;
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
// cout<<"(foldUp): done"<<endl;
}
......@@ -345,7 +341,7 @@ int main(int argc, char** argv)
}
// ==============================================================================================
static bool commandToAll(const string& section, ObjectRepository* rep, Command cmd)
static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository> rep, Command cmd)
{
if( verb )
cout << "\n||=======******** " << section << " ********=========||\n" << endl;
......@@ -520,29 +516,27 @@ int setValue( const string& args, UInterface& ui )
{
int err = 0;
auto conf = ui.getConf();
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList(args, conf);
auto sl = UniSetTypes::getSInfoList(args, conf);
if( verb )
cout << "====== setValue ======" << endl;
for( SList::iterator it = sl.begin(); it != sl.end(); ++it )
for( auto && it : sl )
{
try
{
UniversalIO::IOType t = conf->getIOType(it->si.id);
UniversalIO::IOType t = conf->getIOType(it.si.id);
if( verb )
{
cout << " value: " << it->val << endl;
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " value: " << it.val << endl;
cout << " name: (" << it.si.id << ") " << it.fname << endl;
cout << " iotype: " << t << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n\n";
cout << " text: " << conf->oind->getTextName(it.si.id) << "\n\n";
}
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
if( it.si.node == DefaultObjectId )
it.si.node = conf->getLocalNode();
switch(t)
{
......@@ -550,18 +544,18 @@ int setValue( const string& args, UInterface& ui )
case UniversalIO::DO:
case UniversalIO::AI:
case UniversalIO::AO:
ui.setValue(it->si.id, it->val, it->si.node);
ui.setValue(it.si.id, it.val, it.si.node);
break;
default:
if( !quiet )
cerr << "FAILED: Unknown 'iotype' for " << it->fname << endl;
cerr << "FAILED: Unknown 'iotype' for " << it.fname << endl;
err = 1;
break;
}
}
catch(Exception& ex)
catch( const Exception& ex )
{
if( !quiet )
cerr << "(setValue): " << ex << endl;;
......@@ -579,27 +573,26 @@ int getValue( const string& args, UInterface& ui )
int err = 0;
auto conf = ui.getConf();
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, conf );
auto sl = UniSetTypes::getSInfoList( args, conf );
if( !quiet )
cout << "====== getValue ======" << endl;
for( SList::iterator it = sl.begin(); it != sl.end(); ++it )
for( auto && it : sl )
{
try
{
UniversalIO::IOType t = conf->getIOType(it->si.id);
UniversalIO::IOType t = conf->getIOType(it.si.id);
if( !quiet )
{
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " name: (" << it.si.id << ") " << it.fname << endl;
cout << " iotype: " << t << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n\n";
cout << " text: " << conf->oind->getTextName(it.si.id) << "\n\n";
}
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
if( it.si.node == DefaultObjectId )
it.si.node = conf->getLocalNode();
switch(t)
{
......@@ -608,15 +601,15 @@ int getValue( const string& args, UInterface& ui )
case UniversalIO::AO:
case UniversalIO::AI:
if( !quiet )
cout << " value: " << ui.getValue(it->si.id, it->si.node) << endl;
cout << " value: " << ui.getValue(it.si.id, it.si.node) << endl;
else
cout << ui.getValue(it->si.id, it->si.node);
cout << ui.getValue(it.si.id, it.si.node);
break;
default:
if( !quiet )
cerr << "FAILED: Unknown 'iotype' for " << it->fname << endl;
cerr << "FAILED: Unknown 'iotype' for " << it.fname << endl;
err = 1;
break;
......@@ -638,27 +631,26 @@ int getCalibrate( const std::string& args, UInterface& ui )
{
int err = 0;
auto conf = ui.getConf();
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, conf );
auto sl = UniSetTypes::getSInfoList( args, conf );
if( !quiet )
cout << "====== getCalibrate ======" << endl;
for( SList::iterator it = sl.begin(); it != sl.end(); ++it )
for( auto && it : sl )
{
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
if( it.si.node == DefaultObjectId )
it.si.node = conf->getLocalNode();
try
{
if( !quiet )
{
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n";
cout << " name: (" << it.si.id << ") " << it.fname << endl;
cout << " text: " << conf->oind->getTextName(it.si.id) << "\n";
cout << "калибровка: ";
}
IOController_i::CalibrateInfo ci = ui.getCalibrateInfo(it->si);
IOController_i::CalibrateInfo ci = ui.getCalibrateInfo(it.si);
if( !quiet )
cout << ci << endl;
......@@ -682,27 +674,26 @@ int getRawValue( const std::string& args, UInterface& ui )
{
int err = 0;
auto conf = ui.getConf();
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, conf );
auto sl = UniSetTypes::getSInfoList( args, conf );
if( !quiet )
cout << "====== getRawValue ======" << endl;
for( SList::iterator it = sl.begin(); it != sl.end(); ++it )
for( auto && it : sl )
{
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
if( it.si.node == DefaultObjectId )
it.si.node = conf->getLocalNode();
try
{
if( !quiet )
{
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n\n";
cout << " value: " << ui.getRawValue(it->si) << endl;
cout << " name: (" << it.si.id << ") " << it.fname << endl;
cout << " text: " << conf->oind->getTextName(it.si.id) << "\n\n";
cout << " value: " << ui.getRawValue(it.si) << endl;
}
else
cout << ui.getRawValue(it->si);
cout << ui.getRawValue(it.si);
}
catch( const Exception& ex )
{
......@@ -724,11 +715,10 @@ int logRotate( const string& arg, UInterface& ui )
// посылка всем
if( arg.empty() || (arg.c_str())[0] != '-' )
{
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)LogRotate);
commandToAll(conf->getControllersSection(), rep, (Command)LogRotate);
commandToAll(conf->getObjectsSection(), rep, (Command)LogRotate);
delete rep;
}
else // посылка определённому объекту
{
......@@ -761,11 +751,10 @@ int configure( const string& arg, UInterface& ui )
// посылка всем
if( arg.empty() || (arg.c_str())[0] != '-' )
{
ObjectRepository* rep = new ObjectRepository(conf);
auto rep = make_shared<ObjectRepository>(conf);
commandToAll(conf->getServicesSection(), rep, (Command)Configure);
commandToAll(conf->getControllersSection(), rep, (Command)Configure);
commandToAll(conf->getObjectsSection(), rep, (Command)Configure);
delete rep;
}
else // посылка определённому объекту
{
......
......@@ -50,7 +50,7 @@ int main( int argc, char** argv )
return 1;
}
std::list<UniSetTypes::ParamSInfo> lst = UniSetTypes::getSInfoList(sid, conf);
auto lst = UniSetTypes::getSInfoList(sid, conf);
if( lst.empty() )
{
......@@ -60,21 +60,21 @@ int main( int argc, char** argv )
std::list<ExtInfo> l;
for( std::list<UniSetTypes::ParamSInfo>::iterator it = lst.begin(); it != lst.end(); ++it )
for( auto&& it: lst )
{
UniversalIO::IOType t = conf->getIOType( it->si.id );
UniversalIO::IOType t = conf->getIOType( it.si.id );
if( t != UniversalIO::AI && t != UniversalIO::AO )
{
cerr << endl << "Неверный типа датчика '" << t << "' для id='" << it->fname << "'. Тип должен быть AI или AO." << endl << endl;
cerr << endl << "Неверный типа датчика '" << t << "' для id='" << it.fname << "'. Тип должен быть AI или AO." << endl << endl;
return 1;
}
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
if( it.si.node == DefaultObjectId )
it.si.node = conf->getLocalNode();
ExtInfo i;
i.si = it->si;
i.si = it.si;
i.iotype = t;
l.push_back(i);
}
......@@ -129,15 +129,15 @@ int main( int argc, char** argv )
cout << "\r" << " i = " << j << " " << flush;
for( std::list<ExtInfo>::iterator it = l.begin(); it != l.end(); ++it )
for( const auto& it: l )
{
try
{
ui.setValue(it->si, j, DefaultObjectId);
ui.setValue(it.si, j, DefaultObjectId);
}
catch( const Exception& ex )
{
cerr << endl << "save id=" << it->fname << " " << ex << endl;
cerr << endl << "save id=" << it.fname << " " << ex << endl;
}
}
......
......@@ -159,11 +159,6 @@ TEST_CASE("Function (0x04): 'read input registers or memories or read word outpu
ModbusRTU::ReadInputRetMessage ret = mb->read04(slaveaddr, tREG, 1);
REQUIRE( ret.data[0] == 10 );
}
SECTION("Test: read one reg..")
{
ModbusRTU::ReadInputRetMessage ret = mb->read04(slaveaddr, tREG, 1);
REQUIRE( ret.data[0] == 10 );
}
SECTION("Test: read many registers..")
{
ModbusRTU::ReadInputRetMessage ret = mb->read04(slaveaddr, tREG, 4);
......
......@@ -16,7 +16,7 @@ class SMInterface
~SMInterface();
void setValue ( UniSetTypes::ObjectId, long value );
void setUndefinedState( IOController_i::SensorInfo& si, bool undefined, UniSetTypes::ObjectId supplier );
void setUndefinedState( const IOController_i::SensorInfo& si, bool undefined, UniSetTypes::ObjectId supplier );
long getValue ( UniSetTypes::ObjectId id );
......
......@@ -179,7 +179,7 @@ using namespace UniSetTypes;
END_FUNC(SMInterface::getThresholdsList)
}
// --------------------------------------------------------------------------
void SMInterface::setUndefinedState( IOController_i::SensorInfo& si, bool undefined,
void SMInterface::setUndefinedState( const IOController_i::SensorInfo& si, bool undefined,
UniSetTypes::ObjectId sup_id )
{
if( ic )
......
......@@ -80,7 +80,7 @@ class UInterface
//! Выставление состояния датчика
void setValue ( const UniSetTypes::ObjectId id, long value, const UniSetTypes::ObjectId node ) const throw(UI_THROW_EXCEPTIONS);
void setValue ( const UniSetTypes::ObjectId id, long value ) const;
void setValue ( IOController_i::SensorInfo& si, long value, const UniSetTypes::ObjectId supplier );
void setValue ( const IOController_i::SensorInfo& si, long value, const UniSetTypes::ObjectId supplier );
// fast - это удалённый вызов "без подтверждения", он быстрее, но менее надёжен
// т.к. вызывающий никогда не узнает об ошибке, если она была (датчик такой не найдён и т.п.)
......
......@@ -365,7 +365,7 @@ void UInterface::setValue( const ObjectId name, long value ) const
}
void UInterface::setValue( IOController_i::SensorInfo& si, long value, const UniSetTypes::ObjectId supplier )
void UInterface::setValue( const IOController_i::SensorInfo& si, long value, const UniSetTypes::ObjectId supplier )
{
ObjectId old = myid;
......
......@@ -321,7 +321,7 @@ std::list<UniSetTypes::ParamSInfo> UniSetTypes::getSInfoList( const string& str,
res.push_back(item);
}
return res;
return std::move(res);
}
// --------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment