diff options
Diffstat (limited to 'src/plugins/VshlCoreApi.cpp')
-rw-r--r-- | src/plugins/VshlCoreApi.cpp | 209 |
1 files changed, 205 insertions, 4 deletions
diff --git a/src/plugins/VshlCoreApi.cpp b/src/plugins/VshlCoreApi.cpp index 4c32391..b14f949 100644 --- a/src/plugins/VshlCoreApi.cpp +++ b/src/plugins/VshlCoreApi.cpp @@ -48,6 +48,7 @@ static std::string VA_JSON_ATTR_DESCRIPTION = "description"; static std::string VA_JSON_ATTR_VENDOR = "vendor"; static std::string STARTLISTENING_JSON_ATTR_REQUEST = "request_id"; +static std::string SUBSCRIBE2LOGINEVENTS_JSON_ATTR_REQUEST = "request_id"; static std::string EVENTS_JSON_ATTR_VA_ID = "va_id"; static std::string EVENTS_JSON_ATTR_EVENTS = "events"; @@ -65,6 +66,11 @@ static std::unique_ptr<vshlcore::utilities::events::EventRouter> sEventRouter; using json = nlohmann::json; using Level = vshlcore::utilities::logging::Logger::Level; +static const char *signalcomposer_events[] = { + "event.voice", + NULL, +}; + CTLP_ONLOAD(plugin, ret) { if (plugin->api == nullptr) { return -1; @@ -158,6 +164,65 @@ CTLP_CAPI(onDialogStateEvent, source, argsJ, eventJ) { return 0; } +CTLP_CAPI(onLoginPairReceivedEvent, source, argsJ, eventJ) { + if (sEventRouter == nullptr) { + return -1; + } + + string eventName = vshlcore::voiceagents::VSHL_EVENT_LOGINPAIR_RECEIVED_EVENT; + json eventJson = json::parse(json_object_to_json_string(eventJ)); + if (eventJson.find(EVENTS_JSON_ATTR_VA_ID) == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onLoginPairReceivedEvent: No voiceagent id found."); + return -1; + } + std::string voiceAgentId(eventJson[EVENTS_JSON_ATTR_VA_ID].get<string>()); + + sEventRouter->handleIncomingEvent(eventName, voiceAgentId, json_object_to_json_string(eventJ)); + + return 0; +} + +CTLP_CAPI(onLoginPairExpiredEvent, source, argsJ, eventJ) { + if (sEventRouter == nullptr) { + return -1; + } + + string eventName = vshlcore::voiceagents::VSHL_EVENT_LOGINPAIR_EXPIRED_EVENT; + json eventJson = json::parse(json_object_to_json_string(eventJ)); + if (eventJson.find(EVENTS_JSON_ATTR_VA_ID) == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onLoginPairExpiredEvent: No voiceagent id found."); + return -1; + } + std::string voiceAgentId(eventJson[EVENTS_JSON_ATTR_VA_ID].get<string>()); + + sEventRouter->handleIncomingEvent(eventName, voiceAgentId, json_object_to_json_string(eventJ)); + + return 0; +} + +CTLP_CAPI(onVoiceTriggerEvent, source, argsJ, eventJ) { + if (sVRRequestProcessor == nullptr) { + return -1; + } + + json eventJson = json::parse(json_object_to_json_string(eventJ)); + if (eventJson.find("uid") == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onVoiceTriggerEvent: No uid found."); + return -1; + } + if (eventJson.find("value") == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onVoiceTriggerEvent: No value found."); + return -1; + } + std::string uid(eventJson["uid"].get<std::string>()); + bool value(eventJson["value"].get<bool>()); + if (uid == "event.voice" && value == true) { + sVRRequestProcessor->startListening(); + } + + return 0; +} + // Helper function to add events for voice agent to controller // configuration. It relies on the controller configuration being // available via the userdata of the API pointer. @@ -208,6 +273,22 @@ static int AddVoiceAgentEvents(std::string &agentApi) { json_object_object_add(eventJ, "action", actionJ); json_object_array_add(eventsJ, eventJ); + eventJ = json_object_new_object(); + uid = agentApi + "/voice_cbl_codepair_received_event"; + uidJ = json_object_new_string(uid.c_str()); + json_object_object_add(eventJ, "uid", uidJ); + actionJ = json_object_new_string("plugin://vshl-core#onLoginPairReceivedEvent"); + json_object_object_add(eventJ, "action", actionJ); + json_object_array_add(eventsJ, eventJ); + + eventJ = json_object_new_object(); + uid = agentApi + "/voice_cbl_codepair_expired_event"; + uidJ = json_object_new_string(uid.c_str()); + json_object_object_add(eventJ, "uid", uidJ); + actionJ = json_object_new_string("plugin://vshl-core#onLoginPairExpiredEvent"); + json_object_object_add(eventJ, "action", actionJ); + json_object_array_add(eventsJ, eventJ); + // Call into controller to add event actions // NOTE: AFAICT the JSON objects end up reused by the controller data // structures, so eventsJ should not be freed with a put after @@ -299,7 +380,7 @@ CTLP_CAPI(loadVoiceAgentsConfig, source, argsJ, eventJ) { return -1; } - string defaultVoiceAgent; + std::string defaultVoiceAgent; if (argsJ != nullptr) { // Parse any agent configs from controller arguments @@ -353,6 +434,86 @@ CTLP_CAPI(loadVoiceAgentsConfig, source, argsJ, eventJ) { return 0; } +// Helper function to add signal composer events for voice triggers to +// controller configuration. It relies on the controller configuration +// being available via the userdata of the API pointer. +static int AddVoiceTriggerEvents(std::string &agentApi, std::string &uuid) { + // Retrieve section config from api handle + CtlConfigT *ctrlConfig = (CtlConfigT*) afb_api_get_userdata(sAfbApi->getApi()); + if (ctrlConfig == nullptr) { + sLogger->log(Level::ERROR, TAG, "AddVoiceAgentEvents: ctrlConfig == nullptr"); + return -1; + } + + CtlSectionT* section = nullptr; + for (int idx = 0; ctrlConfig->sections[idx].key != NULL; ++idx) { + if (!strcasecmp(ctrlConfig->sections[idx].key, "events")) { + section = &(ctrlConfig->sections[idx]); + break; + } + } + if (section == nullptr) { + sLogger->log(Level::ERROR, TAG, "AddVoiceTriggerEvents: events section not found"); + return -1; + } + + // Fill out events JSON array to pass to the controller + json_object* eventsJ = json_object_new_array(); + + json_object *eventJ = json_object_new_object(); + std::string uid = agentApi + "/" + uuid; + json_object *uidJ = json_object_new_string(uid.c_str()); + json_object_object_add(eventJ, "uid", uidJ); + json_object *actionJ = json_object_new_string("plugin://vshl-core#onVoiceTriggerEvent"); + json_object_object_add(eventJ, "action", actionJ); + json_object_array_add(eventsJ, eventJ); + + // Call into controller to add event actions + // NOTE: AFAICT the JSON objects end up reused by the controller data + // structures, so eventsJ should not be freed with a put after + // this call. + int rc = AddActionsToSection(sAfbApi->getApi(), section, eventsJ, 0); + if (rc != 0) { + stringstream message; + message << "AddVoiceTriggerEvents: AddActionsToSection rc = " << rc; + sLogger->log(Level::WARNING, TAG, message.str().c_str()); + } + return rc; +} + +CTLP_CAPI(subscribeVoiceTriggerEvents, source, argsJ, eventJ) { + // Subscribe to signal-composer voice/PTT events + const char **tmp = signalcomposer_events; + json_object *args = json_object_new_object(); + json_object *signals = json_object_new_array(); + + while (*tmp) { + json_object_array_add(signals, json_object_new_string(*tmp++)); + } + json_object_object_add(args, "signal", signals); + if (json_object_array_length(signals)) { + struct json_object *responseJ; + int rc = afb_api_call_sync(sAfbApi->getApi(), "signal-composer", + "subscribe", args, &responseJ, NULL, NULL); + if (rc == 0 && responseJ) { + struct json_object *uuidJ; + json_object_object_get_ex(responseJ, "uuid", &uuidJ); + if (uuidJ) { + std::string uuid(json_object_get_string(uuidJ)); + std::string api("signal-composer"); + AddVoiceTriggerEvents(api, uuid); + } + } + if (responseJ) + json_object_put(responseJ); + } else { + json_object_put(args); + } + + // Always return zero so service will start + return 0; +} + CTLP_CAPI(startListening, source, argsJ, eventJ) { if (sVoiceAgentsDataManager == nullptr) { return -1; @@ -363,7 +524,7 @@ CTLP_CAPI(startListening, source, argsJ, eventJ) { } int result = 0; - string requestId = sVRRequestProcessor->startListening(); + std::string requestId = sVRRequestProcessor->startListening(); if (!requestId.empty()) { json responseJson; @@ -384,7 +545,7 @@ CTLP_CAPI(enumerateVoiceAgents, source, argsJ, eventJ) { if (sVoiceAgentsDataManager == nullptr) { return -1; } - + auto agents = sVoiceAgentsDataManager->getAllVoiceAgents(); std::string defaultAgentId(sVoiceAgentsDataManager->getDefaultVoiceAgent()); @@ -442,7 +603,7 @@ CTLP_CAPI(subscribe, source, argsJ, eventJ) { sLogger->log(Level::ERROR, TAG, "subscribe: No events array found in subscribe json"); return -1; } - list<string> events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get<list<string>>()); + std::list<std::string> events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get<list<string>>()); // Subscribe this client for the listed events. auto request = vshlcore::afb::AFBRequestImpl::create(source->request); @@ -483,3 +644,43 @@ CTLP_CAPI(setDefaultVoiceAgent, source, argsJ, eventJ) { afb_req_success(source->request, NULL, NULL); return 0; } + +CTLP_CAPI(subscribeToLoginEvents, source, argsJ, eventJ) { + if (sVoiceAgentsDataManager == nullptr) { + return -1; + } + + if (eventJ == nullptr) { + sLogger->log(Level::WARNING, TAG, "subscribeToLoginEvents: No arguments supplied."); + return -1; + } + + if (sVRRequestProcessor == nullptr) { + return -1; + } + + json subscribeJson = json::parse(json_object_to_json_string(eventJ)); + if (subscribeJson.find(EVENTS_JSON_ATTR_VA_ID) == subscribeJson.end()) { + sLogger->log(Level::ERROR, TAG, "subscribeToLoginEvents: No voiceagent id found in subscribe json"); + return -1; + } + std::string voiceAgentId(subscribeJson[EVENTS_JSON_ATTR_VA_ID].get<string>()); + if (subscribeJson.find(EVENTS_JSON_ATTR_EVENTS) == subscribeJson.end()) { + sLogger->log(Level::ERROR, TAG, "subscribeToLoginEvents: No events array found in subscribe json"); + return -1; + } + std::list<std::string> events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get<list<string>>()); + + int result = 0; + std::string requestId = sVRRequestProcessor->subscribeToLoginEvents(voiceAgentId, &events); + + if (!requestId.empty()) { + json responseJson; + responseJson[SUBSCRIBE2LOGINEVENTS_JSON_ATTR_REQUEST] = requestId; + afb_req_success(source->request, json_tokener_parse(responseJson.dump().c_str()), NULL); + } else { + afb_req_fail(source->request, NULL, "Failed to subscribeToLoginEvents..."); + } + + return 0; +} |