1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include <condition_variable>
28 #include "OCPlatform.h"
33 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
35 DiscoveredResourceMap discoveredResources;
36 std::shared_ptr<OCResource> curResource;
37 std::shared_ptr<OCResource> DISensorResource;
38 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 std::mutex curResourceLock;
51 Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
63 LightSensor() : m_intensity(0), m_name("")
76 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
77 const int &eCode, const int &sequenceNumber)
82 if (eCode == OC_STACK_OK)
84 std::cout << "OBSERVE RESULT:" << std::endl;
85 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
86 rep.getValue("on-off", mylight.m_on_off);
87 rep.getValue("color", mylight.m_color);
88 rep.getValue("dim", mylight.m_dim);
89 rep.getValue("name", mylight.m_name);
91 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
92 std::cout << "\tcolor: " << mylight.m_color << std::endl;
93 std::cout << "\tdim: " << mylight.m_dim << std::endl;
95 if (observe_count() > 10)
97 std::cout << "Cancelling Observe..." << std::endl;
98 OCStackResult result = curResource->cancelObserve();
100 std::cout << "Cancel result: " << result << std::endl;
102 std::cout << "DONE" << std::endl;
108 std::cout << "onObserve Response error: " << eCode << std::endl;
111 catch (std::exception &e)
113 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
119 void onLightIntensityObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
120 const int &eCode, const int &sequenceNumber)
125 if (eCode == OC_STACK_OK)
127 std::cout << "OBSERVE RESULT:" << std::endl;
128 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
131 std::cout << "\tintensity: " << rep.getValue<int>("intensity") << std::endl;
134 if (observe_count() > 10)
136 std::cout << "Cancelling Observe..." << std::endl;
137 OCStackResult result = curResource->cancelObserve();
139 std::cout << "Cancel result: " << result << std::endl;
141 std::cout << "DONE" << std::endl;
147 std::cout << "onObserve Response error: " << eCode << std::endl;
150 catch (std::exception &e)
152 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
157 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
162 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
164 std::cout << "POST request was successful" << std::endl;
166 if (rep.hasAttribute("createduri"))
168 std::cout << "\tUri of the created resource: "
169 << rep.getValue<std::string>("createduri") << std::endl;
173 rep.getValue("on-off", mylight.m_on_off);
174 rep.getValue("color", mylight.m_color);
175 rep.getValue("dim", mylight.m_dim);
177 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
178 std::cout << "\tcolor: " << mylight.m_color << std::endl;
179 std::cout << "\tdim: " << mylight.m_dim << std::endl;
182 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
183 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
184 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
185 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
187 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
192 std::cout << "onPost2 Response error: " << eCode << std::endl;
195 catch (std::exception &e)
197 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
202 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
207 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
209 std::cout << "POST request was successful" << std::endl;
211 if (rep.hasAttribute("createduri"))
213 std::cout << "\tUri of the created resource: "
214 << rep.getValue<std::string>("createduri") << std::endl;
218 rep.getValue("on-off", mylight.m_on_off);
219 rep.getValue("color", mylight.m_color);
220 rep.getValue("dim", mylight.m_dim);
222 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
223 std::cout << "\tcolor: " << mylight.m_color << std::endl;
224 std::cout << "\tdim: " << mylight.m_dim << std::endl;
227 OCRepresentation rep2;
229 std::cout << "Posting light representation..." << std::endl;
231 mylight.m_on_off = true;
233 rep2.setValue("on-off", mylight.m_on_off);
235 curResource->post(rep2, QueryParamsMap(), &onPost2);
239 std::cout << "onPost Response error: " << eCode << std::endl;
242 catch (std::exception &e)
244 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
248 // Local function to put a different state for this re<< std::endlsource
249 void postLightRepresentation(std::shared_ptr<OCResource> resource)
253 OCRepresentation rep;
255 std::cout << "Posting light representation..." << std::endl;
257 mylight.m_on_off = false;
259 rep.setValue("on-off", mylight.m_on_off);
261 // Invoke resource's post API with rep, query map and the callback parameter
262 resource->post(rep, QueryParamsMap(), &onPost);
266 // callback handler on PUT request
267 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
273 if (eCode == OC_STACK_OK)
275 std::cout << "PUT request was successful" << std::endl;
277 /*rep.getValue("on-off", mylight.m_on_off);
278 rep.getValue("dim", mylight.m_dim);
279 rep.getValue("color", mylight.m_color);
281 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
282 std::cout << "\tcolor: " << mylight.m_color << std::endl;
283 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
285 //postLightRepresentation(curResource);
289 std::cout << "onPut Response error: " << eCode << std::endl;
292 catch (std::exception &e)
294 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
298 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
301 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
308 if (eCode == OC_STACK_OK)
310 std::cout << "PUT request was successful" << std::endl;
313 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
314 DISensorResource->get(test, &onGetForDISensor);
318 std::cout << "onPut Response error: " << eCode << std::endl;
321 catch (std::exception &e)
323 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
327 // Local function to put a different state for this resource
328 void putLightRepresentation(std::shared_ptr<OCResource> resource)
332 OCRepresentation rep;
334 std::cout << "Putting light representation..." << std::endl;
336 mylight.m_on_off = true;
338 std::cout << "Sending request to: " << resource->uri() << std::endl;
339 rep.setValue("on-off", mylight.m_on_off);
341 // Invoke resource's put API with rep, query map and the callback parameter
343 resource->post(rep, QueryParamsMap(), &onPut);
347 // Callback handler on GET request
348 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
353 if (eCode == OC_STACK_OK)
355 std::cout << "GET request was successful" << std::endl;
356 std::cout << "Resource URI: " << rep.getUri() << std::endl;
358 std::cout << "Payload: " << rep.getPayload() << std::endl;
359 std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
361 rep.getValue("on-off", mylight.m_on_off);
363 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
365 postLightRepresentation(curResource);
369 std::cout << "onGET Response error: " << eCode << std::endl;
372 catch (std::exception &e)
374 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
378 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
384 if (eCode == OC_STACK_OK)
386 std::cout << "GET request was successful" << std::endl;
387 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
389 std::cout << "Payload: " << rep.getPayload() << std::endl;
391 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
395 std::cout << "onGET Response error: " << eCode << std::endl;
398 catch (std::exception &e)
400 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
405 void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
411 if (eCode == OC_STACK_OK)
413 std::cout << "GET request was successful" << std::endl;
414 //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
416 std::cout << "Payload: " << rep.getPayload() << std::endl;
418 std::cout << "\tlightIntensity: " << rep.getValue<int>("intensity") << std::endl;
420 // iterating over all elements
421 OCRepresentation::const_iterator itr = rep.begin();
422 OCRepresentation::const_iterator endItr = rep.end();
424 for(;itr!=endItr;++itr)
426 std::cout << itr->attrname() << " ";
427 std::cout << itr->getValue<int>() << std::endl;
430 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onLightIntensityObserve);
434 std::cout << "onGET Response error: " << eCode << std::endl;
437 catch (std::exception &e)
439 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
443 // Local function to get representation of light resource
444 void getLightRepresentation(std::shared_ptr<OCResource> resource)
448 std::cout << "Getting Light Representation..." << std::endl;
449 // Invoke resource's get API with the callback parameter
452 std::cout << "Sending request to: " << resource->uri() << std::endl;
453 resource->get(test, &onGet);
457 void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
461 std::cout << "onGetDiscomfortIndex" << std::endl;
464 if (eCode == OC_STACK_OK)
466 std::cout << "GET request was successful" << std::endl;
468 std::cout << "Payload: " << rep.getPayload() << std::endl;
470 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
474 std::cout << "onGET Response error: " << eCode << std::endl;
477 catch (std::exception &e)
479 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
483 void onObserveDiscomfort(const HeaderOptions headerOptions, const OCRepresentation &rep,
484 const int &eCode, const int &sequenceNumber)
489 if (eCode == OC_STACK_OK)
491 std::cout << "OBSERVE RESULT:" << std::endl;
492 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
493 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
495 if (observe_count() > 10)
497 std::cout << "Cancelling Observe..." << std::endl;
498 OCStackResult result = curResource->cancelObserve();
500 std::cout << "Cancel result: " << result << std::endl;
502 std::cout << "DONE" << std::endl;
508 std::cout << "onObserve Response error: " << eCode << std::endl;
511 catch (std::exception &e)
513 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
518 // Local function to get representation of light resource
519 void getDiscomfortRepresentation(std::shared_ptr<OCResource> resource)
523 std::cout << "Getting Discomfort Representation..." << std::endl;
524 // Invoke resource's get API with the callback parameter
527 std::cout << "Sending request to: " << resource->uri() << std::endl;
528 resource->get(test, &onGetDiscomfortIndex);
529 //resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
536 // Local function to get representation of light resource
537 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
541 std::cout << "Getting Light Representation..." << std::endl;
542 // Invoke resource's get API with the callback parameter
545 std::cout << "Sending request to: " << resource->uri() << std::endl;
546 resource->get(test, &onGetForLightIntensitySensor);
550 // Callback to found resources
551 void foundResource(std::shared_ptr<OCResource> resource)
553 std::cout << "In foundResource\n";
554 std::string resourceURI = resource->uri();
555 std::string hostAddress;
560 // Do some operations with resource object.
563 std::cout << "DISCOVERED Resource:" << std::endl;
564 // Get the resource URI
565 resourceURI = resource->uri();
566 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
568 // Get the resource host address
569 hostAddress = resource->host();
570 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
572 // Get the resource types
573 std::cout << "\tList of resource types: " << std::endl;
574 for (auto &resourceTypes : resource->getResourceTypes())
576 std::cout << "\t\t" << resourceTypes << std::endl;
577 /*if (resourceTypes == "oic.r.light")
579 curResource = resource;
580 // Call a local function which will internally invoke get API on the resource pointer
581 getLightRepresentation(resource);
583 if (resourceTypes == "oic.r.discomfortindex")
585 curResource = resource;
586 std::cout << "\t\tGet discomfort representation " << std::endl;
587 // Call a local function which will internally invoke get API on the resource pointer
588 getDiscomfortRepresentation(resource);
592 // Get the resource interfaces
593 std::cout << "\tList of resource interfaces: " << std::endl;
594 for (auto &resourceInterfaces : resource->getResourceInterfaces())
596 std::cout << "\t\t" << resourceInterfaces << std::endl;
601 // Resource is invalid
602 std::cout << "Resource is invalid" << std::endl;
606 catch (std::exception &e)
608 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
614 std::cout << std::endl;
615 std::cout << "---------------------------------------------------------------------\n";
616 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
617 std::cout << " ObserveType : 1 - Observe" << std::endl;
618 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
619 std::cout << "---------------------------------------------------------------------\n\n";
622 void checkObserverValue(int value)
626 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
627 std::cout << "<===Setting ObserveType to Observe===>\n\n";
631 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
632 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
636 std::cout << "<===Invalid ObserveType selected."
637 << " Setting ObserveType to Observe===>\n\n";
641 static FILE *client_open(const char *path, const char *mode)
645 return fopen("./oic_svr_db_client.json", mode);
648 int main(int argc, char *argv[])
651 std::ostringstream requestURI;
652 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
658 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
662 checkObserverValue(std::stoi(argv[1]));
666 std::cout << "<===Invalid number of command line arguments===>\n\n";
670 catch (std::exception &)
672 std::cout << "<===Invalid input arguments===>\n\n";
676 // Create PlatformConfig object
679 OC::ServiceType::InProc,
683 OC::QualityOfService::LowQos,
687 OCPlatform::Configure(cfg);
690 // makes it so that all boolean values are printed as 'true/false' in this stream
691 std::cout.setf(std::ios::boolalpha);
692 // Find all resources
693 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
695 OCPlatform::findResource("", requestURI.str(),
696 CT_DEFAULT, &foundResource);
697 std::cout << "Finding Resource... " << std::endl;
699 // Find resource is done twice so that we discover the original resources a second time.
700 // These resources will have the same uniqueidentifier (yet be different objects), so that
701 // we can verify/show the duplicate-checking code in foundResource(above);
702 OCPlatform::findResource("", requestURI.str(),
703 CT_DEFAULT, &foundResource);
704 std::cout << "Finding Resource for second time..." << std::endl;
706 // A condition variable will free the mutex it is given, then do a non-
707 // intensive block until 'notify' is called on it. In this case, since we
708 // don't ever call cv.notify, this should be a non-processor intensive version
711 std::condition_variable cv;
712 std::unique_lock<std::mutex> lock(blocker);
716 catch (OCException &e)
718 oclog() << "Exception in main: " << e.what();