aboutsummaryrefslogtreecommitdiffstats
path: root/src/hs-vuiadapter.cpp
blob: cbcc8e0106942c6c31bf511134d01d1c24013952 (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
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
/*
 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 "hs-vuiadapter.h"
#include "hs-clientmanager.h"
#include "hs-proxy.h"
#include "hs-appinfo.h"
#include "unistd.h"

/**
 * event handler
 *
 * #### Parameters
 * - api : the api
 * - event : received event name
 * - object : received json object
 *
 * #### Return
 * 0 : event can transfer to others
 * 1 : event not transfer to others
 */
int event_handler(afb_api_t api, const char *event, struct json_object *object)
{
    return HS_VuiAdapter::instance()->onEvent(api, event, object);
}

/* -------------------------------------Vui_Navigation------------------------------------------ */

const std::map<std::string, Vui_Navigation::func_handler> Vui_Navigation::func_list = {
    {"set_destination",         &Vui_Navigation::set_destination},
    {"cancel_navigation",       &Vui_Navigation::cancel_navigation}
};
const char _vui_prefixe[] = "vui";
const char _poi[] = "poi";
const char _navigation[] = "navigation";
const char _destination[] = "destination";
const char _coordinate[] = "coordinate";
const char _latitudeInDegrees[] = "latitudeInDegrees";
const char _longitudeInDegrees[] = "longitudeInDegrees";
const char _setDestination[] = "setDestination";
const char _cancelDestination[] = "cancelDestination";
const char _startNavigation[] = "startNavigation";
const char _stopNavigation[] = "stopNavigation";

/**
 * init
 *
 * #### Parameters
 *  - api : the api
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::init(afb_api_t api)
{
    std::list<std::string> ev_list;
    for(auto &it : func_list) {
        ev_list.push_back(it.first);
        std::string ev_name = std::string(_vshl_capabilities) + '/' + it.first;
        AFB_INFO("setEventHook event %s", ev_name.c_str());
        setEventHook(ev_name.c_str(), event_handler);
    }
    HS_VshlCapabilitiesProxy proxy;
    proxy.subscribe(api, _navigation, ev_list);
}

/**
 * handle event
 *
 * #### Parameters
 *  - api : the api serving the request
 *  - event  : event name
 *  - object : event json object
 *
 * #### Return
 * 0 : continue transfer
 * 1 : blocked
 *
 */
int Vui_Navigation::onEvent(afb_api_t api, const char *event, struct json_object *object)
{
    std::string ev(event);
    auto pos = ev.find('/');
    auto ip = func_list.find(ev.substr(pos + 1));
    int ret = 0;
    if(ip != func_list.end()) {
        (this->*(ip->second))(api, object);
        ret = 1;
    }
    return ret;
}

/**
 * set_destination event handler
 *
 * #### Parameters
 *  - api : the api serving the reques
 *  - object : event json object
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::set_destination(afb_api_t api, struct json_object *object)
{
    AFB_INFO("set dest: %s", json_object_to_json_string(object));
    struct json_object *j_dest, *j_coord, *j_latitude, *j_longitude;
    struct json_object *j_obj = json_tokener_parse(json_object_get_string(object));
    if(json_object_object_get_ex(j_obj, _destination, &j_dest)
    && json_object_object_get_ex(j_dest, _coordinate, &j_coord)
    && json_object_object_get_ex(j_coord, _latitudeInDegrees, &j_latitude)
    && json_object_object_get_ex(j_coord, _longitudeInDegrees, &j_longitude)) {
        m_dest = std::make_pair(json_object_get_double(j_latitude), json_object_get_double(j_longitude));
    }
    else {
        AFB_WARNING("input data error.");
        return;
    }

    auto b_pair = std::make_pair<bool, bool>(false, false);
    if(HS_ClientManager::instance()->isAppStarted(std::string(_poi))) {
        b_pair.first = true;
        set_destination2poi(api);
    }
    else {
        this->addListenAppId(_poi);
        std::string id = HS_AppInfo::instance()->getAppProperty(_poi, _keyId);
        HS_AfmMainProxy afm_proxy;
        afm_proxy.start(api, id);
    }

    if(HS_ClientManager::instance()->isAppStarted(std::string(_navigation))) {
        b_pair.second = true;
        start_navigation(api);
    }
    else {
        this->addListenAppId(_navigation);
        std::string id = HS_AppInfo::instance()->getAppProperty(_navigation, _keyId);
        HS_AfmMainProxy afm_proxy;
        afm_proxy.start(api, id);
    }
    m_start_flg.swap(b_pair);
    if (!listenAppEmpty()) {
        HS_ClientManager::instance()->addListener(this);
    }
}

/**
 * cancel_navigation event handler
 *
 * #### Parameters
 *  - api : the api serving the reques
 *  - object : event json object
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::cancel_navigation(afb_api_t api, struct json_object *object)
{
    HS_ClientManager::instance()->pushEvent(_stopNavigation, nullptr);
}

/**
 * notify
 *
 * #### Parameters
 *  - api : the api
 *  - appid : application id
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::notify(afb_api_t api, std::string appid)
{
    if(isListenAppId(appid)) {
        if (appid == _poi) {
            m_start_flg.first = true;
            sleep(1);
            set_destination2poi(api);
        }
        else if(appid == _navigation) {
            m_start_flg.second = true;
            start_navigation(api);
        }
        else {
            AFB_WARNING("%s isn't interest app.", appid.c_str());
            return;
        }
    }
    if(m_start_flg.first && m_start_flg.second) {
        HS_ClientManager::instance()->removeListener(this);
        clearListenAppSet();
    }
}

/**
 * set destination to poiapp
 *
 * #### Parameters
 *  - api : the api
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::set_destination2poi(afb_api_t api)
{
    struct json_object *param = json_object_new_object();
    json_object_object_add(param, _latitudeInDegrees, json_object_new_double(m_dest.first));
    json_object_object_add(param, _longitudeInDegrees, json_object_new_double(m_dest.second));
    HS_ClientManager::instance()->pushEvent(_setDestination, param);
}

/**
 * set destination and start navigation
 *
 * #### Parameters
 *  - api : the ap
 *
 * #### Return
 * None
 *
 */
void Vui_Navigation::start_navigation(afb_api_t api)
{
    HS_ClientManager::instance()->pushEvent(_startNavigation, nullptr);
    HS_ClientManager::instance()->pushEvent("showWindow", nullptr, _navigation);
}

/* -------------------------------------HS_VuiAdapter------------------------------------------ */

HS_VuiAdapter* HS_VuiAdapter::me = nullptr;

/**
 * get instance
 *
 * #### Parameters
 *  - Nothing
 *
 * #### Return
 * HS_VuiAdapter instance pointer
 *
 */
HS_VuiAdapter* HS_VuiAdapter::instance(void)
{
    if(me == nullptr)
        me = new HS_VuiAdapter();

    return me;
}

/**
 * init
 *
 * #### Parameters
 *  - api : the api
 *
 * #### Return
 * None
 *
 */
void HS_VuiAdapter::init(afb_api_t api)
{
    if(afb_api_require_api(api, _vshl_capabilities, 0)) {
        AFB_INFO("%s api isn't existing.", _vshl_capabilities);
        return;
    }

    std::string uid = std::string(_vui_prefixe) + std::string("-") + _navigation;
    module_list[uid] = new Vui_Navigation(uid);

    for(auto &it : module_list) {
        it.second->init(api);
    }
}

/**
 * handle event
 *
 * #### Parameters
 *  - api : the api serving the request
 *  - event  : event name
 *  - object : event json object
 *
 * #### Return
 * 0 : continue transfer
 * 1 : blocked
 *
 */
int HS_VuiAdapter::onEvent(afb_api_t api, const char *event, struct json_object *object)
{
    for(auto &it : module_list) {
        if(it.second->onEvent(api, event, object))
            return 1;
    }
    return 0;
}