1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // OCClient.cpp : Defines the entry point for the console application.
28 #include <condition_variable>
29 #include "OCPlatform.h"
34 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
36 DiscoveredResourceMap discoveredResources;
37 std::shared_ptr<OCResource> curResource;
38 std::shared_ptr<OCResource> DISensorResource;
39 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
40 std::mutex curResourceLock;
52 Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
65 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
66 const int &eCode, const int &sequenceNumber)
71 if (eCode == OC_STACK_OK)
73 std::cout << "OBSERVE RESULT:" << std::endl;
74 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
75 rep.getValue("on-off", mylight.m_on_off);
76 rep.getValue("color", mylight.m_color);
77 rep.getValue("dim", mylight.m_dim);
78 rep.getValue("name", mylight.m_name);
80 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
81 std::cout << "\tcolor: " << mylight.m_color << std::endl;
82 std::cout << "\tdim: " << mylight.m_dim << std::endl;
84 if (observe_count() > 10)
86 std::cout << "Cancelling Observe..." << std::endl;
87 OCStackResult result = curResource->cancelObserve();
89 std::cout << "Cancel result: " << result << std::endl;
91 std::cout << "DONE" << std::endl;
97 std::cout << "onObserve Response error: " << eCode << std::endl;
100 catch (std::exception &e)
102 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
107 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
112 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
114 std::cout << "POST request was successful" << std::endl;
116 if (rep.hasAttribute("createduri"))
118 std::cout << "\tUri of the created resource: "
119 << rep.getValue<std::string>("createduri") << std::endl;
123 rep.getValue("on-off", mylight.m_on_off);
124 rep.getValue("color", mylight.m_color);
125 rep.getValue("dim", mylight.m_dim);
127 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
128 std::cout << "\tcolor: " << mylight.m_color << std::endl;
129 std::cout << "\tdim: " << mylight.m_dim << std::endl;
132 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
133 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
134 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
135 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
137 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
142 std::cout << "onPost2 Response error: " << eCode << std::endl;
145 catch (std::exception &e)
147 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
152 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
157 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
159 std::cout << "POST request was successful" << std::endl;
161 if (rep.hasAttribute("createduri"))
163 std::cout << "\tUri of the created resource: "
164 << rep.getValue<std::string>("createduri") << std::endl;
168 rep.getValue("on-off", mylight.m_on_off);
169 rep.getValue("color", mylight.m_color);
170 rep.getValue("dim", mylight.m_dim);
172 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
173 std::cout << "\tcolor: " << mylight.m_color << std::endl;
174 std::cout << "\tdim: " << mylight.m_dim << std::endl;
177 OCRepresentation rep2;
179 std::cout << "Posting light representation..." << std::endl;
181 mylight.m_on_off = true;
183 rep2.setValue("on-off", mylight.m_on_off);
185 curResource->post(rep2, QueryParamsMap(), &onPost2);
189 std::cout << "onPost Response error: " << eCode << std::endl;
192 catch (std::exception &e)
194 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
198 // Local function to put a different state for this re<< std::endlsource
199 void postLightRepresentation(std::shared_ptr<OCResource> resource)
203 OCRepresentation rep;
205 std::cout << "Posting light representation..." << std::endl;
207 mylight.m_on_off = "false";
209 rep.setValue("on-off", mylight.m_on_off);
211 // Invoke resource's post API with rep, query map and the callback parameter
212 resource->post(rep, QueryParamsMap(), &onPost);
216 // callback handler on PUT request
217 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
223 if (eCode == OC_STACK_OK)
225 std::cout << "PUT request was successful" << std::endl;
227 /*rep.getValue("on-off", mylight.m_on_off);
228 rep.getValue("dim", mylight.m_dim);
229 rep.getValue("color", mylight.m_color);
231 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
232 std::cout << "\tcolor: " << mylight.m_color << std::endl;
233 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
235 //postLightRepresentation(curResource);
239 std::cout << "onPut Response error: " << eCode << std::endl;
242 catch (std::exception &e)
244 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
248 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
251 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
258 if (eCode == OC_STACK_OK)
260 std::cout << "PUT request was successful" << std::endl;
263 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
264 DISensorResource->get(test, &onGetForDISensor);
268 std::cout << "onPut Response error: " << eCode << std::endl;
271 catch (std::exception &e)
273 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
277 // Local function to put a different state for this resource
278 void putLightRepresentation(std::shared_ptr<OCResource> resource)
282 OCRepresentation rep;
284 std::cout << "Putting light representation..." << std::endl;
286 mylight.m_on_off = true;
288 std::cout << "Sending request to: " << resource->uri() << std::endl;
289 rep.setValue("on-off", mylight.m_on_off);
291 // Invoke resource's put API with rep, query map and the callback parameter
293 resource->put(rep, QueryParamsMap(), &onPut);
297 // Callback handler on GET request
298 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
303 if (eCode == OC_STACK_OK)
305 std::cout << "GET request was successful" << std::endl;
306 std::cout << "Resource URI: " << rep.getUri() << std::endl;
308 std::cout << "Payload: " << rep.getPayload() << std::endl;
309 std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
311 rep.getValue("on-off", mylight.m_on_off);
313 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
315 putLightRepresentation(curResource);
319 std::cout << "onGET Response error: " << eCode << std::endl;
322 catch (std::exception &e)
324 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
328 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
334 if (eCode == OC_STACK_OK)
336 std::cout << "GET request was successful" << std::endl;
337 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
339 std::cout << "Payload: " << rep.getPayload() << std::endl;
341 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
345 std::cout << "onGET Response error: " << eCode << std::endl;
348 catch (std::exception &e)
350 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
354 // Local function to get representation of light resource
355 void getLightRepresentation(std::shared_ptr<OCResource> resource)
359 std::cout << "Getting Light Representation..." << std::endl;
360 // Invoke resource's get API with the callback parameter
363 std::cout << "Sending request to: " << resource->uri() << std::endl;
364 resource->get(test, &onGet);
368 // Callback to found resources
369 void foundResource(std::shared_ptr<OCResource> resource)
371 std::cout << "In foundResource\n";
372 std::string resourceURI = resource->uri();
373 std::string hostAddress;
378 // Do some operations with resource object.
381 std::cout << "DISCOVERED Resource:" << std::endl;
382 // Get the resource URI
383 resourceURI = resource->uri();
384 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
386 // Get the resource host address
387 hostAddress = resource->host();
388 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
390 // Get the resource types
391 std::cout << "\tList of resource types: " << std::endl;
392 for (auto &resourceTypes : resource->getResourceTypes())
394 std::cout << "\t\t" << resourceTypes << std::endl;
395 if (resourceTypes == "oic.r.light")
397 curResource = resource;
398 // Call a local function which will internally invoke get API on the resource pointer
399 getLightRepresentation(resource);
403 // Get the resource interfaces
404 std::cout << "\tList of resource interfaces: " << std::endl;
405 for (auto &resourceInterfaces : resource->getResourceInterfaces())
407 std::cout << "\t\t" << resourceInterfaces << std::endl;
412 // Resource is invalid
413 std::cout << "Resource is invalid" << std::endl;
417 catch (std::exception &e)
419 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
425 std::cout << std::endl;
426 std::cout << "---------------------------------------------------------------------\n";
427 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
428 std::cout << " ObserveType : 1 - Observe" << std::endl;
429 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
430 std::cout << "---------------------------------------------------------------------\n\n";
433 void checkObserverValue(int value)
437 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
438 std::cout << "<===Setting ObserveType to Observe===>\n\n";
442 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
443 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
447 std::cout << "<===Invalid ObserveType selected."
448 << " Setting ObserveType to Observe===>\n\n";
452 static FILE *client_open(const char *path, const char *mode)
456 return fopen("./oic_svr_db_client.json", mode);
459 int main(int argc, char *argv[])
462 std::ostringstream requestURI;
463 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
469 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
473 checkObserverValue(std::stoi(argv[1]));
477 std::cout << "<===Invalid number of command line arguments===>\n\n";
481 catch (std::exception &)
483 std::cout << "<===Invalid input arguments===>\n\n";
487 // Create PlatformConfig object
490 OC::ServiceType::InProc,
494 OC::QualityOfService::LowQos,
498 OCPlatform::Configure(cfg);
501 // makes it so that all boolean values are printed as 'true/false' in this stream
502 std::cout.setf(std::ios::boolalpha);
503 // Find all resources
504 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
506 OCPlatform::findResource("", requestURI.str(),
507 CT_DEFAULT, &foundResource);
508 std::cout << "Finding Resource... " << std::endl;
510 // Find resource is done twice so that we discover the original resources a second time.
511 // These resources will have the same uniqueidentifier (yet be different objects), so that
512 // we can verify/show the duplicate-checking code in foundResource(above);
513 OCPlatform::findResource("", requestURI.str(),
514 CT_DEFAULT, &foundResource);
515 std::cout << "Finding Resource for second time..." << std::endl;
517 // A condition variable will free the mutex it is given, then do a non-
518 // intensive block until 'notify' is called on it. In this case, since we
519 // don't ever call cv.notify, this should be a non-processor intensive version
522 std::condition_variable cv;
523 std::unique_lock<std::mutex> lock(blocker);
527 catch (OCException &e)
529 oclog() << "Exception in main: " << e.what();