void putRoomRepresentation(std::shared_ptr<OCResource> resource);
void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
-// callback handler on GET request
-void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void printRoomRepresentation(const OCRepresentation& rep)
{
- if(eCode == SUCCESS_RESPONSE)
- {
- std::cout << "GET request was successful" << std::endl;
+ std::cout << "\tResource URI: " << rep.getUri() << std::endl;
- std::cout << "\tResource URI: " << rep.getUri() << std::endl;
+ if(rep.hasAttribute("name"))
+ {
+ std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
+ }
- std::vector<OCRepresentation> children = rep.getChildren();
+ std::vector<OCRepresentation> children = rep.getChildren();
- for(auto oit = children.begin(); oit != children.end(); ++oit)
+ for(auto oit = children.begin(); oit != children.end(); ++oit)
+ {
+ std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
+ if(oit->getUri().find("light") != std::string::npos)
{
- std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
- if(oit->getUri().find("light") != std::string::npos)
+ if(oit->hasAttribute("state") && oit->hasAttribute("color"))
{
- bool state = false;
- int color = 0;
- oit->getValue("state", state);
- oit->getValue("color", color);
-
- std::cout << "\t\tstate:" << state << std::endl;
- std::cout << "\t\tcolor:" << color << std::endl;
+ std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
+ std::cout << "\t\tcolor:" << oit->getValue<int>("color") << std::endl;
}
- else if(oit->getUri().find("fan") != std::string::npos)
+ }
+ else if(oit->getUri().find("fan") != std::string::npos)
+ {
+ if(oit->hasAttribute("state") && oit->hasAttribute("speed"))
{
- bool state = false;
- int speed = 0;
- oit->getValue("state", state);
- oit->getValue("speed", speed);
-
- std::cout << "\t\tstate:" << state << std::endl;
- std::cout << "\t\tspeed:" << speed << std::endl;
+ std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
+ std::cout << "\t\tspeed:" << oit->getValue<int>("speed") << std::endl;
}
}
+ }
+}
+
+// callback handler on GET request
+void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ if(eCode == SUCCESS_RESPONSE)
+ {
+ std::cout << "GET request was successful" << std::endl;
+
+ printRoomRepresentation(rep);
putRoomRepresentation(curResource);
}
}
}
+void onGet1(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ if(eCode == SUCCESS_RESPONSE)
+ {
+ std::cout << "GET request was successful" << std::endl;
+
+ printRoomRepresentation(rep);
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+}
+
+// Local function to get representation of room resource
+void getRoomRepresentation(std::shared_ptr<OCResource> resource,
+ std::string interface, GetCallback getCallback)
+{
+ if(resource)
+ {
+ std::cout << "Getting room representation on: "<< interface << std::endl;
+
+ resource->get("core.room", interface, QueryParamsMap(), getCallback);
+ }
+}
+
// Local function to put a different state for this resource
void putRoomRepresentation(std::shared_ptr<OCResource> resource)
{
if(resource)
{
OCRepresentation rep;
- std::cout << "Putting room representation..."<<std::endl;
+ std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
bool state = true;
int speed = 10;
{
std::cout << "PUT request was successful" << std::endl;
- std::cout << "\tResource URI: " << rep.getUri() << std::endl;
-
- std::vector<OCRepresentation> children = rep.getChildren();
-
- for(auto oit = children.begin(); oit != children.end(); ++oit)
- {
- std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
- if(oit->getUri().find("light") != std::string::npos)
- {
- bool state = false;
- int color = 0;
- oit->getValue("state", state);
- oit->getValue("color", color);
- std::cout << "\t\tstate:" << state << std::endl;
- std::cout << "\t\tcolor:" << color << std::endl;
- }
- else if(oit->getUri().find("fan") != std::string::npos)
- {
- bool state = false;
- int speed = 0;
- oit->getValue("state", state);
- oit->getValue("speed", speed);
- std::cout << "\t\tstate:" << state << std::endl;
- std::cout << "\t\tspeed:" << speed << std::endl;
- }
-
- }
+ printRoomRepresentation(rep);
+ getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
}
else
{
}
}
-
-// Local function to get representation of light resource
-void getRoomRepresentation(std::shared_ptr<OCResource> resource)
-{
- if(resource)
- {
- std::cout << "Getting Room Representation..."<<std::endl;
- // Invoke resource's get API with the callback parameter
-
- QueryParamsMap qp;
- resource->get("core.room", BATCH_INTERFACE, qp, &onGet);
- }
-}
-
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
if(curResource)
{
std::cout << "Found another resource, ignoring"<<std::endl;
+ return;
}
std::string resourceURI;
{
curResource = resource;
// Call a local function which will internally invoke get API on the resource pointer
- getRoomRepresentation(resource);
+ getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
}
}
else
// Room members
std::string m_roomUri;
+ std::string m_roomName;
std::vector<std::string> m_roomTypes;
std::vector<std::string> m_roomInterfaces;
OCResourceHandle m_roomHandle;
public:
/// Constructor
- RoomResource(): m_lightState(false), m_lightColor(0), m_fanState(false), m_fanSpeed(0)
+ RoomResource(): m_roomName("John's Room"), m_lightState(false),
+ m_lightColor(0), m_fanState(false), m_fanSpeed(0)
{
m_lightUri = "/a/light"; // URI of the resource
m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
+ m_roomRep.setValue("name", m_roomName);
m_roomRep.setUri(m_roomUri);
m_roomRep.setResourceTypes(m_roomTypes);
m_roomRep.setResourceInterfaces(m_roomInterfaces);
// This will internally create and register the resource.
OCStackResult result = platform.registerResource(
m_roomHandle, m_roomUri, m_roomTypes[0],
- m_roomInterfaces[0], NULL, //entityHandlerRoom,
+ m_roomInterfaces[0], entityHandlerRoom,
OC_DISCOVERABLE | OC_OBSERVABLE
);
{
cout << "\t\t\trequestType : PUT\n";
+ QueryParamsMap queryParamsMap = request->getQueryParameters();
+
entityHandlerLight(request, response);
entityHandlerFan(request, response);
+ OCRepresentation rep;
+ rep = myRoomResource.getRoomRepresentation();
+
if(response)
{
- response->setResourceRepresentation(myRoomResource.getRoomRepresentation());
+ response->setErrorCode(200);
+
+ auto findRes = queryParamsMap.find("if");
+
+ if(findRes != queryParamsMap.end())
+ {
+ response->setResourceRepresentation(rep, findRes->second);
+ }
+ else
+ {
+ response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
+ }
}
}
else if(requestType == "POST")
{
OCPlatform platform(cfg);
- // Invoke createResource function of class light.
-
myRoomResource.createResources(platform);
// Perform app tasks