summaryrefslogtreecommitdiff
path: root/test/freetextsearch-service/main.cpp
blob: 92405775c08f45b2bfc73ea6cfb6ba0f97414a20 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2015, 2016 TomTom International B.V.
* \copyright Copyright (C) 2016, PCA Peugeot Citroen
* \author Peter Goedegebure (Peter.Goedegebure@tomtom.com)
* \author Philippe Colliot <philippe.colliot@mpsa.com>
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
* <date>, <name>, <description of change>
*
* @licence end@
*/
#include <iostream>
#include <unistd.h>

#include <CommonAPI/CommonAPI.hpp>
#include <org/genivi/CommonTypes.hpp>
#include <org/genivi/navigation/NavigationTypes.hpp>
#include <v0/org/genivi/navigation/freetextsearchservice/FreeTextSearchProxy.hpp>

using namespace org::genivi;
using namespace org::genivi::navigation;
using namespace v0::org::genivi::navigation::freetextsearchservice;

// Sequence:
// - get version info synchronous
// - start asynchronous request
//   - when done is received (pageDone), request next page.
//     - when done is received (pageDone), cancel the request and free the locationHandles.
//       - when responses are received (cancelDone, freeHandleDone), we're done.
//
bool pageDone = false;
bool cancelDone = false;
bool freeHandlesDone = false;

FreeTextSearch::Addresses resultAddresses;
FreeTextSearch::POIs resultPois;

void printAddress(FreeTextSearch::Address address) {
    std::cout << "Address: ";
    std::cout << "score=" << address.getScore();
    std::cout << ", countryCode=" << address.getCountryCode();
    std::cout << ", stateCode=" << address.getStateCode();
    std::cout << ", mapCode=" << address.getMapCode();
    std::cout << ", places=";
    FreeTextSearch::FtsStringList places = address.getPlaces();
    bool first = true;
    for (unsigned int index = 0; index < places.size(); index++) {
    	if (first) {
    		first = false;
    	} else {
    		std::cout << ",";
    	}
    	std::cout << places.at(index);
    }
    FreeTextSearch::AddressDetails addressDetails = address.getAddressDetails();
    if (addressDetails.isType<FreeTextSearch::StreetDetails>()) {
    	FreeTextSearch::StreetDetails streetDetails = addressDetails.get<FreeTextSearch::StreetDetails>();
    	std::cout << ", streetName=" << streetDetails.getStreetName();
    	std::cout << ", houseNumber=" << streetDetails.getHouseNumber();
    	std::cout << ", (fromInput=" << streetDetails.getHouseNumberFromInput() << ")";
    }

    std::cout << ", postalCodes=";
    FreeTextSearch::FtsStringList postalCodes = address.getPostalCodes();
    first = true;
    for (unsigned int index = 0; index < postalCodes.size(); index++) {
    	if (first) {
    		first = false;
    	} else {
    		std::cout << ",";
    	}
    	std::cout << postalCodes.at(index);
    }

    NavigationTypes::Coordinate2D coordinate = address.getCoordinate();
    std::cout << ", coordinate=(" << coordinate.getLatitude() << "," << coordinate.getLongitude() << ")";
    std::cout << ", distance=" << address.getDistance();
    if (address.getFuzzyMatch()) {
    	std::cout << ", fuzzy";
    }
    std::cout << ", locationHandle=" << address.getLocationHandle();
    std::cout << std::endl;
}

void printPoi(FreeTextSearch::POI poi) {
    std::cout << "POI: ";
    std::cout << "poiName=" << poi.getPoiName();
    std::cout << ", brandNames=";
    FreeTextSearch::FtsStringList brandNames = poi.getBrandNames();
    bool first = true;
    for (unsigned int index = 0; index < brandNames.size(); index++) {
    	if (first) {
    		first = false;
    	} else {
    		std::cout << ",";
    	}
    	std::cout << brandNames.at(index);
    }
    std::cout << ", categoryCode=" << poi.getCategoryCode();
    std::cout << ", countryCode=" << poi.getCountryCode();
    std::cout << ", stateCode=" << poi.getStateCode();
    std::cout << ", mapCode=" << poi.getMapCode();
    std::cout << ", places=";
    FreeTextSearch::FtsStringList places = poi.getPlace();
    first = true;
    for (unsigned int index = 0; index < places.size(); index++) {
    	if (first) {
    		first = false;
    	} else {
    		std::cout << ",";
    	}
    	std::cout << places.at(index);
    }
    std::cout << ", postalCodes=";
    FreeTextSearch::FtsStringList postalCodes = poi.getPostalCode();
    first = true;
    for (unsigned int index = 0; index < postalCodes.size(); index++) {
    	if (first) {
    		first = false;
    	} else {
    		std::cout << ",";
    	}
    	std::cout << postalCodes.at(index);
    }
    std::cout << ", address=" << poi.getAddress();
    std::cout << ", telephone=" << poi.getTelephone();

    NavigationTypes::Coordinate2D coordinate = poi.getCoordinate();
    std::cout << ", coordinate=(" << coordinate.getLatitude() << "," << coordinate.getLongitude() << ")";
    std::cout << ", locationHandle=" << poi.getLocationHandle();
    std::cout << std::endl;
}

