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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "OCReflect.h"
26 #include "OCObserver.h"
27 #include "OCException.h"
28 #include "MyResourceHandler.h"
30 void log(const std::string& msg)
32 std::cerr << msg << '\n';
35 MyResourceHandler::MyResourceHandler(void)
40 MyResourceHandler::~MyResourceHandler(void)
44 void MyResourceHandler::onFoundResource(OCResourceResult *update, void *params){
45 using OC::OCReflect::entity;
46 using OC::OCReflect::method;
47 using OC::OCReflect::remote_resource;
49 using OC::bind_observer;
50 using OC::unbind_observer;
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;
63 //Step2: Find a specific method and invoke it. or Find a specific property and use it.
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");
71 // Individual steps-- each may throw on failure:
72 remote_resource myLight(myResource, "lights/a"); // JFW: this may be subsumed by type OCResource
74 entity e_setPowered = myLight("setPowered");
76 method setPowered = OC::OCReflect::narrow<OCReflect::method>(e_setPowered);
80 // Canonical one-step approach to access a property:
81 std::string manufacturer = update->property<std::string>("manufacturer");
84 entity e_manufacturer = myResource.property("manufacturer");
85 std::string manufacturer_2 = OC::OCReflect::narrow<std::string>(e_manufacturer);
87 //Example to observe a property
88 bind_observer(&myObserverHandler, myResource, "PowerState");
90 //Example to unobserve a property
91 unbind_observer(&myObserverHandler, myResource, "PowerState");
93 catch(OC::reflection_exception& e)
97 catch(std::exception& e)
103 void MyResourceHandler::onCompleted(){
106 void MyResourceHandler::onFailed(){