1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
10 #include "OCObserver.h"
11 #include "OCException.h"
12 #include "MyMultiResourceHandler.h"
16 void log(const std::string& msg)
18 std::cerr << msg << '\n';
23 void OC::MyMultiResourceHandler::onFoundResource(OCResourceResult *update, void *params){
25 //Step1: Find a specific resource that also contains OCReflect byte stream of methods and properties
26 for( auto &resource : update->resources )
28 if(resource.getURI() == "/garage/dimmer/a")
30 performDimmerOperations(resource, update);
32 else if(resource.getURI() == "/garage/door/a")
34 performDoorOperations(resource, update);
39 void OC::MyMultiResourceHandler::performDimmerOperations(OC::OCResource myResource, OC::OCResourceResult *update)
41 using OC::OCReflect::entity;
42 using OC::OCReflect::method;
43 using OC::OCReflect::remote_resource;
45 using OC::bind_observer;
46 using OC::unbind_observer;
50 // Perform resource operation
51 // Note that this resource dimmer has
52 // 'manufacturer' as a property
53 // 'powerState' as a observable property
54 // 'setPowered' as a method
55 // 'getPowered' as a method
57 //Step1: Find a specific method and invoke it. or Find a specific property and use it.
59 // Canonical one-step approach to get a callable function object:
60 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
61 auto setPowered = OCReflect::method(myResource, "setPowered");
63 // invoke the remote method,
64 // invalid arguments return as an OC::OCReflect::reflect_exception
67 // Canonical one-step approach to get a callable function object:
68 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
69 auto getPowered = OCReflect::method(myResource, "getPowered");
71 // invoke the remote method,
72 // invalid arguments return as an OC::OCReflect::reflect_exception
73 // bool power = getPowered(); // Use power variable to do other operations
75 // Canonical one-step approach to access a property:
76 std::string manufacturer = update->property<std::string>("manufacturer");
78 //Example to observe a property
79 bind_observer(&myObserverHandler, myResource, "powerState");
81 //Example to unobserve a property
82 unbind_observer(&myObserverHandler, myResource, "powerState");
84 catch(OC::OCReflect::reflection_exception& e)
88 catch(std::exception& e)
94 void OC::MyMultiResourceHandler::performDoorOperations(OC::OCResource myResource, OC::OCResourceResult *update)
96 // Perform resource operation
97 // Note that this resource door has
98 // 'manufacturer' as a property
99 // 'setLockState' as a method
100 // 'getLockState' as a method
104 // Step1: Find a specific method and invoke it or find a specific property and use it.
106 // Canonical one-step approach to get a callable function object:
107 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
108 auto setLockState = OCReflect::method(myResource, "setLockState");
110 // invoke the remote method,
111 // invalid arguments return as an OC::OCReflect::reflect_exception
114 // Canonical one-step approach to get a callable function object:
115 // OCReflect::method throw OC::OCReflect::reflect_exception if there is no setLevel method in the myResource
116 auto getLockState = OCReflect::method(myResource, "getLockState");
118 // invoke the remote method,
119 // invalid arguments return as an OC::OCReflect::reflect_exception
120 // bool lockState = getLockState(); // use lockState variable for any other operations
122 // Canonical one-step approach to access a property
123 std::string manufacturer = update->property<std::string>("manufacturer");
125 catch(OC::OCReflect::reflection_exception& e)
129 catch(std::exception& e)
135 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onCompleted()
140 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onFailed()