iotivity 0.9.0
[platform/upstream/iotivity.git] / examples / OICSensorBoard / client.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
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 #include "client.h"
22 #include "namedefs.h"
23
24 IoTClient::IoTClient()
25 {
26     cout << "Running IoTClient constructor" << endl;
27     initializePlatform();
28 }
29
30 IoTClient::~IoTClient()
31 {
32     cout << "Running IoTClient destructor" << endl;
33 }
34
35 void IoTClient::initializePlatform()
36 {
37     m_platformConfig = make_shared<PlatformConfig>(ServiceType::InProc, ModeType::Client, "0.0.0.0",
38                                                    0, OC::QualityOfService::HighQos);
39     OCPlatform::Configure(*m_platformConfig);
40     m_resourceDiscoveryCallback = bind(&IoTClient::discoveredResource, this, placeholders::_1);
41 }
42
43 void IoTClient::findResource()
44 {
45     string coap_multicast_discovery = string(OC_WELL_KNOWN_QUERY "?if=" EDISON_RESOURCE_INTERFACE);
46     OCPlatform::findResource("", coap_multicast_discovery.c_str(), m_resourceDiscoveryCallback,
47                              OC::QualityOfService::LowQos);
48 }
49
50 void IoTClient::discoveredResource(shared_ptr<OCResource> Resource)
51 {
52     try
53     {
54         if (Resource)
55         {
56             string resourceUri = Resource->uri();
57             string hostAddress = Resource->host();
58
59             cout << "\nFound Resource" << endl << "Resource Types:" << endl;
60             for (auto& resourceTypes : Resource->getResourceTypes())
61             {
62                 cout << "\t" << resourceTypes << endl;
63             }
64
65             cout << "Resource Interfaces: " << endl;
66             for (auto& resourceInterfaces : Resource->getResourceInterfaces())
67             {
68                 cout << "\t" << resourceInterfaces << endl;
69             }
70             cout << "Resource uri: " << resourceUri << endl;
71             cout << "host: " << hostAddress << endl;
72
73             if (resourceUri == TEMPERATURE_RESOURCE_ENDPOINT)
74             {
75                 m_temperatureSensor = make_shared<TemperatureSensor>(Resource);
76             }
77             else if (resourceUri == LIGHT_RESOURCE_ENDPOINT)
78             {
79                 m_ambientLightSensor = make_shared<AmbientLight>(Resource);
80             }
81             else if (resourceUri == LED_RESOURCE_ENDPOINT)
82             {
83                 m_platformLED = make_shared<LED>(Resource);
84             }
85         }
86         IoTClient::DisplayMenu();
87     }
88     catch (OCException& ex)
89     {
90         cerr << "Caught exception in discoveredResource: " << ex.reason() << endl;
91     }
92 }
93
94 shared_ptr<TemperatureSensor> IoTClient::getTemperatureSensor()
95 {
96     return m_temperatureSensor;
97 }
98
99 shared_ptr<AmbientLight> IoTClient::getAmbientLightSensor()
100 {
101     return m_ambientLightSensor;
102 }
103
104 shared_ptr<LED> IoTClient::getPlatformLED()
105 {
106     return m_platformLED;
107 }
108
109 TemperatureSensor::TemperatureSensor(shared_ptr<OCResource> Resource)
110 {
111     m_isObserved = false;
112     m_resourceHandle = Resource;
113     m_GETCallback = bind(&TemperatureSensor::onGet, this, placeholders::_1, placeholders::_2,
114                          placeholders::_3);
115     m_OBSERVECallback = bind(&TemperatureSensor::onObserve, this, placeholders::_1,
116                              placeholders::_2, placeholders::_3, placeholders::_4);
117 }
118
119 TemperatureSensor::~TemperatureSensor()
120 {
121
122 }
123
124 void TemperatureSensor::startObserve()
125 {
126     if (!m_isObserved)
127     {
128         cout << "Starting observing temperature sensor" << endl;
129         m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
130     }
131     m_isObserved = true;
132 }
133
134 void TemperatureSensor::stopObserve()
135 {
136     if (m_isObserved)
137     {
138         m_resourceHandle->cancelObserve();
139         cout << "Stopped observing temperature sensor" << endl;
140     }
141     m_isObserved = false;
142 }
143
144 void TemperatureSensor::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
145                                   int eCode, int sequenceNumber)
146 {
147     if (eCode == OC_STACK_OK)
148     {
149         double value;
150         rep.getValue(TEMPERATURE_RESOURCE_KEY, value);
151         cout << "Observing TemperatureSensor: Current temperature reading is " << value << endl;
152         cout << "Sequence number: " << sequenceNumber << endl;
153     }
154     else
155     {
156         cerr << "TemperatureSensor: Observer response error" << endl;
157     }
158 }
159
160 void TemperatureSensor::get()
161 {
162     QueryParamsMap params;
163     m_resourceHandle->get(params, m_GETCallback);
164 }
165
166 void TemperatureSensor::onGet(const HeaderOptions& headerOptions,
167                               const OCRepresentation& representation, int errCode)
168 {
169     if (errCode == OC_STACK_OK)
170     {
171         double value;
172         representation.getValue(TEMPERATURE_RESOURCE_KEY, value);
173         cout << endl << endl << "Current temperature reading: " << value << endl;
174     }
175     else {
176         cerr << endl << endl << "Error in GET response from temperature sensor resource" << endl;
177     }
178     IoTClient::DisplayMenu();
179 }
180
181 AmbientLight::AmbientLight(shared_ptr<OCResource> Resource)
182 {
183     m_isObserved = false;
184     m_resourceHandle = Resource;
185     m_GETCallback = bind(&AmbientLight::onGet, this, placeholders::_1, placeholders::_2,
186                          placeholders::_3);
187     m_OBSERVECallback = bind(&AmbientLight::onObserve, this, placeholders::_1, placeholders::_2,
188                              placeholders::_3, placeholders::_4);
189 }
190
191 AmbientLight::~AmbientLight()
192 {
193
194 }
195
196 void AmbientLight::startObserve()
197 {
198     if (!m_isObserved)
199     {
200         cout << "Started observing ambient light sensor" << endl;
201         m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
202     }
203     m_isObserved = true;
204 }
205
206 void AmbientLight::stopObserve()
207 {
208     if (m_isObserved)
209     {
210         m_resourceHandle->cancelObserve();
211         cout << "Stopped observing ambient light sensor" << endl;
212     }
213     m_isObserved = false;
214 }
215
216 void AmbientLight::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
217                              int eCode, int sequenceNumber)
218 {
219     if (eCode == OC_STACK_OK)
220     {
221         int value;
222         rep.getValue(LIGHT_RESOURCE_KEY, value);
223         cout << "Observing AmbientLightSensor: Current ambient light reading is " << value << endl;
224         cout << "Sequence number: " << sequenceNumber << endl;
225     }
226     else
227     {
228         cerr << "TemperatureSensor: Observer response error" << endl;
229     }
230 }
231
232 void AmbientLight::get()
233 {
234     QueryParamsMap params;
235     m_resourceHandle->get(params, m_GETCallback);
236 }
237
238 void AmbientLight::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
239                          int errCode)
240 {
241     if (errCode == OC_STACK_OK)
242     {
243         int value;
244         representation.getValue(LIGHT_RESOURCE_KEY, value);
245         cout << endl << endl << "Current ambient light sensor reading: " << value << endl;
246     }
247     else {
248         cerr << endl << endl << "Error in GET response from ambient light sensor resource" << endl;
249     }
250     IoTClient::DisplayMenu();
251 }
252
253 LED::LED(shared_ptr<OCResource> Resource)
254 {
255     m_resourceHandle = Resource;
256     m_GETCallback = bind(&LED::onGet, this, placeholders::_1, placeholders::_2, placeholders::_3);
257     m_PUTCallback = bind(&LED::onPut, this, placeholders::_1, placeholders::_2, placeholders::_3);
258 }
259
260 LED::~LED()
261 {
262
263 }
264
265 void LED::get()
266 {
267     QueryParamsMap params;
268     m_resourceHandle->get(params, m_GETCallback);
269 }
270
271 void LED::put(int Switch)
272 {
273     QueryParamsMap params;
274     OCRepresentation rep;
275     rep.setValue(LED_RESOURCE_KEY, Switch);
276     m_resourceHandle->put(rep, params, m_PUTCallback);
277 }
278
279 void LED::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
280                 int errCode)
281 {
282     if (errCode == OC_STACK_OK)
283     {
284         int value;
285         representation.getValue(LED_RESOURCE_KEY, value);
286         cout << endl << endl << "LED switch state is: " << value << endl;
287     }
288     else {
289         cerr << endl << endl << "Error in GET response from LED resource" << endl;
290     }
291     IoTClient::DisplayMenu();
292 }
293
294 void LED::onPut(const HeaderOptions& headerOptions, const OCRepresentation& representation,
295                 int errCode)
296 {
297     if (errCode == OC_STACK_OK)
298     {
299         int value;
300         representation.getValue(LED_RESOURCE_KEY, value);
301         cout << endl << endl << "Set LED switch to: " << value << endl;
302     }
303     else {
304         cerr << endl << endl << "Error in PUT response from LED resource" << endl;
305     }
306     IoTClient::DisplayMenu();
307 }
308
309 void IoTClient::DisplayMenu()
310 {
311     cout << "\nEnter:\n0) Display this menu\n1) Get temperature Reading\n2) Start Temperature Observer\n3) Stop Temperature Observer\n4) Get ambient light reading\n5) Start Ambient Light Observer\n6) Stop Ambient Light Observer\n7) Turn LED ON\n8) Turn LED OFF\n9) Quit\n";
312 }
313
314 int main()
315 {
316     IoTClient client;
317     cout << "Performing Discovery..." << endl;
318     client.findResource();
319     int choice;
320     do
321     {
322         cin >> choice;
323         switch (choice)
324         {
325             case 0:
326                 IoTClient::DisplayMenu();
327                 break;
328             case 1:
329                 if (client.getTemperatureSensor())
330                     client.getTemperatureSensor()->get();
331                 else
332                     cout << "Temperature sensor resource not yet discovered" << endl;
333                 break;
334             case 2:
335                 if (client.getTemperatureSensor())
336                     client.getTemperatureSensor()->startObserve();
337                 else
338                     cout << "Temperature sensor resource not yet discovered" << endl;
339                 break;
340             case 3:
341                 if (client.getTemperatureSensor())
342                     client.getTemperatureSensor()->stopObserve();
343                 else
344                     cout << "Temperature sensor resource not yet discovered" << endl;
345                 break;
346             case 4:
347                 if (client.getAmbientLightSensor())
348                     client.getAmbientLightSensor()->get();
349                 else
350                     cout << "Ambient light sensor resource not yet discovered" << endl;
351                 break;
352             case 5:
353                 if (client.getAmbientLightSensor())
354                     client.getAmbientLightSensor()->startObserve();
355                 else
356                     cout << "Ambient light sensor resource not yet discovered" << endl;
357                 break;
358             case 6:
359                 if (client.getAmbientLightSensor())
360                     client.getAmbientLightSensor()->stopObserve();
361                 else
362                     cout << "Ambient light sensor resource not yet discovered" << endl;
363                 break;
364             case 7:
365                 if (client.getPlatformLED())
366                     client.getPlatformLED()->put(1);
367                 else
368                     cout << "LED resource not yet discovered" << endl;
369                 break;
370             case 8:
371                 if (client.getPlatformLED())
372                     client.getPlatformLED()->put(0);
373                 else
374                     cout << "LED resource not yet discovered" << endl;
375                 break;
376             case 9:
377             default:
378                 return 0;
379         }
380     }
381     while (choice != 9);
382     return 0;
383 }