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

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

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