aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/voiceagents/src/VoiceAgentEventsHandler.cpp
blob: bff9d8c836bbfa0e103bcabd7a4ae938e649d45e (plain)
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
/*
 * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * 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 "voiceagents/include/VoiceAgentEventsHandler.h"

#include <json-c/json.h>

static string TAG = "vshlcore::voiceagents::VoiceAgentEventsHandler";
static string VA_VERB_SUBSCRIBE = "subscribe";

using Level = vshlcore::common::interfaces::ILogger::Level;
using namespace vshlcore::common::interfaces;
namespace vshlcore {
namespace voiceagents {

shared_ptr<VoiceAgentEventsHandler> VoiceAgentEventsHandler::create(
    shared_ptr<vshlcore::common::interfaces::ILogger> logger,
    shared_ptr<vshlcore::common::interfaces::IAFBApi> afbApi) {
    auto eventFilter = std::shared_ptr<VoiceAgentEventsHandler>(new VoiceAgentEventsHandler(logger, afbApi));
    return eventFilter;
}

VoiceAgentEventsHandler::VoiceAgentEventsHandler(
    shared_ptr<vshlcore::common::interfaces::ILogger> logger,
    shared_ptr<vshlcore::common::interfaces::IAFBApi> afbApi) :
        mAfbApi(afbApi),
        mLogger(logger) {
}

VoiceAgentEventsHandler::~VoiceAgentEventsHandler() {
    mEventsMap.clear();
}

string VoiceAgentEventsHandler::getName() {
    return TAG;
}

void VoiceAgentEventsHandler::createVshlEventsForVoiceAgent(const string voiceAgentId) {
    // Update the events map with all the VSHL Events.
    for (auto eventName : VSHL_EVENTS) {
        string eventNameWithVAId = createEventNameWithVAId(eventName, voiceAgentId);
        auto it = mEventsMap.find(eventNameWithVAId);
        if (it == mEventsMap.end() && mAfbApi) {
            // create a new event and add it to the map.
            shared_ptr<IAFBApi::IAFBEvent> event = mAfbApi->createEvent(eventNameWithVAId);
            mEventsMap.insert(make_pair(eventNameWithVAId, event));
        }
    }
}

void VoiceAgentEventsHandler::removeVshlEventsForVoiceAgent(const string voiceAgentId) {
    // Update the events map with all the VSHL Events.
    for (auto eventName : VSHL_EVENTS) {
        string eventNameWithVAId = createEventNameWithVAId(eventName, voiceAgentId);
        auto it = mEventsMap.find(eventNameWithVAId);
        if (it != mEventsMap.end()) {
            mEventsMap.erase(it);
        }
    }
}

bool VoiceAgentEventsHandler::subscribeToVshlEventFromVoiceAgent(
    vshlcore::common::interfaces::IAFBRequest& request,
    const string eventName,
    const shared_ptr<VoiceAgent> voiceAgent) {
    auto supportedEventsIt = find(VSHL_EVENTS.begin(), VSHL_EVENTS.end(), eventName);
    if (supportedEventsIt == VSHL_EVENTS.end()) {
        mLogger->log(Level::ERROR, TAG, "Event: " + eventName + " not a known event.");
        return false;
    }

    // Check if the entry for the voiceagent is present in the
    // events map. If not then return false because the responsibility
    // of adding to the map lies in the hands of AddVoiceAgent method.
    string eventNameWithVAId = createEventNameWithVAId(eventName, voiceAgent->getId());
    auto createdEventsIt = mEventsMap.find(eventNameWithVAId);
    if (createdEventsIt == mEventsMap.end()) {
        mLogger->log(Level::ERROR, TAG, "Not able to subscribe. Event doesn't exist, " + eventNameWithVAId);
        return false;
    }
    createdEventsIt->second->subscribe(request);

    if (!callSubscribeVerb(voiceAgent)) {
        mLogger->log(Level::WARNING, TAG, "Failed to subscribe to voiceagent: " + voiceAgent->getId());
    }

    return true;
}

// IEventFilter override.
bool VoiceAgentEventsHandler::onIncomingEvent(const string eventName, const string voiceAgentId, const string payload) {
    string eventNameWithVAId = createEventNameWithVAId(eventName, voiceAgentId);
    auto it = mEventsMap.find(eventNameWithVAId);
    if (it != mEventsMap.end()) {
        json_object* payloadJ = NULL;
        if(payload.length()) {
            payloadJ = json_tokener_parse(payload.c_str());
        } else {
            payloadJ = json_object_new_string("");
        }
        if(!payloadJ) {
            mLogger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
            return false;
        }
        return it->second->publishEvent(payloadJ);
    }

    return true;
}

string VoiceAgentEventsHandler::createEventNameWithVAId(string eventName, string voiceAgentId) {
    return eventName + "#" + voiceAgentId;
}

bool VoiceAgentEventsHandler::callSubscribeVerb(const shared_ptr<VoiceAgent> voiceAgent) {
    if (!voiceAgent) {
        mLogger->log(Level::ERROR, TAG, "Failed to callSubscribeVerb. Invalid input parameter.");
        return false;
    }

    if (!mAfbApi) {
        mLogger->log(
            Level::ERROR, TAG, "Failed to callSubscribeVerb on voicegent: " + voiceAgent->getId() + ", No API.");
        return false;
    }

    // TODO(Naveen): Move to utilities.
    json_object* object = NULL;
    std::string error, info;
    int rc = mAfbApi->callSync(voiceAgent->getApi(), VA_VERB_SUBSCRIBE, NULL, &object, error, info);

    if (object) {
        free(object);
    }

    return true;
}
}  // namespace voiceagents
}  // namespace vshl