1 //******************************************************************
3 // Copyright 2014 Intel Corporation.
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 IoTClient::IoTClient()
26 cout << "Running IoTClient constructor" << endl;
30 IoTClient::~IoTClient()
32 cout << "Running IoTClient destructor" << endl;
35 void IoTClient::initializePlatform()
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);
43 void IoTClient::findResource()
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);
50 void IoTClient::discoveredResource(shared_ptr<OCResource> Resource)
56 string resourceUri = Resource->uri();
57 string hostAddress = Resource->host();
59 cout << "\nFound Resource" << endl << "Resource Types:" << endl;
60 for (auto& resourceTypes : Resource->getResourceTypes())
62 cout << "\t" << resourceTypes << endl;
65 cout << "Resource Interfaces: " << endl;
66 for (auto& resourceInterfaces : Resource->getResourceInterfaces())
68 cout << "\t" << resourceInterfaces << endl;
70 cout << "Resource uri: " << resourceUri << endl;
71 cout << "host: " << hostAddress << endl;
73 if (resourceUri == TEMPERATURE_RESOURCE_ENDPOINT)
75 m_temperatureSensor = make_shared<TemperatureSensor>(Resource);
77 else if (resourceUri == LIGHT_RESOURCE_ENDPOINT)
79 m_ambientLightSensor = make_shared<AmbientLight>(Resource);
81 else if (resourceUri == LED_RESOURCE_ENDPOINT)
83 m_platformLED = make_shared<LED>(Resource);
86 IoTClient::DisplayMenu();
88 catch (OCException& ex)
90 cerr << "Caught exception in discoveredResource: " << ex.reason() << endl;
94 shared_ptr<TemperatureSensor> IoTClient::getTemperatureSensor()
96 return m_temperatureSensor;
99 shared_ptr<AmbientLight> IoTClient::getAmbientLightSensor()
101 return m_ambientLightSensor;
104 shared_ptr<LED> IoTClient::getPlatformLED()
106 return m_platformLED;
109 TemperatureSensor::TemperatureSensor(shared_ptr<OCResource> Resource)
111 m_isObserved = false;
112 m_resourceHandle = Resource;
113 m_GETCallback = bind(&TemperatureSensor::onGet, this, placeholders::_1, placeholders::_2,
115 m_OBSERVECallback = bind(&TemperatureSensor::onObserve, this, placeholders::_1,
116 placeholders::_2, placeholders::_3, placeholders::_4);
119 TemperatureSensor::~TemperatureSensor()
124 void TemperatureSensor::startObserve()
128 cout << "Starting observing temperature sensor" << endl;
129 m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
134 void TemperatureSensor::stopObserve()
138 m_resourceHandle->cancelObserve();
139 cout << "Stopped observing temperature sensor" << endl;
141 m_isObserved = false;
144 void TemperatureSensor::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
145 int eCode, int sequenceNumber)
147 if (eCode == OC_STACK_OK && sequenceNumber != OC_OBSERVE_NO_OPTION)
149 if(sequenceNumber == OC_OBSERVE_REGISTER)
151 cout << "Observe registration action is successful" << endl;
153 else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
155 cout << "Observe De-registration action is successful" << endl;
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;
164 if(sequenceNumber == OC_OBSERVE_NO_OPTION)
166 cerr << "Observe registration or de-registration action is failed" << endl;
170 cerr << "TemperatureSensor: Observer response error" << endl;
175 void TemperatureSensor::get()
177 QueryParamsMap params;
178 m_resourceHandle->get(params, m_GETCallback);
181 void TemperatureSensor::onGet(const HeaderOptions& headerOptions,
182 const OCRepresentation& representation, int errCode)
184 if (errCode == OC_STACK_OK)
187 representation.getValue(TEMPERATURE_RESOURCE_KEY, value);
188 cout << endl << endl << "Current temperature reading in Celsius: " << value << endl;
191 cerr << endl << endl << "Error in GET response from temperature sensor resource" << endl;
193 IoTClient::DisplayMenu();
196 AmbientLight::AmbientLight(shared_ptr<OCResource> Resource)
198 m_isObserved = false;
199 m_resourceHandle = Resource;
200 m_GETCallback = bind(&AmbientLight::onGet, this, placeholders::_1, placeholders::_2,
202 m_OBSERVECallback = bind(&AmbientLight::onObserve, this, placeholders::_1, placeholders::_2,
203 placeholders::_3, placeholders::_4);
206 AmbientLight::~AmbientLight()
211 void AmbientLight::startObserve()
215 cout << "Started observing ambient light sensor" << endl;
216 m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
221 void AmbientLight::stopObserve()
225 m_resourceHandle->cancelObserve();
226 cout << "Stopped observing ambient light sensor" << endl;
228 m_isObserved = false;
231 void AmbientLight::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
232 int eCode, int sequenceNumber)
234 if (eCode == OC_STACK_OK)
237 rep.getValue(LIGHT_RESOURCE_KEY, value);
238 cout << "Observing AmbientLightSensor: Current ambient light reading is " << value << endl;
239 cout << "Sequence number: " << sequenceNumber << endl;
243 cerr << "TemperatureSensor: Observer response error" << endl;
247 void AmbientLight::get()
249 QueryParamsMap params;
250 m_resourceHandle->get(params, m_GETCallback);
253 void AmbientLight::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
256 if (errCode == OC_STACK_OK)
259 representation.getValue(LIGHT_RESOURCE_KEY, value);
260 cout << endl << endl << "Current ambient light sensor reading: " << value << endl;
263 cerr << endl << endl << "Error in GET response from ambient light sensor resource" << endl;
265 IoTClient::DisplayMenu();
268 LED::LED(shared_ptr<OCResource> Resource)
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);
282 QueryParamsMap params;
283 m_resourceHandle->get(params, m_GETCallback);
286 void LED::put(int Switch)
288 QueryParamsMap params;
289 OCRepresentation rep;
290 rep.setValue(LED_RESOURCE_KEY, Switch);
291 m_resourceHandle->put(rep, params, m_PUTCallback);
294 void LED::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
297 if (errCode == OC_STACK_OK)
300 representation.getValue(LED_RESOURCE_KEY, value);
301 cout << endl << endl << "LED switch state is: " << value << endl;
304 cerr << endl << endl << "Error in GET response from LED resource" << endl;
306 IoTClient::DisplayMenu();
309 void LED::onPut(const HeaderOptions& headerOptions, const OCRepresentation& representation,
312 if (errCode == OC_STACK_OK)
315 representation.getValue(LED_RESOURCE_KEY, value);
316 cout << endl << endl << "Set LED switch to: " << value << endl;
319 cerr << endl << endl << "Error in PUT response from LED resource" << endl;
321 IoTClient::DisplayMenu();
324 void IoTClient::DisplayMenu()
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";
332 cout << "Performing Discovery..." << endl;
333 client.findResource();
341 IoTClient::DisplayMenu();
344 if (client.getTemperatureSensor())
345 client.getTemperatureSensor()->get();
347 cout << "Temperature sensor resource not yet discovered" << endl;
350 if (client.getTemperatureSensor())
351 client.getTemperatureSensor()->startObserve();
353 cout << "Temperature sensor resource not yet discovered" << endl;
356 if (client.getTemperatureSensor())
357 client.getTemperatureSensor()->stopObserve();
359 cout << "Temperature sensor resource not yet discovered" << endl;
362 if (client.getAmbientLightSensor())
363 client.getAmbientLightSensor()->get();
365 cout << "Ambient light sensor resource not yet discovered" << endl;
368 if (client.getAmbientLightSensor())
369 client.getAmbientLightSensor()->startObserve();
371 cout << "Ambient light sensor resource not yet discovered" << endl;
374 if (client.getAmbientLightSensor())
375 client.getAmbientLightSensor()->stopObserve();
377 cout << "Ambient light sensor resource not yet discovered" << endl;
380 if (client.getPlatformLED())
381 client.getPlatformLED()->put(1);
383 cout << "LED resource not yet discovered" << endl;
386 if (client.getPlatformLED())
387 client.getPlatformLED()->put(0);
389 cout << "LED resource not yet discovered" << endl;