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 #include <OCPlatform.h>
30 #include <GroupManager.h>
35 namespace PH = std::placeholders;
39 OCResourceHandle resourceHandle;
40 std::vector<OCResourceHandle> resourceHandleVector;
42 shared_ptr<OCResource> g_resource;
43 vector<string> lights;
45 GroupManager *groupMgr = new GroupManager();
47 void onGet(const HeaderOptions& opt, const OCRepresentation &rep,
50 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
53 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep,
56 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
57 const int& eCode, const int& sequenceNumber);
62 shared_ptr<OCResource> g_light;
65 std::vector<std::shared_ptr<OC::OCResource> > listOfResource)
68 for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end();
71 std::string resourceURI = (*rsrc)->uri();
72 std::string hostAddress = (*rsrc)->host();
74 if (resourceURI == "/a/light")
76 cout << "\tResource URI : " << resourceURI << endl;
77 cout << "\tResource Host : " << hostAddress << endl;
79 OCResourceHandle foundResourceHandle;
80 OCStackResult result = OCPlatform::registerResource(
81 foundResourceHandle, (*rsrc));
82 cout << "\tresource registed!" << endl;
83 if (result == OC_STACK_OK)
85 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
86 resourceHandleVector.push_back(foundResourceHandle);
90 cout << "\tresource Error!" << endl;
93 lights.push_back((hostAddress + resourceURI));
102 void foundResource(std::shared_ptr<OCResource> resource)
104 std::string resourceURI;
105 std::string hostAddress;
107 cout << "FOUND RESOURCE" << endl;
113 resourceURI = resource->uri();
114 hostAddress = resource->host();
115 if (resourceURI == "/core/a/collection")
117 g_resource = resource;
119 // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
121 cout << "FOUND " << resourceURI << endl;
122 // printf("\tHOST :: %s\n", resource->host().c_str());
124 else if (resourceURI == "/core/bookmark")
126 resource->observe(ObserveType::Observe, QueryParamsMap(),
131 catch (std::exception& e)
133 std::cout << "Exception: " << e.what() << std::endl;
137 void onGet(const HeaderOptions& /*opt*/, const OCRepresentation &/*rep*/,
140 cout << "\nonGet" << endl;
141 if (eCode == OC_STACK_OK)
142 cout << "\tResult is OK." << endl;
144 cout << "\tInvalid parameter." << endl;
147 void onPut(const HeaderOptions& /*opt*/, const OCRepresentation &/*rep*/,
150 cout << "\nonPut" << endl;
151 if (eCode == OC_STACK_OK)
152 cout << "\tResult is OK." << endl;
154 cout << "\tInvalid parameter." << endl;
157 void onPost(const HeaderOptions& /*opt*/, const OCRepresentation &rep,
160 printf("\nonPost\n");
162 if (rep.hasAttribute("ActionSet"))
164 std::string plainText;
166 if (rep.getValue("ActionSet", plainText))
168 ActionSet *actionset = groupMgr->getActionSetfromString(plainText);
169 if (actionset != NULL)
171 cout << endl << "\tACTIONSET NAME :: "
172 << actionset->actionsetName << endl;
173 for (auto actIter = actionset->listOfAction.begin();
174 actIter != actionset->listOfAction.end(); ++actIter)
176 cout << "\t\tTARGET :: " << (*actIter)->target << endl;
178 for (auto capaIter = (*actIter)->listOfCapability.begin();
179 capaIter != (*actIter)->listOfCapability.end();
182 cout << "\t\t\t" << (*capaIter)->capability << " :: "
183 << (*capaIter)->status << endl;
190 // printf( "\tPlain Text :: %s\n", plainText.c_str() );
192 else if (rep.hasAttribute("DoAction"))
194 std::string plainText;
195 if (rep.getValue("DoAction", plainText))
197 cout << "\t" << plainText << endl;
208 OCRepresentation rep;
210 rep.setValue("DoAction", std::string("AllBulbOff"));
214 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
221 OCRepresentation rep;
223 rep.setValue("DoAction", std::string("AllBulbOn"));
227 OCStackResult res = g_resource->post("a.collection", GROUP_INTERFACE,
228 rep, QueryParamsMap(), &onPost);
230 if( res != OC_STACK_OK )
231 cout << "failed" << endl;
235 void Scheduled_AllbulbOff()
237 groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
239 void Scheduled_AllbulbOffEx()
241 groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
243 void CancelScheduled_AllBulbOff()
245 groupMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
247 void Recursive_allBulbOn()
249 groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
251 void Recursive_allBulbOnEx()
253 groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
256 void CancelRecursive_allBulbOn()
259 groupMgr->cancelActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
262 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
263 const int& eCode, const int& sequenceNumber)
265 if (eCode == OC_STACK_OK)
269 std::cout << "OBSERVE RESULT:" << std::endl;
270 std::cout << "\tSequenceNumber: " << sequenceNumber << endl;
272 if (rep.getValue("level", level))
283 std::cout << "\tlevel: " << level << std::endl;
287 std::cout << "onObserve Response error: " << eCode << std::endl;
292 void createActionSet_AllBulbOff()
294 string actionsetDesc;
295 ActionSet *allBulbOff = new ActionSet();
296 allBulbOff->actionsetName = "AllBulbOff";
298 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
300 Action *action = new Action();
301 action->target = (*iter);
303 Capability *capa = new Capability();
304 capa->capability = "power";
305 capa->status = "off";
307 action->listOfCapability.push_back(capa);
308 allBulbOff->listOfAction.push_back(action);
312 groupMgr->addActionSet(g_resource, allBulbOff, onPut);
318 void createActionSet_AllBulbOn()
320 string actionsetDesc;
321 ActionSet *allBulbOff = new ActionSet();
322 allBulbOff->actionsetName = "AllBulbOn";
324 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
326 Action *action = new Action();
327 action->target = (*iter);
329 Capability *capa = new Capability();
330 capa->capability = "power";
333 action->listOfCapability.push_back(capa);
334 allBulbOff->listOfAction.push_back(action);
338 groupMgr->addActionSet(g_resource, allBulbOff, onPut);
344 void createScheduledActionSet_AllBulbOff()
346 string actionsetDesc;
347 ActionSet *allBulbOff = new ActionSet();
348 allBulbOff->type = OIC::ACTIONSET_TYPE::SCHEDULED;
349 allBulbOff->actionsetName = "AllBulbOffScheduledCall";
351 printf("ENTER(YYYY-MM-DD hh:mm:ss) :: ");
352 int res = scanf("%d-%d-%d %d:%d:%d", &allBulbOff->mTime.tm_year,
353 &allBulbOff->mTime.tm_mon, &allBulbOff->mTime.tm_mday,
354 &allBulbOff->mTime.tm_hour, &allBulbOff->mTime.tm_min,
355 &allBulbOff->mTime.tm_sec);
358 printf("Invalid Input. try again.");
363 allBulbOff->setDelay(allBulbOff->getSecondsFromAbsoluteTime());
364 printf("DELAY :: %ld\n", allBulbOff->getSecondsFromAbsoluteTime());
366 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
368 Action *action = new Action();
369 action->target = (*iter);
371 Capability *capa = new Capability();
372 capa->capability = "power";
373 capa->status = "off";
375 action->listOfCapability.push_back(capa);
376 allBulbOff->listOfAction.push_back(action);
380 groupMgr->addActionSet(g_resource, allBulbOff, onPut);
386 void createRecursiveActionSet_AllBulbOn()
388 string actionsetDesc;
389 ActionSet *allBulbOn = new ActionSet();
390 allBulbOn->type = OIC::ACTIONSET_TYPE::RECURSIVE;
392 allBulbOn->actionsetName = "AllBulbOnRecursiveCall";
393 allBulbOn->mTime.tm_year = 0;
394 allBulbOn->mTime.tm_mon = 0;
395 allBulbOn->mTime.tm_mday = 0;
396 allBulbOn->mTime.tm_hour = 0;
397 allBulbOn->mTime.tm_min = 0;
398 allBulbOn->mTime.tm_sec = 5;
400 allBulbOn->setDelay(allBulbOn->getSecAbsTime());
402 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
404 Action *action = new Action();
405 action->target = (*iter);
407 Capability *capa = new Capability();
408 capa->capability = "power";
411 action->listOfCapability.push_back(capa);
412 allBulbOn->listOfAction.push_back(action);
416 groupMgr->addActionSet(g_resource, allBulbOn, onPut);
424 PlatformConfig config
425 { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0,
426 OC::QualityOfService::LowQos };
430 string resourceURI = "/core/a/collection";
431 string resourceTypeName = "a.collection";
432 string resourceInterface = BATCH_INTERFACE;
433 OCPlatform::Configure(config);
435 // Find lights for group creation.
436 vector<string> types;
437 types.push_back("core.light");
438 groupMgr->findCandidateResources(types, &foundResources, 5);
440 OCStackResult res = OCPlatform::registerResource(resourceHandle, resourceURI,
441 resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
443 if( res != OC_STACK_OK )
445 cout << "Resource registeration failed." << endl;
449 cout << "registerResource is called." << endl;
451 OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
452 OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
463 cout << "1 :: CREATE ACTIONSET" << endl;
464 cout << "2 :: EXECUTE ACTIONSET(ALLBULBON)" << endl;
465 cout << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
466 cout << "4 :: CREATE ACTIONSET(R_ALLBULBON)" << endl;
467 cout << "\t41 :: EXECUTE ACTIONSET 42 :: CANCEL ACTIONSET" << endl;
468 cout << "5 :: CREATE ACTIONSET(S_ALLBULBON)" << endl;
469 cout << "\t51 :: EXECUTE ACTIONSET 52 :: CANCEL ACTIONSET" << endl;
470 cout << "6 :: GET ACTIONSET" << endl;
471 cout << "7 :: DELETE ACTIONSET" << endl;
472 cout << "8 :: QUIT" << endl;
473 cout << "9 :: FIND GROUP" << endl;
474 cout << "0 :: FIND BOOKMARK TO OBSERVE"
482 std::string query = OC_RSRVD_WELL_KNOWN_URI;
483 query.append("?rt=");
484 query.append(resourceTypeName);
486 OCPlatform::findResource("",
491 // OCPlatform::findResource("",
498 std::string query = OC_RSRVD_WELL_KNOWN_URI;
499 query.append("?rt=");
500 query.append("core.bookmark");
502 OCPlatform::findResource("",
506 // OCPlatform::findResource("",
513 createActionSet_AllBulbOff();
514 createActionSet_AllBulbOn();
526 createRecursiveActionSet_AllBulbOn();
530 Recursive_allBulbOn();
534 CancelRecursive_allBulbOn();
539 Recursive_allBulbOnEx();
543 createScheduledActionSet_AllBulbOff();
547 Scheduled_AllbulbOff();
551 CancelScheduled_AllBulbOff();
555 Scheduled_AllbulbOffEx();
559 groupMgr->getActionSet(g_resource, "AllBulbOff", onPost);
563 groupMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
573 OCRepresentation rep;
575 rep.setValue("power", std::string("on"));
577 g_light->put(rep, QueryParamsMap(), &onPut);
584 catch (OCException& e)
586 cout << "ERROR :: " << e.reason() << endl;