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 namespace PH = std::placeholders;
32 OCResourceHandle resourceHandle;
34 shared_ptr< OCResource > g_resource;
35 vector< string > lights;
39 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
41 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
43 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
45 void foundResource(std::shared_ptr< OCResource > resource)
47 std::string resourceURI;
48 std::string hostAddress;
52 cout << "FOUND Resource" << endl;
56 string resourceURI = resource->uri();
57 cout << resourceURI << endl;
58 if (resourceURI == "/core/a/collection")
60 g_resource = resource;
63 g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
64 printf("HOST :: %s\n", resource->host().c_str());
67 catch (std::exception& e)
69 std::cout << "" << std::endl;
73 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
77 std::vector< OCRepresentation > children = rep.getChildren();
79 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
80 for (auto iter = children.begin(); iter != children.end(); ++iter)
82 lights.push_back((*iter).getUri());
83 cout << "\tURI :: " << (*iter).getUri() << endl;
89 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
94 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
98 std::vector< OCRepresentation > children = rep.getChildren();
100 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
101 for (auto iter = children.begin(); iter != children.end(); ++iter)
104 (*iter).getValue("power", power);
106 cout << "\tURI :: " << (*iter).getUri() << endl;
107 cout << "\t\tpower :: " << power << endl;
110 if (rep.hasAttribute("ActionSet"))
112 std::string plainText;
114 rep.getValue("ActionSet", plainText);
116 printf("\tPlain Text :: %s\n", plainText.c_str());
120 printf("Not found ActionSet\n");
126 PlatformConfig config
127 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
133 OCPlatform::Configure(config);
135 string resourceTypeName = "a.collection";
136 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
148 << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
149 cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
154 string actionsetDesc;
155 //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
157 actionsetDesc = "allbulboff";
158 actionsetDesc.append("*");
159 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
161 actionsetDesc.append("uri=").append((*iter));
162 actionsetDesc.append("|");
163 actionsetDesc.append("power=");
164 actionsetDesc.append("off");
165 if ((iter + 1) != lights.end())
167 actionsetDesc.append("*");
171 cout << "ActionSet :: " << actionsetDesc << endl;
173 OCRepresentation rep;
174 rep.setValue("ActionSet", actionsetDesc);
178 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
184 OCRepresentation rep;
186 rep.setValue("DoAction", std::string("allbulboff"));
190 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
196 OCRepresentation rep;
198 rep.setValue("GetActionSet", std::string("allbulboff"));
202 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
208 OCRepresentation rep;
210 rep.setValue("DelActionSet", std::string("allbulboff"));
214 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
228 catch (OCException& e)
230 cout << e.what() << endl;