Imported Upstream version 1.0.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_RSRVD_WELL_KNOWN_URI "?if=" EDISON_RESOURCE_INTERFACE);
46     OCPlatform::findResource("", coap_multicast_discovery.c_str(),  CT_DEFAULT, 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 && sequenceNumber != OC_OBSERVE_NO_OPTION)
148     {
149         if(sequenceNumber == OC_OBSERVE_REGISTER)
150         {
151             cout << "Observe registration action is successful" << endl;
152         }
153         else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
154         {
155             cout << "Observe De-registration action is successful" << endl;
156         }
157         double value;
158         rep.getValue(TEMPERATURE_RESOURCE_KEY, value);
159         cout << "Observing TemperatureSensor: Current temperature reading in Celsius is " << value << endl;
160         cout << "Sequence number: " << sequenceNumber << endl;
161     }
162     else
163     {
164         if(sequenceNumber == OC_OBSERVE_NO_OPTION)
165         {
166             cerr << "Observe registration or de-registration action is failed" << endl;
167         }
168         else
169         {
170             cerr << "TemperatureSensor: Observer response error" << endl;
171         }
172     }
173 }
174
175 void TemperatureSensor::get()
176 {
177     QueryParamsMap params;
178     m_resourceHandle->get(params, m_GETCallback);
179 }
180
181 void TemperatureSensor::onGet(const HeaderOptions& headerOptions,
182                               const OCRepresentation& representation, int errCode)
183 {
184     if (errCode == OC_STACK_OK)
185     {
186         double value;
187         representation.getValue(TEMPERATURE_RESOURCE_KEY, value);
188         cout << endl << endl << "Current temperature reading in Celsius: " << value << endl;
189     }
190     else {
191         cerr << endl << endl << "Error in GET response from temperature sensor resource" << endl;
192     }
193     IoTClient::DisplayMenu();
194 }
195
196 AmbientLight::AmbientLight(shared_ptr<OCResource> Resource)
197 {
198     m_isObserved = false;
199     m_resourceHandle = Resource;
200     m_GETCallback = bind(&AmbientLight::onGet, this, placeholders::_1, placeholders::_2,
201                          placeholders::_3);
202     m_OBSERVECallback = bind(&AmbientLight::onObserve, this, placeholders::_1, placeholders::_2,
203                              placeholders::_3, placeholders::_4);
204 }
205
206 AmbientLight::~AmbientLight()
207 {
208
209 }
210
211 void AmbientLight::startObserve()
212 {
213     if (!m_isObserved)
214     {
215         cout << "Started observing ambient light sensor" << endl;
216         m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
217     }
218     m_isObserved = true;
219 }
220
221 void AmbientLight::stopObserve()
222 {
223     if (m_isObserved)
224     {
225         m_resourceHandle->cancelObserve();
226         cout << "Stopped observing ambient light sensor" << endl;
227     }
228     m_isObserved = false;
229 }
230
231 void AmbientLight::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
232                              int eCode, int sequenceNumber)
233 {
234     if (eCode == OC_STACK_OK)
235     {
236         int value;
237         rep.getValue(LIGHT_RESOURCE_KEY, value);
238         cout << "Observing AmbientLightSensor: Current ambient light reading is " << value << endl;
239         cout << "Sequence number: " << sequenceNumber << endl;
240     }
241     else
242     {
243         cerr << "TemperatureSensor: Observer response error" << endl;
244     }
245 }
246
247 void AmbientLight::get()
248 {
249     QueryParamsMap params;
250     m_resourceHandle->get(params, m_GETCallback);
251 }
252
253 void AmbientLight::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
254                          int errCode)
255 {
256     if (errCode == OC_STACK_OK)
257     {
258         int value;
259         representation.getValue(LIGHT_RESOURCE_KEY, value);
260         cout << endl << endl << "Current ambient light sensor reading: " << value << endl;
261     }
262     else {
263         cerr << endl << endl << "Error in GET response from ambient light sensor resource" << endl;
264     }
265     IoTClient::DisplayMenu();
266 }
267
268 LED::LED(shared_ptr<OCResource> Resource)
269 {
270     m_resourceHandle = Resource;
271     m_GETCallback = bind(&LED::onGet, this, placeholders::_1, placeholders::_2, placeholders::_3);
272     m_PUTCallback = bind(&LED::onPut, this, placeholders::_1, placeholders::_2, placeholders::_3);
273 }
274
275 LED::~LED()
276 {
277
278 }
279
280 void LED::get()
281 {
282     QueryParamsMap params;
283     m_resourceHandle->get(params, m_GETCallback);
284 }
285
286 void LED::put(int Switch)
287 {
288     QueryParamsMap params;
289     OCRepresentation rep;
290     rep.setValue(LED_RESOURCE_KEY, Switch);
291     m_resourceHandle->put(rep, params, m_PUTCallback);
292 }
293
294 void LED::onGet(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 << "LED switch state is: " << value << endl;
302     }
303     else {
304         cerr << endl << endl << "Error in GET response from LED resource" << endl;
305     }
306     IoTClient::DisplayMenu();
307 }
308
309 void LED::onPut(const HeaderOptions& headerOptions, const OCRepresentation& representation,
310                 int errCode)
311 {
312     if (errCode == OC_STACK_OK)
313     {
314         int value;
315         representation.getValue(LED_RESOURCE_KEY, value);
316         cout << endl << endl << "Set LED switch to: " << value << endl;
317     }
318     else {
319         cerr << endl << endl << "Error in PUT response from LED resource" << endl;
320     }
321     IoTClient::DisplayMenu();
322 }
323
324 void IoTClient::DisplayMenu()
325 {
326     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";
327 }
328
329 int main()
330 {
331     IoTClient client;
332     cout << "Performing Discovery..." << endl;
333     client.findResource();
334     int choice;
335     do
336     {
337         cin >> choice;
338         switch (choice)
339         {
340             case 0:
341                 IoTClient::DisplayMenu();
342                 break;
343             case 1:
344                 if (client.getTemperatureSensor())
345                     client.getTemperatureSensor()->get();
346                 else
347                     cout << "Temperature sensor resource not yet discovered" << endl;
348                 break;
349             case 2:
350                 if (client.getTemperatureSensor())
351                     client.getTemperatureSensor()->startObserve();
352                 else
353                     cout << "Temperature sensor resource not yet discovered" << endl;
354                 break;
355             case 3:
356                 if (client.getTemperatureSensor())
357                     client.getTemperatureSensor()->stopObserve();
358                 else
359                     cout << "Temperature sensor resource not yet discovered" << endl;
360                 break;
361             case 4:
362                 if (client.getAmbientLightSensor())
363                     client.getAmbientLightSensor()->get();
364                 else
365                     cout << "Ambient light sensor resource not yet discovered" << endl;
366                 break;
367             case 5:
368                 if (client.getAmbientLightSensor())
369                     client.getAmbientLightSensor()->startObserve();
370                 else
371                     cout << "Ambient light sensor resource not yet discovered" << endl;
372                 break;
373             case 6:
374                 if (client.getAmbientLightSensor())
375                     client.getAmbientLightSensor()->stopObserve();
376                 else
377                     cout << "Ambient light sensor resource not yet discovered" << endl;
378                 break;
379             case 7:
380                 if (client.getPlatformLED())
381                     client.getPlatformLED()->put(1);
382                 else
383                     cout << "LED resource not yet discovered" << endl;
384                 break;
385             case 8:
386                 if (client.getPlatformLED())
387                     client.getPlatformLED()->put(0);
388                 else
389                     cout << "LED resource not yet discovered" << endl;
390                 break;
391             case 9:
392             default:
393                 return 0;
394         }
395     }
396     while (choice != 9);
397     return 0;
398 }