#include "request.h"
#include "provider.h"
#include "context_mgr_impl.h"
+#include "context_trigger/template_manager.h"
/* Context Providers */
#include <internal/device_context_provider.h>
};
static std::list<trigger_item_format_s> __trigger_item_list;
+bool ctx::context_manager_impl::initialized = false;
ctx::context_manager_impl::context_manager_impl()
{
ret = init_custom_context_provider();
IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: custom-context-provider");
+ initialized = true;
+
return true;
}
bool ctx::context_manager_impl::register_trigger_item(const char *subject, int operation, ctx::Json attributes, ctx::Json options, const char* owner)
{
IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
- __trigger_item_list.push_back(trigger_item_format_s(subject, operation, attributes, options, (owner)? owner : ""));
+
+ if (!initialized) {
+ __trigger_item_list.push_back(trigger_item_format_s(subject, operation, attributes, options, (owner)? owner : ""));
+ } else {
+ ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
+ IF_FAIL_RETURN_TAG(tmpl_mgr, false, _E, "Memory allocation failed");
+ tmpl_mgr->register_template(subject, operation, attributes, options, owner);
+ }
return true;
}
bool ctx::context_manager_impl::unregister_trigger_item(const char *subject)
{
IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
- __trigger_item_list.push_back(trigger_item_format_s(subject));
+
+ if (!initialized) {
+ __trigger_item_list.push_back(trigger_item_format_s(subject));
+ } else {
+ ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
+ IF_FAIL_RETURN_TAG(tmpl_mgr, false, _E, "Memory allocation failed");
+ tmpl_mgr->unregister_template(subject);
+ }
return true;
}
private:
std::map<std::string, context_provider_handler*> provider_handle_map;
+ static bool initialized;
static gboolean thread_switcher(gpointer data);
void _publish(const char *subject, ctx::Json &option, int error, ctx::Json &data_updated);
#include <db_mgr.h>
#include <package_manager.h>
#include "rule_manager.h"
-#include "template_manager.h"
#include "context_monitor.h"
#include "rule.h"
#include "timer.h"
int ctx::rule_manager::add_rule(std::string creator, const char* pkg_id, ctx::Json rule, ctx::Json* rule_id)
{
- apply_templates();
bool ret;
int64_t rid;
int ctx::rule_manager::remove_rule(int rule_id)
{
- apply_templates();
bool ret;
// Delete rule from DB
int ctx::rule_manager::enable_rule(int rule_id)
{
- apply_templates();
int error;
std::string query;
std::vector<Json> rule_record;
int ctx::rule_manager::disable_rule(int rule_id)
{
- apply_templates();
bool ret;
int error;
int ctx::rule_manager::get_rule_by_id(std::string pkg_id, int rule_id, ctx::Json* request_result)
{
- apply_templates();
std::string q = "SELECT description FROM context_trigger_rule WHERE (package_id = '";
q += pkg_id;
q += "') and (row_id = ";
return ERR_NONE;
}
-
-void ctx::rule_manager::apply_templates()
-{
- ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
- IF_FAIL_VOID_TAG(tmpl_mgr, _E, "Memory allocation failed");
- tmpl_mgr->apply_templates();
-}
bool rule_equals(ctx::Json& lrule, ctx::Json& rrule);
int get_uninstalled_app(void);
int clear_rule_of_uninstalled_package(bool is_init = false);
- void apply_templates(void);
std::set<std::string> uninstalled_packages;
while(_context_mgr->pop_trigger_item(subject, operation, attributes, options, owner, unregister)) {
if (unregister) {
- query += remove_template(subject);
- _rule_mgr->pause_rule_with_item(subject);
+ unregister_template(subject);
} else {
- query += add_template(subject, operation, attributes, options, owner);
- if (!owner.empty()) {
- _rule_mgr->resume_rule_with_item(subject);
- }
+ register_template(subject, operation, attributes, options, owner);
}
}
- IF_FAIL_VOID(!query.empty());
+}
+
+void ctx::template_manager::register_template(std::string subject, int operation, ctx::Json attributes, ctx::Json options, std::string owner)
+{
+ _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
+ _J("Attr", attributes);
+ _J("Opt", options);
+
+ std::string query = "UPDATE context_trigger_template SET operation=" + int_to_string(operation)
+ + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
+ + "' WHERE name='" + subject + "'; ";
+
+ query += "INSERT OR IGNORE INTO context_trigger_template (name, operation, attributes, options, owner) VALUES ('"
+ + subject + "', " + int_to_string(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
+ + owner + "'); ";
std::vector<Json> record;
bool ret = db_manager::execute_sync(query.c_str(), &record);
IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
+
+ if (!owner.empty()) {
+ _rule_mgr->resume_rule_with_item(subject);
+ }
+}
+
+void ctx::template_manager::unregister_template(std::string subject)
+{
+ _D("[Remove template] Subject: %s", subject.c_str());
+ std::string query = "DELETE FROM context_trigger_template WHERE name = '" + subject + "'; ";
+
+ std::vector<Json> record;
+ bool ret = db_manager::execute_sync(query.c_str(), &record);
+ IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
+
+ _rule_mgr->pause_rule_with_item(subject);
}
+
std::string ctx::template_manager::add_template(std::string &subject, int &operation, ctx::Json &attributes, ctx::Json &options, std::string &owner)
{
_D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
int ctx::template_manager::get_template(std::string &subject, ctx::Json* tmpl)
{
// Update latest template information
- apply_templates();
-
std::string q = "SELECT * FROM context_trigger_template WHERE name = '" + subject + "'";
std::vector<Json> record;
bool init();
void apply_templates();
int get_template(std::string &subject, ctx::Json* tmpl);
+ void register_template(std::string subject, int operation, ctx::Json attributes, ctx::Json options, std::string owner);
+ void unregister_template(std::string subject);
private:
template_manager();