.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"language": "python",
|
"language": "python2",
|
||||||
"globalcontext": { "code": "print(\"GlobalContext: Init!\")\nvar1 = 66.6\nvar2 = 0"},
|
"globalcontext": { "code": "print(\"GlobalContext: Init!\")\nvar1 = 66.6\nvar2 = 0"},
|
||||||
"rules": {
|
"rules": {
|
||||||
"rule01": {
|
"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": {
|
"rule05": {
|
||||||
"condition": {
|
"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}})" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ catkin_package(
|
|||||||
|
|
||||||
add_definitions(
|
add_definitions(
|
||||||
-std=c++11
|
-std=c++11
|
||||||
-D_PYTHON_${USE_PYTHON_VERSION}_
|
-D_PYTHON_VERSION_=${USE_PYTHON_VERSION}
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
@@ -70,11 +70,11 @@ include_directories(
|
|||||||
${PYTHON_INCLUDE_DIRS}
|
${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_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
|
SOURCES
|
||||||
${PROJECT_SOURCE_DIR}/src/BundleActivator
|
${PROJECT_SOURCE_DIR}/src/BundleActivator
|
||||||
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl
|
${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
|
${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}
|
${catkin_LIBRARIES}
|
||||||
${APR_LIBRARIES}
|
${APR_LIBRARIES}
|
||||||
${Boost_LIBRARIES}
|
${Boost_LIBRARIES}
|
||||||
@@ -96,7 +96,7 @@ target_link_libraries(hos_cosgi_scripting_python
|
|||||||
#############
|
#############
|
||||||
|
|
||||||
install(FILES
|
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
|
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/bundles
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -64,12 +64,14 @@ void PyScriptingService::init() noexcept {
|
|||||||
ROS_INFO_STREAM("PyScriptingService: Python Version: " << version);
|
ROS_INFO_STREAM("PyScriptingService: Python Version: " << version);
|
||||||
|
|
||||||
// Initialize HOS Python API
|
// Initialize HOS Python API
|
||||||
#if defined ( _PYTHON_2_ )
|
#if not defined ( _PYTHON_VERSION_ )
|
||||||
|
#error "Python version not defined"
|
||||||
|
#elif ( _PYTHON_VERSION_ == 2 )
|
||||||
inithos();
|
inithos();
|
||||||
#elif defined ( _PYTHON_3_ )
|
#elif ( _PYTHON_VERSION_ == 3 )
|
||||||
init_module_hos();
|
init_module_hos();
|
||||||
#else
|
#else
|
||||||
#error "Python version not defined"
|
#error "Unknown Python version"
|
||||||
#endif
|
#endif
|
||||||
deviceService.reset(new hosapi::PyDeviceRepositoryWrapper(repoService));
|
deviceService.reset(new hosapi::PyDeviceRepositoryWrapper(repoService));
|
||||||
|
|
||||||
|
|||||||
+9
-1
@@ -126,7 +126,15 @@ private:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
/** Language identifier. */
|
/** 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 */
|
/* Initialization */
|
||||||
|
|||||||
+5
-3
@@ -12,12 +12,14 @@
|
|||||||
/**
|
/**
|
||||||
* This function declares the python hos module.
|
* 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();
|
extern "C" void inithos();
|
||||||
#elif defined ( _PYTHON_3_ )
|
#elif ( _PYTHON_VERSION_ == 3 )
|
||||||
extern "C" void init_module_hos();
|
extern "C" void init_module_hos();
|
||||||
#else
|
#else
|
||||||
#error "Python version not defined"
|
#error "Unknown Python version"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+9
-2
@@ -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()) {
|
if (var.IsBool()) {
|
||||||
return boost::python::object(var.AsBool());
|
return boost::python::object(var.AsBool());
|
||||||
} else if (var.IsFloat()) {
|
} 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;
|
libvariant::Variant retval;
|
||||||
if (!obj.is_none()) {
|
if (!obj.is_none()) {
|
||||||
if (PyObject_TypeCheck(obj.ptr(), &PyUnicode_Type)) {
|
if (PyObject_TypeCheck(obj.ptr(), &PyUnicode_Type)) {
|
||||||
|
retval = libvariant::Variant(boost::python::extract<std::string>(boost::python::str(obj).encode()));
|
||||||
|
//retval = libvariant::Variant(boost::python::extract<std::string>(obj));
|
||||||
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyBytes_Type)) {
|
||||||
retval = libvariant::Variant(boost::python::extract<std::string>(obj));
|
retval = libvariant::Variant(boost::python::extract<std::string>(obj));
|
||||||
} else if (PyObject_TypeCheck(obj.ptr(), &PyBool_Type)) {
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyBool_Type)) {
|
||||||
retval = libvariant::Variant(boost::python::extract<bool>(obj));
|
retval = libvariant::Variant(boost::python::extract<bool>(obj));
|
||||||
} else if (PyObject_TypeCheck(obj.ptr(), &PyFloat_Type)) {
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyFloat_Type)) {
|
||||||
retval = libvariant::Variant(boost::python::extract<double>(obj));
|
retval = libvariant::Variant(boost::python::extract<double>(obj));
|
||||||
|
#if ( _PYTHON_VERSION_ == 2 )
|
||||||
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyInt_Type)) {
|
||||||
|
retval = libvariant::Variant(boost::python::extract<int>(obj));
|
||||||
|
#endif
|
||||||
} else if (PyObject_TypeCheck(obj.ptr(), &PyLong_Type)) {
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyLong_Type)) {
|
||||||
retval = libvariant::Variant(boost::python::extract<long>(obj));
|
retval = libvariant::Variant(boost::python::extract<long>(obj));
|
||||||
} else if (PyObject_TypeCheck(obj.ptr(), &PyDict_Type)) {
|
} else if (PyObject_TypeCheck(obj.ptr(), &PyDict_Type)) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace utils {
|
|||||||
* \param[in] variant object.
|
* \param[in] variant object.
|
||||||
* \return boost python 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.
|
* \param[in] boost python object.
|
||||||
* \return variant object.
|
* \return variant object.
|
||||||
*/
|
*/
|
||||||
libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj) noexcept;
|
libvariant::Variant convertBoostPythonTovariant(const boost::python::object& obj);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ set(BUNDLE_NAME "hos_webserver")
|
|||||||
|
|
||||||
bundle(hos_cosgi_webserver
|
bundle(hos_cosgi_webserver
|
||||||
SOURCES
|
SOURCES
|
||||||
${PROJECT_SOURCE_DIR}/src/bundle_activator
|
${PROJECT_SOURCE_DIR}/src/BundleActivator
|
||||||
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/webserver_serviceimpl
|
${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/DevicesHTTPResource
|
||||||
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource
|
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/webserver/resources/SchemaHTTPResource
|
||||||
)
|
)
|
||||||
|
|||||||
+57
@@ -0,0 +1,57 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
#include <hos/cosgi/service/Interface.h>
|
||||||
|
#include <Variant/Variant.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
// 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<std::string>& 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<WebResource> resource) = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace webserver
|
||||||
|
} // namespace service
|
||||||
|
} // namespace cosgi
|
||||||
|
} // namespace hos
|
||||||
|
|
||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* hos_ruleengine_service.h
|
|
||||||
*
|
|
||||||
* Created on: 26.04.2015
|
|
||||||
* Author: matzman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <hos/cosgi/service/Interface.h>
|
|
||||||
#include <memory>
|
|
||||||
#include <Variant/Variant.h>
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
+1
-1
@@ -21,7 +21,7 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#include <hos/cosgi/service/core/CoreService.h>
|
#include <hos/cosgi/service/core/CoreService.h>
|
||||||
#include "hos/cosgi/service/webserver/webserver_serviceimpl.h"
|
#include "hos/cosgi/service/webserver/WebServerServiceImpl.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
+51
-30
@@ -1,18 +1,8 @@
|
|||||||
/*
|
|
||||||
* hos_ruleengine_service_impl.cpp
|
|
||||||
*
|
|
||||||
* Created on: 26.04.2015
|
|
||||||
* Author: matzman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "webserver_serviceimpl.h"
|
#include "WebServerServiceImpl.h"
|
||||||
|
#include <boost/algorithm/string.hpp>
|
||||||
#include <ros/ros.h>
|
#include <ros/ros.h>
|
||||||
#include <exception>
|
|
||||||
#include <iostream>
|
|
||||||
#include <unordered_set>
|
|
||||||
#include <Variant/Variant.h>
|
|
||||||
#include <celix/bundle_context.h>
|
|
||||||
#include <Variant/Variant.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace hos {
|
namespace hos {
|
||||||
@@ -20,19 +10,24 @@ namespace cosgi {
|
|||||||
namespace service {
|
namespace service {
|
||||||
namespace webserver {
|
namespace webserver {
|
||||||
|
|
||||||
using namespace ::libvariant;
|
|
||||||
|
using namespace libvariant;
|
||||||
using namespace httpserver;
|
using namespace httpserver;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<> const char* implementation::ServiceInfo<WebServerServiceImpl>::name = service::webserver::WebServerServiceImpl::SERVICE_NAME;
|
template<> const char* implementation::ServiceInfo<WebServerServiceImpl>::name = service::webserver::WebServerServiceImpl::SERVICE_NAME;
|
||||||
template<> const int implementation::ServiceInfo<WebServerServiceImpl>::major = 0;
|
template<> const int implementation::ServiceInfo<WebServerServiceImpl>::major = 1;
|
||||||
template<> const int implementation::ServiceInfo<WebServerServiceImpl>::minor = 6;
|
template<> const int implementation::ServiceInfo<WebServerServiceImpl>::minor = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WebServerServiceImpl::~WebServerServiceImpl() {
|
WebServerServiceImpl::~WebServerServiceImpl() {
|
||||||
webServer->stop();
|
webServer->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebServerServiceImpl::setCoreService(service::core::CoreService* coreService) {
|
|
||||||
|
void WebServerServiceImpl::setCoreService(service::core::CoreService* coreService) noexcept {
|
||||||
if (coreService && !this->coreService) {
|
if (coreService && !this->coreService) {
|
||||||
this->coreService = coreService;
|
this->coreService = coreService;
|
||||||
this->nodehandle = &coreService->getPrivateNodeHandle();
|
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) {
|
if (repoService && !this->repoService) {
|
||||||
this->repoService = repoService;
|
this->repoService = repoService;
|
||||||
int major = 0, minor = 0;
|
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);
|
create_webserver serverParams(8888);
|
||||||
serverParams.max_threads(5).regex_checking();
|
serverParams.max_threads(5);
|
||||||
webServer.reset(new webserver::webserver(serverParams));
|
webServer.reset(new webserver::webserver(serverParams));
|
||||||
devicesResource.reset(new resources::DevicesHTTPResource(repoService, 2));
|
|
||||||
schemaResource.reset(new resources::SchemaHTTPResource(nodehandle, 2));
|
// Register web resources
|
||||||
webServer->register_resource("/hos/devices", devicesResource.get(), true);
|
registerWebResource( "/schemas",
|
||||||
webServer->register_resource("/hos/schemas", schemaResource.get(), true);
|
std::make_shared<resources::SchemaHTTPResource>(nodehandle, &coreService->getSchemaLoader())
|
||||||
|
);
|
||||||
|
registerWebResource( "/devices",
|
||||||
|
std::make_shared<resources::DevicesHTTPResource>(repoService)
|
||||||
|
);
|
||||||
|
|
||||||
webServer->start(false);
|
webServer->start(false);
|
||||||
initialized = true;
|
initialized = true;
|
||||||
ROS_INFO_STREAM("WebServerService: Initialized and Ready.");
|
ROS_INFO_STREAM("WebServerService: Initialized and Ready.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void WebServerServiceImpl::registerWebResource(const std::string& path, std::shared_ptr<WebResource> 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 webserver
|
||||||
} // namespace service
|
} // namespace service
|
||||||
} // namespace cosgi
|
} // namespace cosgi
|
||||||
} // namespace hos
|
} // namespace hos
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+29
-30
@@ -1,24 +1,16 @@
|
|||||||
/*
|
|
||||||
* hos_ruleengine_service_impl.h
|
|
||||||
*
|
|
||||||
* Created on: 26.04.2015
|
|
||||||
* Author: matzman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
#include <hos/cosgi/service/core/CoreService.h>
|
#include <hos/cosgi/service/webserver/WebServerService.h>
|
||||||
#include <mutex>
|
|
||||||
#include <map>
|
|
||||||
#include <set>
|
|
||||||
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
|
|
||||||
#include <hos/cosgi/service/webserver/webserver_service.h>
|
|
||||||
#include <hos/cosgi/service/Implementation.h>
|
#include <hos/cosgi/service/Implementation.h>
|
||||||
|
#include <hos/cosgi/service/core/CoreService.h>
|
||||||
#include <ros/ros.h>
|
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
|
||||||
|
#include <mutex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
#include <httpserver.hpp>
|
#include <httpserver.hpp>
|
||||||
#include "resources/SchemaHTTPResource.h"
|
#include "resources/SchemaHTTPResource.h"
|
||||||
#include "resources/DevicesHTTPResource.h"
|
#include "resources/DevicesHTTPResource.h"
|
||||||
@@ -32,48 +24,55 @@ namespace service {
|
|||||||
namespace webserver {
|
namespace webserver {
|
||||||
|
|
||||||
|
|
||||||
class WebServerServiceImpl;
|
|
||||||
class WebServerServiceImpl :
|
class WebServerServiceImpl
|
||||||
public virtual WebServerService,
|
: public virtual WebServerService,
|
||||||
public implementation::Service<WebServerServiceImpl> {
|
public implementation::Service<WebServerServiceImpl> {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
service::core::CoreService* coreService = nullptr;
|
service::core::CoreService* coreService = nullptr;
|
||||||
devicerepository::DeviceRepositoryService* repoService = nullptr;
|
devicerepository::DeviceRepositoryService* repoService = nullptr;
|
||||||
ros::NodeHandle* nodehandle = nullptr;
|
ros::NodeHandle* nodehandle = nullptr;
|
||||||
|
|
||||||
|
std::vector<std::string> prefixPath = { "hos" };
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
|
||||||
std::unique_ptr<httpserver::webserver> webServer;
|
std::unique_ptr<httpserver::webserver> webServer;
|
||||||
std::unique_ptr<resources::DevicesHTTPResource> devicesResource;
|
std::map<std::string, std::shared_ptr<WebResource>> webResources;
|
||||||
std::unique_ptr<resources::SchemaHTTPResource> schemaResource;
|
|
||||||
|
|
||||||
void init();
|
void init() noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~WebServerServiceImpl();
|
~WebServerServiceImpl();
|
||||||
|
|
||||||
|
|
||||||
/* Initialization */
|
/* Initialization */
|
||||||
|
|
||||||
void setCoreService(service::core::CoreService* coreService);
|
void setCoreService(service::core::CoreService* coreService) noexcept;
|
||||||
void setRepoService(devicerepository::DeviceRepositoryService* repoService);
|
|
||||||
|
void setRepoService(devicerepository::DeviceRepositoryService* repoService) noexcept;
|
||||||
|
|
||||||
|
|
||||||
/* Public Service Implementations */
|
/* Public Service Implementations */
|
||||||
|
|
||||||
/* Observer Callbacks */
|
virtual void registerWebResource(const std::string& path, std::shared_ptr<WebResource> resource);
|
||||||
/* Internal Stuff */
|
|
||||||
|
|
||||||
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 webserver
|
||||||
} // namespace service
|
} // namespace service
|
||||||
} // namespace cosgi
|
} // namespace cosgi
|
||||||
} // namespace hos
|
} // namespace hos
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+17
-12
@@ -26,21 +26,26 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
DevicesHTTPResource::DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService, size_t prefixPathPieces)
|
|
||||||
: repoService(repoService), prefixPathPieces(prefixPathPieces) {
|
void DevicesHTTPResource::init( const std::vector<std::string>& prefixPath,
|
||||||
|
const std::string& registeredPath,
|
||||||
|
const std::string& absolutePath,
|
||||||
|
httpserver::webserver& webserver,
|
||||||
|
WebServerService& service ) {
|
||||||
|
this->prefixPath = prefixPath;
|
||||||
disallow_all();
|
disallow_all();
|
||||||
set_allowing(MHD_HTTP_METHOD_GET, true);
|
set_allowing(MHD_HTTP_METHOD_GET, true);
|
||||||
set_allowing(MHD_HTTP_METHOD_POST, 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) {
|
void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpserver::http_response** res) {
|
||||||
std::cout << "DevicesHTTPResource::render_GET()" << std::endl;
|
std::cout << "DevicesHTTPResource::render_GET()" << std::endl;
|
||||||
std::cout << req;
|
std::cout << req;
|
||||||
|
|
||||||
try {
|
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.");
|
ROS_ERROR_STREAM("Requested path " << req.get_path() << " has fewer pieces than prefix path.");
|
||||||
throw ResourceException("Internal Server Error", 500);
|
throw ResourceException("Internal Server Error", 500);
|
||||||
} else {
|
} 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) {
|
if (isSetCommand) {
|
||||||
throw ResourceException("No device given for set command.", 400);
|
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());
|
*res = new http_response(http_response_builder(SerializeJSON(result, true), 200).string_response());
|
||||||
} else {
|
} else {
|
||||||
std::string devid = req.get_path_piece(prefixPathPieces);
|
std::string devid = req.get_path_piece(prefixPath.size() + 2);
|
||||||
auto device = repoService->getDevice(devid);
|
auto device = repoService->getDevice(devid);
|
||||||
if (!device.expired()) {
|
if (!device.expired()) {
|
||||||
if (isSetCommand) {
|
if (isSetCommand) {
|
||||||
std::vector<std::string> path;
|
std::vector<std::string> 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));
|
path.push_back(req.get_path_piece(i));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -89,11 +94,11 @@ void DevicesHTTPResource::render_GET(const httpserver::http_request& req, httpse
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::string info;
|
std::string info;
|
||||||
if (req.get_path_pieces_size() > prefixPathPieces+1) {
|
if (req.get_path_pieces_size() > prefixPath.size() + 2) {
|
||||||
info = req.get_path_piece(prefixPathPieces+1);
|
info = req.get_path_piece(prefixPath.size() + 2);
|
||||||
}
|
}
|
||||||
std::stringstream ss;
|
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);
|
ss << "/" << req.get_path_piece(i);
|
||||||
}
|
}
|
||||||
std::string path = ss.str();
|
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;
|
Variant retval;
|
||||||
if (info.empty()) {
|
if (info.empty()) {
|
||||||
retval["type"] = device->getDeviceType();
|
retval["type"] = device->getDeviceType();
|
||||||
@@ -168,7 +173,7 @@ Variant DevicesHTTPResource::getDeviceInfo(devicerepository::Device* device, std
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicesHTTPResource::setDeviceState(devicerepository::Device* device, Variant cmd, std::vector<std::string> path) {
|
void DevicesHTTPResource::setDeviceState(devicerepository::Device* device, const Variant& cmd, std::vector<std::string> path) {
|
||||||
Variant cmdMsg;
|
Variant cmdMsg;
|
||||||
if (path.size() > 0) {
|
if (path.size() > 0) {
|
||||||
Variant& current = cmdMsg;
|
Variant& current = cmdMsg;
|
||||||
|
|||||||
+26
-7
@@ -2,12 +2,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
#include <ros/ros.h>
|
#include <hos/cosgi/service/webserver/WebServerService.h>
|
||||||
#include <httpserver.hpp>
|
|
||||||
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
|
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
|
||||||
#include <Variant/Variant.h>
|
#include <Variant/Variant.h>
|
||||||
|
#include <ros/ros.h>
|
||||||
|
#include <httpserver.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Service Namespace
|
// Service Namespace
|
||||||
namespace hos {
|
namespace hos {
|
||||||
namespace cosgi {
|
namespace cosgi {
|
||||||
@@ -17,19 +20,35 @@ namespace resources {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DevicesHTTPResource : public httpserver::http_resource<DevicesHTTPResource> {
|
class DevicesHTTPResource
|
||||||
|
: public virtual webserver::WebResource,
|
||||||
|
public httpserver::http_resource<DevicesHTTPResource> {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
devicerepository::DeviceRepositoryService* repoService = nullptr;
|
devicerepository::DeviceRepositoryService* repoService = nullptr;
|
||||||
size_t prefixPathPieces;
|
std::vector<std::string> prefixPath;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService, size_t prefixPathPieces);
|
|
||||||
|
DevicesHTTPResource(devicerepository::DeviceRepositoryService* repoService)
|
||||||
|
: repoService(repoService) {}
|
||||||
|
|
||||||
|
virtual void init( const std::vector<std::string>& 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_GET(const httpserver::http_request& req, httpserver::http_response** res);
|
||||||
void render_POST(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<std::string> path = std::vector<std::string>());
|
void setDeviceState( devicerepository::Device* device,
|
||||||
|
const libvariant::Variant& cmd,
|
||||||
|
std::vector<std::string> path = std::vector<std::string>() );
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace resources
|
} // namespace resources
|
||||||
|
|||||||
+14
-6
@@ -3,6 +3,7 @@
|
|||||||
#include <boost/format.hpp>
|
#include <boost/format.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Service Namespace
|
// Service Namespace
|
||||||
namespace hos {
|
namespace hos {
|
||||||
namespace cosgi {
|
namespace cosgi {
|
||||||
@@ -10,27 +11,34 @@ namespace service {
|
|||||||
namespace webserver {
|
namespace webserver {
|
||||||
namespace resources {
|
namespace resources {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
using namespace httpserver;
|
using namespace httpserver;
|
||||||
using namespace libvariant;
|
using namespace libvariant;
|
||||||
|
|
||||||
|
|
||||||
SchemaHTTPResource::SchemaHTTPResource(ros::NodeHandle* nodehandle, size_t prefixPathPieces)
|
|
||||||
: nodehandle(nodehandle), prefixPathPieces(prefixPathPieces) {
|
void SchemaHTTPResource::init( const std::vector<std::string>& prefixPath,
|
||||||
|
const std::string& registeredPath,
|
||||||
|
const std::string& absolutePath,
|
||||||
|
httpserver::webserver& webserver,
|
||||||
|
WebServerService& service ) {
|
||||||
|
this->prefixPath = prefixPath;
|
||||||
disallow_all();
|
disallow_all();
|
||||||
set_allowing(MHD_HTTP_METHOD_GET, true);
|
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) {
|
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.");
|
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());
|
*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());
|
*res = new http_response(http_response_builder("No schema given", 400).string_response());
|
||||||
} else {
|
} else {
|
||||||
std::stringstream ss;
|
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);
|
ss << "/" << req.get_path_piece(i);
|
||||||
}
|
}
|
||||||
auto schemaPath = (boost::format("hos:schema:/%1%") % ss.str()).str();
|
auto schemaPath = (boost::format("hos:schema:/%1%") % ss.str()).str();
|
||||||
|
|||||||
+20
-5
@@ -2,9 +2,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
#include <hos/cosgi/service/webserver/WebServerService.h>
|
||||||
|
#include <hos/utils/SchemaLoader.h>
|
||||||
#include <ros/ros.h>
|
#include <ros/ros.h>
|
||||||
#include <httpserver.hpp>
|
#include <httpserver.hpp>
|
||||||
#include <hos/utils/SchemaLoader.h>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Service Namespace
|
// Service Namespace
|
||||||
namespace hos {
|
namespace hos {
|
||||||
@@ -15,13 +19,24 @@ namespace resources {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SchemaHTTPResource : public httpserver::http_resource<SchemaHTTPResource> {
|
class SchemaHTTPResource
|
||||||
|
: public virtual webserver::WebResource,
|
||||||
|
public httpserver::http_resource<SchemaHTTPResource> {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ros::NodeHandle* nodehandle = nullptr;
|
ros::NodeHandle* nodehandle = nullptr;
|
||||||
std::unique_ptr<utils::SchemaLoader> schemaLoader;
|
utils::SchemaLoader* schemaLoader = nullptr;
|
||||||
size_t prefixPathPieces;
|
std::vector<std::string> prefixPath;
|
||||||
|
|
||||||
public:
|
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<std::string>& 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);
|
void render_GET(const httpserver::http_request& req, httpserver::http_response** res);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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/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.zip
|
||||||
$(find hos_cosgi_core)/../../devel_isolated/hos_celix/share/celix/bundles/shell_tui.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_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_devicerepository/hos_cosgi_devicerepository.zip
|
||||||
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_webserver/hos_cosgi_webserver.zip"/>
|
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_webserver/hos_cosgi_webserver.zip"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user