From 0ca42b34180ea8cfafd6c469c6f8f70a15a2c595 Mon Sep 17 00:00:00 2001 From: Markus Jung Date: Mon, 3 Aug 2015 09:51:31 +0900 Subject: [PATCH] resource-encapsulation: update container bundle resource - update soft sensor resource to get input data - modify soft sensor bundle example and configuration file Change-Id: I9deb7669db33b8464d135a7133184067966fe84b Signed-off-by: Markus Jung Reviewed-on: https://gerrit.iotivity.org/gerrit/2021 Tested-by: jenkins-iotivity Reviewed-by: Hun-je Yeon Reviewed-by: Madan Lanka --- .../src/resourceContainer/SConscript | 4 +- .../bundle-api/include/BundleResource.h | 10 +- .../bundle-api/include/SoftSensorResource.h | 32 ++- .../{src => examples}/ContainerSample.cpp | 0 .../{src => examples}/ContainerSampleClient.cpp | 203 ++++++++++++------- .../examples/HueSampleBundle/src/HueConnector.cpp | 3 +- .../examples/HueSampleBundle/src/HueLight.cpp | 18 +- .../examples/ResourceContainerConfig.xml | 46 ++--- .../include/DiscomfortIndexSensor.h | 42 +--- .../include/DiscomfortIndexSensorResource.h | 7 +- .../include/SoftSensorBundleActivator.h | 3 - .../src/DiscomfortIndexSensor.cpp | 222 ++++----------------- .../src/DiscomfortIndexSensorResource.cpp | 75 ++----- .../src/SoftSensorBundleActivator.cpp | 30 +-- .../resourceContainer/src/SoftSensorResource.cpp | 38 ++++ 15 files changed, 299 insertions(+), 434 deletions(-) rename service/resource-encapsulation/src/resourceContainer/{src => examples}/ContainerSample.cpp (100%) rename service/resource-encapsulation/src/resourceContainer/{src => examples}/ContainerSampleClient.cpp (69%) diff --git a/service/resource-encapsulation/src/resourceContainer/SConscript b/service/resource-encapsulation/src/resourceContainer/SConscript index b0c242e..54d55c6 100644 --- a/service/resource-encapsulation/src/resourceContainer/SConscript +++ b/service/resource-encapsulation/src/resourceContainer/SConscript @@ -196,7 +196,7 @@ Ignore("examples/ResourceContainerConfig.xml", "examples/ResourceContainerConfig containersample_env.AppendUnique(LIBS = ['rcs_container']) -containersampleapp_src = ['src/ContainerSample.cpp'] +containersampleapp_src = ['examples/ContainerSample.cpp'] containersampleapp = containersample_env.Program('ContainerSample',containersampleapp_src) Alias("containersample", containersampleapp) env.AppendTarget('containersample') @@ -207,7 +207,7 @@ env.AppendTarget('containersample') containersampleclient_env = resource_container_env.Clone(); containersample_env.AppendUnique(LIBS = ['rcs_container']) -containersampleclient_src = ['src/ContainerSampleClient.cpp'] +containersampleclient_src = ['examples/ContainerSampleClient.cpp'] containersampleclientapp = containersample_env.Program('ContainerSampleClient',containersampleclient_src) Alias("containersampleclient", containersampleclientapp) env.AppendTarget('containersampleclient') 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 e1b4ebc..27778d2 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/BundleResource.h @@ -29,8 +29,6 @@ #include "NotificationReceiver.h" #include "RCSResourceAttributes.h" -using namespace std; - namespace OIC { namespace Service @@ -61,7 +59,7 @@ namespace OIC * * @return std::list - return list of the attribute names */ - std::list getAttributeNames(); + std::list getAttributeNames(); /** * Initialize attributes of the resource @@ -108,9 +106,9 @@ namespace OIC public: - string m_bundleId; - string m_name, m_uri, m_resourceType, m_address; - map< string, vector< map< string, string > > > m_mapResourceProperty; + std::string m_bundleId; + std::string m_name, m_uri, m_resourceType, m_address; + std::map< std::string, std::vector< std::map< std::string, std::string > > > m_mapResourceProperty; private: NotificationReceiver *m_pNotiReceiver; 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 03fb98c..bd9d1f0 100644 --- a/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/bundle-api/include/SoftSensorResource.h @@ -37,12 +37,6 @@ namespace OIC class SoftSensorResource: public BundleResource { public: - struct SensorData - { - string sensorName; - vector< map< string, string > > data; - }; - /** * Constructor for SoftSensorResource */ @@ -54,11 +48,18 @@ namespace OIC virtual ~SoftSensorResource(); /** + * Initialize input and output attributes for the resource + * + * @return void + */ + virtual void initAttributes(); + + /** * Return all attributes of the resource * * @return RCSResourceAttributes - attributes of the resource */ - virtual RCSResourceAttributes &getAttributes() = 0; + virtual RCSResourceAttributes &getAttributes(); /** * Execute the logic of bundle to set the value of attribute @@ -69,8 +70,7 @@ namespace OIC * * @return void */ - virtual void setAttribute(std::string key, - RCSResourceAttributes::Value &&value) = 0; + virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value); /** * Execute the logic of bundle to get the value of attribute @@ -79,23 +79,19 @@ namespace OIC * * @return RCSResourceAttributes::Value - return value of the attribute */ - virtual RCSResourceAttributes::Value getAttribute(const std::string &key) = 0; + virtual RCSResourceAttributes::Value getAttribute(const std::string &key); /** - * Set Input data to update output value of the soft sensor - * - * @param inputs - input data which soft sensor needed + * SoftSensor logic. Has to be provided by the soft sensor developer. + * This function will be executed if an input attribute is updated. * * @return void */ - virtual void setInputAttribute(SensorData inputs) = 0; + virtual void executeLogic() = 0; public: - unsigned int inputCount; - vector m_vecInputAttributes; - map< string, SensorData > m_mapStoredInputData; - SensorData m_outputs; + std::list m_inputList; }; } } diff --git a/service/resource-encapsulation/src/resourceContainer/src/ContainerSample.cpp b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp similarity index 100% rename from service/resource-encapsulation/src/resourceContainer/src/ContainerSample.cpp rename to service/resource-encapsulation/src/resourceContainer/examples/ContainerSample.cpp diff --git a/service/resource-encapsulation/src/resourceContainer/src/ContainerSampleClient.cpp b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp similarity index 69% rename from service/resource-encapsulation/src/resourceContainer/src/ContainerSampleClient.cpp rename to service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp index e90a6c1..672713c 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/ContainerSampleClient.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp @@ -35,22 +35,23 @@ typedef std::map> DiscoveredRe DiscoveredResourceMap discoveredResources; std::shared_ptr curResource; +std::shared_ptr DISensorResource; static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe; std::mutex curResourceLock; class Light { -public: + public: - bool m_on_off; - int m_color; - int m_dim; - std::string m_name; + bool m_on_off; + int m_color; + int m_dim; + std::string m_name; - Light() : m_on_off(false), m_color(0), m_dim(0), m_name("") - { - } + Light() : m_on_off(false), m_color(0), m_dim(0), m_name("") + { + } }; Light mylight; @@ -61,15 +62,15 @@ int observe_count() return ++oc; } -void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, - const int& eCode, const int& sequenceNumber) +void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep, + const int &eCode, const int &sequenceNumber) { try { - if(eCode == OC_STACK_OK) + if (eCode == OC_STACK_OK) { - std::cout << "OBSERVE RESULT:"< 10) + if (observe_count() > 10) { - std::cout<<"Cancelling Observe..."<cancelObserve(); - std::cout << "Cancel result: "<< result <("createduri") << std::endl; + << rep.getValue("createduri") << std::endl; } else { @@ -138,28 +138,27 @@ void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, co else { std::cout << "onPost2 Response error: " << eCode << std::endl; - std::exit(-1); } } - catch(std::exception& e) + catch (std::exception &e) { std::cout << "Exception: " << e.what() << " in onPost2" << std::endl; } } -void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { try { - if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) + if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) { std::cout << "POST request was successful" << std::endl; - if(rep.hasAttribute("createduri")) + if (rep.hasAttribute("createduri")) { std::cout << "\tUri of the created resource: " - << rep.getValue("createduri") << std::endl; + << rep.getValue("createduri") << std::endl; } else { @@ -185,10 +184,9 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con else { std::cout << "onPost Response error: " << eCode << std::endl; - std::exit(-1); } } - catch(std::exception& e) + catch (std::exception &e) { std::cout << "Exception: " << e.what() << " in onPost" << std::endl; } @@ -197,11 +195,11 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con // Local function to put a different state for this resource void postLightRepresentation(std::shared_ptr resource) { - if(resource) + if (resource) { OCRepresentation rep; - std::cout << "Posting light representation..."< resource) } // callback handler on PUT request -void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { try { - if(eCode == OC_STACK_OK) + if (eCode == OC_STACK_OK) { std::cout << "PUT request was successful" << std::endl; @@ -234,10 +232,36 @@ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons else { std::cout << "onPut Response error: " << eCode << std::endl; - std::exit(-1); } } - catch(std::exception& e) + catch (std::exception &e) + { + std::cout << "Exception: " << e.what() << " in onPut" << std::endl; + } +} + +void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep, + const int eCode) +{ + void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep, + const int eCode); + + try + { + if (eCode == OC_STACK_OK) + { + std::cout << "PUT request was successful" << std::endl; + + QueryParamsMap test; + std::cout << "Sending request to: " << DISensorResource->uri() << std::endl; + DISensorResource->get(test, &onGetForDISensor); + } + else + { + std::cout << "onPut Response error: " << eCode << std::endl; + } + } + catch (std::exception &e) { std::cout << "Exception: " << e.what() << " in onPut" << std::endl; } @@ -246,11 +270,11 @@ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons // Local function to put a different state for this resource void putLightRepresentation(std::shared_ptr resource) { - if(resource) + if (resource) { OCRepresentation rep; - std::cout << "Putting light representation..."< resource) } // Callback handler on GET request -void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) +void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { try { - if(eCode == OC_STACK_OK) + if (eCode == OC_STACK_OK) { std::cout << "GET request was successful" << std::endl; std::cout << "Resource URI: " << rep.getUri() << std::endl; @@ -288,21 +312,45 @@ void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons else { std::cout << "onGET Response error: " << eCode << std::endl; - std::exit(-1); } } - catch(std::exception& e) + catch (std::exception &e) { std::cout << "Exception: " << e.what() << " in onGet" << std::endl; } } +void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep, + const int eCode) +{ + try + { + if (eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << DISensorResource->uri() << std::endl; + + std::cout << "Payload: " << rep.getPayload() << std::endl; + + std::cout << "\tdiscomfortIndex: " << rep.getValue("discomfortIndex") << std::endl; + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + } + } + catch (std::exception &e) + { + std::cout << "Exception: " << e.what() << " in onPut" << std::endl; + } +} + // Local function to get representation of light resource void getLightRepresentation(std::shared_ptr resource) { - if(resource) + if (resource) { - std::cout << "Getting Light Representation..."< resource) void foundResource(std::shared_ptr resource) { std::cout << "In foundResource\n"; - std::string resourceURI; + std::string resourceURI = resource->uri(); std::string hostAddress; try { { std::lock_guard lock(curResourceLock); - if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end()) + if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end()) { std::cout << "Found resource " << resource->uniqueIdentifier() << - " for the first time on server with ID: "<< resource->sid()<sid() << std::endl; discoveredResources[resource->uniqueIdentifier()] = resource; + + if (resourceURI.find("/discomfortIndex") != std::string::npos) + { + std::cout << "discomfortIndex found !!! " << std::endl; + + DISensorResource = resource; + + OCRepresentation rep; + + rep.setValue("humidity", std::string("30")); + rep.setValue("temperature", std::string("27")); + + resource->put(rep, QueryParamsMap(), &onPutForDISensor); + } } else { - std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<uniqueIdentifier() << " again!" << std::endl; } - if(curResource) + if (curResource) { - std::cout << "Found another resource, ignoring"<uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; @@ -353,11 +415,11 @@ void foundResource(std::shared_ptr resource) // Get the resource types std::cout << "\tList of resource types: " << std::endl; - for(auto &resourceTypes : resource->getResourceTypes()) + for (auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; - if(resourceTypes == "oic.light.control") + if (resourceTypes == "oic.light.control") { curResource = resource; // Call a local function which will internally invoke get API on the resource pointer @@ -367,7 +429,7 @@ void foundResource(std::shared_ptr resource) // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; - for(auto &resourceInterfaces : resource->getResourceInterfaces()) + for (auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } @@ -379,9 +441,9 @@ void foundResource(std::shared_ptr resource) } } - catch(std::exception& e) + catch (std::exception &e) { - std::cerr << "Exception in foundResource: "<< e.what() << std::endl; + std::cerr << "Exception in foundResource: " << e.what() << std::endl; } } @@ -410,16 +472,17 @@ void checkObserverValue(int value) else { std::cout << "<===Invalid ObserveType selected." - <<" Setting ObserveType to Observe===>\n\n"; + << " Setting ObserveType to Observe===>\n\n"; } } -static FILE* client_open(const char *path, const char *mode) +static FILE *client_open(const char *path, const char *mode) { return fopen("./oic_svr_db_client.json", mode); } -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) +{ std::ostringstream requestURI; OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink }; @@ -440,14 +503,15 @@ int main(int argc, char* argv[]) { return -1; } } - catch(std::exception& ) + catch (std::exception &) { std::cout << "<===Invalid input arguments===>\n\n"; return -1; } // Create PlatformConfig object - PlatformConfig cfg { + PlatformConfig cfg + { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", @@ -465,15 +529,15 @@ int main(int argc, char* argv[]) { requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light"; OCPlatform::findResource("", requestURI.str(), - CT_DEFAULT, &foundResource); - std::cout<< "Finding Resource... " < lock(blocker); cv.wait(lock); - }catch(OCException& e) + } + catch (OCException &e) { - oclog() << "Exception in main: "< #include +using namespace std; using namespace OIC::Service; HueConnector::HueConnector() @@ -102,7 +103,7 @@ std::string HueConnector::transmit(std::string target, std::string payload) return ""; } -static int writer(char* data, size_t size, size_t nmemb, std::string *buffer_in) +static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in) { buffer_in->append(data, size * nmemb); return size * nmemb; 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 f7b0ba1..2232caf 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/HueSampleBundle/src/HueLight.cpp @@ -22,6 +22,7 @@ #include +using namespace std; using namespace OIC::Service; HueLight::HueLight() @@ -41,24 +42,29 @@ HueLight::~HueLight() } -void HueLight::initAttributes(){ +void HueLight::initAttributes() +{ BundleResource::setAttribute("on-off", false); BundleResource::setAttribute("dim", 0); BundleResource::setAttribute("color", 0); } -RCSResourceAttributes& HueLight::getAttributes(){ +RCSResourceAttributes &HueLight::getAttributes() +{ return BundleResource::getAttributes(); } -RCSResourceAttributes::Value HueLight::getAttribute(const std::string& key){ +RCSResourceAttributes::Value HueLight::getAttribute(const std::string &key) +{ cout << "HueLight::getAttribute called for " << key << " called" << endl; return BundleResource::getAttribute(key); } -void HueLight::setAttribute(std::string attributeName, RCSResourceAttributes::Value&& value){ - cout << "HueLight::setAttribute setting " << attributeName << " to " << value.toString() << std::endl; +void HueLight::setAttribute(std::string attributeName, RCSResourceAttributes::Value &&value) +{ + cout << "HueLight::setAttribute setting " << attributeName << " to " << value.toString() << + std::endl; if (attributeName == "on-off") { @@ -74,7 +80,7 @@ void HueLight::setAttribute(std::string attributeName, RCSResourceAttributes::Va if (attributeName == "color") { // needs conversion *650 - m_connector->transmit(this->m_address+ "/state", "{\"hue\":" + value.toString() + "}"); + m_connector->transmit(this->m_address + "/state", "{\"hue\":" + value.toString() + "}"); } BundleResource::setAttribute(attributeName, std::move(value)); diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml index 2afa664..2621e42 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml +++ b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml @@ -8,44 +8,26 @@ DiscomfortIndexSensor1 oic.softsensor - - - version - string - 1.0 - - - lifetime - int - 60 - - - timestamp - string - - - temperature - string - - - humidity - string - - discomfortIndex int - Thing_TempHumSensor - Thing_TempHumSensor1 + + humidity + double + + + temperature + double + - + oic.bundle.hueSample libHueBundle.so 1.0.0 @@ -57,9 +39,10 @@ - + + --> diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h index ca45b38..01513a2 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h @@ -29,23 +29,12 @@ * This header file is included to define _EXPORT_. */ -#include - #include "SoftSensorResource.h" using namespace OIC::Service; namespace DiscomfortIndexSensorName { -#define PHYSICAL_EA 2 - - typedef struct _physicalInput_ - { - char *m_thingName; - int m_inputNum; - void *m_pInputStruct; - } physicalInput; - typedef enum { SUCCESS = 0, ERROR, ALL_DISCOMPORT, HALF_DISCOMPORT, LITTLE_DISCOMPORT, ALL_COMPORT @@ -53,32 +42,17 @@ namespace DiscomfortIndexSensorName class DiscomfortIndexSensor { - private: - - physicalInput s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA]; - - class InValue - { - public: - std::string m_timestamp; // . - std::string m_discomfortIndex; // Discomfort Index. ( 2 ~ 5 ) - std::string m_humidity; // relative humidity. - std::string m_temperature; // celsius temperature. - }; - - InValue m_DI[PHYSICAL_EA]; - InValue m_result; - public: - DiscomfortIndexSensor(vector inputs); + DiscomfortIndexSensor(); + ~DiscomfortIndexSensor(); - int runLogic(std::vector< SoftSensorResource::SensorData > &sensorData); - DIResult getInput(std::vector< SoftSensorResource::SensorData > &contextDataList, - InValue *data); - DIResult makeDiscomfortIndex(InValue *data); - SoftSensorResource::SensorData setOutput(int property_count, InValue *data); + int executeDISensorLogic(std::map *pInputData, std::string *pOutput); + DIResult makeDiscomfortIndex(); - SoftSensorResource::SensorData m_output; + private: + std::string m_humidity; + std::string m_temperature; + std::string m_discomfortIndex; }; }; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h index fc4ceef..585cbda 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h @@ -21,8 +21,6 @@ #ifndef DISCOMFORTINDEXSENSORRESOURCE_H_ #define DISCOMFORTINDEXSENSORRESOURCE_H_ -#include - #include "SoftSensorResource.h" #include "DiscomfortIndexSensor.h" @@ -33,15 +31,14 @@ class DiscomfortIndexSensorResource : public SoftSensorResource { public: DiscomfortIndexSensorResource(); - DiscomfortIndexSensorResource(vector inputAttributes); ~DiscomfortIndexSensorResource(); - virtual void initAttributes(); + void initAttributes(); virtual RCSResourceAttributes &getAttributes(); virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value); virtual RCSResourceAttributes::Value getAttribute(const std::string &key); - void setInputAttribute(SensorData input); + virtual void executeLogic(); private: DiscomfortIndexSensor *m_pDiscomfortIndexSensor; diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h index 347e4fe..487a654 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h @@ -21,9 +21,6 @@ #ifndef SOFTSENSOR_SAMPLEBUNDLE_H_ #define SOFTSENSOR_SAMPLEBUNDLE_H_ -#include -#include - #include "ResourceContainerBundleAPI.h" #include "BundleActivator.h" #include "BundleResource.h" diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp index 03e1a59..1675fbe 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp @@ -21,6 +21,9 @@ /** * This file contains the exported symbol. */ + +#include + #include "DiscomfortIndexSensor.h" #include "SysTimer.h" @@ -30,221 +33,84 @@ using namespace DiscomfortIndexSensorName; -#define SENSOR_NAME "DiscomfortIndexSensor" - -char *inputName[2] = -{ (char *)"temperature", (char *)"humidity" }; +DiscomfortIndexSensor::DiscomfortIndexSensor() +{ + m_humidity = ""; + m_temperature = ""; + m_discomfortIndex = ""; +} -DiscomfortIndexSensor::DiscomfortIndexSensor(vector inputs) +DiscomfortIndexSensor::~DiscomfortIndexSensor() { - m_result.m_timestamp = ""; - m_result.m_humidity = ""; - m_result.m_temperature = ""; - m_result.m_discomfortIndex = ""; - for (int i = 0; i < PHYSICAL_EA; i++) - { - s_PHYSICAL_SOFTSENSORs[i].m_thingName = (char *) inputs.at(i).c_str(); - s_PHYSICAL_SOFTSENSORs[i].m_inputNum = PHYSICAL_EA; - s_PHYSICAL_SOFTSENSORs[i].m_pInputStruct = (void *) &inputName; - } } -int DiscomfortIndexSensor::runLogic(std::vector< SoftSensorResource::SensorData > &sensorData) +int DiscomfortIndexSensor::executeDISensorLogic(std::map *pInputData, + std::string *pOutput) { std::cout << "[DiscomfortIndexSensor] DiscomfortIndexSensor::" << __func__ << " is called." << std::endl; DIResult result; - if (getInput(sensorData, m_DI) == SUCCESS) - { - if ((result = makeDiscomfortIndex(m_DI)) != SUCCESS) - { - std::cout << "Error : makeDiscomfortIndex() result = " << result << std::endl; - return -1; - } - - m_output = setOutput(4, m_DI); + m_temperature = pInputData->at("temperature"); + m_humidity = pInputData->at("humidity"); - return 0; - } - - return -1; -} - -/** - * Get Input data (temperature, humidity) using resource Client of Iotivity base. - */ -DIResult DiscomfortIndexSensor::getInput(std::vector< SoftSensorResource::SensorData > - &sensorData, InValue *data) -{ - int result_flag = 0; - int contextSize = 0; - - if ((contextSize = sensorData.size()) == 0) + if ((result = makeDiscomfortIndex()) != SUCCESS) { - std::cout << "Physical Context data is not exist." << std::endl; - return ERROR; + std::cout << "Error : makeDiscomfortIndex() result = " << result << std::endl; + return -1; } - for (int i = 0; i < contextSize; i++) - { - for (int k = 0; k < PHYSICAL_EA; k++) - { - if (sensorData[i].sensorName == s_PHYSICAL_SOFTSENSORs[k].m_thingName) - { - std::vector < std::map< std::string, std::string > > lVector = - sensorData[i].data; - int requiredInputNum = s_PHYSICAL_SOFTSENSORs[k].m_inputNum; - char **pchar = (char **) (s_PHYSICAL_SOFTSENSORs[k].m_pInputStruct); - if (requiredInputNum == 0) - { - std::cout << "No input List." << std::endl; - return ERROR; - } - - for (unsigned int j = 0; j < lVector.size(); j++) - { - std::string name = lVector[j]["name"]; - - if (name.compare(*pchar) == 0) - { - data->m_temperature = lVector[j]["value"]; - requiredInputNum--; - } - else if (name.compare(*(++pchar)) == 0) - { - data->m_humidity = lVector[j]["value"]; - requiredInputNum--; - } - } - - if (requiredInputNum == 0) - { - data++; - result_flag++; - } - break; - } // if - } // for - } + (*pOutput) = m_discomfortIndex; - if (result_flag == PHYSICAL_EA) - { - std::cout << "Success : getInput()" << std::endl; - return SUCCESS; - } - - return ERROR; + return 0; } /** - * Calculation of DiscomfortIndex with TEMP&HUMI of InValue. + * Calculation of DiscomfortIndex with TEMP&HUMI. */ -DIResult DiscomfortIndexSensor::makeDiscomfortIndex(InValue *data) +DIResult DiscomfortIndexSensor::makeDiscomfortIndex() { - int discomfortIndex = (int) ERROR; - double sumDI = 0.0; - - m_result.m_temperature = ""; - m_result.m_humidity = ""; + int DILevel = (int) ERROR; + double dDI = 0.0; - for (int i = 0; i < PHYSICAL_EA; i++) - { - if (i != 0) - { - m_result.m_temperature += ", "; - m_result.m_humidity += ", "; - } - - double dI = 0.0; - int t = std::stoi((data + i)->m_temperature); - int h = std::stoi((data + i)->m_humidity); - double F = (9.0 * (double) t) / 5.0 + 32.0; - - std::cout << "Device Number : " << i << std::endl; + int t = std::stoi(m_temperature); + int h = std::stoi(m_humidity); + double F = (9.0 * (double) t) / 5.0 + 32.0; - dI = F - (F - 58.0) * (double) ((100 - h) * 55) / 10000.0; + // calculation of discomfortIndex + dDI = F - (F - 58.0) * (double)((100 - h) * 55) / 10000.0; - std::cout << "Discomfort level : " << dI << ", Temperature :" << t << ", Humidity :" << h - << std::endl; - - (data + i)->m_discomfortIndex = std::to_string(0); - m_result.m_temperature += std::to_string(t) + ", "; - m_result.m_humidity += std::to_string(h) + ", "; - sumDI += dI; - } + std::cout << "Discomfort level : " << dDI << ", Temperature :" << t << ", Humidity :" << h << + std::endl; - sumDI = sumDI / PHYSICAL_EA; - std::cout << "[result] Avg. DI level : " << sumDI << std::endl; - if (sumDI >= 80.0) + m_discomfortIndex = std::to_string(DILevel); + std::cout << "[result] Discomfort Index : " << m_discomfortIndex << std::endl; + if (dDI >= 80.0) { - discomfortIndex = (int) ALL_DISCOMPORT; - std::cout << "DI : " << discomfortIndex << " : All person discomfort. : " << sumDI + DILevel = (int)ALL_DISCOMPORT; + std::cout << "DI : " << DILevel << " : All person discomfort. : " << dDI << std::endl; } - else if (sumDI >= 75.0) + else if (dDI >= 75.0) { - discomfortIndex = (int) HALF_DISCOMPORT; - std::cout << "DI : " << discomfortIndex << " : Half of person discomfort. : " << sumDI + DILevel = (int)HALF_DISCOMPORT; + std::cout << "DI : " << DILevel << " : Half of person discomfort. : " << dDI << std::endl; } - else if (sumDI >= 68.0) + else if (dDI >= 68.0) { - discomfortIndex = (int) LITTLE_DISCOMPORT; - std::cout << "DI : " << discomfortIndex << " : A little person discomfort. : " << sumDI + DILevel = (int)LITTLE_DISCOMPORT; + std::cout << "DI : " << DILevel << " : A little person discomfort. : " << dDI << std::endl; } else { - discomfortIndex = (int) ALL_COMPORT; - std::cout << "DI : " << discomfortIndex << " : All person comfort. : " << sumDI + DILevel = (int)ALL_COMPORT; + std::cout << "DI : " << DILevel << " : All person comfort. : " << dDI << std::endl; } - m_result.m_discomfortIndex = std::to_string(discomfortIndex); - std::cout << "[result] Discomfort Index : " << m_result.m_discomfortIndex << std::endl; - return SUCCESS; -} - -SoftSensorResource::SensorData DiscomfortIndexSensor::setOutput(int property_count, - InValue *data) -{ - SoftSensorResource::SensorData out; - - std::map < std::string, std::string > output_property; - - out.sensorName = SENSOR_NAME; - - output_property.insert(std::make_pair("name", "timestamp")); - output_property.insert(std::make_pair("type", "string")); - output_property.insert(std::make_pair("value", m_result.m_timestamp)); - - out.data.push_back(output_property); - - output_property.clear(); - output_property.insert(std::make_pair("name", "temperature")); - output_property.insert(std::make_pair("type", "string")); - output_property.insert(std::make_pair("value", m_result.m_temperature)); - - out.data.push_back(output_property); - - output_property.clear(); - output_property.insert(std::make_pair("name", "humidity")); - output_property.insert(std::make_pair("type", "string")); - output_property.insert(std::make_pair("value", m_result.m_humidity)); - - out.data.push_back(output_property); - - output_property.clear(); - output_property.insert(std::make_pair("name", "discomfortIndex")); - output_property.insert(std::make_pair("type", "int")); - output_property.insert(std::make_pair("value", m_result.m_discomfortIndex)); - - out.data.push_back(output_property); - - return out; -} - +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp index 80c11ad..de51815 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp @@ -18,21 +18,12 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include - #include "DiscomfortIndexSensorResource.h" -DiscomfortIndexSensorResource::DiscomfortIndexSensorResource() -{ -} -DiscomfortIndexSensorResource::DiscomfortIndexSensorResource(vector inputAttributes) +DiscomfortIndexSensorResource::DiscomfortIndexSensorResource() { - m_pDiscomfortIndexSensor = new DiscomfortIndexSensor(inputAttributes); - m_vecInputAttributes = inputAttributes; - std::cout << "Init attributes" << endl; - initAttributes(); - std::cout << "Init attributes finished" << endl; + m_pDiscomfortIndexSensor = new DiscomfortIndexSensor(); } DiscomfortIndexSensorResource::~DiscomfortIndexSensorResource() @@ -42,65 +33,39 @@ DiscomfortIndexSensorResource::~DiscomfortIndexSensorResource() void DiscomfortIndexSensorResource::initAttributes() { - - BundleResource::setAttribute("temperature", "23"); - BundleResource::setAttribute("humidity", "40"); - BundleResource::setAttribute("discomfortIndex", "5"); + SoftSensorResource::initAttributes(); } RCSResourceAttributes &DiscomfortIndexSensorResource::getAttributes() { - return BundleResource::getAttributes(); + return SoftSensorResource::getAttributes(); } -RCSResourceAttributes::Value DiscomfortIndexSensorResource::getAttribute(const std::string &key) +void DiscomfortIndexSensorResource::setAttribute(std::string key, + RCSResourceAttributes::Value &&value) { - cout << "DiscomfortIndexSensorResource::getAttribute called for " << key << " called" << endl; - return BundleResource::getAttribute(key); + SoftSensorResource::setAttribute(key, std::move(value)); } -void DiscomfortIndexSensorResource::setAttribute(std::string key, RCSResourceAttributes::Value &&value) +RCSResourceAttributes::Value DiscomfortIndexSensorResource::getAttribute(const std::string &key) { - cout << "DiscomfortIndexSensorResource::setAttribute setting " << key << " to " << value.toString() - << std::endl; - BundleResource::setAttribute(key, std::move(value)); + return SoftSensorResource::getAttribute(key); } -void DiscomfortIndexSensorResource::setInputAttribute(SensorData input) +void DiscomfortIndexSensorResource::executeLogic() { - vector::iterator itor = std::find(m_vecInputAttributes.begin(), m_vecInputAttributes.end(), - input.sensorName); - std::vector inData; + std::map mapInputData; + std::string strTemp = getAttribute("temperature").toString(); + std::string strHumid = getAttribute("humidity").toString(); + std::string strDiscomfortIndex; - if (itor != m_vecInputAttributes.end()) + if (!strTemp.empty() && !strHumid.empty()) { - m_mapStoredInputData[input.sensorName] = input; - - if (inputCount == m_mapStoredInputData.size()) - { - for (map< string, SensorData >::iterator mapItor = m_mapStoredInputData.begin(); - mapItor != m_mapStoredInputData.end(); mapItor++) - { - inData.push_back(mapItor->second); - } + mapInputData.insert(std::make_pair("temperature", strTemp)); + mapInputData.insert(std::make_pair("humidity", strHumid)); - m_pDiscomfortIndexSensor->runLogic(inData); - m_outputs = m_pDiscomfortIndexSensor->m_output; + m_pDiscomfortIndexSensor->executeDISensorLogic(&mapInputData, &strDiscomfortIndex); - for (unsigned int i = 0; i < m_outputs.data.size(); i++) - { - if (!m_outputs.data.at(i)["name"].compare("temperature")) - BundleResource::setAttribute("temperature", - RCSResourceAttributes::Value(m_outputs.data.at(i)["value"].c_str())); - - else if (!m_outputs.data.at(i)["name"].compare("humidity")) - BundleResource::setAttribute("humidity", - RCSResourceAttributes::Value(m_outputs.data.at(i)["value"].c_str())); - - else if (!m_outputs.data.at(i)["name"].compare("discomfortIndex")) - BundleResource::setAttribute("discomfortIndex", - RCSResourceAttributes::Value(m_outputs.data.at(i)["value"].c_str())); - } - } + setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str())); } -} +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp index e85c094..77b618f 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp @@ -34,12 +34,10 @@ SoftSensorBundleActivator::~SoftSensorBundleActivator() void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId) { - std::cout << "SoftSensorSampleBundle::activateBundle called" << std::endl; - m_pResourceContainer = resourceContainer; m_bundleId = bundleId; - vector resourceConfig; + std::vector resourceConfig; resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig); @@ -52,8 +50,6 @@ void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resou void SoftSensorBundleActivator::deactivateBundle() { - std::cout << "SoftSensorSampleBundle::deactivateBundle called" << std::endl; - std::vector::iterator itor; for (itor = m_vecResources.begin(); itor != m_vecResources.end();) { @@ -63,27 +59,12 @@ void SoftSensorBundleActivator::deactivateBundle() void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) { - std::cout << "SoftSensorSampleBundle::createResource called" << std::endl; - if (resourceInfo.resourceType == "oic.softsensor") { static int discomfortIndexSensorCount = 1; - std::vector< std::map< std::string, std::string > >::iterator itor_vec; - std::map< std::string, std::string >::iterator itor_map; - std::vector inputs; - - for (itor_vec = resourceInfo.resourceProperty["input"].begin(); - itor_vec != resourceInfo.resourceProperty["input"].end(); itor_vec++) - { - for (itor_map = (*itor_vec).begin(); itor_map != (*itor_vec).end(); itor_map++) - { - inputs.push_back(itor_map->second); - } - } - std::cout << "SoftSensorSampleBundle::creating new discomfort index sensor " << std::endl; // create DISensor resource - DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs); + DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(); newResource->m_bundleId = m_bundleId; newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string( @@ -91,8 +72,7 @@ void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) newResource->m_resourceType = resourceInfo.resourceType; newResource->m_mapResourceProperty = resourceInfo.resourceProperty; - // setting input Attributes count - newResource->inputCount = newResource->m_mapResourceProperty["input"].size(); + newResource->initAttributes(); m_pResourceContainer->registerResource(newResource); m_vecResources.push_back(newResource); @@ -101,8 +81,6 @@ void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) void SoftSensorBundleActivator::destroyResource(BundleResource *resource) { - std::cout << "SoftSensorSampleBundle::destroyResource called" << std::endl; - std::vector ::iterator itor; itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource); @@ -135,4 +113,4 @@ extern "C" void externalCreateResource(resourceInfo resourceInfo) extern "C" void externalDestroyResource(BundleResource *pBundleResource) { bundle->destroyResource(pBundleResource); -} +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp index 24fe7b1..5178384 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp @@ -34,6 +34,44 @@ namespace OIC { } + + void SoftSensorResource::initAttributes() + { + std::vector< std::map< std::string, std::string > >::iterator itor; + + // initialize input attributes + for (itor = m_mapResourceProperty["input"].begin(); itor != m_mapResourceProperty["input"].end(); + itor++) + { + m_inputList.push_back((*itor)["name"]); + BundleResource::setAttribute((*itor)["name"], nullptr); + } + + // initialize output attributes + for (itor = m_mapResourceProperty["output"].begin(); itor != m_mapResourceProperty["output"].end(); + itor++) + BundleResource::setAttribute((*itor)["name"], nullptr); + } + + RCSResourceAttributes &SoftSensorResource::getAttributes() + { + return BundleResource::getAttributes(); + } + + void SoftSensorResource::setAttribute(std::string key, RCSResourceAttributes::Value &&value) + { + std::list::iterator itor = std::find(m_inputList.begin(), m_inputList.end(), key); + + BundleResource::setAttribute(key, value.toString()); + + if (itor != m_inputList.end()) + executeLogic(); + } + + RCSResourceAttributes::Value SoftSensorResource::getAttribute(const std::string &key) + { + return BundleResource::getAttribute(key); + } } } -- 2.7.4