Fixed simpleserver to match correct APIs to set and get Representation from the entit...
authorSudarshan Prasad <sudarshan.prasad@intel.com>
Fri, 22 Aug 2014 09:49:18 +0000 (02:49 -0700)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Fri, 22 Aug 2014 09:49:18 +0000 (02:49 -0700)
Change-Id: I189ccb86bd38d742866c7201c7b00efac12cc827

examples/roomclient.cpp
examples/simpleserver.cpp
include/OCResource.h

index e757473..2077c8f 100644 (file)
@@ -207,6 +207,20 @@ void foundResource(std::shared_ptr<OCResource> resource)
             hostAddress = resource->host();
             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
 
+            // Get the resource types 
+            std::cout << "\tList of resource types: " << std::endl;
+            for(auto &resourceTypes : resource->getResourceTypes())
+            {
+                std::cout << "\t\t" << resourceTypes << std::endl;
+            }
+            
+            // Get the resource interfaces
+            std::cout << "\tList of resource interfaces: " << std::endl;
+            for(auto &resourceInterfaces : resource->getResourceInterfaces())
+            {
+                std::cout << "\t\t" << resourceInterfaces << std::endl;
+            } 
+
             if(resourceURI == "/a/room")
             {
                 curResource = resource;
index 8ecd726..afa9b18 100644 (file)
@@ -47,11 +47,12 @@ public:
     /// Access this property from a TB client
     bool m_state;
     int m_power;
+    std::string m_lightUri;
     OCResourceHandle m_resourceHandle;
 
 public:
     /// Constructor
-    LightResource(): m_state(false), m_power(0){}
+    LightResource(): m_state(false), m_power(0), m_lightUri("/a/light") {}
 
     /* Note that this does not need to be a member function: for classes you do not have
     access to, you can accomplish this with a free function: */
@@ -59,7 +60,7 @@ public:
     /// This function internally calls registerResource API.
     void createResource(OC::OCPlatform& platform)
     {
-        std::string resourceURI = "/a/light"; // URI of the resource
+        std::string resourceURI = m_lightUri; // URI of the resource
         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
 
@@ -82,18 +83,38 @@ public:
         return m_resourceHandle;
     }
 
-    void setRepresentation(AttributeMap& attributeMap)
+    void setRepresentation(OCRepresentation& light)
     {
-        cout << "\t\t\t" << "Received representation: " << endl;
-        cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
-        cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
+        AttributeMap attributeMap = light.getAttributeMap();
 
-        m_state = attributeMap["state"][0].compare("true") == 0;
-        m_power = std::stoi(attributeMap["power"][0]);
+        if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("power") != attributeMap.end())
+        {
+            cout << "\t\t\t" << "Received representation: " << endl;
+            cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
+            cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
+
+            m_state = attributeMap["state"][0].compare("true") == 0;
+            m_power= std::stoi(attributeMap["power"][0]);
+        }
     }
 
-    void getRepresentation(AttributeMap& attributeMap)
+    OCRepresentation getRepresentation()
     {
+        OCRepresentation light;
+
+        light.setUri(m_lightUri);
+
+        std::vector<std::string> interfaces;
+        //interfaces.push_back(m_lightInterface);
+
+        light.setResourceInterfaces(interfaces);
+
+        std::vector<std::string> types;
+        //types.push_back(m_lightType);
+
+        light.setResourceTypes(types);
+
+        AttributeMap attributeMap;
         AttributeValues stateVal;
         if(m_state)
         {
@@ -109,6 +130,10 @@ public:
 
         attributeMap["state"] = stateVal;
         attributeMap["power"] = powerVal;
+
+        light.setAttributeMap(attributeMap);
+
+        return light;
     }
 
     void addType(const OC::OCPlatform& platform, const std::string& type) const
@@ -207,15 +232,25 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
                 // Process query params and do required operations ..
 
                 // Get the representation of this resource at this point and send it as response
-                AttributeMap attributeMap;
-
-                myLightResource.getRepresentation(attributeMap);
+                // AttributeMap attributeMap;
+                OCRepresentation rep;
+                rep = myLightResource.getRepresentation();
 
                 if(response)
                 {
                     // TODO Error Code
                     response->setErrorCode(200);
-                    response->setResourceRepresentation(attributeMap);
+
+                    auto findRes = queryParamsMap.find("if");
+
+                    if(findRes != queryParamsMap.end())
+                    {
+                        response->setResourceRepresentation(rep, findRes->second);
+                    }
+                    else
+                    {
+                        response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
+                    }
                 }
             }
             else if(requestType == "PUT")
@@ -225,22 +260,37 @@ void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<O
                 // Check for query params (if any)
                 QueryParamsMap queryParamsMap = request->getQueryParameters();
 
-                // Check queryParamsMap and do required operations ..
+                cout << "\t\t\tquery params: \n";
+                for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
+                {
+                    cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
+                }
 
                 // Get the representation from the request
-                AttributeMap attributeMap = request->getAttributeRepresentation();
+                OCRepresentation rep = request->getResourceRepresentation();
 
-                myLightResource.setRepresentation(attributeMap);
+                myLightResource.setRepresentation(rep);
 
                 // Do related operations related to PUT request
-
-                myLightResource.getRepresentation(attributeMap);
+                rep = myLightResource.getRepresentation();
 
                 if(response)
                 {
+                    // TODO Error Code
                     response->setErrorCode(200);
-                    response->setResourceRepresentation(attributeMap);
+
+                    auto findRes = queryParamsMap.find("if");
+
+                    if(findRes != queryParamsMap.end())
+                    {
+                        response->setResourceRepresentation(rep, findRes->second);
+                    }
+                    else
+                    {
+                        response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
+                    }
                 }
+
             }
             else if(requestType == "POST")
             {
index cd718b5..0cdf760 100644 (file)
@@ -198,24 +198,6 @@ namespace OC
             return m_interfaces;
         }
 
-        // bool whether this is a collection type, and will have children that can be queried
-        //bool isCollection() const {return m_isCollection;}
-        // a collection of the resource-type names
-        //const std::vector<std::string> resourceTypes() const {return m_resourceTypes;}
-        // a collection of the interfaces supported by this resource
-        //const std::vector<std::string> interfaces() const { return m_interfaces;}
-        // a collection of property objects that allow calling of properties on this Resource
-        //const std::vector<std::string> properties() const { return m_properties;}
-        // a collection of the names of the children of this resource, assuming it supports a collection interface
-        //const std::vector<std::string> children() const {return m_children;}
-
-        /*void post(const AttributeMap&, std::function<void(const int&)> attributeHandler);
-
-        NOTE: dont expose the host ip .. so some kind of handle is required
-        static OCResource::Ptr getResource(const std::string& host, const std::string& resourceURI, const std::string& resourceName,
-            const std::string interfaceName, bool observerable);*/
-
-
     private:
         std::weak_ptr<IClientWrapper> m_clientWrapper;
         std::string m_uri;