From 4434f481c1dd73a5846fea547778553dd9e61d6f Mon Sep 17 00:00:00 2001 From: Minji Park Date: Fri, 11 Sep 2015 17:19:36 +0900 Subject: [PATCH] Add resource container bundle sample - Add BMI Sample Bundle - Modify DiscomfortIndex sensor bundle and rename files and directory - Add sample input sensors for BMI and DiscomfortIndex sensor bundle - remove build warmings - Enabled build for the Android Platform Change-Id: I2f14a0acaf05fcf09f945f2046facb122a376f99 Signed-off-by: Minji Park Reviewed-on: https://gerrit.iotivity.org/gerrit/2461 Tested-by: jenkins-iotivity Reviewed-by: Abhishek Pandey Reviewed-by: Madan Lanka --- .../src/resourceContainer/SConscript | 65 +++-- .../BMISensorBundle/include/BMISensor.h | 79 ++++++ .../include/BMISensorBundleActivator.h | 47 ++++ .../include/BMISensorResource.h | 51 ++++ .../BMISensorBundle/include/SysTimer.h | 34 +++ .../BMISensorBundle/src/BMISensor.cpp | 149 +++++++++++ .../src/BMISensorBundleActivator.cpp | 126 ++++++++++ .../BMISensorBundle/src/BMISensorResource.cpp | 76 ++++++ .../examples/BMISensorBundle/src/SysTimer.cpp | 56 +++++ .../inputSensors/HeightSensorApp/SConscript | 39 +++ .../HeightSensorApp/include/HeightSensorApp.h | 86 +++++++ .../HeightSensorApp/src/HeightSensorApp.cpp | 232 +++++++++++++++++ .../src/inputSensors/SConscript | 27 ++ .../inputSensors/WeightSensorApp/SConscript | 39 +++ .../WeightSensorApp/include/WeightSensorApp.h | 85 +++++++ .../WeightSensorApp/src/WeightSensorApp.cpp | 233 ++++++++++++++++++ .../examples/ContainerSampleClient.cpp | 11 + .../include/DISensorBundleActivator.h} | 12 +- .../include/DiscomfortIndexSensor.h | 51 ++++ .../include/DiscomfortIndexSensorResource.h | 3 +- .../include/SysTimer.h | 35 +++ .../src/DISensorBundleActivator.cpp} | 21 +- .../src/DiscomfortIndexSensor.cpp | 47 ++-- .../src/DiscomfortIndexSensorResource.cpp | 48 +++- .../src/SysTimer.cpp | 56 +++++ .../src/inputSensors/SConscript | 27 ++ .../src/inputSensors/THSensorApp/SConscript | 39 +++ .../THSensorApp/include/ThingResourceServer.h | 81 ++++++ .../THSensorApp/src/ThingResourceServer.cpp | 223 +++++++++++++++++ .../src/inputSensors/THSensorApp1/SConscript | 39 +++ .../include/ThingResourceServer1.h | 80 ++++++ .../THSensorApp1/src/ThingResourceServer1.cpp | 222 +++++++++++++++++ .../examples/ResourceContainerConfig.xml | 85 +++++-- .../include/DiscomfortIndexSensor.h | 59 ----- .../SoftSensorSampleBundle/include/SysTimer.h | 37 --- .../SoftSensorSampleBundle/src/SysTimer.cpp | 56 ----- .../include/DiscoverResourceUnit.h | 68 ++--- .../resourceContainer/src/Configuration.cpp | 47 ++-- .../src/DiscoverResourceUnit.cpp | 53 ++-- .../src/SoftSensorResource.cpp | 13 +- 40 files changed, 2499 insertions(+), 338 deletions(-) create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensor.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorBundleActivator.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/SysTimer.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensor.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorBundleActivator.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/SysTimer.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/include/HeightSensorApp.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/src/HeightSensorApp.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/include/WeightSensorApp.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/src/WeightSensorApp.cpp rename service/resource-encapsulation/src/resourceContainer/examples/{SoftSensorSampleBundle/include/SoftSensorBundleActivator.h => DiscomfortIndexSensorBundle/include/DISensorBundleActivator.h} (85%) create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensor.h rename service/resource-encapsulation/src/resourceContainer/examples/{SoftSensorSampleBundle => DiscomfortIndexSensorBundle}/include/DiscomfortIndexSensorResource.h (91%) create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/SysTimer.h rename service/resource-encapsulation/src/resourceContainer/examples/{SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp => DiscomfortIndexSensorBundle/src/DISensorBundleActivator.cpp} (86%) rename service/resource-encapsulation/src/resourceContainer/examples/{SoftSensorSampleBundle => DiscomfortIndexSensorBundle}/src/DiscomfortIndexSensor.cpp (74%) rename service/resource-encapsulation/src/resourceContainer/examples/{SoftSensorSampleBundle => DiscomfortIndexSensorBundle}/src/DiscomfortIndexSensorResource.cpp (58%) create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/SysTimer.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/include/ThingResourceServer.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/src/ThingResourceServer.cpp create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/SConscript create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/include/ThingResourceServer1.h create mode 100644 service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/src/ThingResourceServer1.cpp delete mode 100644 service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h delete mode 100644 service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SysTimer.h delete mode 100644 service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SysTimer.cpp diff --git a/service/resource-encapsulation/src/resourceContainer/SConscript b/service/resource-encapsulation/src/resourceContainer/SConscript index ac635691f..b50d2f270 100644 --- a/service/resource-encapsulation/src/resourceContainer/SConscript +++ b/service/resource-encapsulation/src/resourceContainer/SConscript @@ -84,16 +84,16 @@ if int(containerJavaSupport): if target_os not in ['windows', 'winrt']: - resource_container_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall']) - if target_os != 'android': - resource_container_env.AppendUnique(CXXFLAGS = ['-pthread']) + resource_container_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall']) + if target_os != 'android': + resource_container_env.AppendUnique(CXXFLAGS = ['-pthread']) if target_os not in ['darwin', 'ios', 'windows', 'winrt']: - resource_container_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined']) + resource_container_env.AppendUnique(LINKFLAGS = ['-Wl,--no-undefined']) if target_os == 'android': - resource_container_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions']) - resource_container_env.PrependUnique(LIBS = ['gnustl_shared', 'log']) + resource_container_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions']) + resource_container_env.PrependUnique(LIBS = ['gnustl_shared', 'log']) try: resource_container_env.AppendUnique(LIBPATH = [os.environ['JAVA_LIB']]) @@ -126,26 +126,49 @@ res_container_shared = resource_container_env.SharedLibrary('rcs_container', res resource_container_env.InstallTarget([res_container_static,res_container_shared], 'libResContainer') ###################################################################### -# build soft sensor sample bundle +# build discomfort index sensor sample bundle ###################################################################### if target_os in ['linux', 'tizen', 'android']: - ss_resource_bundle_env = resource_container_env.Clone() - ss_resource_bundle_env.AppendUnique(CCFLAGS = ['-fPIC']) - - SS_RESOURCE_BUNDLE_DIR = 'examples/SoftSensorSampleBundle/' - ss_resource_bundle_env.AppendUnique(CPPPATH = [ - SS_RESOURCE_BUNDLE_DIR + 'include', - 'include/', - '../../include', - ]) + DI_sensor_bundle_env = resource_container_env.Clone() + DI_sensor_bundle_env.AppendUnique(CCFLAGS = ['-fPIC']) + + DI_SENSOR_BUNDLE_DIR = 'examples/DiscomfortIndexSensorBundle/' + DI_sensor_bundle_env.AppendUnique(CPPPATH = [ DI_SENSOR_BUNDLE_DIR + 'include' ]) + + DI_sensor_bundle_env.PrependUnique(LIBS = ['rcs_container']) + + DI_sensor_bundle_src = [ Glob(DI_SENSOR_BUNDLE_DIR + 'src/*.cpp')] + + DISensorBundle = DI_sensor_bundle_env.SharedLibrary('DISensorBundle', DI_sensor_bundle_src) + DI_sensor_bundle_env.InstallTarget(DISensorBundle, 'libDISensorBundle') + + if target_os == 'linux' : + SConscript(DI_SENSOR_BUNDLE_DIR + 'src/inputSensors/SConscript') + Command("THSensorApp", DI_SENSOR_BUNDLE_DIR + "src/inputSensors/THSensorApp/THSensorApp", Copy("$TARGET", "$SOURCE")) + Command("THSensorApp1", DI_SENSOR_BUNDLE_DIR + "src/inputSensors/THSensorApp1/THSensorApp1", Copy("$TARGET", "$SOURCE")) + +###################################################################### +# build BMI sensor sample bundle +###################################################################### +if target_os in ['linux', 'tizen', 'android']: + BMI_sensor_bundle_env = resource_container_env.Clone() + BMI_sensor_bundle_env.AppendUnique(CCFLAGS = ['-fPIC']) + + BMI_SENSOR_BUNDLE_DIR = 'examples/BMISensorBundle/' + BMI_sensor_bundle_env.AppendUnique(CPPPATH = [ BMI_SENSOR_BUNDLE_DIR + 'include' ]) + + BMI_sensor_bundle_env.PrependUnique(LIBS = ['rcs_container']) + + BMI_sensor_bundle_src = [ Glob(BMI_SENSOR_BUNDLE_DIR + 'src/*.cpp')] - ss_resource_bundle_env.PrependUnique(LIBS = ['rcs_container']) + BMISensorBundle = BMI_sensor_bundle_env.SharedLibrary('BMISensorBundle', BMI_sensor_bundle_src) + BMI_sensor_bundle_env.InstallTarget(BMISensorBundle, 'libBMISensorBundle') - ss_resource_bundle_src = [ Glob(SS_RESOURCE_BUNDLE_DIR + 'src/*.cpp')] + if target_os == 'linux' : + SConscript(BMI_SENSOR_BUNDLE_DIR + 'src/inputSensors/SConscript') + Command("HeightSensorApp", BMI_SENSOR_BUNDLE_DIR + "src/inputSensors/HeightSensorApp/HeightSensorApp", Copy("$TARGET", "$SOURCE")) + Command("WeightSensorApp", BMI_SENSOR_BUNDLE_DIR + "src/inputSensors/WeightSensorApp/WeightSensorApp", Copy("$TARGET", "$SOURCE")) - SoftSensorBundle = ss_resource_bundle_env.SharedLibrary('SoftSensorBundle', ss_resource_bundle_src) - ss_resource_bundle_env.InstallTarget(SoftSensorBundle, 'libSoftSensorBundle') - ###################################################################### # build hue sample bundle ###################################################################### diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensor.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensor.h new file mode 100644 index 000000000..24448ad86 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensor.h @@ -0,0 +1,79 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef BMISENSOR_H_ +#define BMISENSOR_H_ + +#include +#include +#include + +/** + * Who BMI + * weight / height *height + * + * < 18.5 : underweight + * 18.5 <= < 25 : Normal Range + * <= 25 < 30 : Overweight + * <= 30 : Obese + */ + +#define UNKOWNBMI -1 +#define OUTOFDATEBMI 0 +#define UNDERWEIGHT 1 +#define NORMALRANGE 2 +#define OVERWEIGHT 3 +#define OBESE 4 + +#define UNDERWEIGHT_VAL 18.5 +#define NORMALRANGE_VAL 25.9 +#define OVERWEIGHT_VAL 30 + +#define DIFFTIME 5 // valid time difference. (seconds) + + +namespace BMISensorName +{ + typedef enum + { + SUCCESS = 0, ERROR + } BMIResult; + + class BMISensor + { + public: + BMISensor(); + ~BMISensor(); + + int executeBMISensorLogic(std::map *pInputData, + std::string *pOutput); + BMIResult makeBMI(void); + + private: + std::string m_weight; + std::string m_height; + std::string m_BMIResult; + + time_t m_timepstampW; + time_t m_timepstampH; + }; +}; + +#endif /* BMISENSOR_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorBundleActivator.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorBundleActivator.h new file mode 100644 index 000000000..6843263b5 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorBundleActivator.h @@ -0,0 +1,47 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef BMISENSOR_BUNDLE_ACTIVATOR_H_ +#define BMISENSOR_BUNDLE_ACTIVATOR_H_ + +#include "ResourceContainerBundleAPI.h" +#include "BundleActivator.h" +#include "BundleResource.h" + +using namespace OIC::Service; + +class BMISensorBundleActivator : public BundleActivator +{ + public: + BMISensorBundleActivator(); + ~BMISensorBundleActivator(); + + void activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId); + void deactivateBundle(); + + void createResource(resourceInfo resourceInfo); + void destroyResource(BundleResource *pBundleResource); + + std::string m_bundleId; + ResourceContainerBundleAPI *m_pResourceContainer; + std::vector m_vecResources; +}; + +#endif /* BMISENSOR_BUNDLE_ACTIVATOR_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h new file mode 100644 index 000000000..919586aa7 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/BMISensorResource.h @@ -0,0 +1,51 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef BMISENSORRESOURCE_H_ +#define BMISENSORRESOURCE_H_ + +#include "SoftSensorResource.h" +#include "BMISensor.h" + +using namespace BMISensorName; +using namespace OIC::Service; + +class BMISensorResource : public SoftSensorResource +{ + public: + BMISensorResource(); + ~BMISensorResource(); + + void initAttributes(); + virtual RCSResourceAttributes &getAttributes(); + virtual RCSResourceAttributes::Value getAttribute(const std::string &key); + virtual void setAttribute(std::string key, RCSResourceAttributes::Value &&value); + + virtual void executeLogic(); + + virtual void onUpdatedInputResource(const std::string attributeName, + std::vector values); + + private: + BMISensor *m_pBMISensor; + std::map m_mapInputData; +}; + +#endif diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/SysTimer.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/SysTimer.h new file mode 100644 index 000000000..440033c3f --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/include/SysTimer.h @@ -0,0 +1,34 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef SYSTIMER_H_ +#define SYSTIMER_H_ + +#include +#include +#include +class SysTimer +{ + public: + static std::string MilliSecondAsString(); + static std::string UTCSecondAsString(); +}; + +#endif /* SYSTIMER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensor.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensor.cpp new file mode 100644 index 000000000..f56bc07d7 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensor.cpp @@ -0,0 +1,149 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * This file contains the exported symbol. + */ +#include "BMISensor.h" + +#include +#include +#include "SysTimer.h" + +#ifdef __ANDROID__ +#include "OCAndroid.h" +#endif + +using namespace BMISensorName; + + +BMISensor::BMISensor() +{ + m_weight = ""; + m_height = ""; + m_BMIResult = ""; + time(&m_timepstampW); + time(&m_timepstampH); +} + +BMISensor::~BMISensor() +{ + +} + +int BMISensor::executeBMISensorLogic(std::map *pInputData, + std::string *pOutput) +{ + BMIResult result; + + if (pInputData->find("weight") != pInputData->end()) + { + m_weight = pInputData->at("weight"); + time(&m_timepstampW); + } + + if (pInputData->find("height") != pInputData->end()) + { + m_height = pInputData->at("height"); + time(&m_timepstampH); + } + + if ((result = makeBMI()) != SUCCESS) + { + return -1; + } + + (*pOutput) = m_BMIResult; + + return 0; +} + +/** + * Calculation of BMI with Weight&Height + */ +BMIResult BMISensor::makeBMI(void) +{ + double BMIvalue, timediffsecond; + double dWeight, dHeight; + + int BMIResult; + + if (!m_weight.empty() && !m_height.empty()) + { + dWeight = std::stod(m_weight); + dHeight = std::stod(m_height); + + timediffsecond = abs(difftime(m_timepstampW, m_timepstampH)); + + // check if time difference between weight data and height data is valid + if (timediffsecond > DIFFTIME) + { + BMIvalue = 0; + BMIResult = UNKOWNBMI; + std::cout << "[BMISensor] : OUTOFDATEBMI: " << BMIResult << std::endl; + } + else if ((dWeight > 0) && (dHeight > 0)) + { + // calculate BMI + BMIvalue = dWeight / (dHeight * dHeight); + + std::cout << "[BMISensor] height : " << m_height << " weight : " << m_weight + << " BMIvalue : " << BMIvalue << " timediff : " << timediffsecond + << std::endl; + + if (BMIvalue >= OVERWEIGHT_VAL) + { + BMIResult = (int)OBESE; + std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OBESE " << std::endl; + } + else if (BMIvalue >= NORMALRANGE_VAL ) + { + BMIResult = (int)OVERWEIGHT; + std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OVERWEIGHT " << std::endl; + + } + else if (BMIvalue >= UNDERWEIGHT_VAL ) + { + BMIResult = (int)NORMALRANGE; + std::cout << "[BMISensor] : BMIresult:" << BMIResult << " NORMALRANGE " << std::endl; + + } + else + { + BMIResult = (int)UNDERWEIGHT; + std::cout << "[BMISensor] : BMIresult:" << BMIResult << " UNDERWEIGHT " << std::endl; + } + } + else + { + BMIvalue = -1; + BMIResult = UNKOWNBMI; + std::cout << "[BMISensor] : UNKNOWNBMI: " << BMIResult << std::endl; + } + + std::cout << std::endl; + + m_BMIResult = std::to_string(BMIResult); + + return SUCCESS; + } + + return ERROR; +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorBundleActivator.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorBundleActivator.cpp new file mode 100644 index 000000000..b266840b1 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorBundleActivator.cpp @@ -0,0 +1,126 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "BMISensorBundleActivator.h" + +#include +#include +#include "BMISensorResource.h" + + +BMISensorBundleActivator *bundle; + +BMISensorBundleActivator::BMISensorBundleActivator() +{ + m_pResourceContainer = nullptr; +} + +BMISensorBundleActivator::~BMISensorBundleActivator() +{ + m_pResourceContainer = nullptr; +} + +void BMISensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer, + std::string bundleId) +{ + m_pResourceContainer = resourceContainer; + m_bundleId = bundleId; + + std::vector resourceConfig; + + resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig); + + for (vector::iterator itor = resourceConfig.begin(); + itor != resourceConfig.end(); itor++) + { + createResource(*itor); + } +} + +void BMISensorBundleActivator::deactivateBundle() +{ + std::vector::iterator itor; + for (itor = m_vecResources.begin(); itor != m_vecResources.end();) + { + destroyResource(*itor); + } +} + +void BMISensorBundleActivator::createResource(resourceInfo resourceInfo) +{ + if (resourceInfo.resourceType == "oic.softsensor") + { + static int BMISensorCount = 1; + + // create BMISensor resource + BMISensorResource *newResource = new BMISensorResource(); + + newResource->m_bundleId = m_bundleId; + std::string indexCount;//string which will contain the indexCount + stringstream convert; // stringstream used for the conversion + convert << BMISensorCount++;//add the value of Number to the characters in the stream + indexCount = convert.str();//set indexCount to the content of the stream + + newResource->m_uri = "/softsensor/BMIsensor/" + indexCount; + newResource->m_resourceType = resourceInfo.resourceType; + newResource->m_mapResourceProperty = resourceInfo.resourceProperty; + + newResource->initAttributes(); + + m_pResourceContainer->registerResource(newResource); + m_vecResources.push_back(newResource); + } +} + +void BMISensorBundleActivator::destroyResource(BundleResource *resource) +{ + std::vector ::iterator itor; + + itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource); + + if (itor != m_vecResources.end()) + { + m_pResourceContainer->unregisterResource(resource); + m_vecResources.erase(itor); + } +} + +extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer, + std::string bundleId) +{ + bundle = new BMISensorBundleActivator(); + bundle->activateBundle(resourceContainer, bundleId); +} + +extern "C" void externalDeactivateBundle() +{ + bundle->deactivateBundle(); + delete bundle; +} + +extern "C" void externalCreateResource(resourceInfo resourceInfo) +{ + bundle->createResource(resourceInfo); +} + +extern "C" void externalDestroyResource(BundleResource *pBundleResource) +{ + bundle->destroyResource(pBundleResource); +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp new file mode 100644 index 000000000..ce2aece1e --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/BMISensorResource.cpp @@ -0,0 +1,76 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "BMISensorResource.h" + + +BMISensorResource::BMISensorResource() +{ + m_pBMISensor = new BMISensor(); +} + +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); +} + +void BMISensorResource::setAttribute(std::string key, + RCSResourceAttributes::Value &&value) +{ + SoftSensorResource::setAttribute(key, std::move(value)); +} + +void BMISensorResource::executeLogic() +{ + std::string strBMIResult; + + if (m_pBMISensor->executeBMISensorLogic(&m_mapInputData, &strBMIResult) != -1) + setAttribute("BMIresult", RCSResourceAttributes::Value(strBMIResult.c_str())); +} + +void BMISensorResource::onUpdatedInputResource(const std::string attributeName, + std::vector values) +{ + // remove all existing data + m_mapInputData.clear(); + + if (!attributeName.compare("weight")) + m_mapInputData.insert(std::make_pair("weight", values.back().toString())); + + if (!attributeName.compare("height")) + m_mapInputData.insert(std::make_pair("height", values.back().toString())); + + executeLogic(); +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/SysTimer.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/SysTimer.cpp new file mode 100644 index 000000000..702275c13 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/SysTimer.cpp @@ -0,0 +1,56 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "SysTimer.h" + +#include +#include +#include +#include +#include +#include + +std::string SysTimer::MilliSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + long long val = tv.tv_sec * (long long) 1000 + tv.tv_usec / 1000; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} + +std::string SysTimer::UTCSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + unsigned long val = tv.tv_sec; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/SConscript new file mode 100644 index 000000000..22bd8216f --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/SConscript @@ -0,0 +1,39 @@ +## +# linux sample app build script +## + +Import('env') + +# Add third party libraries +lib_env = env.Clone() +SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') +linux_sample_env = lib_env.Clone() + +###################################################################### +# Build flags +###################################################################### +linux_sample_env.AppendUnique(CPPPATH = ['include']) +linux_sample_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread']) +linux_sample_env.AppendUnique(CPPDEFINES = ['LINUX']) +linux_sample_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) +linux_sample_env.AppendUnique(LIBS = ['oc']) +linux_sample_env.AppendUnique(LIBS = ['octbstack']) +linux_sample_env.AppendUnique(LIBS = ['libconnectivity_abstraction']) +linux_sample_env.AppendUnique(LIBS = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +if env.get('SECURED') == '1': + linux_sample_env.AppendUnique(LIBS = ['tinydtls']) + +# On some platforms link order can miss functions so librt needs to be +# re-scanned at the end if present. gcc 4.6 is one with this issue. +if 'rt' in linux_sample_env.get('LIBS'): + linux_sample_env.Append(LIBS = ['rt']) + +###################################################################### +#build sampleapp +###################################################################### +heightsensorapp = linux_sample_env.Program('HeightSensorApp', 'src/HeightSensorApp.cpp') +Alias("heightsensorapp_sample", heightsensorapp) +env.AppendTarget('heightsensorapp') \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/include/HeightSensorApp.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/include/HeightSensorApp.h new file mode 100644 index 000000000..d267c3bfc --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/include/HeightSensorApp.h @@ -0,0 +1,86 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef HEIGHTSENSORAPP_H_ +#define HEIGHTSENSORAPP_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +#include +#include + + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE OC::ModeType::Server +#define COAP_SRVTYPE OC::ServiceType::InProc + +#define RESOURCE_TYPE_NAME "oic.sensor.height" + +// testing case +#define INTERVAL_FOR_CHECK 4 // seconds +#define INTERVAL_FOR_MEASUREMENT 3 // seconds +#define INIT_VAL 1 // default value +#define DIFF_VAL 0.02 // default value + + +class HeightResource +{ + public: + // Access this property from a TB client + double m_height; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OC::OCRepresentation m_resourceRep; + OC::ObservationIds m_interestedObservers; + + public: + // Constructor + HeightResource() : m_height(INIT_VAL), m_resourceHandle(0) + { + m_resourceUri = "/Thing_HeightSensor"; + m_resourceTypes.push_back(RESOURCE_TYPE_NAME); + m_resourceInterfaces.push_back(OC::DEFAULT_INTERFACE); + + printf("Running thing as %s\n", m_resourceUri.c_str()); + m_resourceRep.setUri(m_resourceUri); + m_resourceRep.setResourceTypes(m_resourceTypes); + m_resourceRep.setResourceInterfaces(m_resourceInterfaces); + } + + ~HeightResource() + { + } + + void registerResource(); + + OCResourceHandle getHandle(); + + void setResourceRepresentation(OC::OCRepresentation &rep); + + OC::OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/src/HeightSensorApp.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/src/HeightSensorApp.cpp new file mode 100644 index 000000000..801f8b99c --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/HeightSensorApp/src/HeightSensorApp.cpp @@ -0,0 +1,232 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include + +using namespace OC; +using namespace std; + +int g_Observation = 0; + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request); + +/* + * TempResourceFunctions + */ + +void HeightResource::registerResource() +{ + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + // This will internally create and register the resource. + OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri, + m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } +} + +OCResourceHandle HeightResource::getHandle() +{ + return m_resourceHandle; +} + +void HeightResource::setResourceRepresentation(OCRepresentation &rep) +{ + double tempHeight; + + rep.getValue("height", tempHeight); + + m_height = tempHeight; + + cout << "\t\t\t" << "Received representation: " << endl; + + cout << "\t\t\t\t" << "height" << m_height << endl; + +} + +OCRepresentation HeightResource::getResourceRepresentation() +{ + m_resourceRep.setValue("height", std::to_string(m_height)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +HeightResource g_myResource; + +void *TestSensorVal(void *param) +{ + (void)param; + + bool bFlag = true; + int nSleep_time = INTERVAL_FOR_CHECK; + double nHeight; + + std::cout << "[HeightSensorAPP] ::" << __func__ << " is called." + << std::endl; + + nHeight = INIT_VAL; + + // This function continuously monitors for the changes + while (1) + { + sleep(nSleep_time); + + if (g_Observation) + { + if (bFlag) + { + nSleep_time = INTERVAL_FOR_MEASUREMENT; + nHeight += DIFF_VAL; + g_myResource.m_height = nHeight; + } + else + { + nSleep_time = INTERVAL_FOR_CHECK; + } + + bFlag = bFlag ^ true; + + cout << "\n height updated to : " << g_myResource.m_height << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + } + else + { + nSleep_time = INTERVAL_FOR_CHECK ; + } + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::RequestFlag) + { + cout << "\t\trequestFlag : Request\n"; + + // If the request type is GET + if (requestType == "GET") + { + cout << "\t\t\trequestType : GET\n"; + + // Check for query params (if any) + // Process query params and do required operations .. + + // Get the representation of this resource at this point and send it as response + OCRepresentation rep = g_myResource.getResourceRepresentation(); + + if (response) + { + // TODO Error Code + response->setErrorCode(200); + response->setResourceRepresentation(rep, DEFAULT_INTERFACE); + } + } + else if (requestType == "PUT") + { + // TODO: PUT request operations + } + else if (requestType == "POST") + { + // TODO: POST request operations + } + else if (requestType == "DELETE") + { + // TODO: DELETE request operations + } + } + + if (requestFlag & RequestHandlerFlag::ObserverFlag) + { + pthread_t threadId; + + cout << "\t\trequestFlag : Observer\n"; + g_Observation = 1; + + static int startedThread = 0; + + if (!startedThread) + { + pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL); + startedThread = 1; + } + } + } + else + { + std::cout << "Request invalid" << std::endl; + } + + return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR; +} + +int main() +{ + // Create PlatformConfig object + PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos); + + try + { + OC::OCPlatform::Configure(cfg); + + OC::OCPlatform::startPresence(60); + + g_myResource.registerResource(); + + int input = 0; + cout << "Type any key to terminate" << endl; + cin >> input; + + OC::OCPlatform::stopPresence(); + } + catch (std::exception e) + { + cout << e.what() << endl; + } + + return 0; +} + diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/SConscript new file mode 100644 index 000000000..ad9ae8a91 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/SConscript @@ -0,0 +1,27 @@ +#****************************************************************** +# +# Copyright 2015 Samsung Electronics All Rights Reserved. +# +#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +Import('env') + +target_os = env.get('TARGET_OS') + +if target_os == 'linux' : + SConscript('HeightSensorApp/SConscript') + SConscript('WeightSensorApp/SConscript') \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/SConscript new file mode 100644 index 000000000..31df5caca --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/SConscript @@ -0,0 +1,39 @@ +## +# linux sample app build script +## + +Import('env') + +# Add third party libraries +lib_env = env.Clone() +SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') +linux_sample_env = lib_env.Clone() + +###################################################################### +# Build flags +###################################################################### +linux_sample_env.AppendUnique(CPPPATH = ['include']) +linux_sample_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread']) +linux_sample_env.AppendUnique(CPPDEFINES = ['LINUX']) +linux_sample_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) +linux_sample_env.AppendUnique(LIBS = ['oc']) +linux_sample_env.AppendUnique(LIBS = ['octbstack']) +linux_sample_env.AppendUnique(LIBS = ['libconnectivity_abstraction']) +linux_sample_env.AppendUnique(LIBS = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +if env.get('SECURED') == '1': + linux_sample_env.AppendUnique(LIBS = ['tinydtls']) + +# On some platforms link order can miss functions so librt needs to be +# re-scanned at the end if present. gcc 4.6 is one with this issue. +if 'rt' in linux_sample_env.get('LIBS'): + linux_sample_env.Append(LIBS = ['rt']) + +###################################################################### +#build sampleapp +###################################################################### +weightsensorapp = linux_sample_env.Program('WeightSensorApp', 'src/WeightSensorApp.cpp') +Alias("weightsensorapp_sample", weightsensorapp) +env.AppendTarget('weightsensorapp') \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/include/WeightSensorApp.h b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/include/WeightSensorApp.h new file mode 100644 index 000000000..8bdbfcb10 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/include/WeightSensorApp.h @@ -0,0 +1,85 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef WEIGHTSENSORAPP_H_ +#define WEIGHTSENSORAPP_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +#include +#include + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE OC::ModeType::Server +#define COAP_SRVTYPE OC::ServiceType::InProc + +#define RESOURCE_TYPE_NAME "oic.sensor.weight" + +// testing case +#define INTERVAL_FOR_CHECK 14 // seconds +#define INTERVAL_FOR_MEASUREMENT 3 // seconds +#define INIT_VAL 33 // default value +#define DIFF_VAL 0.01 // default value + + +class WeightResource +{ + public: + // Access this property from a TB client + double m_weight; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OC::OCRepresentation m_resourceRep; + OC::ObservationIds m_interestedObservers; + + public: + // Constructor + WeightResource() : m_weight(INIT_VAL), m_resourceHandle(0) + { + m_resourceUri = "/Thing_WeightSensor"; + m_resourceTypes.push_back(RESOURCE_TYPE_NAME); + m_resourceInterfaces.push_back(OC::DEFAULT_INTERFACE); + + printf("Running thing as %s\n", m_resourceUri.c_str()); + m_resourceRep.setUri(m_resourceUri); + m_resourceRep.setResourceTypes(m_resourceTypes); + m_resourceRep.setResourceInterfaces(m_resourceInterfaces); + } + + ~WeightResource() + { + } + + void registerResource(); + + OCResourceHandle getHandle(); + + void setResourceRepresentation(OC::OCRepresentation &rep); + + OC::OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/src/WeightSensorApp.cpp b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/src/WeightSensorApp.cpp new file mode 100644 index 000000000..2cf051b66 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/BMISensorBundle/src/inputSensors/WeightSensorApp/src/WeightSensorApp.cpp @@ -0,0 +1,233 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include + +using namespace OC; +using namespace std; + + +int g_Observation = 0; + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request); + +/* + * TempResourceFunctions + */ + +void WeightResource::registerResource() +{ + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + // This will internally create and register the resource. + OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri, + m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } +} + +OCResourceHandle WeightResource::getHandle() +{ + return m_resourceHandle; +} + +void WeightResource::setResourceRepresentation(OCRepresentation &rep) +{ + double tempWeight; + + rep.getValue("weight", tempWeight); + + m_weight = tempWeight; + + cout << "\t\t\t" << "Received representation: " << endl; + cout << "\t\t\t\t" << "Weight" << m_weight << endl; + +} + +OCRepresentation WeightResource::getResourceRepresentation() +{ + m_resourceRep.setValue("weight", std::to_string(m_weight)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +WeightResource g_myResource; + +void *TestSensorVal(void *param) +{ + (void)param; + + bool bFlag = true; + int nSleep_time = INTERVAL_FOR_CHECK; + double nWeight; + + std::cout << "[WeightSensorAPP] ::" << __func__ << " is called." + << std::endl; + + nWeight = INIT_VAL; + + // This function continuously monitors for the changes + while (1) + { + sleep(nSleep_time); + + if (g_Observation) + { + if (bFlag) + { + nSleep_time = INTERVAL_FOR_MEASUREMENT; + nWeight += DIFF_VAL; + g_myResource.m_weight = nWeight; + } + else + { + nSleep_time = INTERVAL_FOR_CHECK; + } + + bFlag = bFlag ^ true; + + cout << "\n weight updated to : " << g_myResource.m_weight << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + } + else + { + + nSleep_time = INTERVAL_FOR_CHECK ; + } + + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::RequestFlag) + { + cout << "\t\trequestFlag : Request\n"; + + // If the request type is GET + if (requestType == "GET") + { + cout << "\t\t\trequestType : GET\n"; + + // Check for query params (if any) + // Process query params and do required operations .. + + // Get the representation of this resource at this point and send it as response + OCRepresentation rep = g_myResource.getResourceRepresentation(); + + if (response) + { + // TODO Error Code + response->setErrorCode(200); + response->setResourceRepresentation(rep, DEFAULT_INTERFACE); + } + } + else if (requestType == "PUT") + { + // TODO: PUT request operations + } + else if (requestType == "POST") + { + // TODO: POST request operations + } + else if (requestType == "DELETE") + { + // TODO: DELETE request operations + } + } + + if (requestFlag & RequestHandlerFlag::ObserverFlag) + { + pthread_t threadId; + + cout << "\t\trequestFlag : Observer\n"; + g_Observation = 1; + + static int startedThread = 0; + + if (!startedThread) + { + pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL); + startedThread = 1; + } + } + } + else + { + std::cout << "Request invalid" << std::endl; + } + + return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR; +} + +int main() +{ + // Create PlatformConfig object + PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos); + + try + { + OC::OCPlatform::Configure(cfg); + + OC::OCPlatform::startPresence(60); + + g_myResource.registerResource(); + + int input = 0; + cout << "Type any key to terminate" << endl; + cin >> input; + + OC::OCPlatform::stopPresence(); + } + catch (std::exception e) + { + cout << e.what() << endl; + } + + return 0; +} + diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp index 672713c8c..cbbeb917e 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/ContainerSampleClient.cpp @@ -65,6 +65,7 @@ int observe_count() void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep, const int &eCode, const int &sequenceNumber) { + (void)headerOptions; try { if (eCode == OC_STACK_OK) @@ -105,6 +106,7 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep, void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { + (void)headerOptions; try { if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) @@ -149,6 +151,7 @@ void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, co void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { + (void)headerOptions; try { if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) @@ -213,6 +216,8 @@ void postLightRepresentation(std::shared_ptr resource) // callback handler on PUT request void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { + (void)headerOptions; + (void)rep; try { if (eCode == OC_STACK_OK) @@ -246,6 +251,8 @@ void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep, const int eCode); + (void)headerOptions; + (void)rep; try { if (eCode == OC_STACK_OK) @@ -290,6 +297,7 @@ void putLightRepresentation(std::shared_ptr resource) // Callback handler on GET request void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { + (void)headerOptions; try { if (eCode == OC_STACK_OK) @@ -323,6 +331,7 @@ void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, cons void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode) { + (void)headerOptions; try { if (eCode == OC_STACK_OK) @@ -478,6 +487,8 @@ void checkObserverValue(int value) static FILE *client_open(const char *path, const char *mode) { + (void)path; + return fopen("./oic_svr_db_client.json", mode); } diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DISensorBundleActivator.h similarity index 85% rename from service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h rename to service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DISensorBundleActivator.h index 487a65468..1d29da78a 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DISensorBundleActivator.h @@ -18,8 +18,8 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#ifndef SOFTSENSOR_SAMPLEBUNDLE_H_ -#define SOFTSENSOR_SAMPLEBUNDLE_H_ +#ifndef DISENSOR_BUNDLE_ACTIVATOR_H_ +#define DISENSOR_BUNDLE_ACTIVATOR_H_ #include "ResourceContainerBundleAPI.h" #include "BundleActivator.h" @@ -27,11 +27,11 @@ using namespace OIC::Service; -class SoftSensorBundleActivator : public BundleActivator +class DISensorBundleActivator : public BundleActivator { public: - SoftSensorBundleActivator(); - ~SoftSensorBundleActivator(); + DISensorBundleActivator(); + ~DISensorBundleActivator(); void activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId); void deactivateBundle(); @@ -44,4 +44,4 @@ class SoftSensorBundleActivator : public BundleActivator std::vector m_vecResources; }; -#endif /* SOFTSENSOR_SAMPLEBUNDLE_H_ */ +#endif /* DISENSOR_BUNDLE_ACTIVATOR_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensor.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensor.h new file mode 100644 index 000000000..639f9c72f --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensor.h @@ -0,0 +1,51 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef DISCOMFORTINDEXSENSOR_H_ +#define DISCOMFORTINDEXSENSOR_H_ + +#include +#include + +namespace DiscomfortIndexSensorName +{ + typedef enum + { + SUCCESS = 0, ERROR, ALL_DISCOMPORT, HALF_DISCOMPORT, LITTLE_DISCOMPORT, ALL_COMPORT + } DIResult; + + class DiscomfortIndexSensor + { + public: + DiscomfortIndexSensor(); + ~DiscomfortIndexSensor(); + + int executeDISensorLogic(std::map *pInputData, + std::string *pOutput); + DIResult makeDiscomfortIndex(); + + private: + std::string m_humidity; + std::string m_temperature; + std::string m_discomfortIndex; + }; +}; + +#endif /* DISCOMFORTINDEXSENSOR_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h similarity index 91% rename from service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h rename to service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h index e36321b8a..bdefc7a57 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensorResource.h +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/DiscomfortIndexSensorResource.h @@ -41,10 +41,11 @@ class DiscomfortIndexSensorResource : public SoftSensorResource virtual void executeLogic(); virtual void onUpdatedInputResource(const std::string attributeName, - std::vector values); + std::vector values); private: DiscomfortIndexSensor *m_pDiscomfortIndexSensor; + std::map m_mapInputData; }; #endif diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/SysTimer.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/SysTimer.h new file mode 100644 index 000000000..4123b55fa --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/include/SysTimer.h @@ -0,0 +1,35 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef SYSTIMER_H_ +#define SYSTIMER_H_ + +#include +#include +#include + +class SysTimer +{ + public: + static std::string MilliSecondAsString(); + static std::string UTCSecondAsString(); +}; + +#endif /* SYSTIMER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DISensorBundleActivator.cpp similarity index 86% rename from service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp rename to service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DISensorBundleActivator.cpp index 1a60d57d3..921d6ea07 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DISensorBundleActivator.cpp @@ -18,24 +18,25 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include "SoftSensorBundleActivator.h" -#include "DiscomfortIndexSensorResource.h" +#include "DISensorBundleActivator.h" + #include #include +#include "DiscomfortIndexSensorResource.h" -SoftSensorBundleActivator *bundle; +DISensorBundleActivator *bundle; -SoftSensorBundleActivator::SoftSensorBundleActivator() +DISensorBundleActivator::DISensorBundleActivator() { m_pResourceContainer = nullptr; } -SoftSensorBundleActivator::~SoftSensorBundleActivator() +DISensorBundleActivator::~DISensorBundleActivator() { m_pResourceContainer = nullptr; } -void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer, +void DISensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId) { m_pResourceContainer = resourceContainer; @@ -52,7 +53,7 @@ void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resou } } -void SoftSensorBundleActivator::deactivateBundle() +void DISensorBundleActivator::deactivateBundle() { std::vector::iterator itor; for (itor = m_vecResources.begin(); itor != m_vecResources.end();) @@ -61,7 +62,7 @@ void SoftSensorBundleActivator::deactivateBundle() } } -void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) +void DISensorBundleActivator::createResource(resourceInfo resourceInfo) { if (resourceInfo.resourceType == "oic.softsensor") { @@ -89,7 +90,7 @@ void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) } } -void SoftSensorBundleActivator::destroyResource(BundleResource *resource) +void DISensorBundleActivator::destroyResource(BundleResource *resource) { std::vector ::iterator itor; @@ -105,7 +106,7 @@ void SoftSensorBundleActivator::destroyResource(BundleResource *resource) extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId) { - bundle = new SoftSensorBundleActivator(); + bundle = new DISensorBundleActivator(); bundle->activateBundle(resourceContainer, bundleId); } diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensor.cpp similarity index 74% rename from service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp rename to service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensor.cpp index 1675fbed3..5da9ebbdd 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensor.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensor.cpp @@ -1,30 +1,30 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= /** * This file contains the exported symbol. */ -#include - #include "DiscomfortIndexSensor.h" + +#include #include "SysTimer.h" #ifdef __ANDROID__ @@ -85,7 +85,6 @@ DIResult DiscomfortIndexSensor::makeDiscomfortIndex() std::cout << "Discomfort level : " << dDI << ", Temperature :" << t << ", Humidity :" << h << std::endl; - m_discomfortIndex = std::to_string(DILevel); std::cout << "[result] Discomfort Index : " << m_discomfortIndex << std::endl; if (dDI >= 80.0) { @@ -112,5 +111,9 @@ DIResult DiscomfortIndexSensor::makeDiscomfortIndex() << std::endl; } + std::cout << std::endl; + + m_discomfortIndex = std::to_string(DILevel); + return SUCCESS; } \ 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/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp similarity index 58% rename from service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp rename to service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp index 3202a1578..4b0d6dc26 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/DiscomfortIndexSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp @@ -20,6 +20,9 @@ #include "DiscomfortIndexSensorResource.h" +#include +#include + DiscomfortIndexSensorResource::DiscomfortIndexSensorResource() { @@ -54,24 +57,47 @@ RCSResourceAttributes::Value DiscomfortIndexSensorResource::getAttribute(const s void DiscomfortIndexSensorResource::executeLogic() { - std::map mapInputData; - std::string strTemp = getAttribute("temperature").toString(); - std::string strHumid = getAttribute("humidity").toString(); std::string strDiscomfortIndex; - if (!strTemp.empty() && !strHumid.empty()) - { - mapInputData.insert(std::make_pair("temperature", strTemp)); - mapInputData.insert(std::make_pair("humidity", strHumid)); + m_pDiscomfortIndexSensor->executeDISensorLogic(&m_mapInputData, &strDiscomfortIndex); - m_pDiscomfortIndexSensor->executeDISensorLogic(&mapInputData, &strDiscomfortIndex); + setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str())); - setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str())); + for (auto it : m_mapInputData) + { + setAttribute(it.first, RCSResourceAttributes::Value(it.second.c_str())); } } void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string attributeName, - std::vector values) + std::vector values) { + double sum = 0; + double dConvert; + int inputCount = 0; + std::string itString; -} + for (auto it : values) + { + itString = it.toString(); + std::stringstream ss(itString); //turn the string into a stream + ss >> dConvert; //convert + sum += dConvert; + ++inputCount; + } + + double result = sum / inputCount; + std::string indexCount;//string which will contain the indexCount + std::stringstream convert; // stringstream used for the conversion + convert << result;//add the value of Number to the characters in the stream + indexCount = convert.str();//set indexCount to the content of the stream + + m_mapInputData[attributeName] = indexCount; + + // execute logic only if all the input data are ready + if (m_mapInputData.find("temperature") != m_mapInputData.end() + && m_mapInputData.find("humidity") != m_mapInputData.end()) + { + executeLogic(); + } +} \ No newline at end of file diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/SysTimer.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/SysTimer.cpp new file mode 100644 index 000000000..702275c13 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/SysTimer.cpp @@ -0,0 +1,56 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "SysTimer.h" + +#include +#include +#include +#include +#include +#include + +std::string SysTimer::MilliSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + long long val = tv.tv_sec * (long long) 1000 + tv.tv_usec / 1000; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} + +std::string SysTimer::UTCSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + unsigned long val = tv.tv_sec; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/SConscript new file mode 100644 index 000000000..46c4dbe3b --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/SConscript @@ -0,0 +1,27 @@ +#****************************************************************** +# +# Copyright 2015 Samsung Electronics All Rights Reserved. +# +#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +Import('env') + +target_os = env.get('TARGET_OS') + +if target_os == 'linux' : + SConscript('THSensorApp/SConscript') + SConscript('THSensorApp1/SConscript') diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/SConscript new file mode 100644 index 000000000..9b065edea --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/SConscript @@ -0,0 +1,39 @@ +## +# linux sample sensor app build script +## + +Import('env') + +# Add third party libraries +lib_env = env.Clone() +SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') +linux_sample_env = lib_env.Clone() + +###################################################################### +# Build flags +###################################################################### +linux_sample_env.AppendUnique(CPPPATH = ['include']) +linux_sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-pthread','-std=c++0x']) +linux_sample_env.AppendUnique(CPPDEFINES = ['LINUX']) +linux_sample_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) +linux_sample_env.AppendUnique(LIBS = ['oc']) +linux_sample_env.AppendUnique(LIBS = ['octbstack']) +linux_sample_env.AppendUnique(LIBS = ['libconnectivity_abstraction']) +linux_sample_env.AppendUnique(LIBS = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +if env.get('SECURED') == '1': + linux_sample_env.AppendUnique(LIBS = ['tinydtls']) +# +# On some platforms link order can miss functions so librt needs to be +# re-scanned at the end if present. gcc 4.6 is one with this issue. +if 'rt' in linux_sample_env.get('LIBS'): + linux_sample_env.Append(LIBS = ['rt']) + +###################################################################### +#build sampleapp +###################################################################### +thsensorapp = linux_sample_env.Program('THSensorApp', 'src/ThingResourceServer.cpp') +Alias("thsensorapp_sample", thsensorapp) +env.AppendTarget('thsensorapp_sample') diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/include/ThingResourceServer.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/include/ThingResourceServer.h new file mode 100644 index 000000000..b1107b651 --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/include/ThingResourceServer.h @@ -0,0 +1,81 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef THINGRESOURCESERVER_H_ +#define THINGRESOURCESERVER_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +#include +#include + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE OC::ModeType::Server +#define COAP_SRVTYPE OC::ServiceType::InProc + +#define RESOURCE_TYPE_NAME_TEMP "oic.sensor.temperature" +#define RESOURCE_TYPE_NAME_HUMID "oic.sensor.humidity" + +// Forward declaring the entityHandler + +class TemphumidResource +{ + public: + /// Access this property from a TB client + int m_humid; + int m_temp; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OC::OCRepresentation m_resourceRep; + OC::ObservationIds m_interestedObservers; + + public: + TemphumidResource() : + m_humid(0), m_temp(0), m_resourceHandle(0) + { + m_resourceUri = "/Thing_TempHumSensor"; + m_resourceTypes.push_back(RESOURCE_TYPE_NAME_TEMP); + m_resourceTypes.push_back(RESOURCE_TYPE_NAME_HUMID); + m_resourceInterfaces.push_back(OC::DEFAULT_INTERFACE); + + printf("Running thing as %s\n", m_resourceUri.c_str()); + m_resourceRep.setUri(m_resourceUri); + } + + ~TemphumidResource() + { + } + + void registerResource(); + + OCResourceHandle getHandle(); + + void setResourceRepresentation(OC::OCRepresentation &rep); + + OC::OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/src/ThingResourceServer.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/src/ThingResourceServer.cpp new file mode 100644 index 000000000..2161f0eca --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp/src/ThingResourceServer.cpp @@ -0,0 +1,223 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include + +using namespace OC; +using namespace std; + +int g_Observation = 0; + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request); + +/* + * TempResourceFunctions + */ + +void TemphumidResource::registerResource() +{ + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + // This will internally create and register the resource. + OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri, + m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } + + result = OCPlatform::bindTypeToResource(m_resourceHandle, m_resourceTypes[1]); + + if (OC_STACK_OK != result) + { + cout << "Binding TypeName to Resource was unsuccessful\n"; + } +} + +OCResourceHandle TemphumidResource::getHandle() +{ + return m_resourceHandle; +} + +void TemphumidResource::setResourceRepresentation(OCRepresentation &rep) +{ + int tempHumid; + int tempTemp; + + rep.getValue("humidity", tempTemp); + rep.getValue("temperature", tempHumid); + + m_humid = tempHumid; + m_temp = tempTemp; + + cout << "\t\t\t" << "Received representation: " << endl; + cout << "\t\t\t\t" << "temp: " << m_humid << endl; + cout << "\t\t\t\t" << "humid: " << m_temp << endl; +} + +OCRepresentation TemphumidResource::getResourceRepresentation() +{ + + // This representation is temporaily for soft-sensor-management - name, type, vale + m_resourceRep.setValue("temperature", std::to_string(m_temp)); + m_resourceRep.setValue("humidity", std::to_string(m_humid)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +TemphumidResource g_myResource; + +void *TestSensorVal(void *param) +{ + (void)param; + + g_myResource.m_temp = 27; + g_myResource.m_humid = 48; + + // This function continuously monitors for the changes + while (1) + { + sleep(5); + + if (g_Observation) + { + g_myResource.m_temp += 1; + g_myResource.m_humid -= 1; + + cout << "\ntemp updated to : " << g_myResource.m_temp << endl; + cout << "\nhumid updated to : " << g_myResource.m_humid << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + } + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::RequestFlag) + { + cout << "\t\trequestFlag : Request\n"; + + // If the request type is GET + if (requestType == "GET") + { + cout << "\t\t\trequestType : GET\n"; + + // Check for query params (if any) + // Process query params and do required operations .. + + // Get the representation of this resource at this point and send it as response + OCRepresentation rep = g_myResource.getResourceRepresentation(); + + if (response) + { + // TODO Error Code + response->setErrorCode(200); + response->setResourceRepresentation(rep, DEFAULT_INTERFACE); + } + } + else if (requestType == "PUT") + { + // TODO PUT request operations + } + else if (requestType == "POST") + { + // TODO POST request operations + } + else if (requestType == "DELETE") + { + // TODO DELETE request operations + } + } + + if (requestFlag & RequestHandlerFlag::ObserverFlag) + { + pthread_t threadId; + + cout << "\t\trequestFlag : Observer\n"; + g_Observation = 1; + + static int startedThread = 0; + + if (!startedThread) + { + pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL); + startedThread = 1; + } + } + } + else + { + std::cout << "Request invalid" << std::endl; + } + + return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR; +} + +int main() +{ + // Create PlatformConfig object + PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos); + + try + { + OC::OCPlatform::Configure(cfg); + + OC::OCPlatform::startPresence(60); + + g_myResource.registerResource(); + + int input = 0; + cout << "Type any key to terminate" << endl; + cin >> input; + + OC::OCPlatform::stopPresence(); + } + catch (std::exception e) + { + cout << e.what() << endl; + } + + return 0; +} diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/SConscript b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/SConscript new file mode 100644 index 000000000..31097933e --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/SConscript @@ -0,0 +1,39 @@ +## +# linux sample sensor app build script +## + +Import('env') + +# Add third party libraries +lib_env = env.Clone() +SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') +linux_sample_env = lib_env.Clone() + +###################################################################### +# Build flags +###################################################################### +linux_sample_env.AppendUnique(CPPPATH = ['include']) +linux_sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-pthread','-std=c++0x']) +linux_sample_env.AppendUnique(CPPDEFINES = ['LINUX']) +linux_sample_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) +linux_sample_env.AppendUnique(LIBS = ['oc']) +linux_sample_env.AppendUnique(LIBS = ['octbstack']) +linux_sample_env.AppendUnique(LIBS = ['libconnectivity_abstraction']) +linux_sample_env.AppendUnique(LIBS = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +if env.get('SECURED') == '1': + linux_sample_env.AppendUnique(LIBS = ['tinydtls']) + +# On some platforms link order can miss functions so librt needs to be +# re-scanned at the end if present. gcc 4.6 is one with this issue. +if 'rt' in linux_sample_env.get('LIBS'): + linux_sample_env.Append(LIBS = ['rt']) + +###################################################################### +#build sampleapp +###################################################################### +thsensorapp1 = linux_sample_env.Program('THSensorApp1', 'src/ThingResourceServer1.cpp') +Alias("thsensorapp1_sample", thsensorapp1) +env.AppendTarget('thsensorapp1_sample') diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/include/ThingResourceServer1.h b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/include/ThingResourceServer1.h new file mode 100644 index 000000000..9b4424b1c --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/include/ThingResourceServer1.h @@ -0,0 +1,80 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef THINGRESOURCESERVER_H_ +#define THINGRESOURCESERVER_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +#include +#include + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE OC::ModeType::Server +#define COAP_SRVTYPE OC::ServiceType::InProc + +#define RESOURCE_TYPE_NAME_TEMP "oic.sensor.temperature" +#define RESOURCE_TYPE_NAME_HUMID "oic.sensor.humidity" + + +class TemphumidResource +{ + public: + /// Access this property from a TB client + int m_humid; + int m_temp; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OC::OCRepresentation m_resourceRep; + OC::ObservationIds m_interestedObservers; + + public: + TemphumidResource() : + m_humid(0), m_temp(0), m_resourceHandle(0) + { + m_resourceUri = "/Thing_TempHumSensor1"; + m_resourceTypes.push_back(RESOURCE_TYPE_NAME_TEMP); + m_resourceTypes.push_back(RESOURCE_TYPE_NAME_HUMID); + m_resourceInterfaces.push_back(OC::DEFAULT_INTERFACE); + + printf("Running thing as %s\n", m_resourceUri.c_str()); + m_resourceRep.setUri(m_resourceUri); + } + + ~TemphumidResource() + { + } + + void registerResource(); + + OCResourceHandle getHandle(); + + void setResourceRepresentation(OC::OCRepresentation &rep); + + OC::OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/src/ThingResourceServer1.cpp b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/src/ThingResourceServer1.cpp new file mode 100644 index 000000000..6f2645e1f --- /dev/null +++ b/service/resource-encapsulation/src/resourceContainer/examples/DiscomfortIndexSensorBundle/src/inputSensors/THSensorApp1/src/ThingResourceServer1.cpp @@ -0,0 +1,222 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include + +using namespace OC; +using namespace std; + + +int g_Observation = 0; + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request); + +/* +* TempResourceFunctions +*/ + +void TemphumidResource::registerResource() +{ + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + // This will internally create and register the resource. + OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri, + m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } + + result = OCPlatform::bindTypeToResource(m_resourceHandle, m_resourceTypes[1]); + + if (OC_STACK_OK != result) + { + cout << "Binding TypeName to Resource was unsuccessful\n"; + } +} + +OCResourceHandle TemphumidResource::getHandle() +{ + return m_resourceHandle; +} + +void TemphumidResource::setResourceRepresentation(OCRepresentation &rep) +{ + int tempHumid; + int tempTemp; + + rep.getValue("humidity", tempTemp); + rep.getValue("temperature", tempHumid); + + m_humid = tempHumid; + m_temp = tempTemp; + + cout << "\t\t\t" << "Received representation: " << endl; + cout << "\t\t\t\t" << "temp: " << m_humid << endl; + cout << "\t\t\t\t" << "humid: " << m_temp << endl; +} + +OCRepresentation TemphumidResource::getResourceRepresentation() +{ + m_resourceRep.setValue("temperature", std::to_string(m_temp)); + m_resourceRep.setValue("humidity", std::to_string(m_humid)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +TemphumidResource g_myResource; + +void *TestSensorVal(void *param) +{ + (void)param; + + g_myResource.m_temp = 27; + g_myResource.m_humid = 48; + + // This function continuously monitors for the changes + while (1) + { + sleep(5); + + if (g_Observation) + { + g_myResource.m_temp += 1; + g_myResource.m_humid -= 1; + + cout << "\ntemp updated to : " << g_myResource.m_temp << endl; + cout << "\nhumid updated to : " << g_myResource.m_humid << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + } + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::RequestFlag) + { + cout << "\t\trequestFlag : Request\n"; + + // If the request type is GET + if (requestType == "GET") + { + cout << "\t\t\trequestType : GET\n"; + + // Check for query params (if any) + // Process query params and do required operations .. + + // Get the representation of this resource at this point and send it as response + OCRepresentation rep = g_myResource.getResourceRepresentation(); + + if (response) + { + // TODO Error Code + response->setErrorCode(200); + response->setResourceRepresentation(rep, DEFAULT_INTERFACE); + } + } + else if (requestType == "PUT") + { + // TODO PUT request operations + } + else if (requestType == "POST") + { + // TODO POST request operations + } + else if (requestType == "DELETE") + { + // TODO DELETE request operations + } + } + + if (requestFlag & RequestHandlerFlag::ObserverFlag) + { + pthread_t threadId; + + cout << "\t\trequestFlag : Observer\n"; + g_Observation = 1; + + static int startedThread = 0; + + if (!startedThread) + { + pthread_create(&threadId, NULL, TestSensorVal, (void *)NULL); + startedThread = 1; + } + } + } + else + { + std::cout << "Request invalid" << std::endl; + } + + return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR; +} + +int main() +{ + // Create PlatformConfig object + PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos); + + try + { + OC::OCPlatform::Configure(cfg); + + OC::OCPlatform::startPresence(60); + + g_myResource.registerResource(); + + int input = 0; + cout << "Type any key to terminate" << endl; + cin >> input; + + OC::OCPlatform::stopPresence(); + } + catch (std::exception e) + { + cout << e.what() << endl; + } + + return 0; +} diff --git a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml index 2621e4272..5d917b825 100644 --- a/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml +++ b/service/resource-encapsulation/src/resourceContainer/examples/ResourceContainerConfig.xml @@ -2,47 +2,90 @@ oic.bundle.discomfortIndexSensor - libSoftSensorBundle.so + libDISensorBundle.so 1.0.0 DiscomfortIndexSensor1 - oic.softsensor + oic.softsensor - - discomfortIndex - int - + + discomfortIndex + int + + + humidity + double + + + temperature + double + - - humidity - double - - - temperature - double - + + humidity + double + oic.sensor.humidity + + + temperature + double + oic.sensor.temperature + - + + + oic.bundle.BMISensor + libBMISensorBundle.so + 1.0.0 + + + BMISensor1 + oic.softsensor + + + BMIresult + int + + + + + weight + double + oic.sensor.weight + /Thing_WeightSensor + + + height + double + oic.sensor.height + /Thing_HeightSensor + + + + + + + oic.bundle.hueSample libHueBundle.so 1.0.0 light - oic.light.control + oic.light.control
http://192.168.0.2/api/newdeveloper/lights/1
- + -->
diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h deleted file mode 100644 index 01513a2c8..000000000 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/DiscomfortIndexSensor.h +++ /dev/null @@ -1,59 +0,0 @@ -/****************************************************************** -* -* Copyright 2015 Samsung Electronics All Rights Reserved. -* -* -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -* -******************************************************************/ - -/* -* DiscomfortIndexSensor.h -*/ - -#ifndef DISCOMFORTINDEXSENSOR_H_ -#define DISCOMFORTINDEXSENSOR_H_ - -/** -* This header file is included to define _EXPORT_. -*/ - -#include "SoftSensorResource.h" - -using namespace OIC::Service; - -namespace DiscomfortIndexSensorName -{ - typedef enum - { - SUCCESS = 0, ERROR, ALL_DISCOMPORT, HALF_DISCOMPORT, LITTLE_DISCOMPORT, ALL_COMPORT - } DIResult; - - class DiscomfortIndexSensor - { - public: - DiscomfortIndexSensor(); - ~DiscomfortIndexSensor(); - - int executeDISensorLogic(std::map *pInputData, std::string *pOutput); - DIResult makeDiscomfortIndex(); - - private: - std::string m_humidity; - std::string m_temperature; - std::string m_discomfortIndex; - }; -}; - -#endif /* DISCOMFORTINDEXSENSOR_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SysTimer.h b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SysTimer.h deleted file mode 100644 index 496d4a49f..000000000 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SysTimer.h +++ /dev/null @@ -1,37 +0,0 @@ -/****************************************************************** - * - * Copyright 2014 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ -/* - * SysTimer.h - */ - -#ifndef SYSTIMER_H_ -#define SYSTIMER_H_ - -#include -#include -#include -class SysTimer -{ - public: - static std::string MilliSecondAsString(); - static std::string UTCSecondAsString(); -}; - -#endif /* SYSTIMER_H_ */ diff --git a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SysTimer.cpp b/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SysTimer.cpp deleted file mode 100644 index 032e0fcbd..000000000 --- a/service/resource-encapsulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SysTimer.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/****************************************************************** - * - * Copyright 2014 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -#include -#include -#include -#include -#include - -#include "SysTimer.h" - -#include -std::string SysTimer::MilliSecondAsString() -{ - std::stringstream ss; - struct timeval tv; - - gettimeofday(&tv, NULL); - long long val = tv.tv_sec * (long long) 1000 + tv.tv_usec / 1000; - - ss << val; - std::string strTime = ss.str(); - - return strTime; -} - -std::string SysTimer::UTCSecondAsString() -{ - std::stringstream ss; - struct timeval tv; - - gettimeofday(&tv, NULL); - unsigned long val = tv.tv_sec; - - ss << val; - std::string strTime = ss.str(); - - return strTime; -} diff --git a/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h b/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h index 3c75630bf..e610016da 100644 --- a/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h +++ b/service/resource-encapsulation/src/resourceContainer/include/DiscoverResourceUnit.h @@ -39,49 +39,49 @@ namespace OIC { class DiscoverResourceUnit { - public: - struct DiscoverResourceInfo - { - DiscoverResourceInfo() - : resourceUri(), resourceType(), attributeName() {} - DiscoverResourceInfo(std::string uri, std::string type, std::string name) - : resourceUri(uri), resourceType(type), attributeName(name) {} - std::string resourceUri; - std::string resourceType; - std::string attributeName; - }; + public: + struct DiscoverResourceInfo + { + DiscoverResourceInfo() + : resourceUri(), resourceType(), attributeName() {} + DiscoverResourceInfo(std::string uri, std::string type, std::string name) + : resourceUri(uri), resourceType(type), attributeName(name) {} + std::string resourceUri; + std::string resourceType; + std::string attributeName; + }; - typedef std::shared_ptr Ptr; - typedef std::function UpdatedCBFromServer; - typedef std::function values)> - UpdatedCB; + typedef std::shared_ptr Ptr; + typedef std::function UpdatedCBFromServer; + typedef std::function values)> + UpdatedCB; typedef RemoteResourceUnit::UPDATE_MSG REMOTE_MSG; - DiscoverResourceUnit(const std::string & bundleId); - ~DiscoverResourceUnit(); + DiscoverResourceUnit(const std::string &bundleId); + ~DiscoverResourceUnit(); - void startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB); + void startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB); - private: - std::string m_bundleId; - std::string m_Uri; - std::string m_ResourceType; - std::string m_AttrubuteName; - std::atomic_bool isStartedDiscovery; + private: + std::string m_bundleId; + std::string m_Uri; + std::string m_ResourceType; + std::string m_AttrubuteName; + std::atomic_bool isStartedDiscovery; - std::vector m_vecRemoteResource; - RCSDiscoveryManager::ResourceDiscoveredCallback pDiscoveredCB; - UpdatedCBFromServer pUpdatedCBFromServer; - UpdatedCB pUpdatedCB; + std::vector m_vecRemoteResource; + RCSDiscoveryManager::ResourceDiscoveredCallback pDiscoveredCB; + UpdatedCBFromServer pUpdatedCBFromServer; + UpdatedCB pUpdatedCB; - bool isAlreadyDiscoveredResource(RCSRemoteResourceObject::Ptr discoveredResource); - void discoverdCB(RCSRemoteResourceObject::Ptr remoteObject); + bool isAlreadyDiscoveredResource(RCSRemoteResourceObject::Ptr discoveredResource); + void discoverdCB(RCSRemoteResourceObject::Ptr remoteObject, std::string uri); void onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource); - std::vector - buildInputResourceData(RCSRemoteResourceObject::Ptr updatedResource); + std::vector + buildInputResourceData(RCSRemoteResourceObject::Ptr updatedResource); }; } } diff --git a/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp b/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp index ac1693cac..6737a8b9b 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/Configuration.cpp @@ -18,11 +18,11 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "Configuration.h" + #include #include -#include "Configuration.h" - namespace { const std::string INPUT_RESOURCE = std::string("input"); @@ -34,19 +34,17 @@ namespace OIC { static inline std::string trim_both(const std::string &str) { - int npos = str.find_first_not_of(" \t\v\n\r"); + size_t npos = str.find_first_not_of(" \t\v\n\r"); - if (npos == -1) + if (npos == std::string::npos) { return ""; } - unsigned int n = (unsigned int) npos; - std::string tempString = n == std::string::npos ? str : str.substr(n, str.length()); - - n = tempString.find_last_not_of(" \t\v\n\r"); + std::string tempString = str.substr(npos, str.length()); + npos = tempString.find_last_not_of(" \t\v\n\r"); - return n == std::string::npos ? tempString : tempString.substr(0, n + 1); + return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1); } Configuration::Configuration() @@ -72,12 +70,13 @@ namespace OIC return m_loaded; } - bool Configuration::isHasInput(std::string & bundleId) const + bool Configuration::isHasInput(std::string &bundleId) const { try { return m_mapisHasInput.at(bundleId); - }catch (std::out_of_range &e) + } + catch (std::out_of_range &e) { return false; } @@ -95,11 +94,11 @@ namespace OIC try { for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = - bundle->next_sibling()) + bundle->next_sibling()) { std::map< std::string, std::string > bundleMap; for (subItem = bundle->first_node(); subItem; - subItem = subItem->next_sibling()) + subItem = subItem->next_sibling()) { strKey = subItem->name(); strValue = subItem->value(); @@ -107,7 +106,7 @@ namespace OIC if (strlen(subItem->value()) > 0) { bundleMap.insert( - std::make_pair(trim_both(strKey), trim_both(strValue))); + std::make_pair(trim_both(strKey), trim_both(strValue))); } } configOutput->push_back(bundleMap); @@ -136,7 +135,7 @@ namespace OIC // for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = - bundle->next_sibling()) + bundle->next_sibling()) { // strBundleId = bundle->first_node("id")->value(); @@ -152,7 +151,7 @@ namespace OIC // strVersion = bundle->first_node("version")->value(); bundleConfigMap.insert( - std::make_pair("version", trim_both(strVersion))); + std::make_pair("version", trim_both(strVersion))); configOutput->push_back(bundleConfigMap); @@ -184,7 +183,7 @@ namespace OIC { // for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = - bundle->next_sibling()) + bundle->next_sibling()) { // strBundleId = bundle->first_node("id")->value(); @@ -193,12 +192,12 @@ namespace OIC { // for (resource = bundle->first_node("resources")->first_node( - "resourceInfo"); resource; resource = resource->next_sibling()) + "resourceInfo"); resource; resource = resource->next_sibling()) { resourceInfo tempResourceInfo; for (item = resource->first_node(); item; item = - item->next_sibling()) + item->next_sibling()) { strKey = item->name(); strValue = item->value(); @@ -218,29 +217,29 @@ namespace OIC else { for (subItem = item->first_node(); subItem; subItem = - subItem->next_sibling()) + subItem->next_sibling()) { map< string, string > propertyMap; strKey = subItem->name(); - if(strKey.compare(INPUT_RESOURCE)) + if (strKey.compare(INPUT_RESOURCE)) { m_mapisHasInput[strBundleId] = true; } for (subItem2 = subItem->first_node(); subItem2; - subItem2 = subItem2->next_sibling()) + subItem2 = subItem2->next_sibling()) { string newStrKey = subItem2->name(); string newStrValue = subItem2->value(); propertyMap[trim_both(newStrKey)] = trim_both( - newStrValue); + newStrValue); } tempResourceInfo.resourceProperty[trim_both(strKey)].push_back( - propertyMap); + propertyMap); } } } diff --git a/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp b/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp index f32add1f8..32cfd6e9c 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/DiscoverResourceUnit.cpp @@ -24,19 +24,21 @@ using namespace OIC::Service; -DiscoverResourceUnit::DiscoverResourceUnit(const std::string & bundleId) -: m_bundleId(bundleId) +DiscoverResourceUnit::DiscoverResourceUnit(const std::string &bundleId) + : m_bundleId(bundleId) { pUpdatedCB = nullptr; isStartedDiscovery = false; - pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, - std::placeholders::_1); + pUpdatedCBFromServer = std::bind(&DiscoverResourceUnit::onUpdate, this, - std::placeholders::_1, std::placeholders::_2); + std::placeholders::_1, std::placeholders::_2); } DiscoverResourceUnit::~DiscoverResourceUnit() { + pUpdatedCB = nullptr; + pUpdatedCBFromServer = nullptr; + m_vecRemoteResource.clear(); } @@ -58,14 +60,16 @@ void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB up // TODO may be will changed active discovery if (m_Uri.empty()) { - RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), m_ResourceType, pDiscoveredCB); + pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1, + std::string("")); } else { - RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), m_Uri, m_ResourceType, pDiscoveredCB); + pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1, m_Uri); } + + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), m_ResourceType, + pDiscoveredCB); } catch (InvalidParameterException &e) { @@ -76,28 +80,29 @@ void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB up isStartedDiscovery = true; } -void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject) +void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject, std::string uri) { - if (!isAlreadyDiscoveredResource(remoteObject)) + if (remoteObject && !isAlreadyDiscoveredResource(remoteObject)) { RemoteResourceUnit::Ptr newDiscoveredResource = - RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer); - - m_vecRemoteResource.push_back(newDiscoveredResource); + RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer); - newDiscoveredResource->startMonitoring(); - newDiscoveredResource->startCaching(); + if (uri.empty() || uri.compare(remoteObject->getUri()) == 0) + { + m_vecRemoteResource.push_back(newDiscoveredResource); + newDiscoveredResource->startMonitoring(); + newDiscoveredResource->startCaching(); + } } else { // Already Discovered Resource } - } void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource) { - if(msg == REMOTE_MSG::DATA_UPDATED) + if (msg == REMOTE_MSG::DATA_UPDATED) { if (updatedResource == nullptr) { @@ -131,13 +136,13 @@ void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr } std::vector DiscoverResourceUnit::buildInputResourceData( - RCSRemoteResourceObject::Ptr updatedResource) + RCSRemoteResourceObject::Ptr updatedResource) { (void)updatedResource; std::vector retVector = {}; - for(auto iter : m_vecRemoteResource) + for (auto iter : m_vecRemoteResource) { - if(iter->getRemoteResourceObject()->getCacheState() != CacheState::READY) + if (iter->getRemoteResourceObject()->getCacheState() != CacheState::READY) { continue; } @@ -145,7 +150,7 @@ std::vector DiscoverResourceUnit::buildInputResour try { RCSResourceAttributes::Value value = - iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName); + iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName); retVector.push_back(value); } @@ -159,7 +164,7 @@ std::vector DiscoverResourceUnit::buildInputResour } bool DiscoverResourceUnit::isAlreadyDiscoveredResource( - RCSRemoteResourceObject::Ptr discoveredResource) + RCSRemoteResourceObject::Ptr discoveredResource) { for (auto iter : m_vecRemoteResource) { @@ -170,4 +175,4 @@ bool DiscoverResourceUnit::isAlreadyDiscoveredResource( } } return false; -} +} \ 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 7767e112c..64428731e 100644 --- a/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp +++ b/service/resource-encapsulation/src/resourceContainer/src/SoftSensorResource.cpp @@ -40,14 +40,6 @@ namespace OIC { 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++) @@ -61,10 +53,7 @@ namespace OIC void SoftSensorResource::setAttribute(std::string key, RCSResourceAttributes::Value &&value) { - BundleResource::setAttribute(key, value.toString()); - - if (std::find(m_inputList.begin(), m_inputList.end(), key) != m_inputList.end()) - executeLogic(); + BundleResource::setAttribute(key, std::move(value)); } RCSResourceAttributes::Value SoftSensorResource::getAttribute(const std::string &key) -- 2.34.1