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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file mqttclient.cpp
23 /// @brief Samplecode which controls MQTT-fan plugin using Protocol Plugin Manager.
28 #include "OCPlatform.h"
29 #include "PluginManager.h"
35 const int SUCCESS_RESPONSE = 0;
36 std::shared_ptr<OCResource> curFanResource;
37 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 void putFanRepresentation(std::shared_ptr<OCResource> resource);
49 Fan() : m_state(false), m_power(0), m_name("")
62 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
63 const int &eCode, const int &sequenceNumber)
65 if (eCode == OC_STACK_OK)
67 std::cout << "OBSERVE RESULT:" << std::endl;
68 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
70 rep.getValue("state", myfan.m_state);
71 rep.getValue("power", myfan.m_power);
72 rep.getValue("name", myfan.m_name);
74 std::cout << "\tstate: " << myfan.m_state << std::endl;
75 std::cout << "\tpower: " << myfan.m_power << std::endl;
76 std::cout << "\tname: " << myfan.m_name << std::endl;
78 if (observe_count() > 30)
80 std::cout << "Cancelling Observe..." << std::endl;
81 OCStackResult result = curFanResource->cancelObserve();
83 std::cout << "Cancel result: " << result << std::endl;
85 std::cout << "DONE" << std::endl;
91 std::cout << "onObserve Response error: " << eCode << std::endl;
96 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
98 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
100 std::cout << "POST request was successful" << std::endl;
102 if (rep.hasAttribute("createduri"))
104 std::cout << "\tUri of the created resource: "
105 << rep.getValue<std::string>("createduri") << std::endl;
109 rep.getValue("state", myfan.m_state);
110 rep.getValue("power", myfan.m_power);
111 rep.getValue("name", myfan.m_name);
113 std::cout << "\tstate: " << myfan.m_state << std::endl;
114 std::cout << "\tpower: " << myfan.m_power << std::endl;
115 std::cout << "\tname: " << myfan.m_name << std::endl;
118 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
119 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
120 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
121 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
123 //curFanResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
128 std::cout << "onPost Response error: " << eCode << std::endl;
133 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
135 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
137 std::cout << "POST request was successful" << std::endl;
139 if (rep.hasAttribute("createduri"))
141 std::cout << "\tUri of the created resource: "
142 << rep.getValue<std::string>("createduri") << std::endl;
146 rep.getValue("state", myfan.m_state);
147 rep.getValue("power", myfan.m_power);
148 rep.getValue("name", myfan.m_name);
150 std::cout << "\tstate: " << myfan.m_state << std::endl;
151 std::cout << "\tpower: " << myfan.m_power << std::endl;
152 std::cout << "\tname: " << myfan.m_name << std::endl;
155 OCRepresentation rep2;
157 std::cout << "Posting fan representation..." << std::endl;
159 myfan.m_state = true;
162 rep2.setValue("state", myfan.m_state);
163 rep2.setValue("power", myfan.m_power);
165 curFanResource->post(rep2, QueryParamsMap(), &onPost2);
169 std::cout << "onPost Response error: " << eCode << std::endl;
174 // Local function to put a different state for this resource
175 void postFanRepresentation(std::shared_ptr<OCResource> resource)
179 OCRepresentation rep;
181 std::cout << "Posting fan representation..." << std::endl;
183 myfan.m_state = false;
186 rep.setValue("state", myfan.m_state);
187 rep.setValue("power", myfan.m_power);
189 // Invoke resource's post API with rep, query map and the callback parameter
190 resource->post(rep, QueryParamsMap(), &onPost);
194 // callback handler on PUT request
195 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
197 if (eCode == OC_STACK_OK)
199 std::cout << "PUT request was successful" << std::endl;
201 rep.getValue("state", myfan.m_state);
202 rep.getValue("power", myfan.m_power);
203 rep.getValue("name", myfan.m_name);
205 std::cout << "\tstate: " << myfan.m_state << std::endl;
206 std::cout << "\tpower: " << myfan.m_power << std::endl;
207 std::cout << "\tname: " << myfan.m_name << std::endl;
209 putFanRepresentation(curFanResource);
213 std::cout << "onPut Response error: " << eCode << std::endl;
218 // Local function to put a different state for this resource
219 void putFanRepresentation(std::shared_ptr<OCResource> resource)
223 OCRepresentation rep;
225 std::cout << "Putting fan representation..." << std::endl;
227 myfan.m_state = true;
228 if (myfan.m_power == 1)
233 rep.setValue("state", myfan.m_state);
234 rep.setValue("power", myfan.m_power);
236 // Invoke resource's put API with rep, query map and the callback parameter
237 resource->put(rep, QueryParamsMap(), &onPut);
241 // Callback handler on GET request
242 void onFanGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
244 if (eCode == OC_STACK_OK)
246 std::cout << "GET Fan request was successful" << std::endl;
247 std::cout << "Resource URI: " << rep.getUri() << std::endl;
249 rep.getValue("state", myfan.m_state);
250 rep.getValue("power", myfan.m_power);
251 rep.getValue("name", myfan.m_name);
253 std::cout << "\tstate: " << myfan.m_state << std::endl;
254 std::cout << "\tpower: " << myfan.m_power << std::endl;
255 std::cout << "\tname: " << myfan.m_name << std::endl;
257 putFanRepresentation(curFanResource);
261 std::cout << "onGET Response error: " << eCode << std::endl;
267 // Local function to get representation of fan resource
268 void getFanRepresentation(std::shared_ptr<OCResource> resource)
272 std::cout << "Getting Fan Representation..." << std::endl;
273 // Invoke resource's get API with the callback parameter
276 resource->get(test, &onFanGet);
280 // Callback to found resources
281 void foundResourceFan(std::shared_ptr<OCResource> resource)
285 std::cout << "Found another resource, ignoring" << std::endl;
288 std::string resourceURI;
289 std::string hostAddress;
292 // Do some operations with resource object.
295 std::cout << "DISCOVERED Resource:" << std::endl;
296 // Get the resource URI
297 resourceURI = resource->uri();
298 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
300 // Get the resource host address
301 hostAddress = resource->host();
302 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
304 // Get the resource types
305 std::cout << "\tList of resource types: " << std::endl;
306 for (auto &resourceTypes : resource->getResourceTypes())
308 std::cout << "\t\t" << resourceTypes << std::endl;
311 // Get the resource interfaces
312 std::cout << "\tList of resource interfaces: " << std::endl;
313 for (auto &resourceInterfaces : resource->getResourceInterfaces())
315 std::cout << "\t\t" << resourceInterfaces << std::endl;
318 if (resourceURI == "/a/fan")
320 curFanResource = resource;
321 // Call a local function which will internally invoke get API on the resource pointer
322 putFanRepresentation(curFanResource);
327 // Resource is invalid
328 std::cout << "Resource is invalid" << std::endl;
332 catch (std::exception &e)
334 std::cout << "Exception: " << e.what() << std::endl;
340 std::cout << std::endl;
341 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
342 std::cout << " ObserveType : 1 - Observe" << std::endl;
343 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
348 int main(int argc, char *argv[])
351 std::string key = "Name";
352 std::string state = "";
354 std::ostringstream requestURI;
358 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
362 int value = atoi(argv[1]);
364 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
366 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
368 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
376 // Create PlatformConfig object
379 OC::ServiceType::InProc,
383 OC::QualityOfService::LowQos
386 OCPlatform::Configure(cfg);
390 PluginManager *m_pm = new PluginManager();
391 std::cout << "start light Plugin by Resource Type" << std::endl;
392 m_pm->startPlugins("ResourceType", "oic.fan");
394 // makes it so that all boolean values are printed as 'true/false' in this stream
395 std::cout.setf(std::ios::boolalpha);
396 // Find all resources
397 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.fan";
398 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResourceFan);
399 std::cout << "Finding Resource... " << std::endl;
406 catch (OCException &e)
408 std::cout << "Exception: " << e.what() << std::endl;