1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 * @file SampleConsumer.cpp
23 * @brief Defines the entry point for the sample consumer application about Resource Hosting.
29 #include "OCPlatform.h"
35 const int SUCCESS_RESPONSE = OC_STACK_OK;
37 #define OC_WELL_KNOWN_COORDINATING_QUERY "/oc/core?rt=Resource.Hosting"
44 std::shared_ptr< OCResource > g_curResource;
45 std::shared_ptr< OCResource > g_curObserveResource;
46 std::mutex curResourceLock;
48 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName);
49 void onObserve(const HeaderOptions &headerOption , const OCRepresentation &rep , const int &eCode,
50 const int &sequenceNumber);
52 void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode);
53 void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode);
54 void onDelete(const HeaderOptions &headerOption , const int eCode);
57 void findResourceCandidate()
61 nmfindResource("" , OC_WELL_KNOWN_COORDINATING_QUERY);
62 std::cout << "Finding Resource... " << std::endl;
65 catch (OCException &e)
67 std::cout << "Exception for find resource : " << e.reason() << std::endl;
71 void startObserve(std::shared_ptr< OCResource > resource)
75 std::cout << "startObserve() error : resource == null" << std::endl;
79 if(g_curObserveResource == NULL)
81 g_curObserveResource = resource;
82 std::cout << "request for new observation" << std::endl;
84 else if(g_curObserveResource == g_curResource)
86 std::cout << "already registered same observation" << std::endl;
91 std::cout << "change observed resource" << std::endl;
92 g_curObserveResource->cancelObserve();
93 g_curObserveResource = resource;
97 if (OC_STACK_OK != resource->observe(ObserveType::Observe , test , &onObserve))
98 std::cout << "To Fail resource observe() process" << std::endl;
101 void startGet(std::shared_ptr< OCResource > resource)
104 if (resource == NULL)
106 std::cout << "startObserve() error : resource == null" << std::endl;
111 std::cout << "URI :" << resource->uri() << std::endl;
112 if (OC_STACK_OK != resource->get(test, &onGet))
113 std::cout << "To Fail resource get() process" << std::endl;
116 void startPut(std::shared_ptr< OCResource > resource)
118 if (resource == NULL)
120 std::cout << "startObserve() error : resource == null" << std::endl;
124 g_curResource = resource;
125 OCRepresentation rep;
126 rep.setValue("temperature", 25);
127 rep.setValue("humidity", 10);
130 if (OC_STACK_OK != resource->put(rep, test, &onPut))
131 std::cout << "To Fail resource put() process" << std::endl;
134 void startDelete(std::shared_ptr< OCResource > resource)
136 if (resource == NULL)
138 std::cout << "startObserve() error : resource == null" << std::endl;
142 g_curResource = resource;
143 if (OC_STACK_OK != resource->deleteResource(&onDelete))
144 std::cout << "To Fail resource delete() process" << std::endl;
153 void onObserve(const HeaderOptions &headerOption , const OCRepresentation &rep , const int &eCode,
154 const int &sequenceNumber)
156 std::cout << "onObserve" << std::endl;
158 if (eCode <= OC_STACK_OK)
160 std::cout << std::endl;
161 std::cout << "========================================================" << std::endl;
162 std::cout << "Receive OBSERVE RESULT:" << std::endl;
163 std::cout << "\tUri: " << rep.getUri() << std::endl;
164 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
165 std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
166 std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
168 if (observe_count() > 30)
170 std::cout << "Cancelling Observe..." << std::endl;
171 OCStackResult result = g_curResource->cancelObserve();
173 std::cout << "Cancel result: " << result << std::endl;
175 std::cout << "DONE" << std::endl;
181 std::cout << "onObserve Response error: " << eCode << std::endl;
186 void foundResource(std::shared_ptr< OCResource > resource)
188 std::string resourceURI;
189 std::string hostAddress;
191 std::cout << "foundResource" << std::endl;
195 std::cout << "mutex lock passed" << std::endl;
198 std::cout << resource->uri() << std::endl;
199 if (resource->uri() == "/a/TempHumSensor")
201 std::cout << std::endl;
202 std::cout << "========================================================" << std::endl;
203 std::cout << "DISCOVERED Resource(Consumer):" << std::endl;
205 resourceURI = resource->uri();
206 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
208 hostAddress = resource->host();
209 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
211 g_curResource = resource;
216 std::cout << "Resource is invalid" << std::endl;
220 catch (std::exception &e)
222 std::cout << "Exception: " << e.what() << " in foundResource" << std::endl;
226 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName)
228 return OCPlatform::findResource(host , resourceName , OC_ALL, &foundResource);
231 void getRepresentation(std::shared_ptr< OCResource > resource)
235 std::cout << "Getting Light Representation..." << std::endl;
239 void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode)
243 if (eCode == OC_STACK_OK)
245 std::cout << "PUT request was successful" << std::endl;
248 rep.getValue("temperature", temperature);
249 rep.getValue("humidity", humidity);
252 std::cout << "\t temperature: " << temperature << std::endl;
253 std::cout << "\t humidity: " << humidity << std::endl;
257 std::cout << "onPut Response error: " << eCode << std::endl;
261 catch (std::exception &e)
263 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
267 //callback hadnler on DELETE request
268 void onDelete(const HeaderOptions &headerOption , const int eCode)
272 if (eCode == OC_STACK_RESOURCE_DELETED)
274 std::cout << "DELETE request was successful" << std::endl;
278 std::cout << "onDelete Response error: " << eCode << std::endl;
282 catch (std::exception &e)
284 std::cout << "Exception: " << e.what() << " in onDelete" << std::endl;
288 // callback handler on GET request
289 void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode)
291 std::cout << "GET request was successful1" << std::endl;
292 if (eCode == SUCCESS_RESPONSE)
294 std::cout << "GET request was successful" << std::endl;
295 std::cout << "Resource URI: " << rep.getUri().c_str() << std::endl;
296 std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
297 std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
301 std::cout << "onGET Response error: " << eCode << std::endl;
306 void getLightRepresentation(std::shared_ptr< OCResource > resource)
310 std::cout << "Getting Light Representation..." << std::endl;
313 resource->get(test , &onGet);
319 std::cout << std::endl;
320 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
321 std::cout << " ObserveType : 1 - Observe" << std::endl;
322 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
327 std::cout << std::endl;
328 std::cout << "********************************************" << std::endl;
329 std::cout << "* method Type : 1 - Observe *" << std::endl;
330 std::cout << "* method Type : 2 - Get *" << std::endl;
331 std::cout << "* method Type : 3 - Put *" << std::endl;
332 std::cout << "* method Type : 4 - Delete *" << std::endl;
333 std::cout << "********************************************" << std::endl;
334 std::cout << std::endl;
337 int main(int argc , char *argv[])
343 OCPlatform::Configure(cfg);
345 std::cout << "Created Platform..." << std::endl;
347 g_curResource = NULL;
348 g_curObserveResource = NULL;
350 findResourceCandidate();
355 if(g_curResource == NULL)
367 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
368 std::cout << "Invalid input type, please try again" << std::endl;
375 startObserve(g_curResource);
378 startGet(g_curResource);
381 startPut(g_curResource);
385 startDelete(g_curResource);
386 }catch(OCException e) {
387 std::cout<< "Caught OCException [Code: "<<e.code()<<" Reason: "<<e.reason()<<std::endl;
391 std::cout << "Invalid input, please try again" << std::endl;