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']])
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
######################################################################
--- /dev/null
+//******************************************************************
+//
+// 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 <string>
+#include <map>
+#include <time.h>
+
+/**
+ * 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<std::string, std::string> *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_ */
--- /dev/null
+//******************************************************************
+//
+// 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<BundleResource *> m_vecResources;
+};
+
+#endif /* BMISENSOR_BUNDLE_ACTIVATOR_H_ */
--- /dev/null
+//******************************************************************
+//
+// 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<RCSResourceAttributes::Value> values);
+
+ private:
+ BMISensor *m_pBMISensor;
+ std::map<std::string, std::string> m_mapInputData;
+};
+
+#endif
--- /dev/null
+//******************************************************************
+//
+// 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 <stdio.h>
+#include <stdlib.h>
+#include <string>
+class SysTimer
+{
+ public:
+ static std::string MilliSecondAsString();
+ static std::string UTCSecondAsString();
+};
+
+#endif /* SYSTIMER_H_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <iostream>
+#include <stdlib.h>
+#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<std::string, std::string> *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
--- /dev/null
+//******************************************************************
+//
+// 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 <algorithm>
+#include <sstream>
+#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<resourceInfo> resourceConfig;
+
+ resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
+
+ for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
+ itor != resourceConfig.end(); itor++)
+ {
+ createResource(*itor);
+ }
+}
+
+void BMISensorBundleActivator::deactivateBundle()
+{
+ std::vector<BundleResource *>::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 <BundleResource *>::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
--- /dev/null
+//******************************************************************
+//
+// 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<RCSResourceAttributes::Value> 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
--- /dev/null
+//******************************************************************
+//
+// 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 <cstdlib>
+#include <sys/time.h>
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <stdint.h>
+
+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;
+}
--- /dev/null
+##
+# 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
--- /dev/null
+//******************************************************************
+//
+// 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 <functional>
+#include <pthread.h>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include <string>
+#include <cstdlib>
+
+
+#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<std::string> m_resourceTypes;
+ std::vector<std::string> 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_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <HeightSensorApp.h>
+
+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<OC::OCResourceResponse>();
+
+ 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;
+}
+
--- /dev/null
+#******************************************************************
+#
+# 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
--- /dev/null
+##
+# 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
--- /dev/null
+//******************************************************************
+//
+// 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 <functional>
+#include <pthread.h>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include <string>
+#include <cstdlib>
+
+#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<std::string> m_resourceTypes;
+ std::vector<std::string> 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_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <WeightSensorApp.h>
+
+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<OC::OCResourceResponse>();
+
+ 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;
+}
+
void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
const int &eCode, const int &sequenceNumber)
{
+ (void)headerOptions;
try
{
if (eCode == OC_STACK_OK)
void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
+ (void)headerOptions;
try
{
if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
+ (void)headerOptions;
try
{
if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
// 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)
void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
const int eCode);
+ (void)headerOptions;
+ (void)rep;
try
{
if (eCode == OC_STACK_OK)
// Callback handler on GET request
void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
{
+ (void)headerOptions;
try
{
if (eCode == OC_STACK_OK)
void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
const int eCode)
{
+ (void)headerOptions;
try
{
if (eCode == OC_STACK_OK)
static FILE *client_open(const char *path, const char *mode)
{
+ (void)path;
+
return fopen("./oic_svr_db_client.json", mode);
}
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef SOFTSENSOR_SAMPLEBUNDLE_H_
-#define SOFTSENSOR_SAMPLEBUNDLE_H_
+#ifndef DISENSOR_BUNDLE_ACTIVATOR_H_
+#define DISENSOR_BUNDLE_ACTIVATOR_H_
#include "ResourceContainerBundleAPI.h"
#include "BundleActivator.h"
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();
std::vector<BundleResource *> m_vecResources;
};
-#endif /* SOFTSENSOR_SAMPLEBUNDLE_H_ */
+#endif /* DISENSOR_BUNDLE_ACTIVATOR_H_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <map>
+#include <string>
+
+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<std::string, std::string> *pInputData,
+ std::string *pOutput);
+ DIResult makeDiscomfortIndex();
+
+ private:
+ std::string m_humidity;
+ std::string m_temperature;
+ std::string m_discomfortIndex;
+ };
+};
+
+#endif /* DISCOMFORTINDEXSENSOR_H_ */
virtual void executeLogic();
virtual void onUpdatedInputResource(const std::string attributeName,
- std::vector<RCSResourceAttributes::Value> values);
+ std::vector<RCSResourceAttributes::Value> values);
private:
DiscomfortIndexSensor *m_pDiscomfortIndexSensor;
+ std::map<std::string, std::string> m_mapInputData;
};
#endif
--- /dev/null
+//******************************************************************
+//
+// 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 <stdio.h>
+#include <stdlib.h>
+#include <string>
+
+class SysTimer
+{
+ public:
+ static std::string MilliSecondAsString();
+ static std::string UTCSecondAsString();
+};
+
+#endif /* SYSTIMER_H_ */
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include "SoftSensorBundleActivator.h"
-#include "DiscomfortIndexSensorResource.h"
+#include "DISensorBundleActivator.h"
+
#include <algorithm>
#include <sstream>
+#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;
}
}
-void SoftSensorBundleActivator::deactivateBundle()
+void DISensorBundleActivator::deactivateBundle()
{
std::vector<BundleResource *>::iterator itor;
for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
}
}
-void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo)
+void DISensorBundleActivator::createResource(resourceInfo resourceInfo)
{
if (resourceInfo.resourceType == "oic.softsensor")
{
}
}
-void SoftSensorBundleActivator::destroyResource(BundleResource *resource)
+void DISensorBundleActivator::destroyResource(BundleResource *resource)
{
std::vector <BundleResource *>::iterator itor;
extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
std::string bundleId)
{
- bundle = new SoftSensorBundleActivator();
+ bundle = new DISensorBundleActivator();
bundle->activateBundle(resourceContainer, bundleId);
}
-/******************************************************************
- *
- * 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 <iostream>
-
#include "DiscomfortIndexSensor.h"
+
+#include <iostream>
#include "SysTimer.h"
#ifdef __ANDROID__
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)
{
<< std::endl;
}
+ std::cout << std::endl;
+
+ m_discomfortIndex = std::to_string(DILevel);
+
return SUCCESS;
}
\ No newline at end of file
#include "DiscomfortIndexSensorResource.h"
+#include <string>
+#include <sstream>
+
DiscomfortIndexSensorResource::DiscomfortIndexSensorResource()
{
void DiscomfortIndexSensorResource::executeLogic()
{
- std::map<std::string, std::string> 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<RCSResourceAttributes::Value> values)
+ std::vector<RCSResourceAttributes::Value> 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
--- /dev/null
+//******************************************************************
+//
+// 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 <cstdlib>
+#include <sys/time.h>
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <stdint.h>
+
+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;
+}
--- /dev/null
+#******************************************************************
+#
+# 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')
--- /dev/null
+##
+# 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')
--- /dev/null
+//******************************************************************
+//
+// 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 <functional>
+#include <pthread.h>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include <string>
+#include <cstdlib>
+
+#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<std::string> m_resourceTypes;
+ std::vector<std::string> 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_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <ThingResourceServer.h>
+
+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<OC::OCResourceResponse>();
+
+ 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;
+}
--- /dev/null
+##
+# 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')
--- /dev/null
+//******************************************************************
+//
+// 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 <functional>
+#include <pthread.h>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include <string>
+#include <cstdlib>
+
+#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<std::string> m_resourceTypes;
+ std::vector<std::string> 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_ */
--- /dev/null
+//******************************************************************
+//
+// 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 <ThingResourceServer1.h>
+
+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<OC::OCResourceResponse>();
+
+ 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;
+}
<container>
<bundle>
<id>oic.bundle.discomfortIndexSensor</id>
- <path>libSoftSensorBundle.so</path>
+ <path>libDISensorBundle.so</path>
<version>1.0.0</version>
<resources>
<resourceInfo>
<name>DiscomfortIndexSensor1</name>
- <resourceType>oic.softsensor</resourceType>
+ <resourceType>oic.softsensor</resourceType>
<outputs>
- <output>
- <name>discomfortIndex</name>
- <type>int</type>
- </output>
+ <output>
+ <name>discomfortIndex</name>
+ <type>int</type>
+ </output>
+ <output>
+ <name>humidity</name>
+ <type>double</type>
+ </output>
+ <output>
+ <name>temperature</name>
+ <type>double</type>
+ </output>
</outputs>
<inputs>
- <input>
- <name>humidity</name>
- <type>double</type>
- </input>
- <input>
- <name>temperature</name>
- <type>double</type>
- </input>
+ <input>
+ <name>humidity</name>
+ <type>double</type>
+ <resourceType>oic.sensor.humidity</resourceType>
+ </input>
+ <input>
+ <name>temperature</name>
+ <type>double</type>
+ <resourceType>oic.sensor.temperature</resourceType>
+ </input>
</inputs>
</resourceInfo>
</resources>
</bundle>
- <bundle>
+
+ <bundle>
+ <id>oic.bundle.BMISensor</id>
+ <path>libBMISensorBundle.so</path>
+ <version>1.0.0</version>
+ <resources>
+ <resourceInfo>
+ <name>BMISensor1</name>
+ <resourceType>oic.softsensor</resourceType>
+ <outputs>
+ <output>
+ <name>BMIresult</name>
+ <type>int</type>
+ </output>
+ </outputs>
+ <inputs>
+ <input>
+ <name>weight</name>
+ <type>double</type>
+ <resourceType>oic.sensor.weight</resourceType>
+ <resourceUri>/Thing_WeightSensor</resourceUri>
+ </input>
+ <input>
+ <name>height</name>
+ <type>double</type>
+ <resourceType>oic.sensor.height</resourceType>
+ <resourceUri>/Thing_HeightSensor</resourceUri>
+ </input>
+ </inputs>
+ </resourceInfo>
+ </resources>
+ </bundle>
+
+ <bundle>
<id>oic.bundle.hueSample</id>
<path>libHueBundle.so</path>
<version>1.0.0</version>
<resources>
<resourceInfo>
<name>light</name>
- <resourceType>oic.light.control</resourceType>
+ <resourceType>oic.light.control</resourceType>
<address>http://192.168.0.2/api/newdeveloper/lights/1</address>
</resourceInfo>
</resources>
</bundle>
- <!--
- <bundle>
+ <!--
+ <bundle>
<id>oic.bundle.hueJavaSample</id>
- <path>../../../../../../../../service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar</path>
+ <path>../../../../../../../../service/resource-encapsulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar</path>
<libraryPath>.</libraryPath>
<uri>/hueJava</uri>
<activator>org.iotivity.bundle.hue.HueBundleActivator</activator>
<resourceInfo>
<name>light</name>
<uri>light/1</uri>
- <resourceType>oic.light.control</resourceType>
+ <resourceType>oic.light.control</resourceType>
<address>http://192.168.0.2/api/newdeveloper/lights/1</address>
</resourceInfo>
</resources>
</bundle>
- -->
+ -->
</container>
+++ /dev/null
-/******************************************************************
-*
-* 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<std::string, std::string> *pInputData, std::string *pOutput);
- DIResult makeDiscomfortIndex();
-
- private:
- std::string m_humidity;
- std::string m_temperature;
- std::string m_discomfortIndex;
- };
-};
-
-#endif /* DISCOMFORTINDEXSENSOR_H_ */
+++ /dev/null
-/******************************************************************
- *
- * 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 <stdio.h>
-#include <stdlib.h>
-#include <string>
-class SysTimer
-{
- public:
- static std::string MilliSecondAsString();
- static std::string UTCSecondAsString();
-};
-
-#endif /* SYSTIMER_H_ */
+++ /dev/null
-/******************************************************************
- *
- * 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 <cstdlib>
-#include <sys/time.h>
-#include <string>
-#include <iostream>
-#include <sstream>
-
-#include "SysTimer.h"
-
-#include <stdint.h>
-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;
-}
{
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<DiscoverResourceUnit> Ptr;
- typedef std::function<void(RemoteResourceUnit::UPDATE_MSG,
- RCSRemoteResourceObject::Ptr)> UpdatedCBFromServer;
- typedef std::function<void(const std::string attributeName,
- std::vector<RCSResourceAttributes::Value> values)>
- UpdatedCB;
+ typedef std::shared_ptr<DiscoverResourceUnit> Ptr;
+ typedef std::function<void(RemoteResourceUnit::UPDATE_MSG,
+ RCSRemoteResourceObject::Ptr)> UpdatedCBFromServer;
+ typedef std::function<void(const std::string attributeName,
+ std::vector<RCSResourceAttributes::Value> 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<RemoteResourceUnit::Ptr> m_vecRemoteResource;
- RCSDiscoveryManager::ResourceDiscoveredCallback pDiscoveredCB;
- UpdatedCBFromServer pUpdatedCBFromServer;
- UpdatedCB pUpdatedCB;
+ std::vector<RemoteResourceUnit::Ptr> 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<RCSResourceAttributes::Value>
- buildInputResourceData(RCSRemoteResourceObject::Ptr updatedResource);
+ std::vector<RCSResourceAttributes::Value>
+ buildInputResourceData(RCSRemoteResourceObject::Ptr updatedResource);
};
}
}
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "Configuration.h"
+
#include <stdexcept>
#include <utility>
-#include "Configuration.h"
-
namespace
{
const std::string INPUT_RESOURCE = std::string("input");
{
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()
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;
}
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();
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);
// <bundle>
for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle =
- bundle->next_sibling())
+ bundle->next_sibling())
{
// <id>
strBundleId = bundle->first_node("id")->value();
// <version>
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);
{
// <bundle>
for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle =
- bundle->next_sibling())
+ bundle->next_sibling())
{
// <id>
strBundleId = bundle->first_node("id")->value();
{
// <resourceInfo>
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();
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);
}
}
}
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();
}
// 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)
{
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)
{
}
std::vector<RCSResourceAttributes::Value> DiscoverResourceUnit::buildInputResourceData(
- RCSRemoteResourceObject::Ptr updatedResource)
+ RCSRemoteResourceObject::Ptr updatedResource)
{
(void)updatedResource;
std::vector<RCSResourceAttributes::Value> retVector = {};
- for(auto iter : m_vecRemoteResource)
+ for (auto iter : m_vecRemoteResource)
{
- if(iter->getRemoteResourceObject()->getCacheState() != CacheState::READY)
+ if (iter->getRemoteResourceObject()->getCacheState() != CacheState::READY)
{
continue;
}
try
{
RCSResourceAttributes::Value value =
- iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName);
+ iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName);
retVector.push_back(value);
}
}
bool DiscoverResourceUnit::isAlreadyDiscoveredResource(
- RCSRemoteResourceObject::Ptr discoveredResource)
+ RCSRemoteResourceObject::Ptr discoveredResource)
{
for (auto iter : m_vecRemoteResource)
{
}
}
return false;
-}
+}
\ No newline at end of file
{
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++)
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)