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");
124 int main(int argc, char* argv[])
126 ostringstream requestURI;
127 requestURI << OC_WELL_KNOWN_QUERY << "?rt=a.collection";
130 OCConnectivityType connectivityType = OC_WIFI;
136 std::size_t inputValLen;
137 int optionSelected = stoi(argv[1], &inputValLen);
139 if(inputValLen == strlen(argv[1]))
141 if(optionSelected == 0)
143 connectivityType = OC_ETHERNET;
145 else if(optionSelected == 1)
147 connectivityType = OC_WIFI;
151 std::cout << "Invalid connectivity type selected. Using default WIFI"
157 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
162 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
167 std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
168 std::cout<<"ConnectivityType: Default WIFI\n";
169 std::cout<<"ConnectivityType 0: ETHERNET\n";
170 std::cout<<"ConnectivityType 1: WIFI\n";
174 PlatformConfig config
175 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
181 OCPlatform::Configure(config);
183 string resourceTypeName = "a.collection";
185 OCPlatform::findResource("", requestURI.str(),
186 connectivityType, &foundResource);
188 OCPlatform::findResource("", requestURI.str(), &foundResource);
201 << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
202 cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
207 string actionsetDesc;
208 //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
210 actionsetDesc = "allbulboff";
211 actionsetDesc.append("*");
212 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
214 actionsetDesc.append("uri=").append((*iter));
215 actionsetDesc.append("|");
216 actionsetDesc.append("power=");
217 actionsetDesc.append("off");
218 if ((iter + 1) != lights.end())
220 actionsetDesc.append("*");
224 cout << "ActionSet :: " << actionsetDesc << endl;
226 OCRepresentation rep;
227 rep.setValue("ActionSet", actionsetDesc);
231 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
237 OCRepresentation rep;
239 rep.setValue("DoAction", std::string("allbulboff"));
243 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
249 OCRepresentation rep;
251 rep.setValue("GetActionSet", std::string("allbulboff"));
255 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
261 OCRepresentation rep;
263 rep.setValue("DelActionSet", std::string("allbulboff"));
267 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
281 catch (OCException& e)
283 cout << e.what() << endl;