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;
119 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
124 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
126 std::cout << "POST request was successful" << std::endl;
128 if (rep.hasAttribute("createduri"))
130 std::cout << "\tUri of the created resource: "
131 << rep.getValue<std::string>("createduri") << std::endl;
135 rep.getValue("on-off", mylight.m_on_off);
136 rep.getValue("color", mylight.m_color);
137 rep.getValue("dim", mylight.m_dim);
139 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
140 std::cout << "\tcolor: " << mylight.m_color << std::endl;
141 std::cout << "\tdim: " << mylight.m_dim << std::endl;
144 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
145 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
146 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
147 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
149 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
154 std::cout << "onPost2 Response error: " << eCode << std::endl;
157 catch (std::exception &e)
159 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
164 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
169 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
171 std::cout << "POST request was successful" << std::endl;
173 if (rep.hasAttribute("createduri"))
175 std::cout << "\tUri of the created resource: "
176 << rep.getValue<std::string>("createduri") << std::endl;
180 rep.getValue("on-off", mylight.m_on_off);
181 rep.getValue("color", mylight.m_color);
182 rep.getValue("dim", mylight.m_dim);
184 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
185 std::cout << "\tcolor: " << mylight.m_color << std::endl;
186 std::cout << "\tdim: " << mylight.m_dim << std::endl;
189 OCRepresentation rep2;
191 std::cout << "Posting light representation..." << std::endl;
193 mylight.m_on_off = true;
195 rep2.setValue("on-off", mylight.m_on_off);
197 curResource->post(rep2, QueryParamsMap(), &onPost2);
201 std::cout << "onPost Response error: " << eCode << std::endl;
204 catch (std::exception &e)
206 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
210 // Local function to put a different state for this re<< std::endlsource
211 void postLightRepresentation(std::shared_ptr<OCResource> resource)
215 OCRepresentation rep;
217 std::cout << "Posting light representation..." << std::endl;
219 mylight.m_on_off = "false";
221 rep.setValue("on-off", mylight.m_on_off);
223 // Invoke resource's post API with rep, query map and the callback parameter
224 resource->post(rep, QueryParamsMap(), &onPost);
228 // callback handler on PUT request
229 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
235 if (eCode == OC_STACK_OK)
237 std::cout << "PUT request was successful" << std::endl;
239 /*rep.getValue("on-off", mylight.m_on_off);
240 rep.getValue("dim", mylight.m_dim);
241 rep.getValue("color", mylight.m_color);
243 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
244 std::cout << "\tcolor: " << mylight.m_color << std::endl;
245 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
247 //postLightRepresentation(curResource);
251 std::cout << "onPut Response error: " << eCode << std::endl;
254 catch (std::exception &e)
256 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
260 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
263 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
270 if (eCode == OC_STACK_OK)
272 std::cout << "PUT request was successful" << std::endl;
275 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
276 DISensorResource->get(test, &onGetForDISensor);
280 std::cout << "onPut Response error: " << eCode << std::endl;
283 catch (std::exception &e)
285 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
289 // Local function to put a different state for this resource
290 void putLightRepresentation(std::shared_ptr<OCResource> resource)
294 OCRepresentation rep;
296 std::cout << "Putting light representation..." << std::endl;
298 mylight.m_on_off = true;
300 std::cout << "Sending request to: " << resource->uri() << std::endl;
301 rep.setValue("on-off", mylight.m_on_off);
303 // Invoke resource's put API with rep, query map and the callback parameter
305 resource->put(rep, QueryParamsMap(), &onPut);
309 // Callback handler on GET request
310 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
315 if (eCode == OC_STACK_OK)
317 std::cout << "GET request was successful" << std::endl;
318 std::cout << "Resource URI: " << rep.getUri() << std::endl;
320 std::cout << "Payload: " << rep.getPayload() << std::endl;
321 std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
323 rep.getValue("on-off", mylight.m_on_off);
325 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
327 putLightRepresentation(curResource);
331 std::cout << "onGET Response error: " << eCode << std::endl;
334 catch (std::exception &e)
336 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
340 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
346 if (eCode == OC_STACK_OK)
348 std::cout << "GET request was successful" << std::endl;
349 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
351 std::cout << "Payload: " << rep.getPayload() << std::endl;
353 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
357 std::cout << "onGET Response error: " << eCode << std::endl;
360 catch (std::exception &e)
362 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
367 void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
373 if (eCode == OC_STACK_OK)
375 std::cout << "GET request was successful" << std::endl;
376 //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
378 std::cout << "Payload: " << rep.getPayload() << std::endl;
380 std::cout << "\lightIntensity: " << rep.getValue<int>("lightintensity") << std::endl;
384 std::cout << "onGET Response error: " << eCode << std::endl;
387 catch (std::exception &e)
389 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
393 // Local function to get representation of light resource
394 void getLightRepresentation(std::shared_ptr<OCResource> resource)
398 std::cout << "Getting Light Representation..." << std::endl;
399 // Invoke resource's get API with the callback parameter
402 std::cout << "Sending request to: " << resource->uri() << std::endl;
403 resource->get(test, &onGet);
407 // Local function to get representation of light resource
408 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
412 std::cout << "Getting Light Representation..." << std::endl;
413 // Invoke resource's get API with the callback parameter
416 std::cout << "Sending request to: " << resource->uri() << std::endl;
417 resource->get(test, &onGetForLightIntensitySensor);
421 // Callback to found resources
422 void foundResource(std::shared_ptr<OCResource> resource)
424 std::cout << "In foundResource\n";
425 std::string resourceURI = resource->uri();
426 std::string hostAddress;
431 // Do some operations with resource object.
434 std::cout << "DISCOVERED Resource:" << std::endl;
435 // Get the resource URI
436 resourceURI = resource->uri();
437 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
439 // Get the resource host address
440 hostAddress = resource->host();
441 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
443 // Get the resource types
444 std::cout << "\tList of resource types: " << std::endl;
445 for (auto &resourceTypes : resource->getResourceTypes())
447 std::cout << "\t\t" << resourceTypes << std::endl;
448 /*if (resourceTypes == "oic.r.light")
450 curResource = resource;
451 // Call a local function which will internally invoke get API on the resource pointer
452 getLightRepresentation(resource);
454 if (resourceTypes == "oic.r.lightintensity")
456 curResource = resource;
457 // Call a local function which will internally invoke get API on the resource pointer
458 getLightIntensityRepresentation(resource);
462 // Get the resource interfaces
463 std::cout << "\tList of resource interfaces: " << std::endl;
464 for (auto &resourceInterfaces : resource->getResourceInterfaces())
466 std::cout << "\t\t" << resourceInterfaces << std::endl;
471 // Resource is invalid
472 std::cout << "Resource is invalid" << std::endl;
476 catch (std::exception &e)
478 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
484 std::cout << std::endl;
485 std::cout << "---------------------------------------------------------------------\n";
486 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
487 std::cout << " ObserveType : 1 - Observe" << std::endl;
488 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
489 std::cout << "---------------------------------------------------------------------\n\n";
492 void checkObserverValue(int value)
496 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
497 std::cout << "<===Setting ObserveType to Observe===>\n\n";
501 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
502 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
506 std::cout << "<===Invalid ObserveType selected."
507 << " Setting ObserveType to Observe===>\n\n";
511 static FILE *client_open(const char *path, const char *mode)
515 return fopen("./oic_svr_db_client.json", mode);
518 int main(int argc, char *argv[])
521 std::ostringstream requestURI;
522 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
528 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
532 checkObserverValue(std::stoi(argv[1]));
536 std::cout << "<===Invalid number of command line arguments===>\n\n";
540 catch (std::exception &)
542 std::cout << "<===Invalid input arguments===>\n\n";
546 // Create PlatformConfig object
549 OC::ServiceType::InProc,
553 OC::QualityOfService::LowQos,
557 OCPlatform::Configure(cfg);
560 // makes it so that all boolean values are printed as 'true/false' in this stream
561 std::cout.setf(std::ios::boolalpha);
562 // Find all resources
563 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
565 OCPlatform::findResource("", requestURI.str(),
566 CT_DEFAULT, &foundResource);
567 std::cout << "Finding Resource... " << std::endl;
569 // Find resource is done twice so that we discover the original resources a second time.
570 // These resources will have the same uniqueidentifier (yet be different objects), so that
571 // we can verify/show the duplicate-checking code in foundResource(above);
572 OCPlatform::findResource("", requestURI.str(),
573 CT_DEFAULT, &foundResource);
574 std::cout << "Finding Resource for second time..." << std::endl;
576 // A condition variable will free the mutex it is given, then do a non-
577 // intensive block until 'notify' is called on it. In this case, since we
578 // don't ever call cv.notify, this should be a non-processor intensive version
581 std::condition_variable cv;
582 std::unique_lock<std::mutex> lock(blocker);
586 catch (OCException &e)
588 oclog() << "Exception in main: " << e.what();