Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / examples / client / MyMultiResourceHandler.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 #include <iostream>
7
8 #include "ocapi.h"
9 #include "OCReflect.h"
10 #include "OCObserver.h"
11 #include "OCException.h"
12 #include "MyMultiResourceHandler.h"
13
14 namespace {
15
16 void log(const std::string& msg)
17 {
18  std::cerr << msg << '\n';
19 }
20
21 } // namespace
22
23 void OC::MyMultiResourceHandler::onFoundResource(OCResourceResult *update, void *params){
24
25         //Step1: Find a specific resource that also contains OCReflect byte stream of methods and properties
26         for( auto &resource : update->resources ) 
27         {
28                 if(resource.getURI() == "/garage/dimmer/a")
29                 {
30                         performDimmerOperations(resource, update);
31                 }
32                 else if(resource.getURI() == "/garage/door/a")
33                 {
34                         performDoorOperations(resource, update);
35                 }
36         }
37 }
38
39 void OC::MyMultiResourceHandler::performDimmerOperations(OC::OCResource myResource, OC::OCResourceResult *update)
40 {
41         using OC::OCReflect::entity;
42         using OC::OCReflect::method;
43         using OC::OCReflect::remote_resource;
44
45         using OC::bind_observer;
46         using OC::unbind_observer;
47
48         try
49         {
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 
56
57                 //Step1: Find a specific method and invoke it. or Find a specific property and use it.
58
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");
62
63                 // invoke the remote method, 
64                 // invalid arguments return as an OC::OCReflect::reflect_exception
65                 setPowered(true); 
66
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");
70
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
74
75                 // Canonical one-step approach to access a property:
76                 std::string manufacturer = update->property<std::string>("manufacturer");
77
78                 //Example to observe a property
79                 bind_observer(&myObserverHandler, myResource, "powerState");
80
81                 //Example to unobserve a property
82                 unbind_observer(&myObserverHandler, myResource, "powerState");
83         }
84         catch(OC::OCReflect::reflection_exception& e)
85         {
86                 log(e.what());
87         }
88         catch(std::exception& e)
89         {
90                 log(e.what());
91         }
92 }
93
94 void OC::MyMultiResourceHandler::performDoorOperations(OC::OCResource myResource, OC::OCResourceResult *update)
95 {
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
101
102         try
103         {
104                 // Step1: Find a specific method and invoke it or find a specific property and use it.
105
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");
109
110                 // invoke the remote method, 
111                 // invalid arguments return as an OC::OCReflect::reflect_exception
112                 setLockState(true); 
113
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");
117
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
121
122                 // Canonical one-step approach to access a property
123                 std::string manufacturer = update->property<std::string>("manufacturer");
124         }
125         catch(OC::OCReflect::reflection_exception& e)
126         {
127                 log(e.what());
128         }
129         catch(std::exception& e)
130         {
131                 log(e.what());
132         }
133 }
134
135 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onCompleted()
136 {
137
138 }
139
140 void OC::MyMultiResourceHandler::MyMultiResourceHandler::onFailed()
141 {
142
143 }