add autobuild tree
[apps/agl-service-navigation.git] / libnavi / src / JsonResponseAnalyzer.cpp
1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
3
4 #include <traces.h>
5
6 #include "JsonResponseAnalyzer.h"
7
8 /**
9  *  @brief Response analysis of navicore_getallroutes
10  *  @param res_json JSON string of response
11  *  @return Map information for the key sent in the request
12  */
13 std::map< int32_t, naviapi::variant > JsonResponseAnalyzer::AnalyzeResponseGetPosition( std::string& res_json )
14 {
15         std::map< int32_t, naviapi::variant > ret;
16
17         TRACE_DEBUG("AnalyzeResponseGetPosition json_obj:\n%s\n", json_object_to_json_string(json_obj));
18
19         // convert to Json Object
20         struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
21
22         // Check key
23         struct json_object *json_map_ary = NULL;
24         if( json_object_object_get_ex(json_obj, "response", &json_map_ary) )
25         {
26                 // Check if the response is array information
27                 if( json_object_is_type(json_map_ary, json_type_array) )
28                 {
29                         for (int i = 0; i < json_object_array_length(json_map_ary); ++i) 
30                         {
31                                 struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i);
32                 
33                                 if( json_object_is_type( j_elem, json_type_object) )
34                                 {
35                                         // Check key
36                                         struct json_object* key = NULL;
37                                         struct json_object* value = NULL;
38                                         if( json_object_object_get_ex(j_elem, "key", &key) 
39                                             &&  json_object_object_get_ex(j_elem, "value", &value) )
40                                         {
41                                                 if( json_object_is_type(key, json_type_int) )
42                                                 {
43                                                         uint32_t req_key = (uint32_t)json_object_get_int(key);
44
45                                                         switch( req_key )
46                                                         {
47                                                         case naviapi::NAVICORE_LATITUDE:
48                                                                 ret[req_key]._double = json_object_get_double(value);
49                                                                 break;
50
51                                                         case naviapi::NAVICORE_LONGITUDE:
52                                                                 ret[req_key]._double = json_object_get_double(value);
53                                                                 break;
54
55                                                         case naviapi::NAVICORE_TIMESTAMP:
56                                                                 ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value);
57                                                                 break;
58
59                                                         case naviapi::NAVICORE_HEADING:
60                                                                 ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value);
61                                                                 break;
62
63                                                         case naviapi::NAVICORE_SPEED:
64                                                                 ret[req_key]._int32_t = json_object_get_int(value);
65                                                                 break;
66
67                                                         case naviapi::NAVICORE_SIMULATION_MODE:
68                                                                 ret[req_key]._bool = json_object_get_boolean(value);
69                                                                 break;
70
71                                                         default:
72                                                                 TRACE_WARN("unknown key type.\n");
73                                                                 break;
74                                                         }
75                                                 }
76                                         }
77                                 }
78                                 else
79                                 {
80                                         TRACE_WARN("element type is not object.\n");
81                                         break;
82                                 }
83                         }
84                 }
85                 else
86                 {
87                         TRACE_WARN("response type is not array.\n");
88                 }
89         }
90
91         json_object_put(json_obj);
92         return ret;
93 }
94
95 /**
96  *  @brief Response analysis of navicore_getallroutes
97  *  @param res_json JSON string of response
98  *  @return Route handle array
99  */
100 std::vector< uint32_t > JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes( std::string& res_json )
101 {
102         std::vector< uint32_t > routeList;
103
104         TRACE_DEBUG("AnalyzeResponseGetAllRoutes json_obj:\n%s\n", json_object_to_json_string(json_obj));
105
106         // convert to Json Object
107         struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
108
109         // Check key
110         struct json_object *json_route_ary = NULL;
111         if( json_object_object_get_ex(json_obj, "response", &json_route_ary) )
112         {
113                 // Check if the response is array information
114                 if( json_object_is_type(json_route_ary, json_type_array) )
115                 {
116                         for (int i = 0; i < json_object_array_length(json_route_ary); ++i) 
117                         {
118                                 struct json_object* j_elem = json_object_array_get_idx(json_route_ary, i);
119
120                                 // Check that it is an element
121                                 struct json_object *json_route_obj = NULL;
122                                 if( json_object_object_get_ex(j_elem, "route", &json_route_obj) )
123                                 {
124                                         // Check if it is an int value
125                                         if( json_object_is_type(json_route_obj, json_type_int) )
126                                         {
127                                                 uint32_t routeHandle = (uint32_t)json_object_get_int(json_route_obj);
128                                                 routeList.push_back( routeHandle );
129                                         }
130                                         else
131                                         {
132                                                 TRACE_WARN("route value is not integer.\n");
133                                                 break;
134                                         }
135                                 }
136                                 else
137                                 {
138                                         TRACE_WARN("key route is not found.\n");
139                                         break;
140                                 }
141                         }
142                 }
143                 else
144                 {
145                         TRACE_WARN("response type is not array.\n");
146                 }
147         }
148
149         json_object_put(json_obj);
150         return routeList;
151 }
152
153 /**
154  *  @brief Response analysis of navicore_createroute
155  *  @param res_json JSON string of response
156  *  @return Route handle
157  */
158 uint32_t JsonResponseAnalyzer::AnalyzeResponseCreateRoute( std::string& res_json )
159 {
160         uint32_t routeHandle = 0;
161
162         TRACE_DEBUG("AnalyzeResponseCreateRoute json_obj:\n%s\n", json_object_to_json_string(json_obj));
163
164         // convert to Json Object
165         struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
166
167         // Check key
168         struct json_object *json_root_obj = NULL;
169         struct json_object *json_route_obj = NULL;
170         if( json_object_object_get_ex(json_obj, "response", &json_root_obj)
171             &&  json_object_object_get_ex(json_root_obj, "route", &json_route_obj) )
172         {
173                 // Check if the response is array information
174                 if( json_object_is_type(json_route_obj, json_type_int) )
175                 {
176                         // Get route handle
177                         routeHandle = (uint32_t)json_object_get_int(json_route_obj);
178                 }
179                 else
180                 {
181                         TRACE_WARN("response type is not integer.\n");
182                 }
183         }
184
185         json_object_put(json_obj);
186         return routeHandle;
187 }
188
189 /**
190  *  @brief Response analysis of navicore_getallsessions
191  *  @param res_json JSON string of response
192  *  @return Map of session information
193  */
194 std::map<uint32_t, std::string> JsonResponseAnalyzer::AnalyzeResponseGetAllSessions( std::string& res_json )
195 {
196         std::map<uint32_t, std::string> session_map;
197
198         TRACE_DEBUG("AnalyzeResponseGetAllSessions json_obj:\n%s\n", json_object_to_json_string(json_obj));
199
200         // convert to Json Object
201         struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
202
203         // Check key
204         struct json_object *json_map_ary = NULL;
205         if( json_object_object_get_ex(json_obj, "response", &json_map_ary) )
206         {
207                 // Check if the response is array information
208                 if( json_object_is_type(json_map_ary, json_type_array) )
209                 {
210                         for (int i = 0; i < json_object_array_length(json_map_ary); ++i) 
211                         {
212                                 struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i);
213
214                                 if( json_object_is_type( j_elem, json_type_object) )
215                                 {
216                                         // Check key
217                                         struct json_object* handle = NULL;
218                                         struct json_object* client = NULL;
219                                         if( json_object_object_get_ex(j_elem, "sessionHandle", &handle) 
220                                             &&  json_object_object_get_ex(j_elem, "client", &client) )
221                                         {
222                                                 if( json_object_is_type(handle, json_type_int)
223                                                     &&  json_object_is_type(client, json_type_string) )
224                                                 {
225                                                         uint32_t sessionHandle = (uint32_t)json_object_get_int(handle);
226                                                         std::string clientName = std::string( json_object_get_string(client) );
227
228                                                         // add to map
229                                                         session_map[sessionHandle] = clientName;
230                                                 }
231                                                 else
232                                                 {
233                                                         TRACE_WARN("invalid response.\n");
234                                                         break;
235                                                 }
236                                         }
237                                 }
238                                 else
239                                 {
240                                         TRACE_WARN("element type is not object.\n");
241                                         break;
242                                 }
243                         }
244                 }
245                 else
246                 {
247                         TRACE_WARN("response type is not array.\n");
248                 }
249         }
250     
251         json_object_put(json_obj);
252         return session_map;
253 }
254