New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / examples / fridgeclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
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
25
26 #include <iostream>
27 #include <stdexcept>
28 #include <condition_variable>
29 #include <mutex>
30 #include "OCPlatform.h"
31 #include "OCApi.h"
32
33 using namespace OC;
34 namespace PH = std::placeholders;
35
36 class ClientFridge
37 {
38     public:
39     ClientFridge(PlatformConfig &cfg) : m_platform(cfg)
40     {
41         std::cout << "Fridge Client has started " <<std::endl;
42         FindCallback f (std::bind(&ClientFridge::foundDevice, this, PH::_1));
43
44         OCStackResult result = m_platform.findResource(
45                 "", "coap://224.0.1.187/oc/core?rt=intel.fridge", f);
46
47         if(OC_STACK_OK != result)
48         {
49             throw new std::runtime_error("Fridge Find Resource Failed");
50         }
51
52         std::cout << "Waiting to discover fridge... "<<std::endl;
53         {
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);
58             m_cv.wait(lk);
59         }
60     }
61
62     private:
63     void foundDevice(std::shared_ptr<OCResource> resource)
64     {
65         if(resource && resource->uri() == "/device")
66         {
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;
70         }
71
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);
79
80         std::vector<std::string> doorTypes = {"intel.fridge.door"};
81
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);
86
87         light->get(QueryParamsMap(), GetCallback(
88                 std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1,
89                     PH::_2, light, 1)
90                 ));
91         leftdoor->get(QueryParamsMap(), GetCallback(
92                 std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1,
93                     PH::_2, leftdoor, 2)
94                 ));
95         rightdoor->get(QueryParamsMap(), GetCallback(
96                 std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1,
97                     PH::_2, rightdoor, 3)
98                 ));
99     }
100
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)
107     {
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;
110
111         std::cout << "The Attribute Data is: "<<std::endl;
112
113         switch(getId)
114         {
115             case 1:
116                 bool isOn;
117                 rep.getValue("on",isOn);
118                 std::cout<<"The fridge light is "<< ((isOn)?"":"not ") <<"on"<<std::endl;
119             break;
120             case 2:
121             case 3:
122                 bool isOpen;
123                 std::string side;
124                 rep.getValue("open", isOpen);
125                 rep.getValue("side", side);
126                 std::cout << "Door is "<<isOpen<<" and is on the "<<side<<std::endl;
127             break;
128         }
129     }
130
131     OCPlatform m_platform;
132     std::mutex m_mutex;
133     std::condition_variable m_cv;
134 };
135
136 int main()
137 {
138     PlatformConfig cfg
139     {
140         ServiceType::InProc,
141         ModeType::Client,
142         "0.0.0.0",
143         0,
144         QualityOfService::NonConfirmable
145     };
146
147     ClientFridge cf (cfg);
148     return 0;
149 }