void printAllResults(std::string title) {
	std::cout << std::endl;
	std::cout << title << " - current results:" << std::endl;

	// TODO merge Addresses and POIs based on score. For first print all addresses and then all POIs.
	for (unsigned int index=0; index < resultAddresses.size(); ++index) {
		printAddress(resultAddresses.at(index));
	}

	for (unsigned int index=0; index < resultPois.size(); ++index) {
		printPoi(resultPois.at(index));
	}

	std::cout << std::endl;
}

void ftsRequestCallback(const CommonAPI::CallStatus& callStatus,
		const FreeTextSearch::ftsRequestError& error, const FreeTextSearch::RequestId& responseId,
		const NavigationTypes::Handle& freeTextSearchHandle) {
    std::cout << "   Result of ftsRequest (asynchronous)" << error << std::endl;
    std::cout << "   callStatus: " << ((callStatus == CommonAPI::CallStatus::SUCCESS) ? "SUCCESS" : "NO_SUCCESS")
              << std::endl;
    std::cout << "   error = " << error << std::endl;
    std::cout << "   responseId = " << responseId << std::endl;
    std::cout << "   freeTextSearchHandle = " << freeTextSearchHandle << std::endl;
}

void ftsNextPageCallback(const CommonAPI::CallStatus& callStatus,
		const FreeTextSearch::RequestId& responseId) {
    std::cout << "   Result of ftsNextPage (asynchronous)" << std::endl;
    std::cout << "   callStatus: " << ((callStatus == CommonAPI::CallStatus::SUCCESS) ? "SUCCESS" : "NO_SUCCESS")
              << std::endl;
    std::cout << "   responseId = " << responseId << std::endl;
}

void ftsCancelCallback(const CommonAPI::CallStatus& callStatus,
		const FreeTextSearch::RequestId& responseId) {
    std::cout << "   Result of ftsCancel (asynchronous)" << std::endl;
    std::cout << "   callStatus: " << ((callStatus == CommonAPI::CallStatus::SUCCESS) ? "SUCCESS" : "NO_SUCCESS")
              << std::endl;
    std::cout << "   responseId = " << responseId << std::endl;

    cancelDone = true;
}

void deleteLocationHandlesCallback(const CommonAPI::CallStatus& callStatus,
		const FreeTextSearch::RequestId& responseId) {
    std::cout << "   Result of deleteLocationHandles (asynchronous)" << std::endl;
    std::cout << "   callStatus: " << ((callStatus == CommonAPI::CallStatus::SUCCESS) ? "SUCCESS" : "NO_SUCCESS")
              << std::endl;
    std::cout << "   responseId = " << responseId << std::endl;

    freeHandlesDone = true;
}

