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 <condition_variable>
32 #define DO_ACTION "DoAction"
33 #define GET_ACTIONSET "GetActionSet"
34 #define ACTIONSET "ActionSet"
35 #define DELETE_ACTIONSET "DelActionSet"
39 namespace PH = std::placeholders;
40 std::mutex resourceLock;
42 OCResourceHandle resourceHandle;
43 shared_ptr< OCResource > g_resource;
44 vector< string > lights;
46 std::condition_variable cv;
50 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
52 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
54 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
56 void foundResource(std::shared_ptr< OCResource > resource)
58 std::lock_guard<std::mutex> lock(resourceLock);
61 std::cout << "Found another resource, ignoring"<<std::endl;
65 std::string resourceURI;
66 std::string hostAddress;
70 cout << "FOUND Resource" << endl;
74 string resourceURI = resource->uri();
75 cout << resourceURI << endl;
76 cout << "HOST :: " << resource->host() << endl;
77 if (resourceURI == "/core/a/collection")
79 g_resource = resource;
80 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
82 printf("HOST :: %s\n", resource->host().c_str());
85 catch (std::exception& e)
87 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
91 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
95 std::vector< OCRepresentation > children = rep.getChildren();
97 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
98 for (auto iter = children.begin(); iter != children.end(); ++iter)
100 lights.push_back((*iter).getUri());
101 cout << "\tURI :: " << (*iter).getUri() << endl;
108 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
113 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
115 printf("\nonPost\n");
117 std::vector< OCRepresentation > children = rep.getChildren();
119 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
120 for (auto iter = children.begin(); iter != children.end(); ++iter)
123 (*iter).getValue("power", power);
125 cout << "\tURI :: " << (*iter).getUri() << endl;
126 cout << "\t\tpower :: " << power << endl;
129 if (rep.hasAttribute("ActionSet"))
131 std::string plainText;
133 rep.getValue("ActionSet", plainText);
135 printf("\tPlain Text :: %s\n", plainText.c_str());
139 printf("Not found ActionSet\n");
143 string buildActionSetDesc(unsigned int delay = 0, unsigned int type = 0)
145 string actionsetDesc = "";
146 actionsetDesc = "allbulboff";
147 actionsetDesc.append("*");
148 actionsetDesc.append(std::to_string(delay)); // Set delay time.
149 actionsetDesc.append(" ");
150 actionsetDesc.append(std::to_string(type)); // Set action type.
151 actionsetDesc.append("*");
152 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
154 actionsetDesc.append("uri=").append((*iter));
155 actionsetDesc.append("|");
156 actionsetDesc.append("power=");
157 actionsetDesc.append("off");
158 if ((iter + 1) != lights.end())
160 actionsetDesc.append("*");
163 return actionsetDesc;
166 bool isResourceReady()
171 int main(int argc, char* argv[])
173 ostringstream requestURI;
174 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=a.collection";
176 PlatformConfig config
177 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
183 OCPlatform::Configure(config);
185 string resourceTypeName = "a.collection";
187 OCPlatform::findResource("", requestURI.str(),
188 CT_DEFAULT, &foundResource);
190 //Non-intensive block until foundResource callback is called by OCPlatform
191 //and onGet gets resource.
192 //isResourceReady takes care of spurious wake-up
194 std::unique_lock<std::mutex> lock(blocker);
195 cv.wait(lock, isResourceReady);
202 cout << endl << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
203 cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
206 OCRepresentation rep;
207 string actionsetDesc;
215 actionsetDesc = buildActionSetDesc();
216 if(!actionsetDesc.empty())
218 cout << "ActionSet :: " << actionsetDesc << endl;
219 rep.setValue("ActionSet", actionsetDesc);
223 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
228 rep.setValue(DO_ACTION, std::string("allbulboff"));
231 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
236 rep.setValue(GET_ACTIONSET, std::string("allbulboff"));
239 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
244 rep.setValue("DelActionSet", std::string("allbulboff"));
247 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
252 cout << "Invalid option" << endl;
258 catch (OCException& e)
260 cout << e.what() << endl;