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