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*/,
109 const OCRepresentation& /*rep*/, const int /*eCode*/)
114 void onPost(const HeaderOptions& /*headerOptions*/,
115 const OCRepresentation& rep, const int /*eCode*/)
117 printf("\nonPost\n");
119 std::vector< OCRepresentation > children = rep.getChildren();
121 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
122 for (auto iter = children.begin(); iter != children.end(); ++iter)
125 (*iter).getValue("power", power);
127 cout << "\tURI :: " << (*iter).getUri() << endl;
128 cout << "\t\tpower :: " << power << endl;
131 if (rep.hasAttribute("ActionSet"))
133 std::string plainText;
135 rep.getValue("ActionSet", plainText);
137 printf("\tPlain Text :: %s\n", plainText.c_str());
141 printf("Not found ActionSet\n");
145 string buildActionSetDesc(unsigned int delay = 0, unsigned int type = 0)
147 string actionsetDesc = "";
148 actionsetDesc = "allbulboff";
149 actionsetDesc.append("*");
150 actionsetDesc.append(std::to_string(delay)); // Set delay time.
151 actionsetDesc.append(" ");
152 actionsetDesc.append(std::to_string(type)); // Set action type.
153 actionsetDesc.append("*");
154 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
156 actionsetDesc.append("uri=").append((*iter));
157 actionsetDesc.append("|");
158 actionsetDesc.append("power=");
159 actionsetDesc.append("off");
160 if ((iter + 1) != lights.end())
162 actionsetDesc.append("*");
165 return actionsetDesc;
168 bool isResourceReady()
173 int main(int /*argc*/, char** /*argv[]*/)
175 ostringstream requestURI;
176 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=a.collection";
178 PlatformConfig config
179 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
185 OCPlatform::Configure(config);
187 string resourceTypeName = "a.collection";
189 OCPlatform::findResource("", requestURI.str(),
190 CT_DEFAULT, &foundResource);
192 //Non-intensive block until foundResource callback is called by OCPlatform
193 //and onGet gets resource.
194 //isResourceReady takes care of spurious wake-up
196 std::unique_lock<std::mutex> lock(blocker);
197 cv.wait(lock, isResourceReady);
204 cout << endl << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
205 cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
208 OCRepresentation rep;
209 string actionsetDesc;
217 actionsetDesc = buildActionSetDesc();
218 if(!actionsetDesc.empty())
220 cout << "ActionSet :: " << actionsetDesc << endl;
221 rep.setValue("ActionSet", actionsetDesc);
225 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
230 rep.setValue(DO_ACTION, std::string("allbulboff"));
233 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
238 rep.setValue(GET_ACTIONSET, std::string("allbulboff"));
241 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
246 rep.setValue("DelActionSet", std::string("allbulboff"));
249 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
254 cout << "Invalid option" << endl;
259 catch (OCException& e)
261 cout << e.what() << endl;