modify bundle resource and request handler to update multiple attributes
authorMinji Park <minjii.park@samsung.com>
Wed, 16 Sep 2015 11:32:10 +0000 (20:32 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 17 Sep 2015 14:10:03 +0000 (14:10 +0000)
- modify set, get request handler in the container
- add setAttributes in bundle resource to update multiple attributes
- modify protocol bridge, softsensor resource template and samples

Change-Id: I361c0b2a8d01f3a783e2a22bb2469aee0445a9e0
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2585
Reviewed-by: Markus Jung <markus.jung@samsung.com>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
13 files changed:
service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h
service/resource-encapsulation/src/resourceContainer/bundle-api/include/ProtocolBridgeResource.h
service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h
service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h
service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp
service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h
service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp
service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/include/HueLight.h
service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp
service/resource-encapsulation/src/resourceContainer/src/BundleResource.cpp
service/resource-encapsulation/src/resourceContainer/src/ProtocolBridgeResource.cpp
service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp
service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp

index 27778d2..339000b 100644 (file)
@@ -57,7 +57,7 @@ namespace OIC
                 /**
                 * Return the list of attribute names of the resource
                 *
-                * @return std::list - return list of the attribute names
+                * @return List of the attribute names
                 */
                 std::list<std::string> getAttributeNames();
 
@@ -71,7 +71,7 @@ namespace OIC
                 /**
                 * Register notification receiver(resource container) to notify for the changes of attributes
                 *
-                * @param pNotiReceiver Notification Receiver to get notification from bundle resource
+                * @param pNotiReceiver Notification Receiver to get notification from bundle resource
                 *
                 * @return void
                 */
@@ -80,30 +80,38 @@ namespace OIC
                 /**
                 * Return all attributes of the resource
                 *
-                * @return RCSResourceAttributes - attributes of the resource
+                * @return Attributes of the resource
                 */
                 virtual RCSResourceAttributes &getAttributes();
 
                 /**
-                * Execute the logic of bundle to set the value of attribute
-                *
-                * @param key - name of attribute to set
+                * Set attributes of the resource
                 *
-                * @param value - value of attribute to set
+                * @param attrs Attributes to set
                 *
                 * @return void
                 */
-                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value);
+                virtual void setAttributes(RCSResourceAttributes &attrs);
 
                 /**
                 * Execute the logic of bundle to get the value of attribute
                 *
-                * @param key - key of attribute to get
+                * @param key Key of attribute to get
                 *
-                * @return RCSResourceAttributes::Value - return value of the attribute
+                * @return Value of the attribute
                 */
                 virtual RCSResourceAttributes::Value getAttribute(const std::string &key);
 
+                /**
+                * Execute the logic of bundle to set the value of attribute
+                *
+                * @param key Name of attribute to set
+                *
+                * @param value Value of attribute to set
+                *
+                * @return void
+                */
+                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value);
 
             public:
                 std::string m_bundleId;
@@ -113,9 +121,8 @@ namespace OIC
             private:
                 NotificationReceiver *m_pNotiReceiver;
                 RCSResourceAttributes m_resourceAttributes;
-
         };
     }
 }
 
-#endif
+#endif
\ No newline at end of file
index 4174fde..2bd5645 100644 (file)
@@ -51,33 +51,50 @@ namespace OIC
                 virtual ~ProtocolBridgeResource();
 
                 /**
-                * Return all attributes of the resource
+                * Initialize attributes of the resource
                 *
-                * @return RCSResourceAttributes - attributes of the resource
+                * @return void
                 */
-                virtual RCSResourceAttributes &getAttributes() = 0;
+                virtual void initAttributes() = 0;
 
                 /**
-                * Execute the logic of bundle to set the value of attribute
+                * Return all attributes of the resource
                 *
-                * @param key - name of attribute to set
+                * @return Attributes of the resource
+                */
+                virtual RCSResourceAttributes &getAttributes();
+
+                /**
+                * Set attributes of the resource
                 *
-                * @param value - value of attribute to set
+                * @param attrs Attributes to set
                 *
                 * @return void
                 */
-                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value) = 0;
+                virtual void setAttributes(RCSResourceAttributes &attrs);
 
                 /**
                 * Execute the logic of bundle to get the value of attribute
                 *
-                * @param key - key of attribute to get
+                * @param key Key of attribute to get
                 *
-                * @return RCSResourceAttributes::Value - return value of the attribute
+                * @return Value of the attribute
                 */
                 virtual RCSResourceAttributes::Value getAttribute(const std::string &key) = 0;
