diff --git a/src/hos_cosgi_ruleengine/test/rulepkg01.json b/src/hos_cosgi_ruleengine/test/rulepkg01.json index 3f741eab..aa5a61df 100644 --- a/src/hos_cosgi_ruleengine/test/rulepkg01.json +++ b/src/hos_cosgi_ruleengine/test/rulepkg01.json @@ -1,5 +1,5 @@ { - "language": "python", + "language": "python2", "globalcontext": { "code": "print(\"GlobalContext: Init!\")\nvar1 = 66.6\nvar2 = 0"}, "rules": { "rule01": { @@ -53,7 +53,7 @@ } ] }, - "script": { "code": "print(\"Dimmer 0 reached 100\")\nDevices[\"dummySwitch1\"].sendCommand({\"switch\": {\"set\": True}})" } + "script": { "code": "print(\"Dimmer 0 reached 100\")\nDevices[\"dummySwitch1\"].sendCommand({u\"switch\": {u\"set\": True}})" } }, "rule05": { "condition": { @@ -68,7 +68,7 @@ } ] }, - "script": { "code": "print(\"Dimmer 0 left 100\")\nDevices[\"dummySwitch1\"].sendCommand({\"switch\": {\"set\": False}})" } + "script": { "code": "print(\"Dimmer 0 left 100\")\nDevices[\"dummySwitch1\"].sendCommand({u\"switch\": {\"set\": False}})" } } } } \ No newline at end of file diff --git a/src/hos_cosgi_scripting_python/CMakeLists.txt b/src/hos_cosgi_scripting_python/CMakeLists.txt index 8307c597..6505c625 100644 --- a/src/hos_cosgi_scripting_python/CMakeLists.txt +++ b/src/hos_cosgi_scripting_python/CMakeLists.txt @@ -59,7 +59,7 @@ catkin_package( add_definitions( -std=c++11 - -D_PYTHON_${USE_PYTHON_VERSION}_ + -D_PYTHON_VERSION_=${USE_PYTHON_VERSION} ) include_directories( @@ -70,11 +70,11 @@ include_directories( ${PYTHON_INCLUDE_DIRS} ) -set(BUNDLE_SYMBOLICNAME "hos.hos_scripting_python") +set(BUNDLE_SYMBOLICNAME "hos.hos_scripting_python${USE_PYTHON_VERSION}") set(BUNDLE_VERSION "1.0.0") -set(BUNDLE_NAME "hos_scripting_python") +set(BUNDLE_NAME "hos_scripting_python${USE_PYTHON_VERSION}") -bundle(hos_cosgi_scripting_python +bundle(hos_cosgi_scripting_python${USE_PYTHON_VERSION} SOURCES ${PROJECT_SOURCE_DIR}/src/BundleActivator ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl @@ -82,7 +82,7 @@ bundle(hos_cosgi_scripting_python ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI ) -target_link_libraries(hos_cosgi_scripting_python +target_link_libraries(hos_cosgi_scripting_python${USE_PYTHON_VERSION} ${catkin_LIBRARIES} ${APR_LIBRARIES} ${Boost_LIBRARIES} @@ -96,7 +96,7 @@ target_link_libraries(hos_cosgi_scripting_python ############# install(FILES - ${CMAKE_BINARY_DIR}/hos_cosgi_scripting_python.zip + ${CMAKE_BINARY_DIR}/hos_cosgi_scripting_python${USE_PYTHON_VERSION}.zip DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/bundles ) diff --git a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.cpp b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.cpp index c7ccffe2..16101f5c 100644 --- a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.cpp +++ b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.cpp @@ -64,12 +64,14 @@ void PyScriptingService::init() noexcept { ROS_INFO_STREAM("PyScriptingService: Python Version: " << version); // Initialize HOS Python API - #if defined ( _PYTHON_2_ ) + #if not defined ( _PYTHON_VERSION_ ) + #error "Python version not defined" + #elif ( _PYTHON_VERSION_ == 2 ) inithos(); - #elif defined ( _PYTHON_3_ ) + #elif ( _PYTHON_VERSION_ == 3 ) init_module_hos(); #else - #error "Python version not defined" + #error "Unknown Python version" #endif deviceService.reset(new hosapi::PyDeviceRepositoryWrapper(repoService)); diff --git a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.h b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.h index 7d44395f..119a5761 100644 --- a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.h +++ b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl.h @@ -126,7 +126,15 @@ private: public: /** Language identifier. */ - static constexpr const char* LANGUAGE = "python"; + #if not defined ( _PYTHON_VERSION_ ) + #error "Python version not defined" + #elif ( _PYTHON_VERSION_ == 2 ) + static constexpr const char* LANGUAGE = "python2"; + #elif ( _PYTHON_VERSION_ == 3 ) + static constexpr const char* LANGUAGE = "python3"; + #else + #error "Unknown Python version" + #endif /* Initialization */ diff --git a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI.h b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI.h index 6b0dea95..0afb2bde 100644 --- a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI.h +++ b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI.h @@ -12,12 +12,14 @@ /** * This function declares the python hos module. */ -#if defined ( _PYTHON_2_ ) +#if not defined ( _PYTHON_VERSION_ ) + #error "Python version not defined" +#elif ( _PYTHON_VERSION_ == 2 ) extern "C" void inithos(); -#elif defined ( _PYTHON_3_ ) +#elif ( _PYTHON_VERSION_ == 3 ) extern "C" void init_module_hos(); #else - #error "Python version not defined" + #error "Unknown Python version" #endif diff --git a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.cpp b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.cpp index d1617db9..cbf1725a 100644 --- a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.cpp +++ b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.cpp @@ -15,7 +15,7 @@ namespace utils { -boost::python::object convertVariantToBoostPython(const libvariant::Variant& var) noexcept { +boost::python::object convertVariantToBoostPython(const libvariant::Variant& var) { if (var.IsBool()) { return boost::python::object(var.AsBool()); } else if (var.IsFloat()) { @@ -45,15 +45,22 @@ boost::python::object convertVariantToBoostPython(const libvariant::Variant& var } -libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj) noexcept { +libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj) { libvariant::Variant retval; if (!obj.is_none()) { if (PyObject_TypeCheck(obj.ptr(), &PyUnicode_Type)) { + retval = libvariant::Variant(boost::python::extract(boost::python::str(obj).encode())); + //retval = libvariant::Variant(boost::python::extract(obj)); + } else if (PyObject_TypeCheck(obj.ptr(), &PyBytes_Type)) { retval = libvariant::Variant(boost::python::extract(obj)); } else if (PyObject_TypeCheck(obj.ptr(), &PyBool_Type)) { retval = libvariant::Variant(boost::python::extract(obj)); } else if (PyObject_TypeCheck(obj.ptr(), &PyFloat_Type)) { retval = libvariant::Variant(boost::python::extract(obj)); + #if ( _PYTHON_VERSION_ == 2 ) + } else if (PyObject_TypeCheck(obj.ptr(), &PyInt_Type)) { + retval = libvariant::Variant(boost::python::extract(obj)); + #endif } else if (PyObject_TypeCheck(obj.ptr(), &PyLong_Type)) { retval = libvariant::Variant(boost::python::extract(obj)); } else if (PyObject_TypeCheck(obj.ptr(), &PyDict_Type)) { diff --git a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.h b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.h index dc2e489e..6b8fb688 100644 --- a/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.h +++ b/src/hos_cosgi_scripting_python/src/hos/cosgi/service/scripting/python/utils/utils.h @@ -22,7 +22,7 @@ namespace utils { * \param[in] variant object. * \return boost python object. */ -boost::python::object convertVariantToBoostPython(const libvariant::Variant& var) noexcept; +boost::python::object convertVariantToBoostPython(const libvariant::Variant& var); /** @@ -31,7 +31,7 @@ boost::python::object convertVariantToBoostPython(const libvariant::Variant& var * \param[in] boost python object. * \return variant object. */ -libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj) noexcept; +libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj); diff --git a/src/hos_cosgi_webserver/CMakeLists.txt b/src/hos_cosgi_webserver/CMakeLists.txt index 14961e68..b75a0f09 100644 --- a/src/hos_cosgi_webserver/CMakeLists.txt +++ b/src/hos_cosgi_webserver/CMakeLists.txt @@ -52,8 +52,8 @@ set(BUNDLE_NAME "hos_webserver") bundle(hos_cosgi_webserver SOURCES - ${PROJECT_SOURCE_DIR}/src/bundle_activator - ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/webserver_serviceimpl + ${PROJECT_SOURCE_DIR}/src/BundleActivator + ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/WebServerServiceImpl ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource ${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource ) diff --git a/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/WebServerService.h b/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/WebServerService.h new file mode 100644 index 00000000..59e79e42 --- /dev/null +++ b/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/WebServerService.h @@ -0,0 +1,57 @@ + +#pragma once + + +#include +#include +#include +#include + + +// forward declarations +namespace httpserver { class webserver;} + + +// service namespace +namespace hos { +namespace cosgi { +namespace service { +namespace webserver { + + +// forward declarations +class WebServerService; + + + +class WebResource + : public virtual interface::Object { + +public: + virtual void init( const std::vector& prefixPath, + const std::string& registeredPath, + const std::string& absolutePath, + httpserver::webserver& webserver, + WebServerService& service ) = 0; +}; + + + +class WebServerService + : public virtual interface::Service { + +public: + + static constexpr const char* SERVICE_NAME = "hos_webserver_service"; + + virtual void registerWebResource(const std::string& path, std::shared_ptr resource) = 0; + +}; + + + +} // namespace webserver +} // namespace service +} // namespace cosgi +} // namespace hos + diff --git a/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/webserver_service.h b/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/webserver_service.h deleted file mode 100644 index 2958c967..00000000 --- a/src/hos_cosgi_webserver/include/hos_cosgi_webserver/hos/cosgi/service/webserver/webserver_service.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * hos_ruleengine_service.h - * - * Created on: 26.04.2015 - * Author: matzman - */ - -#pragma once - -#include -#include -#include - - -namespace hos { -namespace cosgi { -namespace service { -namespace webserver { - - -class WebServerService : public virtual interface::Service { -public: - static constexpr const char* SERVICE_NAME = "hos_webserver_service"; -}; - - -} // namespace webserver -} // namespace service -} // namespace cosgi -} // namespace hos - - diff --git a/src/hos_cosgi_webserver/src/bundle_activator.cpp b/src/hos_cosgi_webserver/src/BundleActivator.cpp similarity index 98% rename from src/hos_cosgi_webserver/src/bundle_activator.cpp rename to src/hos_cosgi_webserver/src/BundleActivator.cpp index d005de04..30fdc299 100644 --- a/src/hos_cosgi_webserver/src/bundle_activator.cpp +++ b/src/hos_cosgi_webserver/src/BundleActivator.cpp @@ -21,7 +21,7 @@ extern "C" { } #include -#include "hos/cosgi/service/webserver/webserver_serviceimpl.h" +#include "hos/cosgi/service/webserver/WebServerServiceImpl.h" #include #include diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.cpp b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.cpp similarity index 56% rename from src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.cpp rename to src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.cpp index 3ba0e864..73646cc9 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.cpp +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.cpp @@ -1,18 +1,8 @@ -/* - * hos_ruleengine_service_impl.cpp - * - * Created on: 26.04.2015 - * Author: matzman - */ -#include "webserver_serviceimpl.h" +#include "WebServerServiceImpl.h" +#include #include -#include -#include -#include -#include -#include -#include + namespace hos { @@ -20,19 +10,24 @@ namespace cosgi { namespace service { namespace webserver { -using namespace ::libvariant; + +using namespace libvariant; using namespace httpserver; + template<> const char* implementation::ServiceInfo::name = service::webserver::WebServerServiceImpl::SERVICE_NAME; -template<> const int implementation::ServiceInfo::major = 0; -template<> const int implementation::ServiceInfo::minor = 6; +template<> const int implementation::ServiceInfo::major = 1; +template<> const int implementation::ServiceInfo::minor = 0; + + WebServerServiceImpl::~WebServerServiceImpl() { webServer->stop(); } -void WebServerServiceImpl::setCoreService(service::core::CoreService* coreService) { + +void WebServerServiceImpl::setCoreService(service::core::CoreService* coreService) noexcept { if (coreService && !this->coreService) { this->coreService = coreService; this->nodehandle = &coreService->getPrivateNodeHandle(); @@ -45,7 +40,8 @@ void WebServerServiceImpl::setCoreService(service::core::CoreService* coreServic } } -void WebServerServiceImpl::setRepoService(devicerepository::DeviceRepositoryService* repoService) { + +void WebServerServiceImpl::setRepoService(devicerepository::DeviceRepositoryService* repoService) noexcept { if (repoService && !this->repoService) { this->repoService = repoService; int major = 0, minor = 0; @@ -57,28 +53,53 @@ void WebServerServiceImpl::setRepoService(devicerepository::DeviceRepositoryServ } } -void WebServerServiceImpl::init() { + +void WebServerServiceImpl::init() noexcept { + // Create and start webserver create_webserver serverParams(8888); - serverParams.max_threads(5).regex_checking(); + serverParams.max_threads(5); webServer.reset(new webserver::webserver(serverParams)); - devicesResource.reset(new resources::DevicesHTTPResource(repoService, 2)); - schemaResource.reset(new resources::SchemaHTTPResource(nodehandle, 2)); - webServer->register_resource("/hos/devices", devicesResource.get(), true); - webServer->register_resource("/hos/schemas", schemaResource.get(), true); + + // Register web resources + registerWebResource( "/schemas", + std::make_shared(nodehandle, &coreService->getSchemaLoader()) + ); + registerWebResource( "/devices", + std::make_shared(repoService) + ); + webServer->start(false); initialized = true; ROS_INFO_STREAM("WebServerService: Initialized and Ready."); } +void WebServerServiceImpl::registerWebResource(const std::string& path, std::shared_ptr resource) { + std::string apath = getAbsolutePath(path); + if (webResources.find(apath) == webResources.end()) { + resource->init(prefixPath, path, apath, *webServer, *this); + webResources[apath] = resource; + } +} + + +std::string WebServerServiceImpl::getAbsolutePath(const std::string& path) { + std::stringstream ss; + for (auto p : prefixPath) { + ss << "/" << p; + } + std::string tpath = boost::algorithm::trim_copy(path); + if (tpath[0] != '/') { + ss << "/"; + } + ss << tpath; + return ss.str(); +} + + + } // namespace webserver } // namespace service } // namespace cosgi } // namespace hos - - - - - - diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.h b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.h similarity index 56% rename from src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.h rename to src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.h index 0d10d0e6..f09ff42e 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/webserver_serviceimpl.h +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/WebServerServiceImpl.h @@ -1,24 +1,16 @@ -/* - * hos_ruleengine_service_impl.h - * - * Created on: 26.04.2015 - * Author: matzman - */ #pragma once -#include -#include -#include -#include -#include -#include +#include #include - -#include +#include +#include +#include #include #include +#include +#include #include #include "resources/SchemaHTTPResource.h" #include "resources/DevicesHTTPResource.h" @@ -32,48 +24,55 @@ namespace service { namespace webserver { -class WebServerServiceImpl; -class WebServerServiceImpl : - public virtual WebServerService, - public implementation::Service { + +class WebServerServiceImpl + : public virtual WebServerService, + public implementation::Service { + private: service::core::CoreService* coreService = nullptr; devicerepository::DeviceRepositoryService* repoService = nullptr; ros::NodeHandle* nodehandle = nullptr; + std::vector prefixPath = { "hos" }; bool initialized = false; std::unique_ptr webServer; - std::unique_ptr devicesResource; - std::unique_ptr schemaResource; + std::map> webResources; - void init(); + void init() noexcept; public: ~WebServerServiceImpl(); + /* Initialization */ - void setCoreService(service::core::CoreService* coreService); - void setRepoService(devicerepository::DeviceRepositoryService* repoService); + void setCoreService(service::core::CoreService* coreService) noexcept; + + void setRepoService(devicerepository::DeviceRepositoryService* repoService) noexcept; + /* Public Service Implementations */ - /* Observer Callbacks */ - /* Internal Stuff */ + virtual void registerWebResource(const std::string& path, std::shared_ptr resource); - bool isInitialized() { return initialized; } - devicerepository::DeviceRepositoryService* getDeviceRepository() { return repoService; } + /* Internal */ + + bool isInitialized() noexcept { + return initialized; + } + + std::string getAbsolutePath(const std::string& path); + }; + } // namespace webserver } // namespace service } // namespace cosgi } // namespace hos - - - diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.cpp b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.cpp index f1cc9d36..2be546d4 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.cpp +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.cpp @@ -26,21 +26,26 @@ public: }; -DevicesHTTPResource::DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService, size_t prefixPathPieces) - : repoService(repoService), prefixPathPieces(prefixPathPieces) { + +void DevicesHTTPResource::init( const std::vector& prefixPath, + const std::string& registeredPath, + const std::string& absolutePath, + httpserver::webserver& webserver, + WebServerService& service ) { + this->prefixPath = prefixPath; disallow_all(); set_allowing(MHD_HTTP_METHOD_GET, true); set_allowing(MHD_HTTP_METHOD_POST, true); + webserver.register_resource(absolutePath, this, true); } - void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpserver::http_response** res) { std::cout << "DevicesHTTPResource::render_GET()" << std::endl; std::cout << req; try { - if (req.get_path_pieces_size() < prefixPathPieces) { + if (req.get_path_pieces_size() < prefixPath.size() + 1) { ROS_ERROR_STREAM("Requested path " << req.get_path() << " has fewer pieces than prefix path."); throw ResourceException("Internal Server Error", 500); } else { @@ -62,7 +67,7 @@ void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpse } } - if (req.get_path_pieces_size() == prefixPathPieces) { + if (req.get_path_pieces_size() == prefixPath.size() + 1) { if (isSetCommand) { throw ResourceException("No device given for set command.", 400); } @@ -72,12 +77,12 @@ void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpse } *res = new http_response(http_response_builder(SerializeJSON(result, true), 200).string_response()); } else { - std::string devid = req.get_path_piece(prefixPathPieces); + std::string devid = req.get_path_piece(prefixPath.size() + 2); auto device = repoService->getDevice(devid); if (!device.expired()) { if (isSetCommand) { std::vector path; - for (int i = prefixPathPieces+1; i < req.get_path_pieces_size(); i++) { + for (int i = prefixPath.size() + 2; i < req.get_path_pieces_size(); i++) { path.push_back(req.get_path_piece(i)); } try { @@ -89,11 +94,11 @@ void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpse } } else { std::string info; - if (req.get_path_pieces_size() > prefixPathPieces+1) { - info = req.get_path_piece(prefixPathPieces+1); + if (req.get_path_pieces_size() > prefixPath.size() + 2) { + info = req.get_path_piece(prefixPath.size() + 2); } std::stringstream ss; - for (int i = prefixPathPieces+2; i < req.get_path_pieces_size(); i++) { + for (int i = prefixPath.size() + 3; i < req.get_path_pieces_size(); i++) { ss << "/" << req.get_path_piece(i); } std::string path = ss.str(); @@ -116,7 +121,7 @@ void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpse -Variant DevicesHTTPResource::getDeviceInfo(devicerepository::Device* device, std::string info, const std::string path) { +Variant DevicesHTTPResource::getDeviceInfo(devicerepository::Device* device, const std::string& info, const std::string& path) { Variant retval; if (info.empty()) { retval["type"] = device->getDeviceType(); @@ -168,7 +173,7 @@ Variant DevicesHTTPResource::getDeviceInfo(devicerepository::Device* device, std return retval; } -void DevicesHTTPResource::setDeviceState(devicerepository::Device* device, Variant cmd, std::vector path) { +void DevicesHTTPResource::setDeviceState(devicerepository::Device* device, const Variant& cmd, std::vector path) { Variant cmdMsg; if (path.size() > 0) { Variant& current = cmdMsg; diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.h b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.h index e67ae97c..b234d281 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.h +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/DevicesHTTPResource.h @@ -2,12 +2,15 @@ #pragma once -#include -#include +#include #include #include +#include +#include #include + + // Service Namespace namespace hos { namespace cosgi { @@ -17,19 +20,35 @@ namespace resources { -class DevicesHTTPResource : public httpserver::http_resource { +class DevicesHTTPResource + : public virtual webserver::WebResource, + public httpserver::http_resource { + private: devicerepository::DeviceRepositoryService* repoService = nullptr; - size_t prefixPathPieces; + std::vector prefixPath; + public: - DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService, size_t prefixPathPieces); + + DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService) + : repoService(repoService) {} + + virtual void init( const std::vector& prefixPath, + const std::string& registeredPath, + const std::string& absolutePath, + httpserver::webserver& webserver, + webserver::WebServerService& service ); void render_GET(const httpserver::http_request& req, httpserver::http_response** res); void render_POST(const httpserver::http_request& req, httpserver::http_response** res); - ::libvariant::Variant getDeviceInfo(devicerepository::Device* device, const std::string info = std::string(), const std::string path = std::string()); + libvariant::Variant getDeviceInfo( devicerepository::Device* device, + const std::string& info = std::string(), + const std::string& path = std::string() ); - void setDeviceState(devicerepository::Device* device, ::libvariant::Variant cmd, std::vector path = std::vector()); + void setDeviceState( devicerepository::Device* device, + const libvariant::Variant& cmd, + std::vector path = std::vector() ); }; } // namespace resources diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.cpp b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.cpp index 9f8be790..a4eaa87b 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.cpp +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.cpp @@ -3,6 +3,7 @@ #include + // Service Namespace namespace hos { namespace cosgi { @@ -10,27 +11,34 @@ namespace service { namespace webserver { namespace resources { + + using namespace httpserver; using namespace libvariant; -SchemaHTTPResource::SchemaHTTPResource(ros::NodeHandle* nodehandle, size_t prefixPathPieces) - : nodehandle(nodehandle), prefixPathPieces(prefixPathPieces) { + +void SchemaHTTPResource::init( const std::vector& prefixPath, + const std::string& registeredPath, + const std::string& absolutePath, + httpserver::webserver& webserver, + WebServerService& service ) { + this->prefixPath = prefixPath; disallow_all(); set_allowing(MHD_HTTP_METHOD_GET, true); - schemaLoader.reset(new utils::SchemaLoader()); + webserver.register_resource(absolutePath, this, true); } void SchemaHTTPResource::render_GET(const httpserver::http_request& req, httpserver::http_response** res) { - if (req.get_path_pieces_size() < prefixPathPieces) { + if (req.get_path_pieces_size() < prefixPath.size() + 1) { ROS_ERROR_STREAM("Requested path " << req.get_path() << " has fewer pieces than prefix path."); *res = new http_response(http_response_builder("Internal Server Error", 500).string_response()); - } else if (req.get_path_pieces_size() == prefixPathPieces) { + } else if (req.get_path_pieces_size() == prefixPath.size() + 1) { *res = new http_response(http_response_builder("No schema given", 400).string_response()); } else { std::stringstream ss; - for (int i = prefixPathPieces; i < req.get_path_pieces_size(); i++) { + for (int i = prefixPath.size() + 1; i < req.get_path_pieces_size(); i++) { ss << "/" << req.get_path_piece(i); } auto schemaPath = (boost::format("hos:schema:/%1%") % ss.str()).str(); diff --git a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.h b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.h index b8424f13..7e79077c 100644 --- a/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.h +++ b/src/hos_cosgi_webserver/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource.h @@ -2,9 +2,13 @@ #pragma once +#include +#include #include #include -#include +#include + + // Service Namespace namespace hos { @@ -15,13 +19,24 @@ namespace resources { -class SchemaHTTPResource : public httpserver::http_resource { +class SchemaHTTPResource + : public virtual webserver::WebResource, + public httpserver::http_resource { + private: ros::NodeHandle* nodehandle = nullptr; - std::unique_ptr schemaLoader; - size_t prefixPathPieces; + utils::SchemaLoader* schemaLoader = nullptr; + std::vector prefixPath; + public: - SchemaHTTPResource(ros::NodeHandle* nodehandle, size_t prefixPathPieces); + SchemaHTTPResource(ros::NodeHandle* nodehandle, utils::SchemaLoader* schemaLoader) + : nodehandle(nodehandle), schemaLoader(schemaLoader) {} + + virtual void init( const std::vector& prefixPath, + const std::string& registeredPath, + const std::string& absolutePath, + httpserver::webserver& webserver, + WebServerService& service ); void render_GET(const httpserver::http_request& req, httpserver::http_response** res); }; diff --git a/src/hos_launch/launch/hos_cosgi.launch b/src/hos_launch/launch/hos_cosgi.launch index 19c90dd7..42694f4e 100644 --- a/src/hos_launch/launch/hos_cosgi.launch +++ b/src/hos_launch/launch/hos_cosgi.launch @@ -9,7 +9,7 @@ $(find hos_cosgi_core)/../../devel_isolated/hos_celix/share/celix/bundles/log_writer_syslog.zip $(find hos_cosgi_core)/../../devel_isolated/hos_celix/share/celix/bundles/shell.zip $(find hos_cosgi_core)/../../devel_isolated/hos_celix/share/celix/bundles/shell_tui.zip - $(find hos_cosgi_core)/../../build_isolated/hos_cosgi_scripting_python/hos_cosgi_scripting_python.zip + $(find hos_cosgi_core)/../../build_isolated/hos_cosgi_scripting_python/hos_cosgi_scripting_python2.zip $(find hos_cosgi_core)/../../build_isolated/hos_cosgi_ruleengine/hos_cosgi_ruleengine.zip $(find hos_cosgi_core)/../../build_isolated/hos_cosgi_devicerepository/hos_cosgi_devicerepository.zip $(find hos_cosgi_core)/../../build_isolated/hos_cosgi_webserver/hos_cosgi_webserver.zip"/>