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 /// This fridgeclient represents a client trying to discover the associated
22 /// fridgeserver. The device resource is the only one available for discovery
23 /// on the server, so we have to take the fact that we know the device tag
24 /// to then generate a Resource object
28 #include <condition_variable>
30 #include "OCPlatform.h"
34 namespace PH = std::placeholders;
39 ClientFridge(PlatformConfig &cfg) : m_platform(cfg)
41 std::cout << "Fridge Client has started " <<std::endl;
42 FindCallback f (std::bind(&ClientFridge::foundDevice, this, PH::_1));
44 OCStackResult result = m_platform.findResource(
45 "", "coap://224.0.1.187/oc/core?rt=intel.fridge", f);
47 if(OC_STACK_OK != result)
49 throw new std::runtime_error("Fridge Find Resource Failed");
52 std::cout << "Waiting to discover fridge... "<<std::endl;
54 // we want to block this thread until the client has finished
55 // its duties, so we block on the CV until we have completed
56 // what we are looking to do
57 std::unique_lock<std::mutex> lk(m_mutex);
63 void foundDevice(std::shared_ptr<OCResource> resource)
65 if(resource && resource->uri() == "/device")
67 std::cout << "Discovered a device object"<<std::endl;
68 std::cout << "\tHost: "<<resource->host()<<std::endl;
69 std::cout << "\tURI: "<<resource->uri() <<std::endl;
72 // we have now found a resource, so lets create a few resource objects
73 // for the other resources that we KNOW are associated with the intel.fridge
74 // server, and query them.
75 std::vector<std::string> lightTypes = {"intel.fridge.light"};
76 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
77 OCResource::Ptr light = m_platform.constructResourceObject(resource->host(),
78 "/light", false, lightTypes, ifaces);
80 std::vector<std::string> doorTypes = {"intel.fridge.door"};
82 OCResource::Ptr leftdoor = m_platform.constructResourceObject(resource->host(),
83 "/door/left", false, doorTypes, ifaces);
84 OCResource::Ptr rightdoor = m_platform.constructResourceObject(resource->host(),
85 "/door/right", false, doorTypes, ifaces);
87 light->get(QueryParamsMap(), GetCallback(
88 std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1,
91 leftdoor->get(QueryParamsMap(), GetCallback(
92 std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1,
95 rightdoor->get(QueryParamsMap(), GetCallback(
96 std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1,
101 // Note that resourceName, resource, and getId are all bound via the std::bind mechanism.
102 // it is possible to attach ANY arbitrary data to do whatever you would like here. It may,
103 // however be a better fit to wrap each call in an object so a fuller context (and additional
104 // requests) can be easily made inside of a simple context
105 void getResponse(const std::string& resourceName, const OCRepresentation rep, const int eCode,
106 OCResource::Ptr resource, int getId)
108 std::cout << "Got a response from get from the "<<resourceName<< std::endl;
109 std::cout << "Get ID is "<<getId<<" and resource URI is "<<resource->uri()<<std::endl;
111 std::cout << "The Attribute Data is: "<<std::endl;
117 rep.getValue("on",isOn);
118 std::cout<<"The fridge light is "<< ((isOn)?"":"not ") <<"on"<<std::endl;
124 rep.getValue("open", isOpen);
125 rep.getValue("side", side);
126 std::cout << "Door is "<<isOpen<<" and is on the "<<side<<std::endl;
131 OCPlatform m_platform;
133 std::condition_variable m_cv;
144 QualityOfService::NonConfirmable
147 ClientFridge cf (cfg);