+
+                /**
+                * Execute the logic of bundle to set the value of attribute
+                *
+                * @param key Name of attribute to set
+                *
+                * @param value Value of attribute to set
+                *
+                * @return void
+                */
+                virtual void setAttribute(std::string key,
+                                          RCSResourceAttributes::Value &&value) = 0;
         };
     }
 }
 
-#endif
+#endif
\ No newline at end of file
index 86eb419..4b1bd1d 100644 (file)
@@ -57,29 +57,38 @@ namespace OIC
                 /**
                 * Return all attributes of the resource
                 *
-                * @return RCSResourceAttributes - attributes of the resource
+                * @return RCSResourceAttributes Attributes of the resource
                 */
                 virtual RCSResourceAttributes &getAttributes();
 
                 /**
-                * Execute the logic of bundle to set the value of attribute
-                *
-                * @param key - name of attribute to set
+                * Set attributes of the resource
                 *
-                * @param value - value of attribute to set
+                * @param attrs Attributes to set
                 *
                 * @return void
                 */
-                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value);
+                virtual void setAttributes(RCSResourceAttributes &attrs);
 
                 /**
                 * Execute the logic of bundle to get the value of attribute
                 *
-                * @param key - key of attribute to get
+                * @param key Key of attribute to get
+                *
+                * @return Value of the attribute
+                */
+                virtual RCSResourceAttributes::Value getAttribute(const std::string &key) = 0;
+
+                /**
+                * Execute the logic of bundle to set the value of attribute
+                *
+                * @param key Name of attribute to set
                 *
-                * @return RCSResourceAttributes::Value - return value of the attribute
+                * @param value Value of attribute to set
+                *
+                * @return void
                 */
-                virtual RCSResourceAttributes::Value getAttribute(const std::string &key);
+                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value) = 0;
 
                 /**
                 * SoftSensor logic. Has to be provided by the soft sensor developer.
@@ -90,7 +99,7 @@ namespace OIC
                 virtual void executeLogic() = 0;
 
                 virtual void onUpdatedInputResource(const std::string attributeName,
-                        std::vector<RCSResourceAttributes::Value> values) = 0;
+                                                    std::vector<RCSResourceAttributes::Value> values) = 0;
 
 
             public:
@@ -99,4 +108,4 @@ namespace OIC
     }
 }
 
-#endif
+#endif
\ No newline at end of file
index 919586a..f350b9c 100644 (file)
@@ -33,8 +33,6 @@ class BMISensorResource : public SoftSensorResource
         BMISensorResource();
         ~BMISensorResource();
 
-        void initAttributes();
-        virtual RCSResourceAttributes &getAttributes();
         virtual RCSResourceAttributes::Value getAttribute(const std::string &key);
         virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value);
 
index ce2aece..1f1cd1b 100644 (file)
@@ -31,25 +31,15 @@ BMISensorResource::~BMISensorResource()
     delete m_pBMISensor;
 }
 
-void BMISensorResource::initAttributes()
-{
-    SoftSensorResource::initAttributes();
-}
-
-RCSResourceAttributes &BMISensorResource::getAttributes()
-{
-    return SoftSensorResource::getAttributes();
-}
-
 RCSResourceAttributes::Value BMISensorResource::getAttribute(const std::string &key)
 {
-    return SoftSensorResource::getAttribute(key);
+    return BundleResource::getAttribute(key);
 }
 
 void BMISensorResource::setAttribute(std::string key,
                                      RCSResourceAttributes::Value &&value)
 {
-    SoftSensorResource::setAttribute(key, std::move(value));
+    BundleResource::setAttribute(key, std::move(value));
 }
 
 void BMISensorResource::executeLogic()
index bdefc7a..b784e20 100644 (file)
@@ -33,8 +33,6 @@ class DiscomfortIndexSensorResource : public SoftSensorResource
         DiscomfortIndexSensorResource();
         ~DiscomfortIndexSensorResource();
 
-        void initAttributes();
-        virtual RCSResourceAttributes &getAttributes();
         virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value);
         virtual RCSResourceAttributes::Value getAttribute(const std::string &key);
 
index 4b0d6dc..01740be 100644 (file)
@@ -34,25 +34,15 @@ DiscomfortIndexSensorResource::~DiscomfortIndexSensorResource()
     delete m_pDiscomfortIndexSensor;
 }
 
-void DiscomfortIndexSensorResource::initAttributes()
-{
-    SoftSensorResource::initAttributes();
-}
-
-RCSResourceAttributes &DiscomfortIndexSensorResource::getAttributes()
-{
-    return SoftSensorResource::getAttributes();
-}
-
 void DiscomfortIndexSensorResource::setAttribute(std::string key,
         RCSResourceAttributes::Value &&value)
 {
-    SoftSensorResource::setAttribute(key, std::move(value));
+    BundleResource::setAttribute(key, std::move(value));
 }
 
 RCSResourceAttributes::Value DiscomfortIndexSensorResource::getAttribute(const std::string &key)
 {
-    return SoftSensorResource::getAttribute(key);
+    return BundleResource::getAttribute(key);
 }
 
 void DiscomfortIndexSensorResource::executeLogic()
index 3390908..e20b3d8 100644 (file)
@@ -33,20 +33,21 @@ namespace OIC
         class HueLight: public ProtocolBridgeResource
         {
 
-        public:
-            HueLight();
-            HueLight(HueConnector* connector, std::string address);
-            virtual ~HueLight();
-            virtual void initAttributes();
-            virtual RCSResourceAttributes& getAttributes();
-
-            virtual void setAttribute(std::string key, RCSResourceAttributes::Value&&);
-
-            virtual RCSResourceAttributes::Value getAttribute(const std::string& key);
-        private:
-            std::string m_address;
-            HueConnector* m_connector;
-        };}
+            public:
+                HueLight();
+                HueLight(HueConnector *connector, std::string address);
+                virtual ~HueLight();
+
+                virtual void initAttributes();
+
+                virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&);
+                virtual RCSResourceAttributes::Value getAttribute(const std::string &key);
+
+            private:
+                std::string m_address;
+                HueConnector *m_connector;
+        };
+    }
 }
 
 #endif /* HUEGATEWAYCLIENT_H_ */
