1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 // garageclient.cpp is the client program for garageserver.cpp, which
22 // uses different representation in OCRepresention.
27 #include <condition_variable>
28 #include "OCPlatform.h"
33 const int SUCCESS_RESPONSE = 0;
34 std::shared_ptr<OCResource> curResource;
35 std::mutex curResourceLock;
43 std::vector<bool> m_lightStates;
44 std::vector<int> m_lightPowers;
45 OCRepresentation m_lightRep;
46 std::vector<OCRepresentation> m_reps;
47 std::vector<std::vector<int>> m_hingeStates;
49 Garage() : m_state(false), m_name("")
56 void printRepresentation(const OCRepresentation& rep)
59 // Check if attribute "name" exists, and then getValue
60 if(rep.hasAttribute("name"))
62 myGarage.m_name = rep["name"];
64 std::cout << "\tname: " << myGarage.m_name << std::endl;
66 // You can directly try to get the value. this function
67 // return false if there is no attribute "state"
68 if(!rep.getValue("state", myGarage.m_state))
70 std::cout << "Attribute state doesn't exist in the representation\n";
72 std::cout << "\tstate: " << myGarage.m_state << std::endl;
74 OCRepresentation rep2 = rep;
76 std::cout << "Number of attributes in rep2: "
77 << rep2.numberOfAttributes() << std::endl;
79 if(rep2.erase("name"))
81 std::cout << "attribute: name, was removed successfully from rep2.\n";
84 std::cout << "Number of attributes in rep2: "
85 << rep2.numberOfAttributes() << std::endl;
88 if(rep.isNULL("nullAttribute"))
90 std::cout << "\tnullAttribute is null." << std::endl;
94 std::cout << "\tnullAttribute is not null." << std::endl;
97 myGarage.m_lightRep = rep["light"];
99 myGarage.m_lightStates = myGarage.m_lightRep["states"];
100 myGarage.m_lightPowers = myGarage.m_lightRep["powers"];
102 std::cout << "\tlightRep: states: ";
105 for(auto state: myGarage.m_lightStates)
114 std::cout << "," << state;
118 std::cout << std::endl;
119 std::cout << "\tlightRep: powers: ";
121 for(auto power: myGarage.m_lightPowers)
130 std::cout << "," << power;
133 std::cout << std::endl;
135 // Get vector of representations
136 myGarage.m_reps = rep["reps"];
139 for(auto& rep : myGarage.m_reps)
141 for(auto& attribute : rep)
143 std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
144 << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
149 std::cout << "\tjson: " << rep["json"] << std::endl;
150 myGarage.m_hingeStates = rep["hinges"];
152 std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
153 rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;
157 // callback handler on PUT request
158 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
160 if(eCode == SUCCESS_RESPONSE)
162 std::cout << "PUT request was successful" << std::endl;
164 printRepresentation(rep);
168 std::cout << "onPut Response error: " << eCode << std::endl;
173 // Local function to put a different state for this resource
174 void putLightRepresentation(std::shared_ptr<OCResource> resource)
178 OCRepresentation rep;
180 std::cout << "Putting light representation..."<<std::endl;
182 myGarage.m_state = true;
184 rep["state"] = myGarage.m_state;
186 // Create QueryParameters Map and add query params (if any)
187 QueryParamsMap queryParamsMap;
189 // Invoke resource's pit API with rep, query map and the callback parameter
190 resource->put(rep, queryParamsMap, &onPut);
194 // Callback handler on GET request
195 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
197 if(eCode == SUCCESS_RESPONSE)
199 std::cout << "GET request was successful" << std::endl;
200 std::cout << "Resource URI: " << rep.getUri() << std::endl;
202 printRepresentation(rep);
204 putLightRepresentation(curResource);
208 std::cout << "onGET Response error: " << eCode << std::endl;
213 // Local function to get representation of light resource
214 void getLightRepresentation(std::shared_ptr<OCResource> resource)
218 std::cout << "Getting Light Representation..."<<std::endl;
219 // Invoke resource's get API with the callback parameter
222 resource->get(test, &onGet);
226 // Callback to found resources
227 void foundResource(std::shared_ptr<OCResource> resource)
229 std::lock_guard<std::mutex> lock(curResourceLock);
232 std::cout << "Found another resource, ignoring"<<std::endl;
236 std::string resourceURI;
237 std::string hostAddress;
240 // Do some operations with resource object.
243 std::cout<<"DISCOVERED Resource:"<<std::endl;
244 // Get the resource URI
245 resourceURI = resource->uri();
246 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
248 // Get the resource host address
249 hostAddress = resource->host();
250 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
252 // Get the resource types
253 std::cout << "\tList of resource types: " << std::endl;
254 for(auto &resourceTypes : resource->getResourceTypes())
256 std::cout << "\t\t" << resourceTypes << std::endl;
259 // Get the resource interfaces
260 std::cout << "\tList of resource interfaces: " << std::endl;
261 for(auto &resourceInterfaces : resource->getResourceInterfaces())
263 std::cout << "\t\t" << resourceInterfaces << std::endl;
266 if(resourceURI == "/a/garage")
268 curResource = resource;
269 // Call a local function which will internally invoke
270 // get API on the resource pointer
271 getLightRepresentation(resource);
276 // Resource is invalid
277 std::cout << "Resource is invalid" << std::endl;
281 catch(std::exception& e)
283 std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
287 int main(int argc, char* argv[]) {
289 std::ostringstream requestURI;
291 // Create PlatformConfig object
293 OC::ServiceType::InProc,
294 OC::ModeType::Client,
297 OC::QualityOfService::LowQos
300 OCPlatform::Configure(cfg);
303 // Find all resources
304 requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.garage";
306 OCPlatform::findResource("", requestURI.str(),
307 OC_ALL, &foundResource);
309 std::cout<< "Finding Resource... " <<std::endl;
311 // A condition variable will free the mutex it is given, then do a non-
312 // intensive block until 'notify' is called on it. In this case, since we
313 // don't ever call cv.notify, this should be a non-processor intensive version
316 std::condition_variable cv;
317 std::unique_lock<std::mutex> lock(blocker);
320 catch(OCException& e)
322 std::cerr << "Exception in GarageClient: "<<e.what();