int main() {
    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::get();

    std::string domain = "local";
	std::string instance = "org.genivi.navigation.freetextsearchservice";

    std::shared_ptr<FreeTextSearchProxyDefault> myProxy = runtime->buildProxy < FreeTextSearchProxy > (domain, instance);

    while (!myProxy->isAvailable()) {
        usleep(10);
    }

    /*
     * Subscribe to broadcasts
     */
    myProxy->getFtsResultAddressesSelectiveEvent().subscribe([&](FreeTextSearch::RequestId responseId, FreeTextSearch::Addresses addresses, bool moreAvailable){
        std::cout << "Received ftsResultAddresses, responseId = " << responseId << std::endl;
        std::cout << "moreAvailable = " << moreAvailable << std::endl;

        for (unsigned int index=0; index < addresses.size(); ++index) {
        	resultAddresses.push_back(addresses.at(index));
        }

        printAllResults("New addresses received");
    });

    myProxy->getFtsResultPoisSelectiveEvent().subscribe([&](FreeTextSearch::RequestId responseId, FreeTextSearch::POIs pois, bool moreAvailable){
        std::cout << "Received ftsResultPois, responseId = " << responseId << std::endl;
        std::cout << "moreAvailable = " << moreAvailable << std::endl;

        for (unsigned int index=0; index < pois.size(); ++index) {
        	resultPois.push_back(pois.at(index));
        }

        printAllResults("New POIs received");
    });

    myProxy->getFtsDoneSelectiveEvent().subscribe([&](FreeTextSearch::RequestId responseId, FreeTextSearch::FtsStatus ftsStatus) {
        std::cout << "Received ftsDone, responseId = " << responseId << std::endl;
        std::cout << "ftsStatus = " << ftsStatus << std::endl;

        pageDone = true;
    });

    FreeTextSearch::RequestId requestId = 0;
    FreeTextSearch::RequestId responseId;
    CommonAPI::CallStatus callStatus;

    // Get the API version info.
    CommonTypes::Version version;
    std::cout << "Call getVersion (synchronous), requesId = " << requestId << std::endl;
    myProxy->getVersion(requestId, callStatus, responseId, version);
    std::cout << "   callStatus: " << ((callStatus == CommonAPI::CallStatus::SUCCESS) ? "SUCCESS" : "NO_SUCCESS")
              << std::endl;
    std::cout << "   responseId = " << responseId << std::endl;
    std::cout << "   version = " << version.getVersionMajor() << "." << version.getVersionMinor()
    		  << "." << version.getVersionMicro() << " (" << version.getDate() << ")" << std::endl;

    requestId++;

    // Clear results
    resultAddresses.clear();
    resultPois.clear();

    // Search parameters
    FreeTextSearch::FtsString inputString = "lucht";
    NavigationTypes::Coordinate2D* searchLocation = new NavigationTypes::Coordinate2D(48.053250, 8.324500);
    FreeTextSearch::ShapeList searchShapes;
    FreeTextSearch::PageSize pageSize = 20;
    FreeTextSearch::SearchOptions searchOptions = FreeTextSearch::SearchOption::ADDRESS;
    FreeTextSearch::FtsString searchConditions = "";
    FreeTextSearch::FuzzyLevel fuzzyLevel = 5;
    // TODO At least the synchronous call always has the 'error' parameter. So there has to be a 'NO ERROR' value defined.
    FreeTextSearch::ftsRequestError error;
    NavigationTypes::Handle freeTextSearchHandle;

    pageDone = false;
    std::cout << "Call ftsRequest (asynchronous), requesId = " << requestId << std::endl;
    std::function<void(const CommonAPI::CallStatus&,
    		           const FreeTextSearch::ftsRequestError&, const FreeTextSearch::RequestId&,
    		    		const NavigationTypes::Handle&)> ftsRequestCallbackFunction = ftsRequestCallback;
    myProxy->ftsRequestAsync(requestId, inputString, *searchLocation, searchShapes, pageSize, searchOptions, searchConditions, fuzzyLevel,
    		                 ftsRequestCallbackFunction);

    while (!pageDone) {
        std::cout << "Waiting for first page results." << std::endl;
        usleep(50000);
    }

    requestId++;
    pageDone = false;
    std::cout << "Call ftsNext (asynchronous), requesId = " << requestId << std::endl;
    std::function<void(const CommonAPI::CallStatus&,
    		           const FreeTextSearch::RequestId&)> ftsNextPageCallbackFunction = ftsNextPageCallback;
    myProxy->ftsNextPageAsync(requestId, freeTextSearchHandle, searchOptions, ftsNextPageCallbackFunction);

    while (!pageDone) {
        std::cout << "Waiting for second page results." << std::endl;
        usleep(50000);
    }

    requestId++;
    cancelDone = false;
    freeHandlesDone = false;
    std::cout << "Call ftsCancel (asynchronous), requesId = " << requestId << std::endl;
    std::function<void(const CommonAPI::CallStatus&,
    		           const FreeTextSearch::RequestId&)> ftsCancelCallbackFunction = ftsCancelCallback;
    myProxy->ftsCancelAsync(requestId, freeTextSearchHandle, ftsCancelCallbackFunction);

    requestId++;
    std::cout << "Call deleteLocationHandles (asynchronous), requesId = " << requestId << std::endl;
    std::function<void(const CommonAPI::CallStatus&,
    		           const FreeTextSearch::RequestId&)> deleteLocationHandlesCallbackFunction = deleteLocationHandlesCallback;
    FreeTextSearch::LocationHandleList locationHandleList;
    myProxy->deleteLocationHandlesAsync(requestId, locationHandleList, deleteLocationHandlesCallbackFunction);


    while (!(cancelDone && freeHandlesDone)) {
        std::cout << "Waiting for cancel and deleteHandles to finish." << std::endl;
        usleep(50000);
    }


//    while (true) {
//        std::this_thread::sleep_for(std::chrono::seconds(5));
//    }

    return 0;
}