index f378b10..68296cc 100644 (file)
@@ -50,11 +50,6 @@ void HueLight::initAttributes()
     BundleResource::setAttribute("color", 0);
 }
 
-RCSResourceAttributes &HueLight::getAttributes()
-{
-    return BundleResource::getAttributes();
-}
-
 RCSResourceAttributes::Value HueLight::getAttribute(const std::string &key)
 {
     cout << "HueLight::getAttribute called for " << key <<  " called" << endl;
index 731f55d..831d9ca 100644 (file)
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include "BundleResource.h"
-#include "Configuration.h"
+
 #include <list>
 #include <string.h>
 
+#include "InternalTypes.h"
+
+
 namespace OIC
 {
     namespace Service
@@ -42,9 +45,9 @@ namespace OIC
             m_pNotiReceiver = pNotiReceiver;
         }
 
-        std::list< string > BundleResource::getAttributeNames()
+        std::list< std::string > BundleResource::getAttributeNames()
         {
-            std::list< string > ret;
+            std::list< std::string > ret;
             for (RCSResourceAttributes::iterator it = m_resourceAttributes.begin();
                  it != m_resourceAttributes.end(); ++it)
             {
@@ -58,8 +61,19 @@ namespace OIC
             return m_resourceAttributes;
         }
 
+        void BundleResource::setAttributes(RCSResourceAttributes &attrs)
+        {
+            for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
+            {
+                m_resourceAttributes[it->key()] = it->value();
+            }
+        }
+
         void BundleResource::setAttribute(std::string key, RCSResourceAttributes::Value &&value)
         {
+            OC_LOG_V(INFO, CONTAINER_TAG, std::string("set attribute \'" + key + "\', with " +
+                     value.toString()).c_str());
+
             m_resourceAttributes[key] = value;
 
             if (m_pNotiReceiver)
@@ -68,8 +82,9 @@ namespace OIC
 
         RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
         {
+            OC_LOG_V(INFO, CONTAINER_TAG, std::string("get attribute \'" + key + "\'").c_str());
+
             return m_resourceAttributes.at(key);
         }
-
     }
 }
\ No newline at end of file
index 0e0eed0..c78a177 100644 (file)
 
 using namespace OIC::Service;
 
-namespace OIC{
-    namespace Service{
+namespace OIC
+{
+    namespace Service
+    {
         ProtocolBridgeResource::ProtocolBridgeResource()
         {
 
@@ -33,6 +35,28 @@ namespace OIC{
         {
 
         }
+
+        RCSResourceAttributes &ProtocolBridgeResource::getAttributes()
+        {
+            // execute implemented logic to get attribute, and update bundle resource attribute data
+            RCSResourceAttributes attr;
+            for (std::string attrName : getAttributeNames())
+            {
+                attr[attrName] = getAttribute(attrName);
+            }
+            BundleResource::setAttributes(attr);
+
+            return BundleResource::getAttributes();
+        }
+
+        void ProtocolBridgeResource::setAttributes(RCSResourceAttributes &attrs)
+        {
+            // execute implemented logic to set attribute
+            for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
+            {
+                setAttribute(it->key(), std::move(it->value()));
+            }
+        }
     }
 }
 
index 1146238..2ffccf4 100644 (file)
@@ -348,58 +348,75 @@ namespace OIC
         }
 
         RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
-                const RCSResourceAttributes &attributes)
+                const RCSResourceAttributes &)
         {
-            (void) attributes;
+            OCEntityHandlerResult result = OC_EH_ERROR;
+
             RCSResourceAttributes attr;
+            std::string strResourceUri = request.getResourceUri();
 
-            if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
-                && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
+            if (m_mapServers.find(strResourceUri) != m_mapServers.end()
+                && m_mapResources.find(strResourceUri) != m_mapResources.end())
             {
-                auto getFunction = [this, &attr, &request] ()
+                if (m_mapResources[strResourceUri])
                 {
-                    for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
+                    auto getFunction = [this, &attr, &strResourceUri]()
                     {
-                        attr[attrName] = m_mapResources[request.getResourceUri()]->getAttribute(attrName);
-                    }
-                };
-                boost::thread getThread(getFunction);
-                getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
+                        attr = m_mapResources[strResourceUri]->getAttributes();
+                    };
+                    boost::thread getThread(getFunction);
+                    getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
+
+                    result = OC_EH_OK;
+                }
             }
 
-            return RCSGetResponse::create(attr);
+            return RCSGetResponse::create(attr, result, 200);
         }
 
         RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
                 const RCSResourceAttributes &attributes)
         {
-            RCSResourceAttributes attr = attributes;
+            OCEntityHandlerResult result = OC_EH_ERROR;
+
+            RCSResourceAttributes attr;
+            std::list<std::string> lstAttributes;
+            std::string strResourceUri = request.getResourceUri();
 
-            if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
-                && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
+            if (m_mapServers.find(strResourceUri) != m_mapServers.end()
+                && m_mapResources.find(strResourceUri) != m_mapResources.end())
             {
-                auto setFunction = [this, &attr, &request] ()
+                if (m_mapResources[strResourceUri])
                 {
-                    for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
+                    auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
                     {
-                        if (!attr[attrName].toString().empty())
+                        lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
+
+                        for (RCSResourceAttributes::const_iterator itor = attributes.begin();
+                             itor != attributes.end(); itor++)
                         {
-                            m_mapResources[request.getResourceUri()]->setAttribute(attrName,
-                                    attr[attrName].toString());
+                            if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
+                                != lstAttributes.end())
+                            {
+                                attr[itor->key()] = itor->value();
+                            }
                         }
-                    }
-                };
-                boost::thread setThread(setFunction);
-                setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
+
+                        m_mapResources[strResourceUri]->setAttributes(attr);
+                    };
+                    boost::thread setThread(setFunction);
+                    setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
+
+                    result = OC_EH_OK;
+                }
             }
 
