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 // Local function to get representation of light resource
459 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
463 std::cout << "Getting Light Representation..." << std::endl;
464 // Invoke resource's get API with the callback parameter
467 std::cout << "Sending request to: " << resource->uri() << std::endl;
468 resource->get(test, &onGetForLightIntensitySensor);
472 // Callback to found resources
473 void foundResource(std::shared_ptr<OCResource> resource)
475 std::cout << "In foundResource\n";
476 std::string resourceURI = resource->uri();
477 std::string hostAddress;
482 // Do some operations with resource object.
485 std::cout << "DISCOVERED Resource:" << std::endl;
486 // Get the resource URI
487 resourceURI = resource->uri();
488 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
490 // Get the resource host address
491 hostAddress = resource->host();
492 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
494 // Get the resource types
495 std::cout << "\tList of resource types: " << std::endl;
496 for (auto &resourceTypes : resource->getResourceTypes())
498 std::cout << "\t\t" << resourceTypes << std::endl;
499 /*if (resourceTypes == "oic.r.light")
501 curResource = resource;
502 // Call a local function which will internally invoke get API on the resource pointer
503 getLightRepresentation(resource);
505 if (resourceTypes == "oic.r.lightsensor")
507 curResource = resource;
508 // Call a local function which will internally invoke get API on the resource pointer
509 getLightIntensityRepresentation(resource);
513 // Get the resource interfaces
514 std::cout << "\tList of resource interfaces: " << std::endl;
515 for (auto &resourceInterfaces : resource->getResourceInterfaces())
517 std::cout << "\t\t" << resourceInterfaces << std::endl;
522 // Resource is invalid
523 std::cout << "Resource is invalid" << std::endl;
527 catch (std::exception &e)
529 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
535 std::cout << std::endl;
536 std::cout << "---------------------------------------------------------------------\n";
537 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
538 std::cout << " ObserveType : 1 - Observe" << std::endl;
539 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
540 std::cout << "---------------------------------------------------------------------\n\n";
543 void checkObserverValue(int value)
547 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
548 std::cout << "<===Setting ObserveType to Observe===>\n\n";
552 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
553 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
557 std::cout << "<===Invalid ObserveType selected."
558 << " Setting ObserveType to Observe===>\n\n";
562 static FILE *client_open(const char *path, const char *mode)
566 return fopen("./oic_svr_db_client.json", mode);
569 int main(int argc, char *argv[])
572 std::ostringstream requestURI;
573 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
579 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
583 checkObserverValue(std::stoi(argv[1]));
587 std::cout << "<===Invalid number of command line arguments===>\n\n";
591 catch (std::exception &)
593 std::cout << "<===Invalid input arguments===>\n\n";
597 // Create PlatformConfig object
600 OC::ServiceType::InProc,
604 OC::QualityOfService::LowQos,
608 OCPlatform::Configure(cfg);
611 // makes it so that all boolean values are printed as 'true/false' in this stream
612 std::cout.setf(std::ios::boolalpha);
613 // Find all resources
614 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
616 OCPlatform::findResource("", requestURI.str(),
617 CT_DEFAULT, &foundResource);
618 std::cout << "Finding Resource... " << std::endl;
620 // Find resource is done twice so that we discover the original resources a second time.
621 // These resources will have the same uniqueidentifier (yet be different objects), so that
622 // we can verify/show the duplicate-checking code in foundResource(above);
623 OCPlatform::findResource("", requestURI.str(),
624 CT_DEFAULT, &foundResource);
625 std::cout << "Finding Resource for second time..." << std::endl;
627 // A condition variable will free the mutex it is given, then do a non-
628 // intensive block until 'notify' is called on it. In this case, since we
629 // don't ever call cv.notify, this should be a non-processor intensive version
632 std::condition_variable cv;
633 std::unique_lock<std::mutex> lock(blocker);
637 catch (OCException &e)
639 oclog() << "Exception in main: " << e.what();