1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
10 #include "OCReflect.h"
11 #include "OCObserver.h"
12 #include "OCException.h"
13 #include "MyResourceHandler.h"
15 void log(const std::string& msg)
17 std::cerr << msg << '\n';
20 MyResourceHandler::MyResourceHandler(void)
25 MyResourceHandler::~MyResourceHandler(void)
29 void MyResourceHandler::onFoundResource(OCResourceResult *update, void *params){
30 using OC::OCReflect::entity;
31 using OC::OCReflect::method;
32 using OC::OCReflect::remote_resource;
34 using OC::bind_observer;
35 using OC::unbind_observer;
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;
48 //Step2: Find a specific method and invoke it. or Find a specific property and use it.
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");
56 // Individual steps-- each may throw on failure:
57 remote_resource myLight(myResource, "lights/a"); // JFW: this may be subsumed by type OCResource
59 entity e_setPowered = myLight("setPowered");
61 method setPowered = OC::OCReflect::narrow<OCReflect::method>(e_setPowered);
65 // Canonical one-step approach to access a property:
66 std::string manufacturer = update->property<std::string>("manufacturer");
69 entity e_manufacturer = myResource.property("manufacturer");
70 std::string manufacturer_2 = OC::OCReflect::narrow<std::string>(e_manufacturer);
72 //Example to observe a property
73 bind_observer(&myObserverHandler, myResource, "PowerState");
75 //Example to unobserve a property
76 unbind_observer(&myObserverHandler, myResource, "PowerState");
78 catch(OC::OCReflect::reflection_exception& e)
82 catch(std::exception& e)
88 void MyResourceHandler::onCompleted(){
91 void MyResourceHandler::onFailed(){