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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "OCReflect.h"
25 #include "OCObserver.h"
26 #include "OCException.h"
27 #include "MyMultiResourceHandler.h"
31 void log(const std::string& msg)
33 std::cerr << msg << '\n';
38 void OC::MyMultiResourceHandler::onFoundResource(OCResourceResult *update, void *params){
40 //Step1: Find a specific resource that also contains OCReflect byte stream of methods and properties
41 for( auto &resource : update->resources )
43 if(resource.getURI() == "/garage/dimmer/a")
45 performDimmerOperations(resource, update);
47 else if(resource.getURI() == "/garage/door/a")
49 performDoorOperations(resource, update);
54 void OC::MyMultiResourceHandler::performDimmerOperations(OC::OCResource myResource, OC::OCResourceResult *update)
56 using OC::OCReflect::entity;
57 using OC::OCReflect::method;
58 using OC::OCReflect::remote_resource;
60 using OC::bind_observer;
61 using OC::unbind_observer;
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
72 //Step1: Find a specific method and invoke it. or Find a specific property and use it.
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");
78 // invoke the remote method,
79 // invalid arguments return as an OC::OCReflect::reflect_exception
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");
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
90 // Canonical one-step approach to access a property:
91 std::string manufacturer = update->property<std::string>("manufacturer");
93 //Example to observe a property
94 bind_observer(&myObserverHandler, myResource, "powerState");
96 //Example to unobserve a property
97 unbind_observer(&myObserverHandler, myResource, "powerState");
99 catch(OC::reflection_exception& e)
103 catch(std::exception& e)
109 void OC::MyMultiResourceHandler::performDoorOperations(OC::OCResource myResource, OC::OCResourceResult *update)
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
119 // Step1: Find a specific method and invoke it or find a specific property and use it.
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");
125 // invoke the remote method,
126 // invalid arguments return as an OC::OCReflect::reflect_exception
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");
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
137 // Canonical one-step approach to access a property
138 std::string manufacturer = update->property<std::string>("manufacturer");
140 catch(OC::reflection_exception& e)
144 catch(std::exception& e)
150 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onCompleted()
155 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onFailed()