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>
28 #include <ThingsManager.h>
33 namespace PH = std::placeholders;
37 OCResourceHandle resourceHandle;
38 std::vector< OCResourceHandle > resourceHandleVector;
40 shared_ptr< OCResource > g_resource;
41 vector< string > lights;
43 ThingsManager *thingsMgr = new ThingsManager();
45 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
47 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
49 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
51 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
52 const int& sequenceNumber);
57 void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
60 for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
62 std::string resourceURI = (*rsrc)->uri();
63 std::string hostAddress = (*rsrc)->host();
65 if (resourceURI == "/a/light")
68 cout << "\tResource URI : " << resourceURI << endl;
69 cout << "\tResource Host : " << hostAddress << endl;
71 OCResourceHandle foundResourceHandle;
72 OCStackResult result = OCPlatform::registerResource(foundResourceHandle, (*rsrc));
73 cout << "\tresource registed!" << endl;
74 if (result == OC_STACK_OK)
76 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
77 resourceHandleVector.push_back(foundResourceHandle);
81 cout << "\tresource Error!" << endl;
84 lights.push_back((hostAddress + resourceURI));
91 void foundResource(std::shared_ptr< OCResource > resource)
93 std::string resourceURI;
94 std::string hostAddress;
98 cout << "FOUND RESOURCE" << endl;
102 resourceURI = resource->uri();
103 hostAddress = resource->host();
104 if (resourceURI == "/core/a/collection")
106 g_resource = resource;
108 // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
110 printf("\tHOST :: %s\n", resource->host().c_str());
112 else if (resourceURI == "/core/bookmark")
114 resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
117 // p_platform.bindResource(resourceHandle, foundResourceHandle);
121 catch (std::exception& e)
123 std::cout << "" << std::endl;
127 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
129 // std::vector<OCRepresentation> children = rep.getChildren();
131 // cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
132 // for( auto iter = children.begin(); iter != children.end(); ++iter )
134 // lights.push_back((*iter).getUri());
135 // cout << "\tURI :: " << (*iter).getUri() << endl;
139 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
144 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
146 printf("\nonPost\n");
148 if (rep.hasAttribute("ActionSet"))
150 std::string plainText;
152 if (rep.getValue("ActionSet", plainText))
154 ActionSet *actionset = thingsMgr->getActionSetfromString(plainText);
155 if (actionset != NULL)
157 cout << endl << "\tACTIONSET NAME :: " << actionset->actionsetName << endl;
158 for (auto actIter = actionset->listOfAction.begin();
159 actIter != actionset->listOfAction.end(); ++actIter)
161 cout << "\t\tTARGET :: " << (*actIter)->target << endl;
163 for (auto capaIter = (*actIter)->listOfCapability.begin();
164 capaIter != (*actIter)->listOfCapability.end(); ++capaIter)
166 cout << "\t\t\t" << (*capaIter)->capability << " :: " << (*capaIter)->status
174 // printf( "\tPlain Text :: %s\n", plainText.c_str() );
176 else if (rep.hasAttribute("DoAction"))
178 std::string plainText;
179 if (rep.getValue("DoAction", plainText))
181 cout << "\t" << plainText << endl;
192 OCRepresentation rep;
194 rep.setValue("DoAction", std::string("AllBulbOff"));
198 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
204 OCRepresentation rep;
206 rep.setValue("DoAction", std::string("AllBulbOn"));
210 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPost);
214 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode,
215 const int& sequenceNumber)
217 if (eCode == OC_STACK_OK)
221 std::cout << "OBSERVE RESULT:" << std::endl;
222 std::cout << "\tSequenceNumber: " << sequenceNumber << endl;
224 if (rep.getValue("level", level))
235 std::cout << "\tlevel: " << level << std::endl;
239 std::cout << "onObserve Response error: " << eCode << std::endl;
244 void createActionSet_AllBulbOff()
246 string actionsetDesc;
247 ActionSet *allBulbOff = new ActionSet();
248 allBulbOff->actionsetName = "AllBulbOff";
250 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
252 Action *action = new Action();
253 action->target = (*iter);
255 Capability *capa = new Capability();
256 capa->capability = "power";
257 capa->status = "off";
259 action->listOfCapability.push_back(capa);
260 allBulbOff->listOfAction.push_back(action);
262 // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
264 // cout << "ActionSet :: " << actionsetDesc << endl;
266 // OCRepresentation rep;
267 // rep.setValue("ActionSet", actionsetDesc);
271 thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
272 // g_resource->put("a.collection", GROUP_INTERFACE, rep,
273 // QueryParamsMap(), &onPut);
279 void createActionSet_AllBulbOn()
281 string actionsetDesc;
282 ActionSet *allBulbOff = new ActionSet();
283 allBulbOff->actionsetName = "AllBulbOn";
285 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
287 Action *action = new Action();
288 action->target = (*iter);
290 Capability *capa = new Capability();
291 capa->capability = "power";
294 action->listOfCapability.push_back(capa);
295 allBulbOff->listOfAction.push_back(action);
297 // actionsetDesc = thingsMgr->getStringFromActionSet(allBulbOff);
299 // cout << "ActionSet :: " << actionsetDesc << endl;
301 // OCRepresentation rep;
302 // rep.setValue("ActionSet", actionsetDesc);
306 thingsMgr->addActionSet(g_resource, allBulbOff, onPut);
307 // g_resource->put("a.collection", GROUP_INTERFACE, rep,
308 // QueryParamsMap(), &onPut);
316 PlatformConfig config
317 { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
321 string resourceURI = "/core/a/collection";
322 string resourceTypeName = "a.collection";
323 string resourceInterface = BATCH_INTERFACE;
324 OCPlatform::Configure(config);
326 // Find lights for group creation.
327 vector< string > types;
328 types.push_back("core.light");
329 thingsMgr->findCandidateResources(types, &foundResources, 5);
331 OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
332 resourceInterface, NULL,
333 //&entityHandler, // entityHandler
336 cout << "registerResource is called." << endl;
338 OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
339 OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
350 cout << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTIONSET(ALLBULBON)"
351 << "3 :: EXECUTE ACTIONSET(ALLBULBOFF)" << endl;
352 cout << "4 :: GET ACTIONSET 5 :: DELETE ACTIONSET 6 :: QUIT" << endl;
353 cout << "9 :: FIND GROUP 0 :: FIND BOOKMARK TO OBSERVE" << endl;
360 std::string requestURI = OC_WELL_KNOWN_QUERY;
361 requestURI += "?rt=a.collection";
363 OCPlatform::findResource("", requestURI, OC_WIFI, &foundResource);
367 std::string requestURI = OC_WELL_KNOWN_QUERY;
368 requestURI += "?rt=core.bookmark";
370 OCPlatform::findResource("", requestURI, OC_WIFI, &foundResource);
377 //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10";
378 createActionSet_AllBulbOff();
379 createActionSet_AllBulbOn();
386 // thingsMgr->executeActionSet(g_resource, "AllBulbOn", onPost);
393 // thingsMgr->executeActionSet(g_resource, "AllBulbOff", onPost);
398 // OCRepresentation rep;
400 // rep.setValue("GetActionSet", std::string("AllBulbOff"));
404 // g_resource->post("a.collection", GROUP_INTERFACE, rep,
405 // QueryParamsMap(), &onPost);
408 thingsMgr->getActionSet(g_resource, "AllBulbOff", onPost);
412 // OCRepresentation rep;
414 // rep.setValue("DelActionSet", std::string("AllBulbOff"));
418 // g_resource->put("a.collection", GROUP_INTERFACE, rep,
419 // QueryParamsMap(), &onPut);
421 thingsMgr->deleteActionSet(g_resource, "AllBulbOff", onPut);
431 catch (OCException& e)