This commit is contained in:
2015-06-10 20:28:48 +02:00
parent 3451e66f16
commit 632a792f3a
33 changed files with 595 additions and 854 deletions
+16
View File
@@ -0,0 +1,16 @@
x) Rule engine infos:
======================
http[s]://hos/api/<rule-engine-name>/
x) Rule package infos:
======================
http[s]://hos/api/<rule-engine-name>/packages
http[s]://hos/api/<rule-engine-name>/packages/<package-name>
x) Rule infos:
======================
http[s]://hos/api/<rule-engine-name>/packages/<package-name>/rules
http[s]://hos/api/<rule-engine-name>/packages/<package-name>/rules/<rule-name>
@@ -129,6 +129,8 @@ public:
*/
virtual size_t getRulePackageCount() noexcept = 0;
virtual std::string getName() noexcept = 0;
/**
* Registers a rule condition factory under the given type identifier.
*
@@ -132,6 +132,8 @@ public:
virtual void conditionStateChanged(bool newState, const conditions::ConditionTriggerType& trigger,
const libvariant::Variant& lcontext = libvariant::Variant()) noexcept = 0;
virtual libvariant::Variant getConditionJSONRepresentation() = 0;
};
@@ -116,6 +116,8 @@ public:
*/
virtual void setCompositeParent(RuleCompositeCondition* compositeParent) noexcept = 0;
virtual libvariant::Variant getJSONRepresentation() = 0;
};
@@ -55,12 +55,14 @@ protected:
RuleCompositeCondition* compositeParent = nullptr;
bool enabled = false;
libvariant::Variant jsonRepresentation;
void notifyStateChangeToParent(bool newState, ConditionTriggerType category, const libvariant::Variant& lcontext = libvariant::Variant());
public:
RuleConditionImpl(rules::Rule* parent, ConditionTriggerType triggerType, const std::string& type) :
parent(parent), triggerType(triggerType), type(type) {}
RuleConditionImpl(rules::Rule* parent, ConditionTriggerType triggerType, const std::string& type, const libvariant::Variant& jsonRepresentation) :
parent(parent), triggerType(triggerType), type(type), jsonRepresentation(jsonRepresentation) {}
virtual rules::Rule* getParent() noexcept override {
return parent;
@@ -94,6 +96,10 @@ public:
this->enabled;
}
virtual libvariant::Variant getJSONRepresentation() override {
return jsonRepresentation;
}
};
@@ -110,8 +116,10 @@ protected:
public:
RuleCompositeConditionImpl(Rule* parent, ConditionTriggerType category, std::string type) :
RuleConditionImpl(parent, category, type) {}
RuleCompositeConditionImpl(Rule* parent, ConditionTriggerType category, std::string type, const libvariant::Variant& jsonRepresentation) :
RuleConditionImpl(parent, category, type, jsonRepresentation) {
this->jsonRepresentation.Erase("conditions");
}
virtual void childStateChanged( RuleCondition* child,
bool newState,
@@ -133,6 +141,15 @@ public:
this->enabled = enabled;
}
}
virtual libvariant::Variant getJSONRepresentation() override {
libvariant::Variant tmp = jsonRepresentation.Copy();
tmp["conditions"] = libvariant::Variant::List();
for (auto c : children) {
tmp["conditions"].Append(c->getJSONRepresentation());
}
return tmp;
}
};
@@ -66,7 +66,8 @@ void RuleEngineServiceImpl::setRepoService(devicerepository::DeviceRepositorySer
void RuleEngineServiceImpl::init() noexcept {
// Get public parameters
nodehandle->getParam("ruleengine_prefix", ruleEnginePrefix);
nodehandle->getParam("hos_param_prefix", hosParamPrefix);
nodehandle->getParam("ruleengine_name", engineName);
// Register condition factories
registerRuleConditionFactory("logical_or", std::make_shared<rules::conditions::LogicalOrConditionFactory>());
@@ -77,7 +78,9 @@ void RuleEngineServiceImpl::init() noexcept {
// Create rule packages
XmlRpc::XmlRpcValue rawValue;
nodehandle->getParam(ruleEnginePrefix, rawValue);
std::stringstream ss;
ss << hosParamPrefix << "/" << engineName;
nodehandle->getParam(ss.str(), rawValue);
if (rawValue.getType() == XmlRpc::XmlRpcValue::TypeStruct) {
for (auto pkg : rawValue) {
std::string pname = pkg.first;
@@ -98,7 +101,7 @@ void RuleEngineServiceImpl::init() noexcept {
void RuleEngineServiceImpl::addRulePkg(const std::string& name) {
std::stringstream ss;
ss << ruleEnginePrefix << "/" << name;
ss << hosParamPrefix << "/" << engineName << "/" << name;
XmlRpc::XmlRpcValue rawValue;
nodehandle->getParam(ss.str(), rawValue);
addRulePkg(name, static_cast<std::string>(rawValue));
@@ -74,7 +74,8 @@ private:
devicerepository::DeviceRepositoryService* repoService = nullptr;
ros::NodeHandle* nodehandle = nullptr;
std::string ruleEnginePrefix = "/hos/rules/ruleengine0";
std::string hosParamPrefix = "/hos/rules";
std::string engineName = "ruleengine0";
rulePkgMap_t rulePkgs;
rulePkgMap_t noExecRulePkgs;
@@ -110,6 +111,10 @@ public:
virtual size_t getRulePackageCount() noexcept override;
virtual std::string getName() noexcept override {
return engineName;
}
virtual devicerepository::DeviceRepositoryService* getDeviceRepository() noexcept override {
return repoService;
}
@@ -93,6 +93,10 @@ public:
std::shared_ptr<conditions::RuleCondition> createRuleCondition(const Variant& raw) override;
virtual Variant getConditionJSONRepresentation() override {
return condition->getJSONRepresentation();
}
};
@@ -95,7 +95,8 @@ std::shared_ptr<RuleCondition> DeviceStatusMsgConditionFactory::createCondition(
if (raw.HasPath("msgvalue")) {
msgValue = raw["msgvalue"];
}
retval = std::make_shared<DeviceStatusMsgCondition>(parent, deviceID, msgPointer, msgValue);
Variant rep = raw.Copy();
retval = std::make_shared<DeviceStatusMsgCondition>(parent, deviceID, msgPointer, msgValue, rep);
return retval;
}
@@ -154,7 +155,8 @@ std::shared_ptr<RuleCondition> DeviceCommandMsgConditionFactory::createCondition
if (raw.HasPath("msgvalue")) {
msgValue = raw["msgvalue"];
}
retval = std::make_shared<DeviceCommandMsgCondition>(parent, deviceID, msgPointer, msgValue);
Variant rep = raw.Copy();
retval = std::make_shared<DeviceCommandMsgCondition>(parent, deviceID, msgPointer, msgValue, rep);
return retval;
}
@@ -217,7 +219,8 @@ std::shared_ptr<RuleCondition> DeviceStateChangeConditionFactory::createConditio
if (raw.HasPath("tovalue")) {
toValue = raw["tovalue"];
}
retval = std::make_shared<DeviceStateChangeCondition>(parent, deviceID, statePointer, fromValue, toValue);
Variant rep = raw.Copy();
retval = std::make_shared<DeviceStateChangeCondition>(parent, deviceID, statePointer, fromValue, toValue, rep);
return retval;
}
@@ -41,8 +41,9 @@ public:
const std::string& deviceID,
const std::string& msgPointer,
const Variant& msgValue,
const std::set<devicerepository::DeviceEventType> _listenerKeys )
: RuleConditionImpl(parent, category, type), deviceID(deviceID),
const std::set<devicerepository::DeviceEventType> _listenerKeys,
const libvariant::Variant& jsonRepresentation)
: RuleConditionImpl(parent, category, type, jsonRepresentation), deviceID(deviceID),
msgPointer(msgPointer), msgValue(msgValue), _listenerKeys(_listenerKeys) {}
virtual void notificationReceived( interface::Observer<devicerepository::DeviceEventType,
@@ -62,9 +63,10 @@ public:
DeviceStatusMsgCondition( Rule* parent,
const std::string& deviceID,
const std::string& msgPointer,
const Variant& msgValue)
const Variant& msgValue,
const libvariant::Variant& jsonRepresentation)
: DeviceMsgCondition(parent, ConditionTriggerType::Event, "devicestatusmsg", deviceID,
msgPointer, msgValue, {devicerepository::DeviceEventType::STATUS_UPDATE_RECEIVED}) {}
msgPointer, msgValue, {devicerepository::DeviceEventType::STATUS_UPDATE_RECEIVED}, jsonRepresentation) {}
virtual void notificationReceived( interface::Observer<devicerepository::DeviceEventType,
devicerepository::DeviceEvent>::observable_type* o,
@@ -86,9 +88,9 @@ class DeviceCommandMsgCondition
: public DeviceMsgCondition {
public:
DeviceCommandMsgCondition(Rule* parent, const std::string deviceID, const std::string msgPointer, const Variant msgValue)
DeviceCommandMsgCondition(Rule* parent, const std::string deviceID, const std::string msgPointer, const Variant msgValue, const libvariant::Variant& jsonRepresentation)
: DeviceMsgCondition(parent, ConditionTriggerType::Event, "devicecommandmsg", deviceID,
msgPointer, msgValue, {devicerepository::DeviceEventType::COMMAND_RECEIVED}) {}
msgPointer, msgValue, {devicerepository::DeviceEventType::COMMAND_RECEIVED}, jsonRepresentation) {}
virtual void notificationReceived( interface::Observer<devicerepository::DeviceEventType,
devicerepository::DeviceEvent>::observable_type* o,
@@ -116,9 +118,10 @@ public:
const std::string deviceID,
const std::string statePointer,
const Variant fromValue,
const Variant toValue )
const Variant toValue,
const libvariant::Variant& jsonRepresentation )
: DeviceMsgCondition(parent, ConditionTriggerType::Event, "devicestatechange", deviceID, statePointer,
toValue, {devicerepository::DeviceEventType::STATE_CHANGED}), fromValue(fromValue) {}
toValue, {devicerepository::DeviceEventType::STATE_CHANGED}, jsonRepresentation), fromValue(fromValue) {}
virtual void notificationReceived(interface::Observer<devicerepository::DeviceEventType, devicerepository::DeviceEvent>::observable_type* o, const devicerepository::DeviceEventType& key, const devicerepository::DeviceEvent* e) noexcept override;
};
@@ -16,7 +16,8 @@ namespace conditions {
std::shared_ptr<RuleCondition> LogicalAndConditionFactory::createCondition(rules::Rule* parent, const Variant& raw) {
std::shared_ptr<LogicalAndCondition> retval;
if (raw.HasPath("conditions") && raw["conditions"].IsList() && raw["conditions"].Size() > 0) {
retval = std::make_shared<LogicalAndCondition>(parent);
Variant rep = raw.Copy();
retval = std::make_shared<LogicalAndCondition>(parent, rep);
for (auto c : raw["conditions"].AsList()) {
auto cond = parent->createRuleCondition(c);
if (cond) {
@@ -28,8 +28,8 @@ protected:
unsigned trueChildrenCount = 0;
public:
LogicalAndCondition(Rule* parent) :
RuleCompositeConditionImpl(parent, ConditionTriggerType::Both, "logical_and") {}
LogicalAndCondition(Rule* parent, const libvariant::Variant& jsonRepresentation) :
RuleCompositeConditionImpl(parent, ConditionTriggerType::Both, "logical_and", jsonRepresentation) {}
virtual void addChild(std::shared_ptr<RuleCondition> child) override;
@@ -15,7 +15,8 @@ namespace conditions {
std::shared_ptr<RuleCondition> LogicalOrConditionFactory::createCondition(rules::Rule* parent, const Variant& raw) {
std::shared_ptr<LogicalOrCondition> retval;
if (raw.HasPath("conditions") && raw["conditions"].IsList() && raw["conditions"].Size() > 0) {
retval = std::make_shared<LogicalOrCondition>(parent);
Variant rep = raw.Copy();
retval = std::make_shared<LogicalOrCondition>(parent, rep);
for (auto c : raw["conditions"].AsList()) {
auto cond = parent->createRuleCondition(c);
if (cond) {
@@ -28,8 +28,8 @@ protected:
unsigned trueChildrenCount = 0;
public:
LogicalOrCondition(Rule* parent) :
RuleCompositeConditionImpl(parent, ConditionTriggerType::Both, "logical_or") {}
LogicalOrCondition(Rule* parent, const libvariant::Variant& jsonRepresentation) :
RuleCompositeConditionImpl(parent, ConditionTriggerType::Both, "logical_or", jsonRepresentation) {}
virtual void addChild(std::shared_ptr<RuleCondition> child) override;
virtual void childStateChanged(RuleCondition* child, bool newState, ConditionTriggerType category,
@@ -52,6 +52,10 @@ RuleEngineHandlerFactory::RuleEngineHandlerFactory(RuleEngineService* ruleServic
Poco::Net::HTTPResponse::HTTPStatus::HTTP_NOT_FOUND);
}) {
engineInfoFuncs.add("id", [](RuleEngineService* engine, const std::string& category)->Variant {
return engine->getName();
});
engineInfoFuncs.add("languages", [](RuleEngineService* engine, const std::string& category)->Variant {
Variant retval = libvariant::Variant::List();
for (auto e : engine->getExecutionBackends()) {
@@ -74,6 +78,10 @@ RuleEngineHandlerFactory::RuleEngineHandlerFactory(RuleEngineService* ruleServic
return pkg->getName();
});
pkgInfoFuncs.add("parent", [](rules::RulePackage* pkg, const std::string& category)->Variant {
return pkg->getParent()->getName();
});
pkgInfoFuncs.add("language", [](rules::RulePackage* pkg, const std::string& category)->Variant {
return pkg->getLanguage();
});
@@ -106,6 +114,10 @@ RuleEngineHandlerFactory::RuleEngineHandlerFactory(RuleEngineService* ruleServic
return rule->getName();
});
ruleInfoFuncs.add("parent", [](rules::Rule* rule, const std::string& category)->Variant {
return rule->getParent()->getName();
});
ruleInfoFuncs.add("enabled", [](rules::Rule* rule, const std::string& category)->Variant {
return rule->isEnabled();
});
@@ -129,7 +141,7 @@ RuleEngineHandlerFactory::RuleEngineHandlerFactory(RuleEngineService* ruleServic
});
ruleInfoFuncs.add("condition", [](rules::Rule* rule, const std::string& category)->Variant {
return "ToDo";
return rule->getConditionJSONRepresentation();
});
}
@@ -215,24 +227,24 @@ Variant RuleEngineHandlerFactory::getRuleInfo( rules::Rule* rule, const std::str
void RuleEngineHandler::_handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp) {
void RuleEngineHandler::_handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Net::HTMLForm& form) {
if(req.find("Upgrade") != req.end() && Poco::icompare(req["Upgrade"], "websocket") == 0) {
//__handleEvent(req, resp, true);
/*throw webserver::webresource::WebResourceHandlerException("ToDo",
Net::HTTPResponse::HTTP_NOT_IMPLEMENTED);*/
throw webserver::webresource::WebResourceHandlerException("ToDo",
Net::HTTPResponse::HTTP_NOT_IMPLEMENTED);
} else {
if (req.getMethod() == Net::HTTPRequest::HTTP_PUT) {
// Put Command
throw webserver::webresource::WebResourceHandlerException("Still a ToDo.",
Net::HTTPResponse::HTTP_NOT_IMPLEMENTED);
} else if (req.getMethod() == Net::HTTPRequest::HTTP_GET || req.getMethod() == Net::HTTPRequest::HTTP_POST) {
RuleEngineAPIMethod method = getRuleEngineAPIMethod(req);
RuleEngineAPIMethod method = getRuleEngineAPIMethod(req, form);
if (method == RuleEngineAPIMethod::GET) {
__handleInfo(req, resp);
__handleInfo(req, resp, form);
} else if (method == RuleEngineAPIMethod::SET) {
//__handleCommand(req, resp, method);
//__handleCommand(req, resp, method, form.get());
} else if (method == RuleEngineAPIMethod::EVENT) {
//__handleEvent(req, resp, false);
//__handleEvent(req, resp, false, form.get());
} else {
throw webserver::webresource::WebResourceHandlerException("Could not understood request",
Net::HTTPResponse::HTTP_BAD_REQUEST);
@@ -246,78 +258,16 @@ void RuleEngineHandler::_handleRequest(Poco::Net::HTTPServerRequest &req, Poco::
void RuleEngineHandler::__handleInfo(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp) {
void RuleEngineHandler::__handleInfo(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Net::HTMLForm& form) {
std::vector<std::string> pathPieces, matchPieces;
matchUri.getPathSegments(matchPieces);
Poco::URI uri(req.getURI());
uri.getPathSegments(pathPieces);
std::unique_ptr<Net::HTMLForm> form;
if (req.getMethod() == Net::HTTPRequest::HTTP_POST) {
form.reset(new Net::HTMLForm(req, req.stream()));
} else {
form.reset(new Net::HTMLForm(req));
}
// Nothing given, return engine info
if (pathPieces.size() <= matchPieces.size()) {
handleResponse(req, resp, parent->getEngineInfo(ruleService, "", ""));
/*auto pkgs = ruleService->getRulePackages();
// Parse HTTP header "Range" parameter (to support dojo/store)
size_t rangeStart = 0;
size_t rangeStop = pkgs.size() - 1;
std::string rangeRaw;
if (req.has("Range")) {
rangeRaw = req.get("Range");
} else if (req.has("X-Range")) {
rangeRaw = req.get("X-Range");
}
if (!rangeRaw.empty()) {
if (boost::algorithm::starts_with(rangeRaw, "items=")) {
auto range = rangeRaw.substr(6);
std::vector<std::string> splitParts;
boost::algorithm::split(splitParts, range, boost::algorithm::is_any_of("-"));
if (splitParts.size() == 2) {
try {
size_t r1 = std::stol(splitParts[0]);
size_t r2 = std::stol(splitParts[1]);
if (r1 <= r2) {
rangeStart = r1;
rangeStop = std::min(r2, rangeStop);
}
} catch (...) {
ROS_ERROR_STREAM("Could not parse Range parameter \"" << rangeRaw << "\".");
}
}
}
}
// Iterate over packages
Variant result = Variant::List();
if (pkgs.size() > 0) {
for (size_t t = rangeStart; t <= rangeStop; t++) {
auto pkg = pkgs[t].lock();
result.Append(parent->getRulePkgInfo(pkg.get()));
}
}
// There are no devices and a range was given, probably out of range
if (result.Size() == 0 && rangeStart > 0) {
if (rangeStart > 0) {
handleErrorResponse(req, resp, "Out of range", Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
} else {
handleResponse(req, resp, result);
}
// Return devices array
} else {
std::string rangeOut = (boost::format("items %1%-%2%/%3%") % rangeStart % rangeStop % pkgs.size()).str();
resp.set("Content-Range", rangeOut);
resp.set("X-Content-Range", rangeOut);
handleResponse(req, resp, result);
}*/
handleResponse(req, resp, form, parent->getEngineInfo(ruleService, "", ""));
// rule package given
} else if (pathPieces.size() == matchPieces.size() + 1) {
@@ -330,18 +280,11 @@ void RuleEngineHandler::__handleInfo(Poco::Net::HTTPServerRequest &req, Poco::Ne
RuleEngineAPIMethod RuleEngineHandler::getRuleEngineAPIMethod(Net::HTTPServerRequest &req) noexcept {
std::unique_ptr<Net::HTMLForm> form;
RuleEngineAPIMethod RuleEngineHandler::getRuleEngineAPIMethod(Net::HTTPServerRequest &req, Net::HTMLForm& form) noexcept {
RuleEngineAPIMethod retval = RuleEngineAPIMethod::GET; // default
if (req.getMethod() == Net::HTTPRequest::HTTP_POST && req.has("Content-type")
&& req.get("Content-type") == Net::HTMLForm::ENCODING_URL) {
form.reset(new Net::HTMLForm(req, req.stream()));
} else {
form.reset(new Net::HTMLForm(req));
}
std::string methodRaw;
if (form->has("method")) {
methodRaw = form->get("method");
if (form.has("method")) {
methodRaw = form.get("method");
} else if (req.has("API-Method")) {
methodRaw = req.get("API-Method");
} else if (req.has("X-API-Method")) {
@@ -359,7 +302,7 @@ RuleEngineAPIMethod RuleEngineHandler::getRuleEngineAPIMethod(Net::HTTPServerReq
(boost::format("Unknown rule engine api method \"%1%\"") % methodRaw).str(),
Net::HTTPResponse::HTTPStatus::HTTP_BAD_REQUEST );
}
} else if (form->has("onevent") || req.has("On-Event") || req.has("X-On-Event")) {
} else if (form.has("onevent") || req.has("On-Event") || req.has("X-On-Event")) {
retval = RuleEngineAPIMethod::EVENT;
}
return retval;
@@ -119,15 +119,15 @@ public:
bool ssl = false )
: parent(parent), matchUri(matchUri), ruleService(ruleService), ssl(ssl) {}
virtual void _handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp) override;
virtual void _handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Poco::Net::HTMLForm& form) override;
void __handleInfo(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp);
void __handleInfo(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Poco::Net::HTMLForm& form);
void __handleSet(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, RuleEngineAPIMethod method);
void __handleSet(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, RuleEngineAPIMethod method, Poco::Net::HTMLForm& form);
void __handleEvent(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, bool websocket);
void __handleEvent(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Poco::Net::HTMLForm& form, bool websocket);
static RuleEngineAPIMethod getRuleEngineAPIMethod(Poco::Net::HTTPServerRequest &req) noexcept;
static RuleEngineAPIMethod getRuleEngineAPIMethod(Poco::Net::HTTPServerRequest &req, Poco::Net::HTMLForm& form) noexcept;
/*void sendDeviceCommand( devicerepository::Device* device,
Poco::Net::HTTPServerRequest &req,