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)
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)
110 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
112 std::cout << "POST request was successful" << std::endl;
114 if (rep.hasAttribute("createduri"))
116 std::cout << "\tUri of the created resource: "
117 << rep.getValue<std::string>("createduri") << std::endl;
121 rep.getValue("on-off", mylight.m_on_off);
122 rep.getValue("color", mylight.m_color);
123 rep.getValue("dim", mylight.m_dim);
125 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
126 std::cout << "\tcolor: " << mylight.m_color << std::endl;
127 std::cout << "\tdim: " << mylight.m_dim << std::endl;
130 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
131 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
132 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
133 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
135 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
140 std::cout << "onPost2 Response error: " << eCode << std::endl;
143 catch (std::exception &e)
145 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
150 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
154 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
156 std::cout << "POST request was successful" << std::endl;
158 if (rep.hasAttribute("createduri"))
160 std::cout << "\tUri of the created resource: "
161 << rep.getValue<std::string>("createduri") << std::endl;
165 rep.getValue("on-off", mylight.m_on_off);
166 rep.getValue("color", mylight.m_color);
167 rep.getValue("dim", mylight.m_dim);
169 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
170 std::cout << "\tcolor: " << mylight.m_color << std::endl;
171 std::cout << "\tdim: " << mylight.m_dim << std::endl;
174 OCRepresentation rep2;
176 std::cout << "Posting light representation..." << std::endl;
178 mylight.m_on_off = true;
180 rep2.setValue("on-off", mylight.m_on_off);
182 curResource->post(rep2, QueryParamsMap(), &onPost2);
186 std::cout << "onPost Response error: " << eCode << std::endl;
189 catch (std::exception &e)
191 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
195 // Local function to put a different state for this resource
196 void postLightRepresentation(std::shared_ptr<OCResource> resource)
200 OCRepresentation rep;
202 std::cout << "Posting light representation..." << std::endl;
204 mylight.m_on_off = "false";
206 rep.setValue("on-off", mylight.m_on_off);
208 // Invoke resource's post API with rep, query map and the callback parameter
209 resource->post(rep, QueryParamsMap(), &onPost);
213 // callback handler on PUT request
214 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
218 if (eCode == OC_STACK_OK)
220 std::cout << "PUT request was successful" << std::endl;
222 /*rep.getValue("on-off", mylight.m_on_off);
223 rep.getValue("dim", mylight.m_dim);
224 rep.getValue("color", mylight.m_color);
226 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
227 std::cout << "\tcolor: " << mylight.m_color << std::endl;
228 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
230 //postLightRepresentation(curResource);
234 std::cout << "onPut Response error: " << eCode << std::endl;
237 catch (std::exception &e)
239 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
243 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
246 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
251 if (eCode == OC_STACK_OK)
253 std::cout << "PUT request was successful" << std::endl;
256 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
257 DISensorResource->get(test, &onGetForDISensor);
261 std::cout << "onPut Response error: " << eCode << std::endl;
264 catch (std::exception &e)
266 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
270 // Local function to put a different state for this resource
271 void putLightRepresentation(std::shared_ptr<OCResource> resource)
275 OCRepresentation rep;
277 std::cout << "Putting light representation..." << std::endl;
279 mylight.m_on_off = true;
281 std::cout << "Sending request to: " << resource->uri() << std::endl;
282 rep.setValue("on-off", mylight.m_on_off);
284 // Invoke resource's put API with rep, query map and the callback parameter
286 resource->put(rep, QueryParamsMap(), &onPut);
290 // Callback handler on GET request
291 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
295 if (eCode == OC_STACK_OK)
297 std::cout << "GET request was successful" << std::endl;
298 std::cout << "Resource URI: " << rep.getUri() << std::endl;
300 std::cout << "Payload: " << rep.getPayload() << std::endl;
302 rep.getValue("on-off", mylight.m_on_off);
303 rep.getValue("dim", mylight.m_dim);
304 rep.getValue("color", mylight.m_color);
306 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
307 std::cout << "\tcolor: " << mylight.m_color << std::endl;
308 std::cout << "\tdim: " << mylight.m_dim << std::endl;
310 putLightRepresentation(curResource);
314 std::cout << "onGET Response error: " << eCode << std::endl;
317 catch (std::exception &e)
319 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
323 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
328 if (eCode == OC_STACK_OK)
330 std::cout << "GET request was successful" << std::endl;
331 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
333 std::cout << "Payload: " << rep.getPayload() << std::endl;
335 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
339 std::cout << "onGET Response error: " << eCode << std::endl;
342 catch (std::exception &e)
344 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
348 // Local function to get representation of light resource
349 void getLightRepresentation(std::shared_ptr<OCResource> resource)
353 std::cout << "Getting Light Representation..." << std::endl;
354 // Invoke resource's get API with the callback parameter
357 std::cout << "Sending request to: " << resource->uri() << std::endl;
358 resource->get(test, &onGet);
362 // Callback to found resources
363 void foundResource(std::shared_ptr<OCResource> resource)
365 std::cout << "In foundResource\n";
366 std::string resourceURI = resource->uri();
367 std::string hostAddress;
371 std::lock_guard<std::mutex> lock(curResourceLock);
372 if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
374 std::cout << "Found resource " << resource->uniqueIdentifier() <<
375 " for the first time on server with ID: " << resource->sid() << std::endl;
376 discoveredResources[resource->uniqueIdentifier()] = resource;
378 if (resourceURI.find("/discomfortIndex") != std::string::npos)
380 std::cout << "discomfortIndex found !!! " << std::endl;
382 DISensorResource = resource;
384 OCRepresentation rep;
386 rep.setValue("humidity", std::string("30"));
387 rep.setValue("temperature", std::string("27"));
389 resource->put(rep, QueryParamsMap(), &onPutForDISensor);
394 std::cout << "Found resource " << resource->uniqueIdentifier() << " again!" << std::endl;
399 std::cout << "Found another resource, ignoring" << std::endl;
404 // Do some operations with resource object.
407 std::cout << "DISCOVERED Resource:" << std::endl;
408 // Get the resource URI
409 resourceURI = resource->uri();
410 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
412 // Get the resource host address
413 hostAddress = resource->host();
414 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
416 // Get the resource types
417 std::cout << "\tList of resource types: " << std::endl;
418 for (auto &resourceTypes : resource->getResourceTypes())
420 std::cout << "\t\t" << resourceTypes << std::endl;
422 if (resourceTypes == "oic.light.control")
424 curResource = resource;
425 // Call a local function which will internally invoke get API on the resource pointer
426 getLightRepresentation(resource);
430 // Get the resource interfaces
431 std::cout << "\tList of resource interfaces: " << std::endl;
432 for (auto &resourceInterfaces : resource->getResourceInterfaces())
434 std::cout << "\t\t" << resourceInterfaces << std::endl;
439 // Resource is invalid
440 std::cout << "Resource is invalid" << std::endl;
444 catch (std::exception &e)
446 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
452 std::cout << std::endl;
453 std::cout << "---------------------------------------------------------------------\n";
454 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
455 std::cout << " ObserveType : 1 - Observe" << std::endl;
456 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
457 std::cout << "---------------------------------------------------------------------\n\n";
460 void checkObserverValue(int value)
464 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
465 std::cout << "<===Setting ObserveType to Observe===>\n\n";
469 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
470 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
474 std::cout << "<===Invalid ObserveType selected."
475 << " Setting ObserveType to Observe===>\n\n";
479 static FILE *client_open(const char *path, const char *mode)
481 return fopen("./oic_svr_db_client.json", mode);
484 int main(int argc, char *argv[])
487 std::ostringstream requestURI;
488 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
494 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
498 checkObserverValue(std::stoi(argv[1]));
502 std::cout << "<===Invalid number of command line arguments===>\n\n";
506 catch (std::exception &)
508 std::cout << "<===Invalid input arguments===>\n\n";
512 // Create PlatformConfig object
515 OC::ServiceType::InProc,
519 OC::QualityOfService::LowQos,
523 OCPlatform::Configure(cfg);
526 // makes it so that all boolean values are printed as 'true/false' in this stream
527 std::cout.setf(std::ios::boolalpha);
528 // Find all resources
529 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
531 OCPlatform::findResource("", requestURI.str(),
532 CT_DEFAULT, &foundResource);
533 std::cout << "Finding Resource... " << std::endl;
535 // Find resource is done twice so that we discover the original resources a second time.
536 // These resources will have the same uniqueidentifier (yet be different objects), so that
537 // we can verify/show the duplicate-checking code in foundResource(above);
538 OCPlatform::findResource("", requestURI.str(),
539 CT_DEFAULT, &foundResource);
540 std::cout << "Finding Resource for second time..." << std::endl;
542 // A condition variable will free the mutex it is given, then do a non-
543 // intensive block until 'notify' is called on it. In this case, since we
544 // don't ever call cv.notify, this should be a non-processor intensive version
547 std::condition_variable cv;
548 std::unique_lock<std::mutex> lock(blocker);
552 catch (OCException &e)
554 oclog() << "Exception in main: " << e.what();