1 //******************************************************************
3 // Copyright 2014 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 // OCClient.cpp : Defines the entry point for the console application.
27 #include "OCPlatform.h"
28 #include "PluginManager.h"
34 const int SUCCESS_RESPONSE = 0;
35 std::shared_ptr<OCResource> curFanResource;
36 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
38 void putFanRepresentation(std::shared_ptr<OCResource> resource);
48 Fan() : m_state(false), m_power(0), m_name("")
61 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
62 const int &eCode, const int &sequenceNumber)
64 if (eCode == OC_STACK_OK)
66 std::cout << "OBSERVE RESULT:" << std::endl;
67 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
69 rep.getValue("state", myfan.m_state);
70 rep.getValue("power", myfan.m_power);
71 rep.getValue("name", myfan.m_name);
73 std::cout << "\tstate: " << myfan.m_state << std::endl;
74 std::cout << "\tpower: " << myfan.m_power << std::endl;
75 std::cout << "\tname: " << myfan.m_name << std::endl;
77 if (observe_count() > 30)
79 std::cout << "Cancelling Observe..." << std::endl;
80 OCStackResult result = curFanResource->cancelObserve();
82 std::cout << "Cancel result: " << result << std::endl;
84 std::cout << "DONE" << std::endl;
90 std::cout << "onObserve Response error: " << eCode << std::endl;
95 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
97 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
99 std::cout << "POST request was successful" << std::endl;
101 if (rep.hasAttribute("createduri"))
103 std::cout << "\tUri of the created resource: "
104 << rep.getValue<std::string>("createduri") << std::endl;
108 rep.getValue("state", myfan.m_state);
109 rep.getValue("power", myfan.m_power);
110 rep.getValue("name", myfan.m_name);
112 std::cout << "\tstate: " << myfan.m_state << std::endl;
113 std::cout << "\tpower: " << myfan.m_power << std::endl;
114 std::cout << "\tname: " << myfan.m_name << std::endl;
117 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
118 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
119 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
120 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
122 //curFanResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
127 std::cout << "onPost Response error: " << eCode << std::endl;
132 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
134 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
136 std::cout << "POST request was successful" << std::endl;
138 if (rep.hasAttribute("createduri"))
140 std::cout << "\tUri of the created resource: "
141 << rep.getValue<std::string>("createduri") << std::endl;
145 rep.getValue("state", myfan.m_state);
146 rep.getValue("power", myfan.m_power);
147 rep.getValue("name", myfan.m_name);
149 std::cout << "\tstate: " << myfan.m_state << std::endl;
150 std::cout << "\tpower: " << myfan.m_power << std::endl;
151 std::cout << "\tname: " << myfan.m_name << std::endl;
154 OCRepresentation rep2;
156 std::cout << "Posting fan representation..." << std::endl;
158 myfan.m_state = true;
161 rep2.setValue("state", myfan.m_state);
162 rep2.setValue("power", myfan.m_power);
164 curFanResource->post(rep2, QueryParamsMap(), &onPost2);
168 std::cout << "onPost Response error: " << eCode << std::endl;
173 // Local function to put a different state for this resource
174 void postFanRepresentation(std::shared_ptr<OCResource> resource)
178 OCRepresentation rep;
180 std::cout << "Posting fan representation..." << std::endl;
182 myfan.m_state = false;
185 rep.setValue("state", myfan.m_state);
186 rep.setValue("power", myfan.m_power);
188 // Invoke resource's post API with rep, query map and the callback parameter
189 resource->post(rep, QueryParamsMap(), &onPost);
193 // callback handler on PUT request
194 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
196 if (eCode == OC_STACK_OK)
198 std::cout << "PUT request was successful" << std::endl;
200 rep.getValue("state", myfan.m_state);
201 rep.getValue("power", myfan.m_power);
202 rep.getValue("name", myfan.m_name);
204 std::cout << "\tstate: " << myfan.m_state << std::endl;
205 std::cout << "\tpower: " << myfan.m_power << std::endl;
206 std::cout << "\tname: " << myfan.m_name << std::endl;
208 putFanRepresentation(curFanResource);
212 std::cout << "onPut Response error: " << eCode << std::endl;
217 // Local function to put a different state for this resource
218 void putFanRepresentation(std::shared_ptr<OCResource> resource)
222 OCRepresentation rep;
224 std::cout << "Putting fan representation..." << std::endl;
226 myfan.m_state = true;
227 if (myfan.m_power == 1)
232 rep.setValue("state", myfan.m_state);
233 rep.setValue("power", myfan.m_power);
235 // Invoke resource's put API with rep, query map and the callback parameter
236 resource->put(rep, QueryParamsMap(), &onPut);
240 // Callback handler on GET request
241 void onFanGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
243 if (eCode == OC_STACK_OK)
245 std::cout << "GET Fan request was successful" << std::endl;
246 std::cout << "Resource URI: " << rep.getUri() << std::endl;
248 rep.getValue("state", myfan.m_state);
249 rep.getValue("power", myfan.m_power);
250 rep.getValue("name", myfan.m_name);
252 std::cout << "\tstate: " << myfan.m_state << std::endl;
253 std::cout << "\tpower: " << myfan.m_power << std::endl;
254 std::cout << "\tname: " << myfan.m_name << std::endl;
256 putFanRepresentation(curFanResource);
260 std::cout << "onGET Response error: " << eCode << std::endl;
266 // Local function to get representation of fan resource
267 void getFanRepresentation(std::shared_ptr<OCResource> resource)
271 std::cout << "Getting Fan Representation..." << std::endl;
272 // Invoke resource's get API with the callback parameter
275 resource->get(test, &onFanGet);
279 // Callback to found resources
280 void foundResourceFan(std::shared_ptr<OCResource> resource)
284 std::cout << "Found another resource, ignoring" << std::endl;
287 std::string resourceURI;
288 std::string hostAddress;
291 // Do some operations with resource object.
294 std::cout << "DISCOVERED Resource:" << std::endl;
295 // Get the resource URI
296 resourceURI = resource->uri();
297 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
299 // Get the resource host address
300 hostAddress = resource->host();
301 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
303 // Get the resource types
304 std::cout << "\tList of resource types: " << std::endl;
305 for (auto &resourceTypes : resource->getResourceTypes())
307 std::cout << "\t\t" << resourceTypes << std::endl;
310 // Get the resource interfaces
311 std::cout << "\tList of resource interfaces: " << std::endl;
312 for (auto &resourceInterfaces : resource->getResourceInterfaces())
314 std::cout << "\t\t" << resourceInterfaces << std::endl;
317 if (resourceURI == "/a/fan")
319 curFanResource = resource;
320 // Call a local function which will internally invoke get API on the resource pointer
321 putFanRepresentation(curFanResource);
326 // Resource is invalid
327 std::cout << "Resource is invalid" << std::endl;
331 catch (std::exception &e)
333 std::cout << "Exception: " << e.what() << std::endl;
339 std::cout << std::endl;
340 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
341 std::cout << " ObserveType : 1 - Observe" << std::endl;
342 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
347 int main(int argc, char *argv[])
350 std::string key = "Name";
351 std::string state = "";
353 std::ostringstream requestURI;
357 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
361 int value = atoi(argv[1]);
363 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
365 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
367 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
375 // Create PlatformConfig object
378 OC::ServiceType::InProc,
382 OC::QualityOfService::LowQos
385 OCPlatform::Configure(cfg);
389 PluginManager *m_pm = new PluginManager();
390 std::cout << "start light Plugin by Resource Type" << std::endl;
391 m_pm->startPlugins("ResourceType", "oic.fan");
393 // makes it so that all boolean values are printed as 'true/false' in this stream
394 std::cout.setf(std::ios::boolalpha);
395 // Find all resources
396 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.fan";
397 OCPlatform::findResource("", requestURI.str(), OC_ALL, &foundResourceFan);
398 std::cout << "Finding Resource... " << std::endl;
405 catch (OCException &e)
407 std::cout << "Exception: " << e.what() << std::endl;