-            return RCSSetResponse::create(attr);
+            return RCSSetResponse::create(attr, result, 200);
         }
 
         void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
         {
-            OC_LOG_V(INFO, CONTAINER_TAG, std::string("onNotificationReceived\n\tnotification from "
-                     + strResourceUri + ".").c_str());
+            OC_LOG_V(INFO, CONTAINER_TAG, std::string("notification from " + strResourceUri + ".").c_str());
 
             if (m_mapServers.find(strResourceUri) != m_mapServers.end())
             {
index 883ed3a..6239e06 100644 (file)
@@ -55,17 +55,24 @@ namespace OIC
 
         RCSResourceAttributes &SoftSensorResource::getAttributes()
         {
-            return BundleResource::getAttributes();
-        }
+            // execute implemented logic to get attribute, and update bundle resource attribute data
+            RCSResourceAttributes attr;
+            for (std::string attrName : getAttributeNames())
+            {
+                attr[attrName] = getAttribute(attrName);
+            }
+            BundleResource::setAttributes(attr);
 
-        void SoftSensorResource::setAttribute(std::string key, RCSResourceAttributes::Value &&value)
-        {
-            BundleResource::setAttribute(key, std::move(value));
+            return BundleResource::getAttributes();
         }
 
-        RCSResourceAttributes::Value SoftSensorResource::getAttribute(const std::string &key)
+        void SoftSensorResource::setAttributes(RCSResourceAttributes &attrs)
         {
-            return BundleResource::getAttribute(key);
+            // execute implemented logic to set attribute
+            for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
+            {
+                setAttribute(it->key(), std::move(it->value()));
+            }
         }
     }
 }