New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / examples / roomclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // OCClient.cpp : Defines the entry point for the console application.
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33
34 int observe_count()
35 {
36     static int oc = 0;
37     return ++oc;
38 }
39
40 // Forward declaration
41 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
42 void onPut(const OCRepresentation& rep, const int eCode);
43
44 // callback handler on GET request
45 void onGet(const OCRepresentation& rep, const int eCode)
46 {
47     if(eCode == SUCCESS_RESPONSE)
48     {
49         std::cout << "GET request was successful" << std::endl;
50
51         std::cout << "\tResource URI: " << rep.getUri() << std::endl;
52
53         std::vector<OCRepresentation> children = rep.getChildren();
54
55         for(auto oit = children.begin(); oit != children.end(); ++oit)
56         {
57             std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
58             if(oit->getUri().find("light") != std::string::npos)
59             {
60                 bool state = false;
61                 int  color = 0;
62                 oit->getValue("state", state);
63                 oit->getValue("color", color);
64
65                 std::cout << "\t\tstate:" << state << std::endl;
66                 std::cout << "\t\tcolor:" << color << std::endl;
67             }
68             else if(oit->getUri().find("fan") != std::string::npos)
69             {
70                 bool state = false;
71                 int  speed = 0;
72                 oit->getValue("state", state);
73                 oit->getValue("speed", speed);
74
75                 std::cout << "\t\tstate:" << state << std::endl;
76                 std::cout << "\t\tspeed:" << speed << std::endl;
77             }
78         }
79
80         putRoomRepresentation(curResource);
81     }
82     else
83     {
84         std::cout << "onGET Response error: " << eCode << std::endl;
85         std::exit(-1);
86     }
87 }
88
89 // Local function to put a different state for this resource
90 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
91 {
92     if(resource)
93     {
94         OCRepresentation rep;
95         std::cout << "Putting room representation..."<<std::endl;
96
97         bool state = true;
98         int speed = 10;
99         rep.setValue("state", state);
100         rep.setValue("speed", speed);
101
102         // Invoke resource's pit API with attribute map, query map and the callback parameter
103         resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
104     }
105 }
106
107 // callback handler on PUT request
108 void onPut(const OCRepresentation& rep, const int eCode)
109 {
110     if(eCode == SUCCESS_RESPONSE)
111     {
112         std::cout << "PUT request was successful" << std::endl;
113
114         std::cout << "\tResource URI: " << rep.getUri() << std::endl;
115
116         std::vector<OCRepresentation> children = rep.getChildren();
117
118         for(auto oit = children.begin(); oit != children.end(); ++oit)
119         {
120             std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
121             if(oit->getUri().find("light") != std::string::npos)
122             {
123                 bool state = false;
124                 int  color = 0;
125                 oit->getValue("state", state);
126                 oit->getValue("color", color);
127                 std::cout << "\t\tstate:" << state << std::endl;
128                 std::cout << "\t\tcolor:" << color << std::endl;
129             }
130             else if(oit->getUri().find("fan") != std::string::npos)
131             {
132                 bool state = false;
133                 int  speed = 0;
134                 oit->getValue("state", state);
135                 oit->getValue("speed", speed);
136                 std::cout << "\t\tstate:" << state << std::endl;
137                 std::cout << "\t\tspeed:" << speed << std::endl;
138             }
139
140         }
141
142     }
143     else
144     {
145         std::cout << "onPut Response error: " << eCode << std::endl;
146         std::exit(-1);
147     }
148 }
149
150
151 // Local function to get representation of light resource
152 void getRoomRepresentation(std::shared_ptr<OCResource> resource)
153 {
154     if(resource)
155     {
156         std::cout << "Getting Room Representation..."<<std::endl;
157         // Invoke resource's get API with the callback parameter
158
159         QueryParamsMap qp;
160         resource->get("core.room", BATCH_INTERFACE, qp, &onGet);
161     }
162 }
163
164 // Callback to found resources
165 void foundResource(std::shared_ptr<OCResource> resource)
166 {
167     if(curResource)
168     {
169         std::cout << "Found another resource, ignoring"<<std::endl;
170     }
171
172     std::string resourceURI;
173     std::string hostAddress;
174     try
175     {
176         // Do some operations with resource object.
177         if(resource)
178         {
179             std::cout<<"DISCOVERED Resource:"<<std::endl;
180             // Get the resource URI
181             resourceURI = resource->uri();
182             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
183
184             // Get the resource host address
185             hostAddress = resource->host();
186             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
187
188             // Get the resource types
189             std::cout << "\tList of resource types: " << std::endl;
190             for(auto &resourceTypes : resource->getResourceTypes())
191             {
192                 std::cout << "\t\t" << resourceTypes << std::endl;
193             }
194
195             // Get the resource interfaces
196             std::cout << "\tList of resource interfaces: " << std::endl;
197             for(auto &resourceInterfaces : resource->getResourceInterfaces())
198             {
199                 std::cout << "\t\t" << resourceInterfaces << std::endl;
200             }
201
202             if(resourceURI == "/a/room")
203             {
204                 curResource = resource;
205                 // Call a local function which will internally invoke get API on the resource pointer
206                 getRoomRepresentation(resource);
207             }
208         }
209         else
210         {
211             // Resource is invalid
212             std::cout << "Resource is invalid" << std::endl;
213         }
214
215     }
216     catch(std::exception& e)
217     {
218         //log(e.what());
219     }
220 }
221
222 int main(int argc, char* argv[]) {
223
224     // Create PlatformConfig object
225     PlatformConfig cfg {
226         OC::ServiceType::InProc,
227         OC::ModeType::Client,
228         "0.0.0.0",
229         0,
230         OC::QualityOfService::NonConfirmable
231     };
232
233     // Create a OCPlatform instance.
234     // Note: Platform creation is synchronous call.
235
236     try
237     {
238         OCPlatform platform(cfg);
239         std::cout << "Created Platform..."<<std::endl;
240         // Find all resources
241         platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
242         std::cout<< "Finding Resource... " <<std::endl;
243         while(true)
244         {
245             // some operations
246         }
247
248     }catch(OCException& e)
249     {
250         //log(e.what());
251     }
252
253     return 0;
254 }
255