1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/*
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2019 Konsulko Group
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "VshlCoreApi.h"
#include <list>
#include <sstream>
#include <fstream>
#include <filesystem>
#include <json.hpp>
#include <json-c/json.h>
#include "afb/AFBApiImpl.h"
#include "afb/AFBRequestImpl.h"
#include "core/VRRequestProcessor.h"
#include "utilities/events/EventRouter.h"
#include "utilities/logging/Logger.h"
#include "voiceagents/VoiceAgentEventNames.h"
#include "voiceagents/VoiceAgentsDataManager.h"
using namespace std;
CTLP_CAPI_REGISTER("vshl-core-api");
static std::string TAG = "vshlcore::plugins::VshlCoreAPI";
static std::string VA_JSON_ATTR_DEFAULT = "default";
static std::string VA_JSON_ATTR_AGENTS = "agents";
static std::string VA_JSON_ATTR_ID = "id";
static std::string VA_JSON_ATTR_NAME = "name";
static std::string VA_JSON_ATTR_API = "api";
static std::string VA_JSON_ATTR_ACTIVE = "active";
static std::string VA_JSON_ATTR_WWS = "wakewords";
static std::string VA_JSON_ATTR_ACTIVE_WW = "activewakeword";
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";
static std::string CONFIG_JSON_FILE = "/etc/xdg/AGL/voice-high.json";
static std::string JSON_EXT = ".json";
static std::string VA_CONFIG_JSON_DIR = "/etc/xdg/AGL/voiceagents";
static std::shared_ptr<vshlcore::utilities::logging::Logger> sLogger;
static std::shared_ptr<vshlcore::common::interfaces::IAFBApi> sAfbApi;
static std::unique_ptr<vshlcore::core::VRRequestProcessor> sVRRequestProcessor;
static std::unique_ptr<vshlcore::voiceagents::VoiceAgentsDataManager> sVoiceAgentsDataManager;
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;
}
// Logger
sLogger = vshlcore::utilities::logging::Logger::create(plugin->api);
// sLogger->log(Level::INFO, TAG, "Vshl plugin loaded & initialized.");
// AFB Wrapper
sAfbApi = vshlcore::afb::AFBApiImpl::create(plugin->api);
// VRRequestProcessor
auto vrRequestProcessorDelegate = vshlcore::core::VRRequestProcessorDelegate::create(sLogger, sAfbApi);
sVRRequestProcessor = vshlcore::core::VRRequestProcessor::create(sLogger, vrRequestProcessorDelegate);
if (!sVRRequestProcessor) {
sLogger->log(Level::ERROR, TAG, "Failed to create VRRequestProcessor");
return -1;
}
// VoiceAgentDataManager
sVoiceAgentsDataManager = vshlcore::voiceagents::VoiceAgentsDataManager::create(sLogger, sAfbApi);
if (!sVoiceAgentsDataManager) {
sLogger->log(Level::ERROR, TAG, "Failed to create VoiceAgentsDataManager");
return -1;
}
sVoiceAgentsDataManager->addVoiceAgentsChangeObserver(sVRRequestProcessor->getVoiceAgentsChangeObserver());
// EventRouter
sEventRouter = vshlcore::utilities::events::EventRouter::create(sLogger);
if (!sEventRouter) {
sLogger->log(Level::ERROR, TAG, "Failed to create EventRouter");
return -1;
}
sEventRouter->addEventFilter(sVoiceAgentsDataManager->getEventFilter());
return 0;
}
CTLP_CAPI(onAuthStateEvent, source, argsJ, eventJ) {
if (sEventRouter == nullptr) {
return -1;
}
string eventName = vshlcore::voiceagents::VSHL_EVENT_AUTH_STATE_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, "onAuthStateEvent: 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(onConnectionStateEvent, source, argsJ, eventJ) {
if (sEventRouter == nullptr) {
return -1;
}
string eventName = vshlcore::voiceagents::VSHL_EVENT_CONNECTION_STATE_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, "onConnectionStateEvent: 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(onDialogStateEvent, source, argsJ, eventJ) {
if (sEventRouter == nullptr) {
return -1;
}
string eventName = vshlcore::voiceagents::VSHL_EVENT_DIALOG_STATE_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, "onDialogStateEvent: 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(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.
static int AddVoiceAgentEvents(std::string &agentApi) {
// 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, "AddVoiceAgentEvents: 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 + "/voice_authstate_event";
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#onAuthStateEvent");
json_object_object_add(eventJ, "action", actionJ);
json_object_array_add(eventsJ, eventJ);
eventJ = json_object_new_object();
uid = agentApi + "/voice_connectionstate_event";
uidJ = json_object_new_string(uid.c_str());
json_object_object_add(eventJ, "uid", uidJ);
actionJ = json_object_new_string("plugin://vshl-core#onConnectionStateEvent");
json_object_object_add(eventJ, "action", actionJ);
json_object_array_add(eventsJ, eventJ);
eventJ = json_object_new_object();
uid = agentApi + "/voice_dialogstate_event";
uidJ = json_object_new_string(uid.c_str());
json_object_object_add(eventJ, "uid", uidJ);
actionJ = json_object_new_string("plugin://vshl-core#onDialogStateEvent");
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
// this call.
int rc = AddActionsToSection(sAfbApi->getApi(), section, eventsJ, 0);
if (rc != 0) {
stringstream message;
message << "AddVoiceAgentEvents: AddActionsToSection rc = " << rc;
sLogger->log(Level::WARNING, TAG, message.str().c_str());
}
return rc;
}
// Helper function to parse a voiceagent's configuration
static bool parseVoiceAgentConfig(const json &agentJson) {
if (agentJson.find(VA_JSON_ATTR_ID) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_ACTIVE) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_NAME) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_API) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_WWS) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_ACTIVE_WW) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_DESCRIPTION) == agentJson.end() ||
agentJson.find(VA_JSON_ATTR_VENDOR) == agentJson.end()) {
stringstream error;
error << "loadVoiceAgentConfig: One or more missing params in agent "
"config "
<< agentJson.dump();
sLogger->log(Level::WARNING, TAG, error.str().c_str());
return false;
}
std::string id(agentJson[VA_JSON_ATTR_ID].get<string>());
std::string name(agentJson[VA_JSON_ATTR_NAME].get<string>());
std::string api(agentJson[VA_JSON_ATTR_API].get<string>());
std::string description(agentJson[VA_JSON_ATTR_DESCRIPTION].get<string>());
std::string vendor(agentJson[VA_JSON_ATTR_VENDOR].get<string>());
std::string activeWakeword(agentJson[VA_JSON_ATTR_ACTIVE_WW].get<string>());
bool isActive(agentJson[VA_JSON_ATTR_ACTIVE].get<bool>());
shared_ptr<unordered_set<string>> wakewords = std::make_shared<unordered_set<string>>();
json wakewordsJson = agentJson[VA_JSON_ATTR_WWS];
for (auto wwIt = wakewordsJson.begin(); wwIt != wakewordsJson.end(); ++wwIt) {
wakewords->insert(wwIt->get<string>());
}
if (sAfbApi->requireApi(api, true) != 0) {
stringstream error;
error << "loadVoiceAgentConfig: Failed to require API \""
<< api
<< "\" for voiceagent \""
<< name
<< "\".";
sLogger->log(Level::ERROR, TAG, error.str().c_str());
return false;
}
if (AddVoiceAgentEvents(api) != 0) {
stringstream error;
error << "loadVoiceAgentConfig: Failed to add events for voiceagent \""
<< name
<< "\".";
sLogger->log(Level::ERROR, TAG, error.str().c_str());
return false;
}
return sVoiceAgentsDataManager->addNewVoiceAgent(
id, name, description, api, vendor, activeWakeword, isActive, wakewords);
}
// Helper function to parse voiceagents configuration
static void parseVoiceAgentsConfig(const json &agentsConfigJson, std::string &defaultVoiceAgent) {
if (agentsConfigJson.find(VA_JSON_ATTR_AGENTS) != agentsConfigJson.end()) {
json agentsJson = agentsConfigJson[VA_JSON_ATTR_AGENTS];
for (auto agentIt = agentsJson.begin(); agentIt != agentsJson.end(); ++agentIt) {
json agentJson = *agentIt;
parseVoiceAgentConfig(agentJson);
}
}
// Save the default agent if specified
if (agentsConfigJson.find(VA_JSON_ATTR_DEFAULT) != agentsConfigJson.end()) {
defaultVoiceAgent = agentsConfigJson[VA_JSON_ATTR_DEFAULT].get<string>();
}
}
CTLP_CAPI(loadVoiceAgentsConfig, source, argsJ, eventJ) {
if (sVoiceAgentsDataManager == nullptr) {
sLogger->log(Level::WARNING, TAG, "loadVoiceAgentsConfig: Voice service not initialized.");
return -1;
}
std::string defaultVoiceAgent;
if (argsJ != nullptr) {
// Parse any agent configs from controller arguments
json agentsConfigJson = json::parse(json_object_to_json_string(argsJ));
parseVoiceAgentsConfig(agentsConfigJson, defaultVoiceAgent);
}
// Load external configuration if present
filesystem::path agentsConfigFile = CONFIG_JSON_FILE;
if(filesystem::exists(agentsConfigFile)) {
stringstream message;
message << "loadVoiceAgentsConfig: reading " << agentsConfigFile.string();;
sLogger->log(Level::INFO, TAG, message.str().c_str());
ifstream agentsConfigJsonFile(agentsConfigFile.string());
json agentsConfigJson;
agentsConfigJsonFile >> agentsConfigJson;
parseVoiceAgentsConfig(agentsConfigJson, defaultVoiceAgent);
}
// Load any external individual voiceagent configurations
error_code ec;
for (const auto& entry : filesystem::directory_iterator(VA_CONFIG_JSON_DIR, ec)) {
if (entry.is_regular_file(ec) &&
entry.path().extension().string() == JSON_EXT) {
const auto filename = entry.path().string();
stringstream message;
message << "loadVoiceAgentsConfig: reading " << filename;
sLogger->log(Level::INFO, TAG, message.str().c_str());
ifstream jsonFile(filename);
json agentJson;
jsonFile >> agentJson;
parseVoiceAgentConfig(agentJson);
}
}
// Set the default agent if specified
if (!defaultVoiceAgent.empty()) {
stringstream message;
message << "loadVoiceAgentsConfig: setting default agent to " << defaultVoiceAgent;
sLogger->log(Level::INFO, TAG, message.str().c_str());
sVoiceAgentsDataManager->setDefaultVoiceAgent(defaultVoiceAgent);
} else {
sLogger->log(Level::WARNING, TAG, "loadVoiceAgentsConfig: No default agent found");
}
// Always return zero so service will start
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;
}
if (sVRRequestProcessor == nullptr) {
return -1;
}
int result = 0;
std::string requestId = sVRRequestProcessor->startListening();
if (!requestId.empty()) {
json responseJson;
responseJson[STARTLISTENING_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 startListening...");
}
return 0;
}
CTLP_CAPI(cancelListening, source, argsJ, eventJ) {
return 0;
}
CTLP_CAPI(enumerateVoiceAgents, source, argsJ, eventJ) {
if (sVoiceAgentsDataManager == nullptr) {
return -1;
}
auto agents = sVoiceAgentsDataManager->getAllVoiceAgents();
std::string defaultAgentId(sVoiceAgentsDataManager->getDefaultVoiceAgent());
json responseJson;
json agentsJson = json::array();
for (auto agent : agents) {
json agentJson;
agentJson[VA_JSON_ATTR_ID] = agent->getId();
agentJson[VA_JSON_ATTR_NAME] = agent->getName();
agentJson[VA_JSON_ATTR_DESCRIPTION] = agent->getDescription();
agentJson[VA_JSON_ATTR_API] = agent->getApi();
agentJson[VA_JSON_ATTR_VENDOR] = agent->getVendor();
agentJson[VA_JSON_ATTR_ACTIVE] = agent->isActive();
agentJson[VA_JSON_ATTR_ACTIVE_WW] = agent->getActiveWakeword();
auto wakewords = agent->getWakeWords();
if (wakewords != nullptr) {
json wakewordsJson;
for (auto wakeword : *wakewords) {
wakewordsJson.push_back(wakeword);
}
agentJson[VA_JSON_ATTR_WWS] = wakewordsJson;
}
agentsJson.push_back(agentJson);
}
responseJson[VA_JSON_ATTR_AGENTS] = agentsJson;
responseJson[VA_JSON_ATTR_DEFAULT] = defaultAgentId;
afb_req_success(source->request, json_tokener_parse(responseJson.dump().c_str()), NULL);
return 0;
}
CTLP_CAPI(subscribe, source, argsJ, eventJ) {
if (sVoiceAgentsDataManager == nullptr) {
return -1;
}
if (eventJ == nullptr) {
sLogger->log(Level::WARNING, TAG, "subscribe: No arguments supplied.");
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, "subscribe: 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, "subscribe: No events array found in subscribe json");
return -1;
}
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);
for (auto event : events) {
if (!sVoiceAgentsDataManager->subscribeToVshlEventFromVoiceAgent(*request, event, voiceAgentId)) {
sLogger->log(Level::ERROR, TAG, "subscribe: Failed to subscribe to event: " + event);
return -1;
}
}
afb_req_success(source->request, json_object_new_string("Subscription to events successfully completed."), NULL);
return 0;
}
CTLP_CAPI(setDefaultVoiceAgent, source, argsJ, eventJ) {
if (sVoiceAgentsDataManager == nullptr) {
return -1;
}
if (eventJ == nullptr) {
sLogger->log(Level::WARNING, TAG, "setDefaultVoiceAgent: No arguments supplied.");
return -1;
}
json jsonRequest = json::parse(json_object_to_json_string(eventJ));
if (jsonRequest.find(VA_JSON_ATTR_ID) == jsonRequest.end()) {
sLogger->log(Level::ERROR, TAG, "setDefaultVoiceAgent: voice agent id not found in request json");
return -1;
}
std::string voiceAgentId(jsonRequest[VA_JSON_ATTR_ID].get<string>());
if (!sVoiceAgentsDataManager->setDefaultVoiceAgent(voiceAgentId)) {
sLogger->log(Level::ERROR, TAG, "setDefaultVoiceAgent: Failed to set default agent");
return -1;
}
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;
}
|