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 resource
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;
310 rep.getValue("on-off", mylight.m_on_off);
311 rep.getValue("dim", mylight.m_dim);
312 rep.getValue("color", mylight.m_color);
314 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
315 std::cout << "\tcolor: " << mylight.m_color << std::endl;
316 std::cout << "\tdim: " << mylight.m_dim << std::endl;
318 putLightRepresentation(curResource);
322 std::cout << "onGET Response error: " << eCode << std::endl;
325 catch (std::exception &e)
327 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
331 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
337 if (eCode == OC_STACK_OK)
339 std::cout << "GET request was successful" << std::endl;
340 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
342 std::cout << "Payload: " << rep.getPayload() << std::endl;
344 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
348 std::cout << "onGET Response error: " << eCode << std::endl;
351 catch (std::exception &e)
353 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
357 // Local function to get representation of light resource
358 void getLightRepresentation(std::shared_ptr<OCResource> resource)
362 std::cout << "Getting Light Representation..." << std::endl;
363 // Invoke resource's get API with the callback parameter
366 std::cout << "Sending request to: " << resource->uri() << std::endl;
367 resource->get(test, &onGet);
371 // Callback to found resources
372 void foundResource(std::shared_ptr<OCResource> resource)
374 std::cout << "In foundResource\n";
375 std::string resourceURI = resource->uri();
376 std::string hostAddress;
380 std::lock_guard<std::mutex> lock(curResourceLock);
381 if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
383 std::cout << "Found resource " << resource->uniqueIdentifier() <<
384 " for the first time on server with ID: " << resource->sid() << std::endl;
385 discoveredResources[resource->uniqueIdentifier()] = resource;
387 if (resourceURI.find("/discomfortIndex") != std::string::npos)
389 std::cout << "discomfortIndex found !!! " << std::endl;
391 DISensorResource = resource;
393 OCRepresentation rep;
395 rep.setValue("humidity", std::string("30"));
396 rep.setValue("temperature", std::string("27"));
398 resource->put(rep, QueryParamsMap(), &onPutForDISensor);
403 std::cout << "Found resource " << resource->uniqueIdentifier() << " again!" << std::endl;
408 std::cout << "Found another resource, ignoring" << std::endl;
413 // Do some operations with resource object.
416 std::cout << "DISCOVERED Resource:" << std::endl;
417 // Get the resource URI
418 resourceURI = resource->uri();
419 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
421 // Get the resource host address
422 hostAddress = resource->host();
423 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
425 // Get the resource types
426 std::cout << "\tList of resource types: " << std::endl;
427 for (auto &resourceTypes : resource->getResourceTypes())
429 std::cout << "\t\t" << resourceTypes << std::endl;
431 if (resourceTypes == "oic.r.light")
433 curResource = resource;
434 // Call a local function which will internally invoke get API on the resource pointer
435 getLightRepresentation(resource);
439 // Get the resource interfaces
440 std::cout << "\tList of resource interfaces: " << std::endl;
441 for (auto &resourceInterfaces : resource->getResourceInterfaces())
443 std::cout << "\t\t" << resourceInterfaces << std::endl;
448 // Resource is invalid
449 std::cout << "Resource is invalid" << std::endl;
453 catch (std::exception &e)
455 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
461 std::cout << std::endl;
462 std::cout << "---------------------------------------------------------------------\n";
463 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
464 std::cout << " ObserveType : 1 - Observe" << std::endl;
465 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
466 std::cout << "---------------------------------------------------------------------\n\n";
469 void checkObserverValue(int value)
473 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
474 std::cout << "<===Setting ObserveType to Observe===>\n\n";
478 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
479 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
483 std::cout << "<===Invalid ObserveType selected."
484 << " Setting ObserveType to Observe===>\n\n";
488 static FILE *client_open(const char *path, const char *mode)
492 return fopen("./oic_svr_db_client.json", mode);
495 int main(int argc, char *argv[])
498 std::ostringstream requestURI;
499 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
505 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
509 checkObserverValue(std::stoi(argv[1]));
513 std::cout << "<===Invalid number of command line arguments===>\n\n";
517 catch (std::exception &)
519 std::cout << "<===Invalid input arguments===>\n\n";
523 // Create PlatformConfig object
526 OC::ServiceType::InProc,
530 OC::QualityOfService::LowQos,
534 OCPlatform::Configure(cfg);
537 // makes it so that all boolean values are printed as 'true/false' in this stream
538 std::cout.setf(std::ios::boolalpha);
539 // Find all resources
540 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
542 OCPlatform::findResource("", requestURI.str(),
543 CT_DEFAULT, &foundResource);
544 std::cout << "Finding Resource... " << std::endl;
546 // Find resource is done twice so that we discover the original resources a second time.
547 // These resources will have the same uniqueidentifier (yet be different objects), so that
548 // we can verify/show the duplicate-checking code in foundResource(above);
549 OCPlatform::findResource("", requestURI.str(),
550 CT_DEFAULT, &foundResource);
551 std::cout << "Finding Resource for second time..." << std::endl;
553 // A condition variable will free the mutex it is given, then do a non-
554 // intensive block until 'notify' is called on it. In this case, since we
555 // don't ever call cv.notify, this should be a non-processor intensive version
558 std::condition_variable cv;
559 std::unique_lock<std::mutex> lock(blocker);
563 catch (OCException &e)
565 oclog() << "Exception in main: " << e.what();