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("")
64 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
65 const int &eCode, const int &sequenceNumber)
70 if (eCode == OC_STACK_OK)
72 std::cout << "OBSERVE RESULT:" << std::endl;
73 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
74 rep.getValue("on-off", mylight.m_on_off);
75 rep.getValue("color", mylight.m_color);
76 rep.getValue("dim", mylight.m_dim);
77 rep.getValue("name", mylight.m_name);
79 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
80 std::cout << "\tcolor: " << mylight.m_color << std::endl;
81 std::cout << "\tdim: " << mylight.m_dim << std::endl;
83 if (observe_count() > 10)
85 std::cout << "Cancelling Observe..." << std::endl;
86 OCStackResult result = curResource->cancelObserve();
88 std::cout << "Cancel result: " << result << std::endl;
90 std::cout << "DONE" << std::endl;
96 std::cout << "onObserve Response error: " << eCode << std::endl;
99 catch (std::exception &e)
101 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
106 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
111 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
113 std::cout << "POST request was successful" << std::endl;
115 if (rep.hasAttribute("createduri"))
117 std::cout << "\tUri of the created resource: "
118 << rep.getValue<std::string>("createduri") << std::endl;
122 rep.getValue("on-off", mylight.m_on_off);
123 rep.getValue("color", mylight.m_color);
124 rep.getValue("dim", mylight.m_dim);
126 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
127 std::cout << "\tcolor: " << mylight.m_color << std::endl;
128 std::cout << "\tdim: " << mylight.m_dim << std::endl;
131 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
132 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
133 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
134 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
136 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
141 std::cout << "onPost2 Response error: " << eCode << std::endl;
144 catch (std::exception &e)
146 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
151 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
156 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
158 std::cout << "POST request was successful" << std::endl;
160 if (rep.hasAttribute("createduri"))
162 std::cout << "\tUri of the created resource: "
163 << rep.getValue<std::string>("createduri") << std::endl;
167 rep.getValue("on-off", mylight.m_on_off);
168 rep.getValue("color", mylight.m_color);
169 rep.getValue("dim", mylight.m_dim);
171 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
172 std::cout << "\tcolor: " << mylight.m_color << std::endl;
173 std::cout << "\tdim: " << mylight.m_dim << std::endl;
176 OCRepresentation rep2;
178 std::cout << "Posting light representation..." << std::endl;
180 mylight.m_on_off = true;
182 rep2.setValue("on-off", mylight.m_on_off);
184 curResource->post(rep2, QueryParamsMap(), &onPost2);
188 std::cout << "onPost Response error: " << eCode << std::endl;
191 catch (std::exception &e)
193 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
197 // Local function to put a different state for this resource
198 void postLightRepresentation(std::shared_ptr<OCResource> resource)
202 OCRepresentation rep;
204 std::cout << "Posting light representation..." << std::endl;
206 mylight.m_on_off = false;
208 rep.setValue("on-off", mylight.m_on_off);
210 // Invoke resource's post API with rep, query map and the callback parameter
211 resource->post(rep, QueryParamsMap(), &onPost);
215 // callback handler on PUT request
216 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
222 if (eCode == OC_STACK_OK)
224 std::cout << "PUT request was successful" << std::endl;
226 /*rep.getValue("on-off", mylight.m_on_off);
227 rep.getValue("dim", mylight.m_dim);
228 rep.getValue("color", mylight.m_color);
230 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
231 std::cout << "\tcolor: " << mylight.m_color << std::endl;
232 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
234 //postLightRepresentation(curResource);
238 std::cout << "onPut Response error: " << eCode << std::endl;
241 catch (std::exception &e)
243 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
247 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
250 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
257 if (eCode == OC_STACK_OK)
259 std::cout << "PUT request was successful" << std::endl;
262 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
263 DISensorResource->get(test, &onGetForDISensor);
267 std::cout << "onPut Response error: " << eCode << std::endl;
270 catch (std::exception &e)
272 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
276 // Local function to put a different state for this resource
277 void putLightRepresentation(std::shared_ptr<OCResource> resource)
281 OCRepresentation rep;
283 std::cout << "Putting light representation..." << std::endl;
285 mylight.m_on_off = true;
287 std::cout << "Sending request to: " << resource->uri() << std::endl;
288 rep.setValue("on-off", mylight.m_on_off);
290 // Invoke resource's put API with rep, query map and the callback parameter
292 resource->post(rep, QueryParamsMap(), &onPut);
296 // Callback handler on GET request
297 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
302 if (eCode == OC_STACK_OK)
304 std::cout << "GET request was successful" << std::endl;
305 std::cout << "Resource URI: " << rep.getUri() << std::endl;
307 std::cout << "Payload: " << rep.getPayload() << std::endl;
309 rep.getValue("on-off", mylight.m_on_off);
310 rep.getValue("dim", mylight.m_dim);
311 rep.getValue("color", mylight.m_color);
313 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
314 std::cout << "\tcolor: " << mylight.m_color << std::endl;
315 std::cout << "\tdim: " << mylight.m_dim << std::endl;
317 postLightRepresentation(curResource);
321 std::cout << "onGET Response error: " << eCode << std::endl;
324 catch (std::exception &e)
326 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
330 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
336 if (eCode == OC_STACK_OK)
338 std::cout << "GET request was successful" << std::endl;
339 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
341 std::cout << "Payload: " << rep.getPayload() << std::endl;
343 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
347 std::cout << "onGET Response error: " << eCode << std::endl;
350 catch (std::exception &e)
352 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
356 // Local function to get representation of light resource
357 void getLightRepresentation(std::shared_ptr<OCResource> resource)
361 std::cout << "Getting Light Representation..." << std::endl;
362 // Invoke resource's get API with the callback parameter
365 std::cout << "Sending request to: " << resource->uri() << std::endl;
366 resource->get(test, &onGet);
370 // Callback to found resources
371 void foundResource(std::shared_ptr<OCResource> resource)
373 std::cout << "In foundResource\n";
374 std::string resourceURI = resource->uri();
375 std::string hostAddress;
379 std::lock_guard<std::mutex> lock(curResourceLock);
380 if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
382 std::cout << "Found resource " << resource->uniqueIdentifier() <<
383 " for the first time on server with ID: " << resource->sid() << std::endl;
384 discoveredResources[resource->uniqueIdentifier()] = resource;
386 if (resourceURI.find("/discomfortIndex") != std::string::npos)
388 std::cout << "discomfortIndex found !!! " << std::endl;
390 DISensorResource = resource;
392 OCRepresentation rep;
394 rep.setValue("humidity", std::string("30"));
395 rep.setValue("temperature", std::string("27"));
397 resource->put(rep, QueryParamsMap(), &onPutForDISensor);
402 std::cout << "Found resource " << resource->uniqueIdentifier() << " again!" << std::endl;
407 std::cout << "Found another resource, ignoring" << std::endl;
412 // Do some operations with resource object.
415 std::cout << "DISCOVERED Resource:" << std::endl;
416 // Get the resource URI
417 resourceURI = resource->uri();
418 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
420 // Get the resource host address
421 hostAddress = resource->host();
422 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
424 // Get the resource types
425 std::cout << "\tList of resource types: " << std::endl;
426 for (auto &resourceTypes : resource->getResourceTypes())
428 std::cout << "\t\t" << resourceTypes << std::endl;
430 if (resourceTypes == "oic.r.light")
432 curResource = resource;
433 // Call a local function which will internally invoke get API on the resource pointer
434 getLightRepresentation(resource);
438 // Get the resource interfaces
439 std::cout << "\tList of resource interfaces: " << std::endl;
440 for (auto &resourceInterfaces : resource->getResourceInterfaces())
442 std::cout << "\t\t" << resourceInterfaces << std::endl;
447 // Resource is invalid
448 std::cout << "Resource is invalid" << std::endl;
452 catch (std::exception &e)
454 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
460 std::cout << std::endl;
461 std::cout << "---------------------------------------------------------------------\n";
462 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
463 std::cout << " ObserveType : 1 - Observe" << std::endl;
464 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
465 std::cout << "---------------------------------------------------------------------\n\n";
468 void checkObserverValue(int value)
472 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
473 std::cout << "<===Setting ObserveType to Observe===>\n\n";
477 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
478 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
482 std::cout << "<===Invalid ObserveType selected."
483 << " Setting ObserveType to Observe===>\n\n";
487 static FILE *client_open(const char *path, const char *mode)
491 return fopen("./oic_svr_db_client.json", mode);
494 int main(int argc, char *argv[])
497 std::ostringstream requestURI;
498 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
504 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
508 checkObserverValue(std::stoi(argv[1]));
512 std::cout << "<===Invalid number of command line arguments===>\n\n";
516 catch (std::exception &)
518 std::cout << "<===Invalid input arguments===>\n\n";
522 // Create PlatformConfig object
525 OC::ServiceType::InProc,
529 OC::QualityOfService::LowQos,
533 OCPlatform::Configure(cfg);
536 // makes it so that all boolean values are printed as 'true/false' in this stream
537 std::cout.setf(std::ios::boolalpha);
538 // Find all resources
539 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
541 OCPlatform::findResource("", requestURI.str(),
542 CT_DEFAULT, &foundResource);
543 std::cout << "Finding Resource... " << std::endl;
545 // Find resource is done twice so that we discover the original resources a second time.
546 // These resources will have the same uniqueidentifier (yet be different objects), so that
547 // we can verify/show the duplicate-checking code in foundResource(above);
548 OCPlatform::findResource("", requestURI.str(),
549 CT_DEFAULT, &foundResource);
550 std::cout << "Finding Resource for second time..." << std::endl;
552 // A condition variable will free the mutex it is given, then do a non-
553 // intensive block until 'notify' is called on it. In this case, since we
554 // don't ever call cv.notify, this should be a non-processor intensive version
557 std::condition_variable cv;
558 std::unique_lock<std::mutex> lock(blocker);
562 catch (OCException &e)
564 oclog() << "Exception in main: " << e.what();