Commit 3e5b5a27 authored by Pavel Vainerman's avatar Pavel Vainerman

Рефакторинг uniset-admin:

1. Убрал лишние неиспользуемые функции 2. Добавил возможность обращения к удалённым датчикам 3. Новый формат задания списка датчиков в командной строке: id1@node@=val1,id2@node2=val2,id3=val3,id4=val4,...
parent 62c6a8a7
......@@ -3,6 +3,7 @@
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <getopt.h>
// --------------------------------------------------------------------------
#include "ORepHelpers.h"
......@@ -10,10 +11,9 @@
#include "ObjectRepositoryFactory.h"
#include "Exceptions.h"
#include "UniSetObject.h"
#include "UniSetTypes.h"
#include "ObjectsManager.h"
#include "MessageType.h"
//#include "DBServer.h"
//#include "InfoServer.h"
#include "Configuration.h"
#include "ObjectIndex_XML.h"
#include "Debug.h"
......@@ -21,9 +21,6 @@
using namespace std;
using namespace UniSetTypes;
// --------------------------------------------------------------------------
// \todo п╫п╟п╢п╬ п©п╣я─п╣п©п╦я│п╟я┌я▄ я─п╣п╟п╩п╦п╥п╟я├п╦я▌ !!!!
// --------------------------------------------------------------------------
enum Command
{
StartUp,
......@@ -46,16 +43,9 @@ static struct option longopts[] = {
{ "foldUp", no_argument, 0, 'u' },
{ "configure", required_argument, 0, 'r' },
{ "logrotate", required_argument, 0, 'l' },
{ "alarm", required_argument, 0, 'a' },
{ "anotify", required_argument, 0, 'n' },
{ "dnotify", required_argument, 0, 'd' },
{ "info", required_argument, 0, 'i' },
{ "saveValue", required_argument, 0, 'v' },
{ "saveState", required_argument, 0, 't' },
{ "setValue", required_argument, 0, 'x' },
{ "setState", required_argument, 0, 'j' },
{ "getValue", required_argument, 0, 'g' },
{ "getState", required_argument, 0, 'k' },
{ "getRawValue", required_argument, 0, 'w' },
{ "getCalibrate", required_argument, 0, 'y' },
{ "oinfo", required_argument, 0, 'p' },
......@@ -67,27 +57,17 @@ string conffile("configure.xml");
// --------------------------------------------------------------------------
static bool commandToAll(const string section, ObjectRepository *rep, Command cmd);
static void createSections(UniSetTypes::Configuration* c);
static bool separateArgs(string &args, string &arg);
//static bool getID( const string arg, ObjectId id, ObjectId node );
// --------------------------------------------------------------------------
int omap();
int msgmap();
int configure( string args, UniversalInterface &ui );
int logRotate( string args, UniversalInterface &ui );
int alarm( string args, UniversalInterface &ui );
int anotify( string args, UniversalInterface &ui );
int dnotify( string args, UniversalInterface &ui );
int info( string args, UniversalInterface &ui );
int saveValue( string args, UniversalInterface &ui );
int saveState( string args, UniversalInterface &ui );
int setValue( string args, UniversalInterface &ui );
int setState( string args, UniversalInterface &ui );
int getValue( string args, UniversalInterface &ui );
int getRawValue( string args, UniversalInterface &ui );
int getState( string args, UniversalInterface &ui );
int getCalibrate( string args, UniversalInterface &ui );
int oinfo( string args, UniversalInterface &ui );
int configure( const string args, UniversalInterface &ui );
int logRotate( const string args, UniversalInterface &ui );
int setValue( const string args, UniversalInterface &ui, Configuration* conf = UniSetTypes::conf );
int getValue( const string args, UniversalInterface &ui, Configuration* conf = UniSetTypes::conf );
int getRawValue( const string args, UniversalInterface &ui );
int getState( const string args, UniversalInterface &ui );
int getCalibrate( const string args, UniversalInterface &ui );
int oinfo( const string args, UniversalInterface &ui );
// --------------------------------------------------------------------------
static void print_help(int width, const string cmd, const string help, const string tab=" " )
{
......@@ -123,21 +103,13 @@ static void usage()
cout << endl;
print_help(36, "-r|--configure [FullObjName] ","Посылка SystemMessage::ReConfiguration всем объектам (процессам) или заданному по имени (FullObjName).\n");
print_help(36, "-l|--logrotate [FullObjName] ","Посылка SystemMessage::LogRotate всем объектам (процессам) или заданному по имени (FullObjName).\n");
print_help(36, "-a|--alarm [code,cause,character] ","Посылка AlarmMessage(character: 1-Normal, 2-Warning, 3-Alarm, default: Alarm)\n");
print_help(36, "-i|--info [code] ","Посылка InfoMessage\n");
print_help(36, "-p|--oinfo OID ","Получить информацию об объекте (SimpleInfo).\n");
cout << endl;
print_help(48, "-n|--anotify ObjectId,SensorId,value ","Посылка SensorMessage (аналоговый датчик) объекту ObjectId\n");
print_help(48, "-d|--dnotify ObjectId,SensorId,state ","Посылка SensorMessage (дискретный датчик) объекту ObjectId\n");
print_help(48, "-v|--saveValue SensorId=value[,SId2=v2,...] ","Установить значение аналогоых датчиков SensorId в значение value.\n");
print_help(48, "-t|--saveState SensorId=state[,SId2=s2,...] ","Установить значение дискретных датчиков SensorId в значение state.\n");
print_help(48, "-x|--setValue OutputId=value[,SId2=v2,...] ","Подать на аналоговые выходы OuputId значение value.\n");
print_help(48, "-j|--setState OutputId=state[,SId2=s2,...] ","Подать на дискретные выходы OuputId значение state.\n");
print_help(48, "-x|--setValue id1@node1=val,id2@node2=val2,id3=val3,.. ","Выставить значения датчиков\n");
print_help(36, "-g|--getValue id1@node1,id2@node2,id3,id4 ","Получить значения датчиков.\n");
cout << endl;
print_help(36, "-g|--getValue SensorId[,SId2,...] ","Получить значение аналоговых датчиков SensorId.\n");
print_help(36, "-k|--getState SensorId[,SId2,...] ","Получить значение дискретных датчиков SensorId.\n");
print_help(36, "-w|--getRawValue SensorId? ","Получить значение аналогового датчика RawSensorId?.\n");
print_help(36, "-y|--getCalibrate SensorId? ","Получить калибровачную диаграмму?.\n");
print_help(36, "-w|--getRawValue id1@node1=val,id2@node2=val2,id3=val3,.. ","Получить 'сырое' значение.\n");
print_help(36, "-y|--getCalibrate id1@node1=val,id2@node2=val2,id3=val3,.. ","Получить параметры калибровки.\n");
cout << endl;
}
......@@ -157,27 +129,20 @@ int main(int argc, char** argv)
char opt = 0;
while( (opt = getopt_long(argc, argv, "hc:beomsfur:l:a:n:d:i:v:t:x:j:g:k:w:p:y:",longopts,&optindex)) != -1 )
while( (opt = getopt_long(argc, argv, "hc:beomsfur:l:i:x:g:w:y:p:",longopts,&optindex)) != -1 )
{
switch (opt) //разбираем параметры
{
case 'h': //--help
{
// cout<<"(main):received option --help"<<endl;
usage();
}
return 0;
case 'c': //--confile
{
// cout<<"(main):received option --confile='"<<optarg<<"'"<<endl;
conffile = optarg;
}
break;
case 'o': //--omap
{
// cout<<"(main):received option --omap"<<endl;
uniset_init(argc,argv,conffile);
return omap();
}
......@@ -185,7 +150,6 @@ int main(int argc, char** argv)
case 'b': //--create
{
// cout<<"(main):received option --create"<<endl;
uniset_init(argc,argv,conffile);
createSections(conf);
}
......@@ -193,84 +157,19 @@ int main(int argc, char** argv)
case 'm': //--msgmap
{
// cout<<"(main):received option --msgmap"<<endl;
uniset_init(argc,argv,conffile);
return msgmap();
}
break;
case 'a': //--alarm
{
// cout<<"(main):received option --alarm='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return alarm(optarg,ui);
}
break;
case 'i': //--info
{
// cout<<"(main):received option -info='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return info(optarg,ui);
}
break;
case 'n': //--anotify
{
// cout<<"(main):received option --anotify='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return anotify(optarg,ui);
}
break;
case 'd': //--dnotify
{
// cout<<"(main):received option --dnotify='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return dnotify(optarg,ui);
}
break;
case 'v': //--saveValue
{
// cout<<"(main):received option --saveValue='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return saveValue(optarg,ui);
}
break;
case 't': //--saveState
{
// cout<<"(main):received option --saveState='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return saveState(optarg,ui);
}
break;
case 'x': //--setValue
{
// cout<<"(main):received option --setValue='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return setValue(optarg,ui);
}
break;
case 'j': //--setState
{
// cout<<"(main):received option --setState='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return setState(optarg,ui);
}
break;
case 'g': //--getValue
{
// cout<<"(main):received option --getValue='"<<optarg<<"'"<<endl;
......@@ -280,15 +179,6 @@ int main(int argc, char** argv)
}
break;
case 'k': //--getState
{
// cout<<"(main):received option --getState='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return getState(optarg,ui);
}
break;
case 'w': //--getRawValue
{
// cout<<"(main):received option --getRawValue='"<<optarg<<"'"<<endl;
......@@ -340,19 +230,10 @@ int main(int argc, char** argv)
case 'r': //--configure
{
// cout<<"(main):received option --configure='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return configure(optarg,ui);
/*
Command cmd=Configure;
ObjectRepository* rep = new ObjectRepository(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
*/ }
}
break;
case 'f': //--finish
......@@ -373,18 +254,10 @@ int main(int argc, char** argv)
case 'l': //--logrotate
{
// cout<<"(main):received option --logrotate='"<<optarg<<"'"<<endl;
uniset_init(argc,argv,conffile);
UniversalInterface ui(conf);
return logRotate(optarg, ui);
/*
Command cmd=LogRotate;
ObjectRepository* rep = new ObjectRepository(conf);
commandToAll(conf->getServicesSection(), rep, (Command)cmd);
commandToAll(conf->getControllersSection(), rep, (Command)cmd);
commandToAll(conf->getObjectsSection(), rep, (Command)cmd);
delete rep;
*/ }
}
break;
case 'y': //--getCalibrate
......@@ -422,7 +295,6 @@ int main(int argc, char** argv)
}
}
cout <<"\nвсе."<< endl;
return 0;
}
catch(Exception& ex)
......@@ -575,24 +447,6 @@ static void createSections( UniSetTypes::Configuration* rconf )
}
// ==============================================================================================
bool separateArgs(string &args,string &arg)
{
int ind;
if(args=="")
return false;
ind = args.find_first_of(",");
arg = args.substr(0,ind);
args = args.substr(ind+1,args.size());
if (args==arg)
args="";
return true;
}
// ==============================================================================================
int omap()
{
try
......@@ -630,608 +484,169 @@ int msgmap()
}
// --------------------------------------------------------------------------------------
int alarm(string args, UniversalInterface &ui)
int setValue( const string args, UniversalInterface &ui, Configuration* conf )
{
string arg="";
if( args.size() == 0 || strncmp(args.c_str(),"-",1)==0 )
{
AlarmMessage am(UniSetTypes::DefaultObjectId, "Administrator Alarm: Тестовое сообщение",
UniSetTypes::DefaultObjectId, conf->getLocalNode());
TransportMessage tm(am.transport_msg());
cout << "Administrator Alarm: Тестовое сообщение " << endl;
ui.send(conf->getInfoServer(), tm);
return 0;
}
int err = 0;
if(separateArgs(args,arg))
{
UniSetTypes::MessageCode code = uni_atoi(arg);
UniSetTypes::MessageCode cause(UniSetTypes::DefaultMessageCode);
if(separateArgs(args,arg))
{
cause = uni_atoi(arg);
}
else
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList(args, conf);
cout << "====== setValue ======" << endl;
for( SList::iterator it=sl.begin(); it!=sl.end(); it++ )
{
try
{
cerr<<"(alarm): cause is not specified ! \n";
unideb[Debug::WARN] << "cause is not specified ! \n";
return 1;
}
AlarmMessage::Character ch(AlarmMessage::Alarm);
int chid ;
// cout<<"!!!!!! args="<<args<<" arg="<<arg<<" cause="<<cause<<" code="<<code<<endl;
UniversalIO::IOTypes t = conf->getIOType(it->si.id);
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";
if( sscanf( args.c_str(),"%d",&chid) < 1 )
{
cerr<<"(alarm): ch is not specified ! \n";
unideb[Debug::WARN] << "ch is not specified ! \n";
return 1;
}
switch(chid)
{
case 1:
ch = AlarmMessage::Normal;
break;
case 2:
ch = AlarmMessage::Warning;
break;
case 3:
ch = AlarmMessage::Alarm;
break;
}
cout << "alarm character(" << chid << "):\t" << ch << endl;
AlarmMessage am(UniSetTypes::DefaultObjectId,code,cause, conf->getLocalNode(), ch);
TransportMessage tm(am.transport_msg());
cout << "alarm message(" << code << "):\t";
cout << conf->mi->getMessage(code) << endl;
if( cause )
{
cout << "alarm cause(" << cause << "):\t";
cout << conf->mi->getMessage(cause) << endl;
}
cout << endl;
ui.send(conf->getInfoServer(), tm);
cout << "send alarm OK" << endl;
}
else
{
cerr<<"(alarm): params are not specified ! \n";
unideb[Debug::WARN] << "params are not specified ! \n";
return 1;
}
return 0;
}
// --------------------------------------------------------------------------------------
int info(string arg, UniversalInterface &ui)
{
if( arg.size() == 0 || strncmp(arg.c_str(),"-",1)==0 )
{
InfoMessage im(UniSetTypes::DefaultObjectId, "Administrator Info: Тестовое сообщение");
TransportMessage tm(im.transport_msg());
cout << "Administrator Info: Тестовое сообщение " << endl;
ui.send(conf->getInfoServer(), tm);
return 0;
}
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
int icode;
if( sscanf( arg.c_str(),"%d",&icode) < 1 )
{
cerr<<"(info): code is not specified ! \n";
unideb[Debug::WARN] << "code is not specified ! \n";
return 1;
}
UniSetTypes::MessageCode code = icode;
InfoMessage im(UniSetTypes::DefaultObjectId, code);
TransportMessage tm(im.transport_msg());
cout << "info: (" << code << ") ";
cout << conf->mi->getMessage(code);
ui.send(conf->getInfoServer(), tm);
cout << "send info OK" << endl;
return 0;
}
// --------------------------------------------------------------------------------------
int anotify(string args, UniversalInterface &ui)
{
UniSetTypes::ObjectId id;
UniSetTypes::ObjectId sid;
long val;
if( sscanf( args.c_str(),"%ld,%ld,%ld",&id,&sid,&val ) < 3 )
{
cerr << "(anotify): Неверный параметр ObjectId,SensorId,Value"<< endl;
return 1;
}
cout << "anotify --------\n";
cout << " value: " << val << endl;
cout << " sensor: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << endl;
cout << "адресат: " << conf->oind->getMapName(id) << "\n"<<endl;
SensorMessage sm(sid,(long)val);
sm.consumer = id;
TransportMessage tm(sm.transport_msg());
ui.send(id,tm);
return 0;
}
// --------------------------------------------------------------------------------------
int dnotify(string args, UniversalInterface &ui)
{
UniSetTypes::ObjectId id;
UniSetTypes::ObjectId sid;
int ival;
bool val;
if( sscanf( args.c_str(),"%ld,%ld,%d",&id,&sid,&ival ) < 3 )
{
cerr << "(anotify): Неверный параметр ObjectId,SensorId,State"<< endl;
return 1;
}
if (ival == 0)
val = false;
else if( ival == 1 )
val = true;
else
{
cerr << "(anotify): Неверный параметр State. Должен быть булевым!"<< endl;
return 1;
}
cout << "dnotify --------\n";
cout << " state: " << val << endl;
cout << " sensor: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << endl;
cout << "адресат: " << conf->oind->getMapName(id) << "\n\n";
SensorMessage sm(sid,(bool)val);
sm.consumer = id;
TransportMessage tm(sm.transport_msg());
ui.send(id,tm);
return 0;
}
// --------------------------------------------------------------------------------------
int saveValue(string args, UniversalInterface &ui)
{
int err;
err=0;
string arg;
UniSetTypes::ObjectId sid = DefaultObjectId;
UniSetTypes::ObjectId node = DefaultObjectId;
long val;
cout << "====== saveValue ======" << endl;
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld=%ld",&sid,&val ) < 2 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "(digit): !!! пара SensorId=Value #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
else
{
int ind;
string strval;
ind = arg.find_first_of("=");
string name(arg.substr(0,ind));
strval = arg.substr( ind + 1, arg.length() );
sid = conf->getSensorID(name);
if( sid == UniSetTypes::DefaultObjectId || (sscanf( strval.c_str(),"%ld",&val ) < 1) )
{
cout << i <<"\t------------------------"<< endl;
cerr << "(name): !!! пара SensorName=Value #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
cout << i <<"\t------------------------"<< endl;
try
{
// cout <<"\n\t"<<sid<<"\t"<<val<<endl;
cout << " value: " << val << endl;
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n\n";
if( node == DefaultObjectId )
node = conf->getLocalNode();
ui.saveValue(sid,val,UniversalIO::AnalogInput,node);
}
catch(Exception& ex)
{
unideb[Debug::CRIT] << "(saveValue): " << ex << endl;;
err = 1;
}
}
return err;
}
// --------------------------------------------------------------------------------------
int saveState(string args, UniversalInterface &ui)
{
int err;
err=0;
string arg;
UniSetTypes::ObjectId sid(DefaultObjectId);
long inval;
bool val;
cout << "====== saveState ======" << endl;
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld=%ld",&sid,&inval ) < 2 )
switch(t)
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorId=State #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
else
{
int ind;
string strval;
ind = arg.find_first_of("=");
string name(arg.substr(0,ind));
strval = arg.substr( ind + 1, arg.length() );
sid = conf->getSensorID(name);
if( sid == UniSetTypes::DefaultObjectId || (sscanf( strval.c_str(),"%ld",&inval ) < 1) )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorName=State #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
case UniversalIO::DigitalInput:
ui.saveState(it->si.id,(it->val?true:false),t,it->si.node);
break;
case UniversalIO::DigitalOutput:
ui.setState(it->si.id,(it->val?true:false),it->si.node);
break;
case UniversalIO::AnalogInput:
ui.saveValue(it->si.id,it->val,t,it->si.node);
break;
case UniversalIO::AnalogOutput:
ui.setValue(it->si.id,it->val,it->si.node);
break;
default:
cerr << "FAILED: Unknown 'iotype' for " << it->fname << endl;
err = 1;
break;
}
}
if(inval==1)
val=true;
else if(inval==0)
val=false;
else
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! State в паре SensorId=State #"<<i<<" '"<<arg<<"' должен быть булевым !!!!!!\n"<< endl;
err=1;
continue;
}
cout << i <<"\t------------------------"<< endl;
try
{
// cout <<"\n\t"<<sid<<"\t"<<val<<endl;
cout << " state: " << val << endl;
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n\n";
ui.saveState(sid,val,UniversalIO::DigitalInput);
}
catch(Exception& ex)
{
unideb[Debug::CRIT] << "(saveState): " << ex << endl;;
cerr << "(setValue): " << ex << endl;;
err = 1;
}
}
return err;
}
// --------------------------------------------------------------------------------------
int setValue(string args, UniversalInterface &ui)
int getValue( const string args, UniversalInterface &ui, Configuration* conf )
{
int err;
err=0;
string arg;
UniSetTypes::ObjectId sid(DefaultObjectId);
long val;
cout << "====== setValue ======" << endl;
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld=%ld",&sid,&val ) < 2 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorId=Value #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
else
{
int ind;
string strval;
ind = arg.find_first_of("=");
string name(arg.substr(0,ind));
strval = arg.substr( ind + 1, arg.length() );
sid = conf->getSensorID(name);
if( sid == UniSetTypes::DefaultObjectId || (sscanf( strval.c_str(),"%ld",&val ) < 1) )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorName=Value #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
cout << i <<"\t------------------------"<< endl;
int err = 0;
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, UniSetTypes::conf );
cout << "====== getValue ======" << endl;
for( SList::iterator it=sl.begin(); it!=sl.end(); it++ )
{
try
{
// cout <<"\n\t"<<sid<<"\t"<<val<<endl;
cout << " value: " << val << endl;
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n\n";
ui.setValue(sid,val);
}
catch(Exception& ex)
{
unideb[Debug::CRIT] << "(setValue): " << ex << endl;;
err = 1;
}
}
return err;
}
// --------------------------------------------------------------------------------------
int setState(string args, UniversalInterface &ui)
{
int err;
err=0;
string arg;
UniSetTypes::ObjectId sid(DefaultObjectId);
long inval;
bool val;
cout << "====== setState ======" << endl;
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld=%ld",&sid,&inval ) < 2 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorId=State #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
}
}
else
{
int ind;
string strval;
ind = arg.find_first_of("=");
string name(arg.substr(0,ind));
strval = arg.substr( ind + 1, arg.length() );
sid = conf->getSensorID(name);
if( sid == UniSetTypes::DefaultObjectId || (sscanf( strval.c_str(),"%ld",&inval ) < 1) )
UniversalIO::IOTypes t = conf->getIOType(it->si.id);
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";
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
switch(t)
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! пара SensorName=State #"<<i<<" '"<<arg<<"' задана неверно!!!!!!\n"<< endl;
err=1;
continue;
case UniversalIO::DigitalOutput:
case UniversalIO::DigitalInput:
cout << " state: " << ui.getState(it->si.id,it->si.node) << endl;
break;
case UniversalIO::AnalogOutput:
case UniversalIO::AnalogInput:
cout << " value: " << ui.getValue(it->si.id,it->si.node) << endl;
break;
default:
cerr << "FAILED: Unknown 'iotype' for " << it->fname << endl;
err = 1;
break;
}
}
if(inval==1)
val=true;
else if(inval==0)
val=false;
else
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! State в паре SensorId=State #"<<i<<" '"<<arg<<"' должен быть булевым !!!!!!\n"<< endl;
err=1;
continue;
}
cout << i <<"\t------------------------"<< endl;
try
{
// cout <<"\n\t"<<sid<<"\t"<<val<<endl;
cout << " state: " << val << endl;
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n\n";
ui.setState(sid,val);
}
catch(Exception& ex)
{
unideb[Debug::CRIT] << "(setState): " << ex << endl;;
cerr << "(getValue): " << ex << endl;
err = 1;
}
}
return err;
}
// --------------------------------------------------------------------------------------
int getState(string args, UniversalInterface &ui)
int getCalibrate( const std::string args, UniversalInterface &ui )
{
int err;
err=0;
string arg;
ostringstream vout;
vout<<"-----------------\n| ID\t| State\t|\n-----------------\n";
UniSetTypes::ObjectId sid(DefaultObjectId);
int state;
cout << " getState ==============================\n";
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld",&sid ) < 1 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!! SensorID #"<<i<<" '"<<arg<<"' задан неверно!!!!!!!\n"<< endl;
err = 1;
vout<<"| "<<arg<<"\t| ?\t| SensorID задан неверно !!!\n-----------------\n";
continue;
}
}
else
{
sid = conf->getSensorID(arg);
if( sid == UniSetTypes::DefaultObjectId )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! SensorName #"<<i<<" '"<<arg<<"' задан неверно!!!!!!\n"<< endl;
err=1;
vout<<"| "<<arg<<"\t| ?\t| SensorName задан неверно !!!\n-----------------\n";
continue;
}
}
cout << i <<"\t-----------------------"<< endl;
int err = 0;
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, UniSetTypes::conf );
cout << "====== getValue ======" << endl;
for( SList::iterator it=sl.begin(); it!=sl.end(); it++ )
{
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
cout << "getCalibrate --------\n";
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n";
try
{
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << endl;
state = ui.getState(sid);
cout << " state: " <<state << "\n\n";
vout<<"| "<<arg<<"\t| "<<state<<"\t|\n-----------------\n";
cout << "калибровка: ";
IOController_i::CalibrateInfo ci = ui.getCalibrateInfo(it->si);
cout << ci << endl;
}
catch(Exception& ex)
{
cout <<"!!!!!!!!! err: " << ex << endl<<endl;
vout<<"| "<<arg<<"\t| ?\t| "<<ex<<" !!!\n-----------------\n";
cerr << "(getCalibrate): " << ex << endl;;
err = 1;
}
}
}
if( !err )
cout << vout.str() << endl;
return err;
}
// --------------------------------------------------------------------------------------
int getValue(string args, UniversalInterface &ui )
int getRawValue( const std::string args, UniversalInterface &ui )
{
int err;
long value;
err=0;
string arg;
ostringstream vout;
vout<<"-----------------\n| ID\t| Value\t|\n-----------------\n";
UniSetTypes::ObjectId sid(DefaultObjectId);
cout << "\n getValue ==============================\n";
for(int i=1;separateArgs(args,arg);i++)
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld",&sid ) < 1 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!! SensorID #"<<i<<" '"<<arg<<"' задан неверно!!!!!!!\n"<< endl;
err = 1;
vout<<"| "<<arg<<"\t| ?\t| SensorID задан неверно !!!\n-----------------\n";
continue;
}
}
else
{
sid = conf->getSensorID(arg);
if( sid == UniSetTypes::DefaultObjectId )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!!! SensorName #"<<i<<" '"<<arg<<"' задан неверно!!!!!!\n"<< endl;
err=1;
vout<<"| "<<arg<<"\t| ?\t| SensorName задан неверно !!!\n-----------------\n";
continue;
}
}
cout << i <<"\t-----------------------"<< endl;
int err = 0;
typedef std::list<UniSetTypes::ParamSInfo> SList;
SList sl = UniSetTypes::getSInfoList( args, UniSetTypes::conf );
cout << "====== getValue ======" << endl;
for( SList::iterator it=sl.begin(); it!=sl.end(); it++ )
{
if( it->si.node == DefaultObjectId )
it->si.node = conf->getLocalNode();
cout << "getRawValue --------\n";
cout << " name: (" << it->si.id << ") " << it->fname << endl;
cout << " text: " << conf->oind->getTextName(it->si.id) << "\n\n";
try
{
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << endl;
value = ui.getValue(sid);
cout << " value: " << value << "\n\n";
vout<<"| "<<arg<<"\t| "<< value <<"\t|\n-----------------\n";
cout << " value: " << ui.getRawValue(it->si) << endl;
}
catch(Exception& ex)
{
unideb[Debug::CRIT] << "(getValue): " << ex << endl;;
cerr << "(getRawValue): " << ex << endl;;
err = 1;
}
}
}
cout <<vout.str()<<endl;
return err;
}
// --------------------------------------------------------------------------------------
int getCalibrate(string arg, UniversalInterface &ui)
{
UniSetTypes::ObjectId sid(uni_atoi(arg));
if( sid <= 0 )
{
cout << "(getCalibrate): Не задан SensorId аналогового датчика!!!!!!"<< endl;
return 1;
}
cout << "getCalibrate --------\n";
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n";
cout << "калибровка: ";
IOController_i::SensorInfo si;
si.id = sid;
si.node = conf->getLocalNode();
IOController_i::CalibrateInfo ci = ui.getCalibrateInfo(si);
cout << ci << endl;
return 0;
}
// --------------------------------------------------------------------------------------
int getRawValue(string arg, UniversalInterface &ui )
{
UniSetTypes::ObjectId sid(uni_atoi(arg));
if( sid==0 )
{
cout << "(getRawValue): Не задан SensorId аналогового датчика!!!!!!"<< endl;
return 1;
}
IOController_i::SensorInfo si;
si.id = sid;
si.node = conf->getLocalNode();
cout << "getRawValue --------\n";
cout << " name: (" << sid << ") " << conf->oind->getMapName(sid) << endl;
cout << " text: " << conf->oind->getTextName(sid) << "\n\n";
cout << " value: " << ui.getRawValue(si) << endl;
return 0;
}
// --------------------------------------------------------------------------------------
int logRotate(string arg, UniversalInterface &ui )
int logRotate( const string arg, UniversalInterface &ui )
{
// посылка всем
if( arg.empty() || (arg.c_str())[0]!='-' )
......@@ -1260,7 +675,7 @@ int logRotate(string arg, UniversalInterface &ui )
}
// --------------------------------------------------------------------------------------
int configure(string arg, UniversalInterface &ui )
int configure( const string arg, UniversalInterface &ui )
{
// посылка всем
if( arg.empty() || (arg.c_str())[0]!='-' )
......@@ -1288,7 +703,7 @@ int configure(string arg, UniversalInterface &ui )
}
// --------------------------------------------------------------------------------------
int oinfo(string arg, UniversalInterface &ui )
int oinfo( const string arg, UniversalInterface &ui )
{
UniSetTypes::ObjectId oid(uni_atoi(arg));
if( oid==0 )
......@@ -1313,32 +728,3 @@ int oinfo(string arg, UniversalInterface &ui )
}
// --------------------------------------------------------------------------------------
/*
bool getID( const string arg, ObjectId id, ObjectId node )
{
if( isdigit( arg[0] ) )
{
if( sscanf( arg.c_str(),"%ld",&id ) < 1 )
{
cout << i <<"\t------------------------"<< endl;
cerr << "!!!!!!!! SensorID #"<<i<<" '"<<arg<<"' задан неверно!!!!!!!\n"<< endl;
err = 1;
vout<<"| "<<arg<<"\t| ?\t| SensorID задан неверно !!!\n-----------------\n";
continue;
}
}
string::size_type pos = arg.find_first_of(":");
if( pos == string::npos )
{
id = uni_atoi(arg);
return true;
}
id = uni_atoi(name.substr(0,pos-1));
node = uni_atoi( name.substr(pos+1,name.size()) );
return true;
}
*/
// --------------------------------------------------------------------------------------
......@@ -3,7 +3,7 @@
Name: libuniset
Version: 0.99
Release: eter11
Release: eter13
Summary: UniSet - library for building distributed industrial control systems
License: GPL
Group: Development/C++
......@@ -184,6 +184,12 @@ rm -f %buildroot%_libdir/*.la
%exclude %_pkgconfigdir/libUniSet.pc
%changelog
* Thu Nov 11 2010 Pavel Vainerman <pv@altlinux.ru> 0.99-eter13
- uniset-admin refactor. ( [get|set]Value, call remote sensors)
* Mon Nov 08 2010 Pavel Vainerman <pv@altlinux.ru> 0.99-eter12
- fixed minor bug in uniset-codegen
* Mon Oct 18 2010 Pavel Vainerman <pv@altlinux.ru> 0.99-eter9
- new build
......
......@@ -45,6 +45,9 @@ inline void msleep( unsigned int m ) { usleep(m*1000); }
/*! Определения базовых типов библиотеки UniSet */
namespace UniSetTypes
{
class Configuration;
extern Configuration* conf;
typedef std::list<std::string> ListObjectName; /*!< Список объектов типа ObjectName */
typedef ObjectId SysId;
......@@ -249,6 +252,21 @@ namespace UniSetTypes
bool file_exist( const std::string filename );
IDList explode( const std::string str, char sep=',' );
std::list<std::string> explode_str( const std::string str, char sep=',' );
struct ParamSInfo
{
IOController_i::SensorInfo si;
long val;
std::string fname; // fullname id@node or id
};
// Функция разбора строки вида: id1@node1=val1,id2@node2=val2,...
// Если '=' не указано, возвращается val=0
// Если @node не указано, возвращается node=DefaultObjectId
std::list<ParamSInfo> getSInfoList( std::string s, Configuration* conf=UniSetTypes::conf);
bool is_digit( const std::string s );
}
#define atoi atoi##_Do_not_use_atoi_function_directly_Use_getIntProp90,_getArgInt_or_uni_atoi
......
......@@ -179,7 +179,6 @@ using namespace UniSetTypes;
return result;
}
// -------------------------------------------------------------------------
UniSetTypes::IDList UniSetTypes::explode( const string str, char sep )
{
UniSetTypes::IDList l;
......@@ -200,7 +199,105 @@ using namespace UniSetTypes;
return l;
}
// -------------------------------------------------------------------------
std::list<std::string> UniSetTypes::explode_str( const string str, char sep )
{
std::list<std::string> l;
string::size_type prev = 0;
string::size_type pos = 0;
do
{
pos = str.find(sep,prev);
string s(str.substr(prev,pos-prev));
if( !s.empty() )
{
l.push_back(s);
prev=pos+1;
}
}
while( pos!=string::npos );
return l;
}
// ------------------------------------------------------------------------------------------
bool UniSetTypes::is_digit( const std::string s )
{
for( std::string::const_iterator it=s.begin(); it!=s.end(); it++ )
{
if( !isdigit(*it) )
return false;
}
return true;
//return (std::count_if(s.begin(),s.end(),std::isdigit) == s.size()) ? true : false;
}
// --------------------------------------------------------------------------------------
std::list<UniSetTypes::ParamSInfo> UniSetTypes::getSInfoList( string str, Configuration* conf )
{
std::list<UniSetTypes::ParamSInfo> res;
std::list<std::string> l = UniSetTypes::explode_str(str,',');
for( std::list<std::string>::iterator it=l.begin(); it!=l.end(); it++ )
{
UniSetTypes::ParamSInfo item;
std::list<std::string> p = UniSetTypes::explode_str((*it),'=');
std::string s = "";
if( p.size() == 1 )
{
s = *(p.begin());
item.val = 0;
}
else if( p.size() == 2 )
{
s = *(p.begin());
item.val = uni_atoi(*(++p.begin()));
}
else
{
cerr << "WARNING: parse error for '" << (*it) << "'. IGNORE..." << endl;
continue;
}
item.fname = s;
std::list<std::string> t = UniSetTypes::explode_str(s,'@');
if( t.size() == 1 )
{
std::string s_id = *(t.begin());
if( is_digit(s_id) )
item.si.id = uni_atoi(s_id);
else
item.si.id = conf->getSensorID(s_id);
item.si.node = DefaultObjectId;
}
else if( t.size() == 2 )
{
std::string s_id = *(t.begin());
std::string s_node = *(++t.begin());
if( is_digit(s_id) )
item.si.id = uni_atoi(s_id);
else
item.si.id = conf->getSensorID(s_id);
if( is_digit(s_node.c_str()) )
item.si.node = uni_atoi(s_node);
else
item.si.node= conf->getNodeID(s_node);
}
else
{
cerr << "WARNING: parse error for '" << s << "'. IGNORE..." << endl;
continue;
}
res.push_back(item);
}
return res;
}
// --------------------------------------------------------------------------------------
UniversalIO::IOTypes UniSetTypes::getIOType( const std::string stype )
{
if ( stype == "DI" || stype == "di" )
......
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