From 51f35e52b24750149ede58226f259e14c9be5821 Mon Sep 17 00:00:00 2001 From: Minji Park Date: Wed, 16 Sep 2015 20:32:10 +0900 Subject: [PATCH] modify bundle resource and request handler to update multiple attributes - 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/2585 Reviewed-by: Markus Jung Reviewed-by: Hun-je Yeon Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../bundle-api/include/BundleResource.h | 31 ++++++---- .../bundle-api/include/ProtocolBridgeResource.h | 37 ++++++++--- .../bundle-api/include/SoftSensorResource.h | 31 ++++++---- .../BMISensorBundle/include/BMISensorResource.h | 2 - .../BMISensorBundle/src/BMISensorResource.cpp | 14 +---- .../include/DiscomfortIndexSensorResource.h | 2 - .../src/DiscomfortIndexSensorResource.cpp | 14 +---- .../examples/HueSampleBundle/include/HueLight.h | 29 ++++----- .../examples/HueSampleBundle/src/HueLight.cpp | 5 -- .../src/resourceContainer/src/BundleResource.cpp | 23 +++++-- .../src/ProtocolBridgeResource.cpp | 28 ++++++++- .../src/ResourceContainerImpl.cpp | 71 ++++++++++++++-------- .../resourceContainer/src/SoftSensorResource.cpp | 21 ++++--- 13 files changed, 188 insertions(+), 120 deletions(-) diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h index 27778d2..339000b 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h @@ -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 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 diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/ProtocolBridgeResource.h b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/ProtocolBridgeResource.h index 4174fde..2bd5645 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/ProtocolBridgeResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/ProtocolBridgeResource.h @@ -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 diff --git a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h index 86eb419..4b1bd1d 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h @@ -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 values) = 0; + std::vector values) = 0; public: @@ -99,4 +108,4 @@ namespace OIC } } -#endif +#endif \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h index 919586a..f350b9c 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h @@ -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); diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp index ce2aece..1f1cd1b 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp @@ -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() diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h index bdefc7a..b784e20 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h @@ -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); diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp index 4b0d6dc..01740be 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp @@ -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() diff --git a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/include/HueLight.h b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/include/HueLight.h index 3390908..e20b3d8 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/include/HueLight.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/include/HueLight.h @@ -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_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp index f378b10..68296cc 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp @@ -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; diff --git a/service/resource-encapsulation/src/resourceContainer/src/BundleResource.cpp b/service/resource-encapsulation/src/resourceContainer/src/BundleResource.cpp index 731f55d..831d9ca 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/BundleResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/BundleResource.cpp @@ -19,10 +19,13 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "BundleResource.h" -#include "Configuration.h" + #include #include +#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 diff --git a/service/resource-encapsulation/src/resourceContainer/src/ProtocolBridgeResource.cpp b/service/resource-encapsulation/src/resourceContainer/src/ProtocolBridgeResource.cpp index 0e0eed0..c78a177 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ProtocolBridgeResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/ProtocolBridgeResource.cpp @@ -22,8 +22,10 @@ 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())); + } + } } } diff --git a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp index 1146238..2ffccf4 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/ResourceContainerImpl.cpp @@ -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 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()) { diff --git a/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp index 883ed3a..6239e06 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp @@ -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())); + } } } } -- 2.7.4