summaryrefslogtreecommitdiffstats
path: root/ahl-binding/ahl-binding.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ahl-binding/ahl-binding.cpp')
-rw-r--r--ahl-binding/ahl-binding.cpp92
1 files changed, 83 insertions, 9 deletions
diff --git a/ahl-binding/ahl-binding.cpp b/ahl-binding/ahl-binding.cpp
index 036ed88..98d36b6 100644
--- a/ahl-binding/ahl-binding.cpp
+++ b/ahl-binding/ahl-binding.cpp
@@ -89,6 +89,24 @@ void ahl_api_get_roles(afb_req_t req)
}
/**
+ * @brief Callback invoked when clients call the verb 'subscribe'.
+ * @param[in] req Request to handle.
+ */
+void ahl_api_subscribe(afb_req_t req)
+{
+ ahl_binding_t::instance().subscribe(req);
+}
+
+/**
+ * @brief Callback invoked when clients call the verb 'unsubscribe'.
+ * @param[in] req Request to handle.
+ */
+void ahl_api_unsubscribe(afb_req_t req)
+{
+ ahl_binding_t::instance().unsubscribe(req);
+}
+
+/**
* @brief Callback invoked when clients call a 'role' verb.
* @param[in] req Request to handle.
*
@@ -166,6 +184,13 @@ int ahl_binding_t::init()
afb_api_seal(handle_);
AFB_API_NOTICE(handle_, "API is now sealed!");
+ volume_changed_ = afb_api_make_event(handle_, "volume_changed");
+ if(!afb_event_is_valid(volume_changed_))
+ {
+ AFB_API_ERROR(handle_, "Failed to create the \"volume_changed\" event!");
+ return -2;
+ }
+
if (update_streams()) return -1;
return 0;
}
@@ -283,6 +308,30 @@ void ahl_binding_t::load_static_verbs()
{
throw std::runtime_error("Failed to add 'get_role' verb to the API.");
}
+
+ if (afb_api_add_verb(
+ handle_,
+ "subscribe",
+ "Subscribe to \"volume_changed\" event",
+ ahl_api_subscribe,
+ nullptr,
+ nullptr,
+ AFB_SESSION_NONE_X2, 0))
+ {
+ throw std::runtime_error("Failed to add 'subscribe' verb to the API.");
+ }
+
+ if (afb_api_add_verb(
+ handle_,
+ "unsubscribe",
+ "Unsubscribe to \"volume_changed\" event",
+ ahl_api_unsubscribe,
+ nullptr,
+ nullptr,
+ AFB_SESSION_NONE_X2, 0))
+ {
+ throw std::runtime_error("Failed to add 'unsubscribe' verb to the API.");
+ }
}
void ahl_binding_t::load_controller_configs()
@@ -381,26 +430,51 @@ int ahl_binding_t::create_api_verb(role_t* r)
return 0;
}
-void ahl_binding_t::get_roles(afb_req_t req)
+void ahl_binding_t::get_roles(afb_req_t req) const
{
- json_bool verbose = FALSE;
- json_object* arg = afb_req_json(req);
+ json_bool verbose = FALSE;
+ json_object* arg = afb_req_json(req);
json_object* jverbose;
if (arg != nullptr)
{
- json_bool ret = json_object_object_get_ex(arg, "verbose", &jverbose);
- if (ret) verbose = json_object_get_boolean(jverbose);
+ json_bool ret = json_object_object_get_ex(arg, "verbose", &jverbose);
+ if (ret) verbose = json_object_get_boolean(jverbose);
}
json_object* result = json_object_new_array();
for(const auto& r : roles_)
- {
- if (verbose == TRUE || r.device_uri().size())
- json_object_array_add(result, json_object_new_string(r.uid().c_str()));
- }
+ {
+ if (verbose == TRUE || r.device_uri().size())
+ json_object_array_add(result, json_object_new_string(r.uid().c_str()));
+ }
afb_req_success(req, result, nullptr);
}
+void ahl_binding_t::subscribe(afb_req_t req) const
+{
+ if (afb_req_subscribe(req, volume_changed_))
+ afb_req_fail(req, "Failed to subscribe to \"volume_changed\" event!", nullptr);
+ else
+ afb_req_success(req, nullptr, "Subscribed to \"volume_changed\" event!");
+}
+
+void ahl_binding_t::unsubscribe(afb_req_t req) const
+{
+ if (afb_req_unsubscribe(req, volume_changed_))
+ afb_req_fail(req, "Failed to unsubscribe from \"volume_changed\" event!", nullptr);
+ else
+ afb_req_success(req, nullptr, "Unsubscribed from \"volume_changed\" event!");
+}
+
+int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) const
+{
+ json_object* data = json_object_new_object();
+ json_object_object_add(data, "role", json_object_new_string(role.c_str()));
+ json_object_object_add(data, "volume", json_object_new_int(volume));
+
+ return afb_event_push(volume_changed_, data);
+}
+
const std::vector<role_t> ahl_binding_t::roles() const
{
return roles_;