From 735112ef9923306539023c158d9ff9930f000b6b Mon Sep 17 00:00:00 2001 From: Loïc Collignon Date: Tue, 18 Dec 2018 11:55:58 +0100 Subject: Added an 'activerole' verb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Control the currently opened role with the higher priority. Volume changed event nows specify if the corresponding role was the active one when the event was emitted. Bug: SPEC-1313 Change-Id: I87ae89ef03357a3537ada86f1dd3f319d13cbe85 Signed-off-by: Loïc Collignon --- ahl-binding/ahl-binding.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'ahl-binding/ahl-binding.cpp') diff --git a/ahl-binding/ahl-binding.cpp b/ahl-binding/ahl-binding.cpp index af1ae79..84ba60a 100644 --- a/ahl-binding/ahl-binding.cpp +++ b/ahl-binding/ahl-binding.cpp @@ -44,6 +44,18 @@ 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_, + "activerole", + "Controls the currently active role", + ahl_api_activerole, + nullptr, + nullptr, + AFB_SESSION_NONE_X2, 0)) + { + throw std::runtime_error("Failed to add 'activerole' verb to the API."); + } + if (afb_api_add_verb( handle_, "subscribe", @@ -250,6 +262,18 @@ int ahl_binding_t::create_api_verb(role_t* r) return 0; } +role_t* ahl_binding_t::get_active_role() +{ + role_t* active = nullptr; + for(auto& r : roles_) + { + if (r.opened() && (active == nullptr || r.priority() > active->priority())) + active = &r; + } + + return active; +} + /** * @brief Get the singleton instance. * @return The unique instance. @@ -352,12 +376,19 @@ void ahl_binding_t::event(std::string name, json_object* arg) * @param[in] volume New volume. * @return Zero on success, non-zero otherwise. */ -int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) const +int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) { 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)); + bool active = false; + role_t* activerole = get_active_role(); + if (activerole && activerole->uid() == role) + active = true; + + json_object_object_add(data, "active", json_object_new_boolean(active)); + return afb_event_push(volume_changed_, data); } @@ -415,6 +446,17 @@ void ahl_binding_t::get_roles(afb_req_t req) const afb_req_success(req, result, nullptr); } +/** + * @brief Controls the currently active role. + * @param[in] req Request to answer. + */ +void ahl_binding_t::activerole(afb_req_t req) +{ + role_t* role = get_active_role(); + if (!role) afb_req_fail(req, "No active role!", nullptr); + else role->invoke(req); +} + /** * @brief Subscribe to events. * @param[in] req Request to answer. -- cgit 1.2.3-korg