//******************************************************************
//
-// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+// Copyright 2015 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-// OCClient.cpp : Defines the entry point for the console application.
-//
+
#include <string>
#include <map>
#include <cstdlib>
}
+
+void onLightIntensityObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
+ const int &eCode, const int &sequenceNumber)
+{
+ (void)headerOptions;
+ try
+ {
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "OBSERVE RESULT:" << std::endl;
+ std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
+
+
+ std::cout << "\tintensity: " << rep.getValue<int>("intensity") << std::endl;
+
+
+ if (observe_count() > 10)
+ {
+ std::cout << "Cancelling Observe..." << std::endl;
+ OCStackResult result = curResource->cancelObserve();
+
+ std::cout << "Cancel result: " << result << std::endl;
+ sleep(10);
+ std::cout << "DONE" << std::endl;
+ std::exit(0);
+ }
+ }
+ else
+ {
+ std::cout << "onObserve Response error: " << eCode << std::endl;
+ }
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
+ }
+
+}
+
void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
(void)headerOptions;
}
if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
+ {
std::cout << std::endl << "Observe is used." << std::endl << std::endl;
+ }
else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
+ {
std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
+ }
curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
std::cout << "Posting light representation..." << std::endl;
- mylight.m_on_off = "false";
+ mylight.m_on_off = false;
rep.setValue("on-off", mylight.m_on_off);
// Invoke resource's put API with rep, query map and the callback parameter
- resource->put(rep, QueryParamsMap(), &onPut);
+ resource->post(rep, QueryParamsMap(), &onPut);
}
}
std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
- putLightRepresentation(curResource);
+ postLightRepresentation(curResource);
}
else
{
std::cout << "Payload: " << rep.getPayload() << std::endl;
- std::cout << "\lightIntensity: " << rep.getValue<int>("lightintensity") << std::endl;
+ std::cout << "\tlightIntensity: " << rep.getValue<int>("intensity") << std::endl;
+
+ // iterating over all elements
+ OCRepresentation::const_iterator itr = rep.begin();
+ OCRepresentation::const_iterator endItr = rep.end();
+
+ for(;itr!=endItr;++itr)
+ {
+ std::cout << itr->attrname() << " ";
+ std::cout << itr->getValue<int>() << std::endl;
+ }
+
+ curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onLightIntensityObserve);
}
else
{
}
}
+void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
+ const int eCode)
+{
+ (void)headerOptions;
+ std::cout << "onGetDiscomfortIndex" << std::endl;
+ try
+ {
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "GET request was successful" << std::endl;
+
+ std::cout << "Payload: " << rep.getPayload() << std::endl;
+
+ std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ }
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
+ }
+}
+
+void onObserveDiscomfort(const HeaderOptions headerOptions, const OCRepresentation &rep,
+ const int &eCode, const int &sequenceNumber)
+{
+ (void)headerOptions;
+ try
+ {
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "OBSERVE RESULT:" << std::endl;
+ std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
+ std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
+
+ if (observe_count() > 10)
+ {
+ std::cout << "Cancelling Observe..." << std::endl;
+ OCStackResult result = curResource->cancelObserve();
+
+ std::cout << "Cancel result: " << result << std::endl;
+ sleep(10);
+ std::cout << "DONE" << std::endl;
+ std::exit(0);
+ }
+ }
+ else
+ {
+ std::cout << "onObserve Response error: " << eCode << std::endl;
+ }
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
+ }
+
+}
+
+// Local function to get representation of light resource
+void getDiscomfortRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if (resource)
+ {
+ std::cout << "Getting Discomfort Representation..." << std::endl;
+ // Invoke resource's get API with the callback parameter
+
+ QueryParamsMap test;
+ std::cout << "Sending request to: " << resource->uri() << std::endl;
+ resource->get(test, &onGetDiscomfortIndex);
+ //resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
+ }
+}
+
+
+
+
// Local function to get representation of light resource
void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
{
// Call a local function which will internally invoke get API on the resource pointer
getLightRepresentation(resource);
}*/
- if (resourceTypes == "oic.r.lightintensity")
+ if (resourceTypes == "oic.r.discomfortindex")
{
curResource = resource;
+ std::cout << "\t\tGet discomfort representation " << std::endl;
// Call a local function which will internally invoke get API on the resource pointer
- getLightIntensityRepresentation(resource);
+ getDiscomfortRepresentation(resource);
}
}
cv.wait(lock);
}
- catch (OCException &e)
+ catch (const OCException &e)
{
- oclog() << "Exception in main: " << e.what();
+ std::cout << "Exception in main: " << e.what();
}
return 0;