Commit ad34319c authored by Pavel Vainerman's avatar Pavel Vainerman

(REST API): переделал формат json в ответе на запрос заказчиков

по датчику.
parent 9260798b
......@@ -58,7 +58,11 @@ HTTP API:
= Сделать возможность настраивать параметры httpserver-а из командной строки (количество потоков и т.п.)
- ТЕСТЫ (как вариант поизучать про тестовые фреймворки на питоне (pytest?)
- /sensors/XXX?config=name,textname,... - запрос полей (из configure.xml)
- /conf/objects?prop1,prop2,prop3,.. - props from condigure.xml
- /conf/nodes?prop1,prop2,prop3,.. - props from condigure.xml
- /conf/controlers?..
- /conf/services?...
- /conf/...
Version 2.5
============
......
......@@ -16,7 +16,7 @@
Name: libuniset2
Version: 2.6
Release: alt3.2
Release: alt3.3
Summary: UniSet - library for building distributed industrial control systems
License: LGPL
......@@ -511,6 +511,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
# - getInfo() deprecated..
%changelog
* Sun Dec 04 2016 Pavel Vainerman <pv@altlinux.ru> 2.6-alt3.3
- IOC rest api: reformat json reply
* Sat Dec 03 2016 Pavel Vainerman <pv@altlinux.ru> 2.6-alt3.2
- getChangedTime --> getTimeChange
- getInfo( long param ) --> getInfo( string param )
......
......@@ -328,7 +328,7 @@ class IONotifyController:
// http api
Poco::JSON::Object::Ptr request_consumers( const std::string& req, const Poco::URI::QueryParameters& p );
Poco::JSON::Object::Ptr request_lost( const string& req, const Poco::URI::QueryParameters& p );
Poco::JSON::Object::Ptr getConsumers( uniset::ObjectId sid, ConsumerListInfo& clist, bool noEmpty = true );
Poco::JSON::Object::Ptr getConsumers(uniset::ObjectId sid, ConsumerListInfo& clist, bool ifNotEmpty = true );
#endif
private:
......
......@@ -202,6 +202,11 @@ class UniSetObject:
/*! false - завершить работу потока обработки сообщений */
void setActive( bool set );
#ifndef DISABLE_REST_API
// вспомогательные функции
virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root );
#endif
private:
friend class UniSetManager;
......
......@@ -7,5 +7,5 @@ Name: libUniSet2
Description: Support library for UniSet
Requires: libxml-2.0 sigc++-2.0 omniORB4 libev
Version: @VERSION@
Libs: -L${libdir} -lUniSet2 -lPocoFoundation -lPocoNet
Libs: -L${libdir} -lUniSet2 -lPocoFoundation -lPocoNet -lPocoJSON
Cflags: -I${includedir}/@PACKAGE@ -D__OMNIORB4 -std=c++11 -D_GLIBCXX_USE_NANOSLEEP @REST_API_CFLAGS@
......@@ -72,6 +72,7 @@ struct ShortIOInfo
unsigned long tv_sec;
unsigned long tv_nsec;
long supplier;
long supplier_node;
};
}
......
......@@ -391,7 +391,7 @@ void UniSetObject::push( const TransportMessage& tm )
Poco::JSON::Object::Ptr UniSetObject::httpGet( const Poco::URI::QueryParameters& p )
{
Poco::JSON::Object::Ptr jret = new Poco::JSON::Object();
Poco::JSON::Object::Ptr jdata = uniset::json::make_child(jret, myname);
Poco::JSON::Object::Ptr jdata = uniset::json::make_child(jret, "object");
jdata->set("name", myname);
jdata->set("id", getId());
......@@ -408,6 +408,19 @@ Poco::JSON::Object::Ptr UniSetObject::httpHelp( const Poco::URI::QueryParameters
uniset::json::help::object myhelp(myname);
return myhelp;
}
// ------------------------------------------------------------------------------------------
Poco::JSON::Object::Ptr UniSetObject::httpGetMyInfo( Poco::JSON::Object::Ptr root )
{
Poco::JSON::Object::Ptr my = uniset::json::make_child(root, "object");
my->set("name", myname);
my->set("id", getId());
my->set("msgCount", countMessages());
my->set("lostMessages", getCountOfLostMessages());
my->set("maxSizeOfMessageQueue", getMaxSizeOfMessageQueue());
my->set("isActive", isActive());
my->set("objectType", getStrType());
return my;
}
#endif
// ------------------------------------------------------------------------------------------
ObjectPtr UniSetObject::getRef() const
......
......@@ -915,6 +915,7 @@ Poco::JSON::Object::Ptr IOController::request_get( const string& req, const Poco
Poco::JSON::Object::Ptr jdata = new Poco::JSON::Object();
Poco::JSON::Array::Ptr jsens = new Poco::JSON::Array();
jdata->set("sensors", jsens);
auto my = httpGetMyInfo(jdata);
Poco::JSON::Object::Ptr nullObject = new Poco::JSON::Object();
for( const auto& s : slist )
......@@ -947,7 +948,7 @@ Poco::JSON::Object::Ptr IOController::request_get( const string& req, const Poco
}
}
return uniset::json::make_object(myname, jdata);
return jdata;
}
// -----------------------------------------------------------------------------
void IOController::getSensorInfo( Poco::JSON::Array::Ptr& jdata, std::shared_ptr<USensorInfo>& s, bool shortInfo )
......@@ -998,6 +999,7 @@ Poco::JSON::Object::Ptr IOController::request_sensors( const string& req, const
{
Poco::JSON::Object::Ptr jdata = new Poco::JSON::Object();
Poco::JSON::Array::Ptr jsens = uniset::json::make_child_array(jdata, "sensors");
auto my = httpGetMyInfo(jdata);
size_t num = 0;
size_t offset = 0;
......
......@@ -1209,13 +1209,15 @@ Poco::JSON::Object::Ptr IONotifyController::httpRequest( const string& req, cons
Poco::JSON::Object::Ptr IONotifyController::request_consumers( const string& req, const Poco::URI::QueryParameters& p )
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
Poco::JSON::Object::Ptr mydata = uniset::json::make_child(json, myname);
Poco::JSON::Array::Ptr jdata = uniset::json::make_child_array(mydata, "consumers");
Poco::JSON::Array::Ptr jdata = uniset::json::make_child_array(json, "sensors");
auto my = httpGetMyInfo(json);
auto oind = uniset_conf()->oind;
std::list<ParamSInfo> slist;
ConsumerListInfo emptyList;
if( p.size() > 0 )
{
if( !p[0].first.empty() )
......@@ -1234,19 +1236,13 @@ Poco::JSON::Object::Ptr IONotifyController::request_consumers( const string& req
// Проход по списку заданных..
if( !slist.empty() )
{
auto jnotfound = uniset::json::make_child_array(mydata, "notfound");
for( const auto& s : slist )
{
auto a = askIOList.find(s.si.id);
if( a == askIOList.end() )
{
jnotfound->add(std::to_string(s.si.id));
continue;
}
// Включаем в ответ все, даже если список заказчиков пустой
jdata->add( getConsumers(s.si.id, emptyList, false) );
else
jdata->add( getConsumers(a->first, a->second, false) );
}
}
......@@ -1254,9 +1250,8 @@ Poco::JSON::Object::Ptr IONotifyController::request_consumers( const string& req
{
for( auto && a : askIOList )
{
// добавляем только датчики с непустым списком заказчиков
// добавляем только датчики только с непустым списком заказчиков
auto jret = getConsumers(a.first, a.second, true);
if( jret )
jdata->add(jret);
}
......@@ -1265,33 +1260,46 @@ Poco::JSON::Object::Ptr IONotifyController::request_consumers( const string& req
return json;
}
// -----------------------------------------------------------------------------
Poco::JSON::Object::Ptr IONotifyController::getConsumers( ObjectId sid, ConsumerListInfo& ci, bool noEmpty )
Poco::JSON::Object::Ptr IONotifyController::getConsumers(ObjectId sid, ConsumerListInfo& ci, bool ifNotEmpty )
{
/* Создаём json
* { {"sensor":
* {"id": "xxxx"},
* {"name": "xxxx"}
* },
* "consumers": [
* {..consumer1 info },
* {..consumer2 info },
* {..consumer3 info },
* {..consumer4 info }
* ]
* }
*/
Poco::JSON::Object::Ptr jret = new Poco::JSON::Object();
auto oind = uniset_conf()->oind;
uniset_rwmutex_rlock lock(ci.mut);
if( ci.clst.empty() && noEmpty )
if( ci.clst.empty() && ifNotEmpty )
return jret;
string strID( std::to_string(sid) );
auto jsens = uniset::json::make_child(jret, strID);
jsens->set("id", strID);
jsens->set("sensor_name", ORepHelpers::getShortName(oind->getMapName(sid)));
auto jcons = uniset::json::make_child(jsens, "consumers");
auto oind = uniset_conf()->oind;
auto jsens = uniset::json::make_child(jret, "sensor");
jsens->set("id", sid);
jsens->set("name", ORepHelpers::getShortName(oind->getMapName(sid)));
auto jcons = uniset::json::make_child_array(jret, "consumers");
for( const auto& c : ci.clst )
{
string cid( std::to_string(c.id) );
auto jconsinfo = uniset::json::make_child(jcons, cid);
jconsinfo->set("id", c.id);
jconsinfo->set("name", ORepHelpers::getShortName(oind->getMapName(c.id)));
jconsinfo->set("lostEvents", c.lostEvents);
jconsinfo->set("attempt", c.attempt);
jconsinfo->set("smCount", c.smCount);
Poco::JSON::Object::Ptr consumer = new Poco::JSON::Object();
consumer->set("id", c.id);
consumer->set("name", ORepHelpers::getShortName(oind->getMapName(c.id)));
consumer->set("node", c.node);
consumer->set("node_name", oind->getNodeName(c.node));
consumer->set("lostEvents", c.lostEvents);
consumer->set("attempt", c.attempt);
consumer->set("smCount", c.smCount);
jcons->add(consumer);
}
return jret;
......@@ -1300,9 +1308,8 @@ Poco::JSON::Object::Ptr IONotifyController::getConsumers( ObjectId sid, Consumer
Poco::JSON::Object::Ptr IONotifyController::request_lost( const string& req, const Poco::URI::QueryParameters& p )
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
Poco::JSON::Object::Ptr mydata = uniset::json::make_child(json, myname);
Poco::JSON::Array::Ptr jdata = uniset::json::make_child_array(mydata, "lost consumers");
Poco::JSON::Array::Ptr jdata = uniset::json::make_child_array(json, "lost consumers");
auto my = httpGetMyInfo(json);
auto oind = uniset_conf()->oind;
......
......@@ -110,14 +110,14 @@ uniset::json::help::object::operator Poco::JSON::Object::Ptr()
return root;
}
// --------------------------------------------------------------------------
Poco::JSON::Object::Ptr json::make_child( Poco::JSON::Object::Ptr& root, const std::__cxx11::string& key )
Poco::JSON::Object::Ptr json::make_child( Poco::JSON::Object::Ptr& root, const std::string& key )
{
Poco::JSON::Object::Ptr child = new Poco::JSON::Object();
root->set(key, child);
return child;
}
// --------------------------------------------------------------------------
Poco::JSON::Array::Ptr json::make_child_array(Poco::JSON::Object::Ptr& root, const std::__cxx11::string& key)
Poco::JSON::Array::Ptr json::make_child_array(Poco::JSON::Object::Ptr& root, const std::string& key)
{
Poco::JSON::Array::Ptr child = new Poco::JSON::Array();
root->set(key, child);
......
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