iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / examples / old_tests / MyMultiResourceHandler.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 #include <iostream>
22
23 #include "ocapi.h"
24 #include "OCReflect.h"
25 #include "OCObserver.h"
26 #include "OCException.h"
27 #include "MyMultiResourceHandler.h"
28
29 namespace {
30
31 void log(const std::string& msg)
32 {
33  std::cerr << msg << '\n';
34 }
35
36 } // namespace
37
38 void OC::MyMultiResourceHandler::onFoundResource(OCResourceResult *update, void *params){
39
40         //Step1: Find a specific resource that also contains OCReflect byte stream of methods and properties
41         for( auto &resource : update->resources ) 
42         {
43                 if(resource.getURI() == "/garage/dimmer/a")
44                 {
45                         performDimmerOperations(resource, update);
46                 }
47                 else if(resource.getURI() == "/garage/door/a")
48                 {
49                         performDoorOperations(resource, update);
50                 }
51         }
52 }
53
54 void OC::MyMultiResourceHandler::performDimmerOperations(OC::OCResource myResource, OC::OCResourceResult *update)
55 {
56         using OC::OCReflect::entity;
57         using OC::OCReflect::method;
58         using OC::OCReflect::remote_resource;
59
60         using OC::bind_observer;
61         using OC::unbind_observer;
62
63         try
64         {
65                 // Perform resource operation
66                 // Note that this resource dimmer has 
67                 // 'manufacturer' as a property
68                 // 'powerState' as a observable property
69                 // 'setPowered' as a method 
70                 // 'getPowered' as a method 
71
72                 //Step1: Find a specific method and invoke it. or Find a specific property and use it.
73
74                 // Canonical one-step approach to get a callable function object:
75                 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
76                 auto setPowered = OCReflect::method(myResource, "setPowered");
77
78                 // invoke the remote method, 
79                 // invalid arguments return as an OC::OCReflect::reflect_exception
80                 setPowered(true); 
81
82                 // Canonical one-step approach to get a callable function object:
83                 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
84                 auto getPowered = OCReflect::method(myResource, "getPowered");
85
86                 // invoke the remote method, 
87                 // invalid arguments return as an OC::OCReflect::reflect_exception
88                 // bool power = getPowered(); // Use power variable to do other operations
89
90                 // Canonical one-step approach to access a property:
91                 std::string manufacturer = update->property<std::string>("manufacturer");
92
93                 //Example to observe a property
94                 bind_observer(&myObserverHandler, myResource, "powerState");
95
96                 //Example to unobserve a property
97                 unbind_observer(&myObserverHandler, myResource, "powerState");
98         }
99         catch(OC::reflection_exception& e)
100         {
101                 log(e.what());
102         }
103         catch(std::exception& e)
104         {
105                 log(e.what());
106         }
107 }
108
109 void OC::MyMultiResourceHandler::performDoorOperations(OC::OCResource myResource, OC::OCResourceResult *update)
110 {
111         // Perform resource operation
112         // Note that this resource door has 
113         // 'manufacturer' as a property
114         // 'setLockState' as a method 
115         // 'getLockState' as a method
116
117         try
118         {
119                 // Step1: Find a specific method and invoke it or find a specific property and use it.
120
121                 // Canonical one-step approach to get a callable function object:
122                 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
123                 auto setLockState = OCReflect::method(myResource, "setLockState");
124
125                 // invoke the remote method, 
126                 // invalid arguments return as an OC::OCReflect::reflect_exception
127                 setLockState(true); 
128
129                 // Canonical one-step approach to get a callable function object:
130                 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
131                 auto getLockState = OCReflect::method(myResource, "getLockState");
132
133                 // invoke the remote method, 
134                 // invalid arguments return as an OC::OCReflect::reflect_exception
135                 // bool lockState = getLockState(); // use lockState variable for any other operations
136
137                 // Canonical one-step approach to access a property
138                 std::string manufacturer = update->property<std::string>("manufacturer");
139         }
140         catch(OC::reflection_exception& e)
141         {
142                 log(e.what());
143         }
144         catch(std::exception& e)
145         {
146                 log(e.what());
147         }
148 }
149
150 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onCompleted()
151 {
152
153 }
154
155 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onFailed()
156 {
157
158 }