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("")
64 LightSensor() : m_intensity(0), m_name("")
77 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
78 const int &eCode, const int &sequenceNumber)
83 if (eCode == OC_STACK_OK)
85 std::cout << "OBSERVE RESULT:" << std::endl;
86 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
87 rep.getValue("on-off", mylight.m_on_off);
88 rep.getValue("color", mylight.m_color);
89 rep.getValue("dim", mylight.m_dim);
90 rep.getValue("name", mylight.m_name);
92 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
93 std::cout << "\tcolor: " << mylight.m_color << std::endl;
94 std::cout << "\tdim: " << mylight.m_dim << std::endl;
96 if (observe_count() > 10)
98 std::cout << "Cancelling Observe..." << std::endl;
99 OCStackResult result = curResource->cancelObserve();
101 std::cout << "Cancel result: " << result << std::endl;
103 std::cout << "DONE" << std::endl;
109 std::cout << "onObserve Response error: " << eCode << std::endl;
112 catch (std::exception &e)
114 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
120 void onLightIntensityObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
121 const int &eCode, const int &sequenceNumber)
126 if (eCode == OC_STACK_OK)
128 std::cout << "OBSERVE RESULT:" << std::endl;
129 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
132 std::cout << "\tintensity: " << rep.getValue<int>("intensity") << std::endl;
135 if (observe_count() > 10)
137 std::cout << "Cancelling Observe..." << std::endl;
138 OCStackResult result = curResource->cancelObserve();
140 std::cout << "Cancel result: " << result << std::endl;
142 std::cout << "DONE" << std::endl;
148 std::cout << "onObserve Response error: " << eCode << std::endl;
151 catch (std::exception &e)
153 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
158 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
163 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
165 std::cout << "POST request was successful" << std::endl;
167 if (rep.hasAttribute("createduri"))
169 std::cout << "\tUri of the created resource: "
170 << rep.getValue<std::string>("createduri") << std::endl;
174 rep.getValue("on-off", mylight.m_on_off);
175 rep.getValue("color", mylight.m_color);
176 rep.getValue("dim", mylight.m_dim);
178 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
179 std::cout << "\tcolor: " << mylight.m_color << std::endl;
180 std::cout << "\tdim: " << mylight.m_dim << std::endl;
183 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
184 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
185 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
186 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
188 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
193 std::cout << "onPost2 Response error: " << eCode << std::endl;
196 catch (std::exception &e)
198 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
203 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
208 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
210 std::cout << "POST request was successful" << std::endl;
212 if (rep.hasAttribute("createduri"))
214 std::cout << "\tUri of the created resource: "
215 << rep.getValue<std::string>("createduri") << std::endl;
219 rep.getValue("on-off", mylight.m_on_off);
220 rep.getValue("color", mylight.m_color);
221 rep.getValue("dim", mylight.m_dim);
223 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
224 std::cout << "\tcolor: " << mylight.m_color << std::endl;
225 std::cout << "\tdim: " << mylight.m_dim << std::endl;
228 OCRepresentation rep2;
230 std::cout << "Posting light representation..." << std::endl;
232 mylight.m_on_off = true;
234 rep2.setValue("on-off", mylight.m_on_off);
236 curResource->post(rep2, QueryParamsMap(), &onPost2);
240 std::cout << "onPost Response error: " << eCode << std::endl;
243 catch (std::exception &e)
245 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
249 // Local function to put a different state for this re<< std::endlsource
250 void postLightRepresentation(std::shared_ptr<OCResource> resource)
254 OCRepresentation rep;
256 std::cout << "Posting light representation..." << std::endl;
258 mylight.m_on_off = "false";
260 rep.setValue("on-off", mylight.m_on_off);
262 // Invoke resource's post API with rep, query map and the callback parameter
263 resource->post(rep, QueryParamsMap(), &onPost);
267 // callback handler on PUT request
268 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
274 if (eCode == OC_STACK_OK)
276 std::cout << "PUT request was successful" << std::endl;
278 /*rep.getValue("on-off", mylight.m_on_off);
279 rep.getValue("dim", mylight.m_dim);
280 rep.getValue("color", mylight.m_color);
282 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
283 std::cout << "\tcolor: " << mylight.m_color << std::endl;
284 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
286 //postLightRepresentation(curResource);
290 std::cout << "onPut Response error: " << eCode << std::endl;
293 catch (std::exception &e)
295 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
299 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
302 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
309 if (eCode == OC_STACK_OK)
311 std::cout << "PUT request was successful" << std::endl;
314 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
315 DISensorResource->get(test, &onGetForDISensor);
319 std::cout << "onPut Response error: " << eCode << std::endl;
322 catch (std::exception &e)
324 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
328 // Local function to put a different state for this resource
329 void putLightRepresentation(std::shared_ptr<OCResource> resource)
333 OCRepresentation rep;
335 std::cout << "Putting light representation..." << std::endl;
337 mylight.m_on_off = true;
339 std::cout << "Sending request to: " << resource->uri() << std::endl;
340 rep.setValue("on-off", mylight.m_on_off);
342 // Invoke resource's put API with rep, query map and the callback parameter
344 resource->put(rep, QueryParamsMap(), &onPut);
348 // Callback handler on GET request
349 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
354 if (eCode == OC_STACK_OK)
356 std::cout << "GET request was successful" << std::endl;
357 std::cout << "Resource URI: " << rep.getUri() << std::endl;
359 std::cout << "Payload: " << rep.getPayload() << std::endl;
360 std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
362 rep.getValue("on-off", mylight.m_on_off);
364 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
366 putLightRepresentation(curResource);
370 std::cout << "onGET Response error: " << eCode << std::endl;
373 catch (std::exception &e)
375 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
379 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
385 if (eCode == OC_STACK_OK)
387 std::cout << "GET request was successful" << std::endl;
388 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
390 std::cout << "Payload: " << rep.getPayload() << std::endl;
392 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
396 std::cout << "onGET Response error: " << eCode << std::endl;
399 catch (std::exception &e)
401 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
406 void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
412 if (eCode == OC_STACK_OK)
414 std::cout << "GET request was successful" << std::endl;
415 //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
417 std::cout << "Payload: " << rep.getPayload() << std::endl;
419 std::cout << "\tlightIntensity: " << rep.getValue<int>("intensity") << std::endl;
421 // iterating over all elements
422 OCRepresentation::const_iterator itr = rep.begin();
423 OCRepresentation::const_iterator endItr = rep.end();
425 for(;itr!=endItr;++itr)
427 std::cout << itr->attrname() << " ";
428 std::cout << itr->getValue<int>() << std::endl;
431 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onLightIntensityObserve);
435 std::cout << "onGET Response error: " << eCode << std::endl;
438 catch (std::exception &e)
440 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
444 // Local function to get representation of light resource
445 void getLightRepresentation(std::shared_ptr<OCResource> resource)
449 std::cout << "Getting Light Representation..." << std::endl;
450 // Invoke resource's get API with the callback parameter
453 std::cout << "Sending request to: " << resource->uri() << std::endl;
454 resource->get(test, &onGet);
458 void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
462 std::cout << "onGetDiscomfortIndex" << std::endl;
465 if (eCode == OC_STACK_OK)
467 std::cout << "GET request was successful" << std::endl;
469 std::cout << "Payload: " << rep.getPayload() << std::endl;
471 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
475 std::cout << "onGET Response error: " << eCode << std::endl;
478 catch (std::exception &e)
480 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
484 void onObserveDiscomfort(const HeaderOptions headerOptions, const OCRepresentation &rep,
485 const int &eCode, const int &sequenceNumber)
490 if (eCode == OC_STACK_OK)
492 std::cout << "OBSERVE RESULT:" << std::endl;
493 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
494 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
496 if (observe_count() > 10)
498 std::cout << "Cancelling Observe..." << std::endl;
499 OCStackResult result = curResource->cancelObserve();
501 std::cout << "Cancel result: " << result << std::endl;
503 std::cout << "DONE" << std::endl;
509 std::cout << "onObserve Response error: " << eCode << std::endl;
512 catch (std::exception &e)
514 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
519 // Local function to get representation of light resource
520 void getDiscomfortRepresentation(std::shared_ptr<OCResource> resource)
524 std::cout << "Getting Discomfort Representation..." << std::endl;
525 // Invoke resource's get API with the callback parameter
528 std::cout << "Sending request to: " << resource->uri() << std::endl;
529 resource->get(test, &onGetDiscomfortIndex);
530 //resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
537 // Local function to get representation of light resource
538 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
542 std::cout << "Getting Light Representation..." << std::endl;
543 // Invoke resource's get API with the callback parameter
546 std::cout << "Sending request to: " << resource->uri() << std::endl;
547 resource->get(test, &onGetForLightIntensitySensor);
551 // Callback to found resources
552 void foundResource(std::shared_ptr<OCResource> resource)
554 std::cout << "In foundResource\n";
555 std::string resourceURI = resource->uri();
556 std::string hostAddress;
561 // Do some operations with resource object.
564 std::cout << "DISCOVERED Resource:" << std::endl;
565 // Get the resource URI
566 resourceURI = resource->uri();
567 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
569 // Get the resource host address
570 hostAddress = resource->host();
571 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
573 // Get the resource types
574 std::cout << "\tList of resource types: " << std::endl;
575 for (auto &resourceTypes : resource->getResourceTypes())
577 std::cout << "\t\t" << resourceTypes << std::endl;
578 /*if (resourceTypes == "oic.r.light")
580 curResource = resource;
581 // Call a local function which will internally invoke get API on the resource pointer
582 getLightRepresentation(resource);
584 if (resourceTypes == "oic.r.discomfortindex")
586 curResource = resource;
587 std::cout << "\t\tGet discomfort representation " << std::endl;
588 // Call a local function which will internally invoke get API on the resource pointer
589 getDiscomfortRepresentation(resource);
593 // Get the resource interfaces
594 std::cout << "\tList of resource interfaces: " << std::endl;
595 for (auto &resourceInterfaces : resource->getResourceInterfaces())
597 std::cout << "\t\t" << resourceInterfaces << std::endl;
602 // Resource is invalid
603 std::cout << "Resource is invalid" << std::endl;
607 catch (std::exception &e)
609 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
615 std::cout << std::endl;
616 std::cout << "---------------------------------------------------------------------\n";
617 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
618 std::cout << " ObserveType : 1 - Observe" << std::endl;
619 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
620 std::cout << "---------------------------------------------------------------------\n\n";
623 void checkObserverValue(int value)
627 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
628 std::cout << "<===Setting ObserveType to Observe===>\n\n";
632 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
633 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
637 std::cout << "<===Invalid ObserveType selected."
638 << " Setting ObserveType to Observe===>\n\n";
642 static FILE *client_open(const char *path, const char *mode)
646 return fopen("./oic_svr_db_client.json", mode);
649 int main(int argc, char *argv[])
652 std::ostringstream requestURI;
653 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
659 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
663 checkObserverValue(std::stoi(argv[1]));
667 std::cout << "<===Invalid number of command line arguments===>\n\n";
671 catch (std::exception &)
673 std::cout << "<===Invalid input arguments===>\n\n";
677 // Create PlatformConfig object
680 OC::ServiceType::InProc,
684 OC::QualityOfService::LowQos,
688 OCPlatform::Configure(cfg);
691 // makes it so that all boolean values are printed as 'true/false' in this stream
692 std::cout.setf(std::ios::boolalpha);
693 // Find all resources
694 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
696 OCPlatform::findResource("", requestURI.str(),
697 CT_DEFAULT, &foundResource);
698 std::cout << "Finding Resource... " << std::endl;
700 // Find resource is done twice so that we discover the original resources a second time.
701 // These resources will have the same uniqueidentifier (yet be different objects), so that
702 // we can verify/show the duplicate-checking code in foundResource(above);
703 OCPlatform::findResource("", requestURI.str(),
704 CT_DEFAULT, &foundResource);
705 std::cout << "Finding Resource for second time..." << std::endl;
707 // A condition variable will free the mutex it is given, then do a non-
708 // intensive block until 'notify' is called on it. In this case, since we
709 // don't ever call cv.notify, this should be a non-processor intensive version
712 std::condition_variable cv;
713 std::unique_lock<std::mutex> lock(blocker);
717 catch (OCException &e)
719 oclog() << "Exception in main: " << e.what();