//******************************************************************
//
-// 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>
}
};
+class LightSensor
+{
+public:
+ int m_intensity;
+
+ std::string m_name;
+
+ LightSensor() : m_intensity(0), m_name("")
+ {
+ }
+};
+
Light mylight;
int observe_count()
}
+
+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);
}
}
-// Local function to put a different state for this resource
+// Local function to put a different state for this re<< std::endlsource
void postLightRepresentation(std::shared_ptr<OCResource> resource)
{
if (resource)
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 << "Resource URI: " << rep.getUri() << std::endl;
std::cout << "Payload: " << rep.getPayload() << std::endl;
+ std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
rep.getValue("on-off", mylight.m_on_off);
- rep.getValue("dim", mylight.m_dim);
- rep.getValue("color", mylight.m_color);
std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
- std::cout << "\tcolor: " << mylight.m_color << std::endl;
- std::cout << "\tdim: " << mylight.m_dim << std::endl;
- putLightRepresentation(curResource);
+ postLightRepresentation(curResource);
}
else
{
}
}
+
+void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
+ const int eCode)
+{
+ (void)headerOptions;
+ try
+ {
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "GET request was successful" << std::endl;
+ //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
+
+ std::cout << "Payload: " << rep.getPayload() << 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
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ }
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
+ }
+}
+
// Local function to get representation of light resource
void getLightRepresentation(std::shared_ptr<OCResource> resource)
{
}
}
-// Callback to found resources
-void foundResource(std::shared_ptr<OCResource> resource)
+void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
+ const int eCode)
{
- std::cout << "In foundResource\n";
- std::string resourceURI = resource->uri();
- std::string hostAddress;
+ (void)headerOptions;
+ std::cout << "onGetDiscomfortIndex" << std::endl;
try
{
+ if (eCode == OC_STACK_OK)
{
- std::lock_guard<std::mutex> lock(curResourceLock);
- if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
- {
- std::cout << "Found resource " << resource->uniqueIdentifier() <<
- " for the first time on server with ID: " << resource->sid() << std::endl;
- discoveredResources[resource->uniqueIdentifier()] = resource;
-
- if (resourceURI.find("/discomfortIndex") != std::string::npos)
- {
- std::cout << "discomfortIndex found !!! " << std::endl;
+ std::cout << "GET request was successful" << std::endl;
- DISensorResource = resource;
+ std::cout << "Payload: " << rep.getPayload() << std::endl;
- OCRepresentation rep;
+ 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;
+ }
+}
- rep.setValue("humidity", std::string("30"));
- rep.setValue("temperature", std::string("27"));
+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;
- resource->put(rep, QueryParamsMap(), &onPutForDISensor);
- }
- }
- else
+ if (observe_count() > 10)
{
- std::cout << "Found resource " << resource->uniqueIdentifier() << " again!" << std::endl;
- }
+ std::cout << "Cancelling Observe..." << std::endl;
+ OCStackResult result = curResource->cancelObserve();
- if (curResource)
- {
- std::cout << "Found another resource, ignoring" << std::endl;
- return;
+ 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)
+{
+ if (resource)
+ {
+ std::cout << "Getting Light 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, &onGetForLightIntensitySensor);
+ }
+}
+
+// Callback to found resources
+void foundResource(std::shared_ptr<OCResource> resource)
+{
+ std::cout << "In foundResource\n";
+ std::string resourceURI = resource->uri();
+ std::string hostAddress;
+ try
+ {
+
// Do some operations with resource object.
if (resource)
for (auto &resourceTypes : resource->getResourceTypes())
{
std::cout << "\t\t" << resourceTypes << std::endl;
-
- if (resourceTypes == "oic.r.light")
+ /*if (resourceTypes == "oic.r.light")
{
curResource = resource;
// Call a local function which will internally invoke get API on the resource pointer
getLightRepresentation(resource);
+ }*/
+ 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
+ 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;