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_WELL_KNOWN_QUERY "?if=" EDISON_RESOURCE_INTERFACE);
46 OCPlatform::findResource("", coap_multicast_discovery.c_str(), 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)
150 rep.getValue(TEMPERATURE_RESOURCE_KEY, value);
151 cout << "Observing TemperatureSensor: Current temperature reading is " << value << endl;
152 cout << "Sequence number: " << sequenceNumber << endl;
156 cerr << "TemperatureSensor: Observer response error" << endl;
160 void TemperatureSensor::get()
162 QueryParamsMap params;
163 m_resourceHandle->get(params, m_GETCallback);
166 void TemperatureSensor::onGet(const HeaderOptions& headerOptions,
167 const OCRepresentation& representation, int errCode)
169 if (errCode == OC_STACK_OK)
172 representation.getValue(TEMPERATURE_RESOURCE_KEY, value);
173 cout << endl << endl << "Current temperature reading: " << value << endl;
176 cerr << endl << endl << "Error in GET response from temperature sensor resource" << endl;
178 IoTClient::DisplayMenu();
181 AmbientLight::AmbientLight(shared_ptr<OCResource> Resource)
183 m_isObserved = false;
184 m_resourceHandle = Resource;
185 m_GETCallback = bind(&AmbientLight::onGet, this, placeholders::_1, placeholders::_2,
187 m_OBSERVECallback = bind(&AmbientLight::onObserve, this, placeholders::_1, placeholders::_2,
188 placeholders::_3, placeholders::_4);
191 AmbientLight::~AmbientLight()
196 void AmbientLight::startObserve()
200 cout << "Started observing ambient light sensor" << endl;
201 m_resourceHandle->observe(ObserveType::Observe, QueryParamsMap(), m_OBSERVECallback);
206 void AmbientLight::stopObserve()
210 m_resourceHandle->cancelObserve();
211 cout << "Stopped observing ambient light sensor" << endl;
213 m_isObserved = false;
216 void AmbientLight::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
217 int eCode, int sequenceNumber)
219 if (eCode == OC_STACK_OK)
222 rep.getValue(LIGHT_RESOURCE_KEY, value);
223 cout << "Observing AmbientLightSensor: Current ambient light reading is " << value << endl;
224 cout << "Sequence number: " << sequenceNumber << endl;
228 cerr << "TemperatureSensor: Observer response error" << endl;
232 void AmbientLight::get()
234 QueryParamsMap params;
235 m_resourceHandle->get(params, m_GETCallback);
238 void AmbientLight::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
241 if (errCode == OC_STACK_OK)
244 representation.getValue(LIGHT_RESOURCE_KEY, value);
245 cout << endl << endl << "Current ambient light sensor reading: " << value << endl;
248 cerr << endl << endl << "Error in GET response from ambient light sensor resource" << endl;
250 IoTClient::DisplayMenu();
253 LED::LED(shared_ptr<OCResource> Resource)
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);
267 QueryParamsMap params;
268 m_resourceHandle->get(params, m_GETCallback);
271 void LED::put(int Switch)
273 QueryParamsMap params;
274 OCRepresentation rep;
275 rep.setValue(LED_RESOURCE_KEY, Switch);
276 m_resourceHandle->put(rep, params, m_PUTCallback);
279 void LED::onGet(const HeaderOptions& headerOptions, const OCRepresentation& representation,
282 if (errCode == OC_STACK_OK)
285 representation.getValue(LED_RESOURCE_KEY, value);
286 cout << endl << endl << "LED switch state is: " << value << endl;
289 cerr << endl << endl << "Error in GET response from LED resource" << endl;
291 IoTClient::DisplayMenu();
294 void LED::onPut(const HeaderOptions& headerOptions, const OCRepresentation& representation,
297 if (errCode == OC_STACK_OK)
300 representation.getValue(LED_RESOURCE_KEY, value);
301 cout << endl << endl << "Set LED switch to: " << value << endl;
304 cerr << endl << endl << "Error in PUT response from LED resource" << endl;
306 IoTClient::DisplayMenu();
309 void IoTClient::DisplayMenu()
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";
317 cout << "Performing Discovery..." << endl;
318 client.findResource();
326 IoTClient::DisplayMenu();
329 if (client.getTemperatureSensor())
330 client.getTemperatureSensor()->get();
332 cout << "Temperature sensor resource not yet discovered" << endl;
335 if (client.getTemperatureSensor())
336 client.getTemperatureSensor()->startObserve();
338 cout << "Temperature sensor resource not yet discovered" << endl;
341 if (client.getTemperatureSensor())
342 client.getTemperatureSensor()->stopObserve();
344 cout << "Temperature sensor resource not yet discovered" << endl;
347 if (client.getAmbientLightSensor())
348 client.getAmbientLightSensor()->get();
350 cout << "Ambient light sensor resource not yet discovered" << endl;
353 if (client.getAmbientLightSensor())
354 client.getAmbientLightSensor()->startObserve();
356 cout << "Ambient light sensor resource not yet discovered" << endl;
359 if (client.getAmbientLightSensor())
360 client.getAmbientLightSensor()->stopObserve();
362 cout << "Ambient light sensor resource not yet discovered" << endl;
365 if (client.getPlatformLED())
366 client.getPlatformLED()->put(1);
368 cout << "LED resource not yet discovered" << endl;
371 if (client.getPlatformLED())
372 client.getPlatformLED()->put(0);
374 cout << "LED resource not yet discovered" << endl;