Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / examples / client / MyResourceHandler.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 #include <iostream>
8
9 #include "ocapi.h"
10 #include "OCReflect.h"
11 #include "OCObserver.h"
12 #include "OCException.h"
13 #include "MyResourceHandler.h"
14
15 void log(const std::string& msg)
16 {
17  std::cerr << msg << '\n';
18 }
19
20 MyResourceHandler::MyResourceHandler(void)
21 {
22 }
23
24
25 MyResourceHandler::~MyResourceHandler(void)
26 {
27 }
28
29 void MyResourceHandler::onFoundResource(OCResourceResult *update, void *params){
30         using OC::OCReflect::entity;
31         using OC::OCReflect::method;
32         using OC::OCReflect::remote_resource;
33
34         using OC::bind_observer;
35         using OC::unbind_observer;
36
37         try
38         {
39                 //Step1: Find a specific resource that also contains OCReflect byte stream of methods and properties
40                 OCResource myResource;
41                 for ( auto &resource : update->resources ) {
42                         if(resource.getURI() == "/light/a"){
43                                 myResource = resource;
44                                 break;
45                         }
46                 }
47
48                 //Step2: Find a specific method and invoke it. or Find a specific property and use it.
49
50                 // Canonical one-step approach to get a callable function object:
51                 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
52                 auto setLevel = OC::OCReflect::method(myResource, "setLevel");
53
54                 setLevel(75, 255); 
55
56                 // Individual steps-- each may throw on failure:
57                 remote_resource myLight(myResource, "lights/a");        // JFW: this may be subsumed by type OCResource
58
59                 entity e_setPowered = myLight("setPowered");
60
61                 method setPowered = OC::OCReflect::narrow<OCReflect::method>(e_setPowered);
62
63                 setPowered(true);
64
65                 // Canonical one-step approach to access a property:
66                 std::string manufacturer = update->property<std::string>("manufacturer");
67
68                 // Individual steps:
69                 entity e_manufacturer = myResource.property("manufacturer");
70                 std::string manufacturer_2 = OC::OCReflect::narrow<std::string>(e_manufacturer);
71
72                 //Example to observe a property
73                 bind_observer(&myObserverHandler, myResource, "PowerState");
74
75                 //Example to unobserve a property
76                 unbind_observer(&myObserverHandler, myResource, "PowerState");
77         }
78         catch(OC::OCReflect::reflection_exception& e)
79         {
80                 log(e.what());
81         }
82         catch(std::exception& e)
83         {
84                 log(e.what());
85         }
86 }
87
88 void MyResourceHandler::onCompleted(){
89 }
90
91 void MyResourceHandler::onFailed(){
92 }