roomserver/client updated to demonstrate simple attribute support in collections.
authorSashi Penta <sashi.kumar.penta@intel.com>
Wed, 22 Oct 2014 06:19:01 +0000 (23:19 -0700)
committerSashi Penta <sashi.kumar.penta@intel.com>
Thu, 23 Oct 2014 20:28:12 +0000 (13:28 -0700)
Addressed review comments.

Change-Id: I4a054f1990954a9fd9dd3e98feddd204fd692676

examples/roomclient.cpp
examples/roomserver.cpp
include/OCResourceResponse.h

index 9630925..4ea1cf2 100644 (file)
@@ -41,41 +41,47 @@ int observe_count()
 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);
     }
@@ -86,13 +92,40 @@ void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons
     }
 }
 
+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;
@@ -111,34 +144,9 @@ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons
     {
         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
     {
@@ -147,26 +155,13 @@ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons
     }
 }
 
-
-// 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;
@@ -203,7 +198,7 @@ void foundResource(std::shared_ptr<OCResource> resource)
             {
                 curResource = resource;
                 // Call a local function which will internally invoke get API on the resource pointer
-                getRoomRepresentation(resource);
+                getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
             }
         }
         else
index a59c236..bae9eab 100644 (file)
@@ -47,6 +47,7 @@ public:
 
     // 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;
@@ -72,7 +73,8 @@ public:
 
 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
@@ -99,6 +101,7 @@ public:
         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);
@@ -110,7 +113,7 @@ public:
         // 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
                                   );
 
@@ -295,12 +298,28 @@ OCEntityHandlerResult entityHandlerRoom(std::shared_ptr<OCResourceRequest> reque
             {
                 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")
@@ -488,8 +507,6 @@ int main()
     {
         OCPlatform platform(cfg);
 
-        // Invoke createResource function of class light.
-
         myRoomResource.createResources(platform);
 
         // Perform app tasks
index 37bb7ae..4cd371f 100644 (file)
@@ -311,40 +311,6 @@ namespace OC
             m_payload = payload.str();
         }
 
-
-        /** TODO remove this once after above function stabilize.
-        *  API to set the entire resource attribute representation
-        *  @param attributeMap reference containing the name value pairs representing the
-        *  resource's attributes
-        */
-        void setResourceRepresentation(AttributeMap& attributes) {
-
-            // TODO To be refactored
-            ostringstream payload;
-
-            payload << "{";
-
-            // TODO fix this (do this programmatically)
-            payload << "\"href\":\"/a/room\"";
-
-            payload << ",\"rep\":{";
-
-            for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
-            {
-                if(itr != attributes.begin())
-                {
-                    payload << ',';
-                }
-                // cout << itr->first << ":" <, itr->second.front() << endl;
-                payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
-
-            }
-
-            payload << "}}";
-
-            m_payload = payload.str();
-        }
-
     private:
         std::string m_newResourceUri;
         std::string m_payload;