From 90d2f0673771f3b0afb0aa2c103354e2b35fe155 Mon Sep 17 00:00:00 2001 From: Rami jung Date: Fri, 20 Mar 2015 17:58:51 +0900 Subject: [PATCH] [SSM] adding sample SoftSensor plugin & appls . V.02 BMISensor : SoftSensor plugin. after receiving height(m) and weight(kg) , it shows BMI(Body Mass Index) Weight Seonsr : A dummy pseudo thing sending the weight(kg) Height Seonsr : A dummy pseudo thing sending the height(m) Change-Id: I5b16643113569bdaa69cf709ee9335b3b3e80e08 Signed-off-by: Rami jung Reviewed-on: https://gerrit.iotivity.org/gerrit/524 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- service/soft-sensor-manager/SConscript | 24 ++ service/soft-sensor-manager/SampleApp/SConscript | 5 + .../SampleApp/linux/HeightSensorApp/SConscript | 30 +++ .../HeightSensorApp/include/HeightSensorApp.h | 97 +++++++ .../linux/HeightSensorApp/src/HeightSensorApp.cpp | 250 ++++++++++++++++++ .../linux/SSMTesterApp/include/SSMTestApp.h | 8 +- .../linux/SSMTesterApp/src/SSMTestApp.cpp | 11 + .../SampleApp/linux/WeightSensorApp/SConscript | 30 +++ .../WeightSensorApp/include/WeightSensorApp.h | 97 +++++++ .../linux/WeightSensorApp/src/WeightSensorApp.cpp | 248 ++++++++++++++++++ .../SoftSensorPlugin/BMISensor/include/BMISensor.h | 127 +++++++++ .../SoftSensorPlugin/BMISensor/include/SysTimer.h | 37 +++ .../SoftSensorPlugin/BMISensor/src/BMISensor.cpp | 289 +++++++++++++++++++++ .../SoftSensorPlugin/BMISensor/src/SysTimer.cpp | 56 ++++ .../SoftSensorPlugin/SoftSensorDescription.xml | 27 ++ 15 files changed, 1335 insertions(+), 1 deletion(-) create mode 100755 service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/SConscript create mode 100644 service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/include/HeightSensorApp.h create mode 100644 service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/src/HeightSensorApp.cpp create mode 100755 service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/SConscript create mode 100644 service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/include/WeightSensorApp.h create mode 100644 service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/src/WeightSensorApp.cpp create mode 100644 service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/BMISensor.h create mode 100644 service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/SysTimer.h create mode 100644 service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/BMISensor.cpp create mode 100644 service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/SysTimer.cpp diff --git a/service/soft-sensor-manager/SConscript b/service/soft-sensor-manager/SConscript index 7a73866..73b970b 100644 --- a/service/soft-sensor-manager/SConscript +++ b/service/soft-sensor-manager/SConscript @@ -64,6 +64,28 @@ DiscomfortIndexSensor_src = [ Glob(DISCOMFORTINDEXSENSOR_DIR + 'src/*.cpp')] DiscomfortIndexSensor = DiscomfortIndexSensor_env.SharedLibrary('DiscomfortIndexSensor', DiscomfortIndexSensor_src) DiscomfortIndexSensor_env.InstallTarget(DiscomfortIndexSensor, 'libDiscomfortIndexSensor') + + + +###################################################################### +# build BMISensor plugin +###################################################################### +BMISensor_env = soft_sensor_manager_env.Clone() + +BMISensor_env.AppendUnique(CCFLAGS = ['-fPIC']) +BMISENSOR_DIR = 'SoftSensorPlugin/BMISensor/' +BMISensor_env.AppendUnique(CPPPATH = [ + BMISENSOR_DIR + 'include', + 'SSMCore/src/SSMInterface/' + ]) + +BMISensor_src = [ Glob(BMISENSOR_DIR + 'src/*.cpp')] + +BMISensor = BMISensor_env.SharedLibrary('BMISensor', BMISensor_src) +BMISensor_env.InstallTarget(BMISensor, 'libBMISensor') + + + ###################################################################### # build SSM CORE ###################################################################### @@ -129,3 +151,5 @@ if target_os == 'linux': Command("SoftSensorDescription.xml", "SoftSensorPlugin/SoftSensorDescription.xml", Copy("$TARGET", "$SOURCE")) Command("THSensorApp","SampleApp/linux/THSensorApp/THSensorApp", Copy("$TARGET", "$SOURCE")) Command("THSensorApp1","SampleApp/linux/THSensorApp1/THSensorApp1", Copy("$TARGET", "$SOURCE")) + Command("HeightSensorApp","SampleApp/linux/HeightSensorApp/HeightSensorApp", Copy("$TARGET", "$SOURCE")) + Command("WeightSensorApp","SampleApp/linux/WeightSensorApp/WeightSensorApp", Copy("$TARGET", "$SOURCE")) diff --git a/service/soft-sensor-manager/SampleApp/SConscript b/service/soft-sensor-manager/SampleApp/SConscript index 545d993..1563346 100644 --- a/service/soft-sensor-manager/SampleApp/SConscript +++ b/service/soft-sensor-manager/SampleApp/SConscript @@ -22,3 +22,8 @@ if target_os == 'linux' : SConscript('linux/SSMTesterApp/SConscript') SConscript('linux/THSensorApp/SConscript') SConscript('linux/THSensorApp1/SConscript') + SConscript('linux/HeightSensorApp/SConscript') + SConscript('linux/WeightSensorApp/SConscript') + + + diff --git a/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/SConscript b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/SConscript new file mode 100755 index 0000000..72a3e36 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/SConscript @@ -0,0 +1,30 @@ +## +# 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 = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +###################################################################### +#build sampleapp +###################################################################### +heightsensorapp = linux_sample_env.Program('HeightSensorApp', 'src/HeightSensorApp.cpp') +Alias("heightsensorapp_sample", heightsensorapp) +env.AppendTarget('heightsensorapp') diff --git a/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/include/HeightSensorApp.h b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/include/HeightSensorApp.h new file mode 100644 index 0000000..5528738 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/include/HeightSensorApp.h @@ -0,0 +1,97 @@ +/****************************************************************** + * + * 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. + * + ******************************************************************/ +/* + * HeightSensorApp.h + */ + +#ifndef HEIGHTSENSORAPP_H_ +#define HEIGHTSENSORAPP_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +using namespace OC; +using namespace std; + +#include +#include + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE ModeType::Server +#define COAP_SRVTYPE ServiceType::InProc + +#define COAP_TYPE_NAME "SoftSensorManager.Sensor" + +// 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 + + + +// Forward declaring the entityHandler + +class HeightResource +{ + public: + /// Access this property from a TB client +// int m_humid; +// int m_temp; + double m_height; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OCRepresentation m_resourceRep; + ObservationIds m_interestedObservers; + + public: + /// Constructor + HeightResource() : + /*m_humid(0), m_temp(0), */ m_height(0), m_resourceHandle(0) + { + m_resourceUri = "/Thing_HeightSensor"; + m_resourceTypes.push_back(COAP_TYPE_NAME); + m_resourceInterfaces.push_back(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(OCRepresentation &rep); + + OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/src/HeightSensorApp.cpp b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/src/HeightSensorApp.cpp new file mode 100644 index 0000000..6a49243 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/HeightSensorApp/src/HeightSensorApp.cpp @@ -0,0 +1,250 @@ + +/****************************************************************** + * + * 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 + +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("2", 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("0", std::string("height")); + m_resourceRep.setValue("1", std::string("double")); + m_resourceRep.setValue("2", std::to_string(m_height)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +HeightResource g_myResource; + +void *TestSensorVal(void *param) +{ + + bool bFlag = true; + int nSleep_time=INTERVAL_FOR_CHECK; + double nHeight; + + std::cout << "[HeightSensorAPP] ::" << __func__ << " is called." + << std::endl; + +// g_myResource.m_height = 1; + + 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; + g_myResource.m_height=0; + + + } + bFlag=bFlag^true; + + + cout << "\n height updated to : " << g_myResource.m_height << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + + } + else{ + + nSleep_time=INTERVAL_FOR_CHECK ; + } + + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::InitFlag) + { + cout << "\t\trequestFlag : Init\n"; + + // entity handler to perform resource initialization operations + } + + 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") + { + // PUT request operations + } + else if (requestType == "POST") + { + // POST request operations + } + else if (requestType == "DELETE") + { + // 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; + } +} + diff --git a/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/include/SSMTestApp.h b/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/include/SSMTestApp.h index a1b45cf..edb6b79 100644 --- a/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/include/SSMTestApp.h +++ b/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/include/SSMTestApp.h @@ -35,13 +35,19 @@ namespace APPMenu { typedef enum { - NONE = 0, REGISTER, UNREGISTER, DISCOMFORT_SAMPLE, ITS_SAMPLE, EXIT = 9 + NONE = 0, REGISTER, UNREGISTER, DISCOMFORT_SAMPLE, ITS_SAMPLE, BMI_SAMPLE, EXIT = 9 } APPMenu; } ; typedef enum { + UNKOWNBMI = 0, UNDERWEIGHT, NORMALRANGE, OVERWEIGHT, OBESE +} BMIResult; + + +typedef enum +{ ALL_DISCOMPORT = 2, HALF_DISCOMPORT, LITTLE_DISCOMPORT, ALL_COMPORT } DIResult; diff --git a/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/src/SSMTestApp.cpp b/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/src/SSMTestApp.cpp index 6f10e62..a68daa6 100644 --- a/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/src/SSMTestApp.cpp +++ b/service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/src/SSMTestApp.cpp @@ -39,6 +39,7 @@ void SSMTestApp::displayMenu() printf(" 2. Unregister Query \n"); printf(" 3. Register DiscomfortIndexSensor sample query \n"); printf(" 4. Register IndoorTrajectorySensor sample query \n"); + printf(" 5. Register BMISensor sample query \n"); printf(" 9. exit \n"); printf("===============================================\n"); printf(" Please Enter the NO: "); @@ -251,6 +252,16 @@ int main() "if Device.IndoorTrajectorySensor.trackeeID == \"9059AF16FEF7\""); break; + case APPMenu::BMI_SAMPLE: + SSMApp->registerQuery("subscribe Device.BMISensor "\ + "if Device.BMISensor.BMIresult > 0"); + + break; + + + + + case APPMenu::EXIT: std::cout << "program exit." << std::endl; break; diff --git a/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/SConscript b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/SConscript new file mode 100755 index 0000000..236ae92 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/SConscript @@ -0,0 +1,30 @@ +## +# 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 = ['libcoap']) +linux_sample_env.AppendUnique(LIBS = ['liboc_logger']) +linux_sample_env.AppendUnique(LIBS = ['pthread']) + +###################################################################### +#build sampleapp +###################################################################### +weightsensorapp = linux_sample_env.Program('WeightSensorApp', 'src/WeightSensorApp.cpp') +Alias("weightsensorapp_sample", weightsensorapp) +env.AppendTarget('weightsensorapp') diff --git a/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/include/WeightSensorApp.h b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/include/WeightSensorApp.h new file mode 100644 index 0000000..a8f6b23 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/include/WeightSensorApp.h @@ -0,0 +1,97 @@ +/****************************************************************** + * + * 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. + * + ******************************************************************/ +/* + * WeightSensorApp.h + */ + +#ifndef WEIGHTSENSORAPP_H_ +#define WEIGHTSENSORAPP_H_ + +#include +#include +#include "OCPlatform.h" +#include "OCApi.h" + +using namespace OC; +using namespace std; + +#include +#include + +#define COAP_IP "0.0.0.0" +#define COAP_PORT 0 +#define COAP_MODE ModeType::Server +#define COAP_SRVTYPE ServiceType::InProc + +#define COAP_TYPE_NAME "SoftSensorManager.Sensor" + +// 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 + + + +// Forward declaring the entityHandler + +class WeightResource +{ + public: + /// Access this property from a TB client +// int m_humid; +// int m_temp; + double m_weight; + std::string m_resourceUri; + std::vector m_resourceTypes; + std::vector m_resourceInterfaces; + OCResourceHandle m_resourceHandle; + OCRepresentation m_resourceRep; + ObservationIds m_interestedObservers; + + public: + /// Constructor + WeightResource() : + m_weight(0), m_resourceHandle(0) + { + m_resourceUri = "/Thing_WeightSensor"; + m_resourceTypes.push_back(COAP_TYPE_NAME); + m_resourceInterfaces.push_back(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(OCRepresentation &rep); + + OCRepresentation getResourceRepresentation(); +}; + +#endif /* THINGRESOURCESERVER_H_ */ diff --git a/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/src/WeightSensorApp.cpp b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/src/WeightSensorApp.cpp new file mode 100644 index 0000000..9bf6314 --- /dev/null +++ b/service/soft-sensor-manager/SampleApp/linux/WeightSensorApp/src/WeightSensorApp.cpp @@ -0,0 +1,248 @@ + +/****************************************************************** + * + * 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 + +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("2", 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("0", std::string("weight")); + m_resourceRep.setValue("1", std::string("double")); + m_resourceRep.setValue("2", std::to_string(m_weight)); + + return m_resourceRep; +} + +// Create the instance of the TemphumidResource class +WeightResource g_myResource; + +void *TestSensorVal(void *param) +{ + + bool bFlag = true; + int nSleep_time=INTERVAL_FOR_CHECK; + double nWeight; + + std::cout << "[WeightSensorAPP] ::" << __func__ << " is called." + << std::endl; + +// g_myResource.m_weight = 33; + nWeight=INIT_VAL; + + // This function continuously monitors for the changes + while (1) + { + + + sleep(nSleep_time); + + + if (g_Observation) + { + + //g_myResource.m_weight+=0.01; + + if(bFlag){ + + nSleep_time =INTERVAL_FOR_MEASUREMENT; + nWeight+=DIFF_VAL; + g_myResource.m_weight=nWeight; + } + else{ + nSleep_time =INTERVAL_FOR_CHECK; + g_myResource.m_weight=0; + + + } + bFlag=bFlag^true; + + cout << "\n weight updated to : " << g_myResource.m_weight << endl; + cout << "Notifying observers with resource handle: " << g_myResource.getHandle() + << endl; + + OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle()); + + if (OC_STACK_NO_OBSERVERS == result) + { + cout << "No More observers, stopping notifications" << endl; + g_Observation = 0; + } + } + else{ + + nSleep_time=INTERVAL_FOR_CHECK ; + } + + } + return NULL; +} + +OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request) +{ + cout << "\tIn Server CPP entity handler:\n"; + + auto response = std::make_shared(); + + if (request) + { + // Get the request type and request flag + std::string requestType = request->getRequestType(); + int requestFlag = request->getRequestHandlerFlag(); + + response->setRequestHandle(request->getRequestHandle()); + response->setResourceHandle(request->getResourceHandle()); + + if (requestFlag & RequestHandlerFlag::InitFlag) + { + cout << "\t\trequestFlag : Init\n"; + + // entity handler to perform resource initialization operations + } + + 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") + { + // PUT request operations + } + else if (requestType == "POST") + { + // POST request operations + } + else if (requestType == "DELETE") + { + // 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; + } +} + diff --git a/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/BMISensor.h b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/BMISensor.h new file mode 100644 index 0000000..d0557e1 --- /dev/null +++ b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/BMISensor.h @@ -0,0 +1,127 @@ +/****************************************************************** + * + * 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. + * + ******************************************************************/ + +/* + * BMISensor.h + */ + +#ifndef BMISENSOR_H_ +#define BMISENSOR_H_ + +/** + * This header file is included to define _EXPORT_. + */ +#include "SSMModelDefinition.h" + +#ifdef __cplusplus +extern "C" +{ +#endif +INTERFACE_DECLSPEC void InitializeContext(ICtxDelegate *pDelegate); +#ifdef __cplusplus +} +; +#endif + + +#if 0 +Who BMI +weight / height*hegith + +< 18.5 :underweight +18.5 <= < 25 : Normal Range +<= 25 < 30 : Overweight +<= 30 : Obese +#endif + +#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 4 // valid time difference. (seconds) + + + + + +namespace BMISensorName +{ +//#define PHYSICAL_EA 2 + + + typedef struct _physicalInput_ + { + char *m_thingName; + int m_inputNum; + void *m_pInputStruct; + } physicalInput; + + typedef enum + { + SUCCESS = 0, ERROR + } DIResult; + + class BMISensor: public ICtxEvent + { + private: + + // static physicalInput s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA]; + + class InValue + { + public: + std::string m_usrname; // each user. ; for future + std::string m_timestamp; + double m_height; // Height. + double m_weight; // Weight + time_t m_timepstampH; + time_t m_timepstampW; + int m_BMIresult; + + }; + + //InValue m_DI[PHYSICAL_EA]; + InValue m_result; + + int runLogic(std::vector< ContextData > &contextDataList); + + public: + BMISensor(); + + void onCtxEvent(enum CTX_EVENT_TYPE eventType, std::vector< ContextData > contextDataList); + + DIResult getInput(std::vector< ContextData > &contextDataList); + DIResult makeBMI(void); + ContextData setOutput(int property_count); + + friend void INTERFACE_DECLSPEC initializeContext(ICtxDelegate *pDelegate); + }; +}; + +#endif /* BMISENSOR_H_ */ diff --git a/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/SysTimer.h b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/SysTimer.h new file mode 100644 index 0000000..1101fe5 --- /dev/null +++ b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/include/SysTimer.h @@ -0,0 +1,37 @@ +/****************************************************************** + * + * 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. + * + ******************************************************************/ +/* + * SysTimer.h + */ + +#ifndef SYSTIMER_H_ +#define SYSTIMER_H_ + +#include +#include +#include +class SysTimer +{ + public: + static std::string MilliSecondAsString(); + static std::string UTCSecondAsString(); +}; + +#endif /* SYSTIMER_H_ */ diff --git a/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/BMISensor.cpp b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/BMISensor.cpp new file mode 100644 index 0000000..a6cdb06 --- /dev/null +++ b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/BMISensor.cpp @@ -0,0 +1,289 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * This file contains the exported symbol. + */ +#include +#include +#include +#include +#include + +#include "BMISensor.h" +#include "SysTimer.h" + +#ifdef __ANDROID__ +#include "OCAndroid.h" +#endif + +using namespace BMISensorName; + +#define SENSOR_NAME "BMISensor" + +#define WEIGHTSENSOR_NAME ((char *)"Thing_WeightSensor") +#define HEIGHTSENSOR_NAME ((char *)"Thing_HeightSensor") + +#define WEIGHT_NAME ((char *)"weight") +#define HEIGHT_NAME ((char *)"height") + + + + +ICtxDelegate *g_pDelegate; + +void InitializeContext(ICtxDelegate *pDelegate) +{ + + std::cout << "[BMISensor] Sensor::" << __func__ << " is called." + << std::endl; + + std::vector < ContextData > contextData; + + BMISensor *eventCls = new BMISensor(); + pDelegate->registerCallback(eventCls); + g_pDelegate = pDelegate; + + std::cout << "BMISensor loaded" << std::endl; + + return; +} + +BMISensor::BMISensor() +{ + m_result.m_usrname = ""; // each user. + m_result.m_timestamp = ""; // measured time + m_result.m_weight = 0; // Height + m_result.m_height = 0; // Weight + time(&(m_result.m_timepstampW)); + time(&(m_result.m_timepstampH)); + m_result.m_BMIresult = UNKOWNBMI; + + + +} + +void BMISensor::onCtxEvent(enum CTX_EVENT_TYPE eventType, + std::vector< ContextData > contextDataList) +{ + + + switch (eventType) + { + case SPF_START: + runLogic(contextDataList); + break; + default: + std::cout << "Not support onCtxEvent" << std::endl; + break; + } +} + +int BMISensor::runLogic(std::vector< ContextData > &contextDataList) +{ + + DIResult result; + + if (getInput(contextDataList) == SUCCESS) + { + + + + + if ((result = makeBMI()) != SUCCESS) + { + std::cout << "Error : makeBMI() result = " << result << std::endl; + return -1; + } + + std::vector < ContextData > outList; + ContextData out = setOutput(1); + outList.push_back(out); + g_pDelegate->addOutput(outList); + + return 0; + } + + return -1; +} + + + +DIResult BMISensor::getInput(std::vector< ContextData > &contextDataList) +{ + + int contextSize = 0; + double temp; + +// std::cout << "[BMISensor] BMISensor::" << "contextDataList.size() : "<< contextDataList.size() << std::endl; + + + + if ((contextSize = contextDataList.size()) == 0) // number of input things + { + std::cout << "Physical Context data is not exist." << std::endl; + return ERROR; + } + + for (int i = 0; i < contextSize; i++) + { + + std::vector < std::map< std::string, std::string > > lVector = + contextDataList[i].outputProperty; + std::string name = lVector[0]["name"]; + + + if (contextDataList[i].rootName == WEIGHTSENSOR_NAME) + { + + + if (name.compare(WEIGHT_NAME) == 0){ + + temp=std::stod( lVector[0]["value"]); + + if (temp > 0){ + time(&(m_result.m_timepstampW)); + m_result.m_weight=temp; + } + + + + } + + + } + else if (contextDataList[i].rootName == HEIGHTSENSOR_NAME) + { + + if (name.compare(HEIGHT_NAME) == 0){ + + temp=std::stod( lVector[0]["value"]); + if (temp > 0){ + time(&(m_result.m_timepstampH)); + m_result.m_height=temp; + } + + } + + } + + } + + return SUCCESS; +} + +/** + * Calculation of BMI with Weight&Height + */ + + + +DIResult BMISensor::makeBMI(void) +{ + + + // std::cout << "[BMISensor] BMISensor::" << __func__ << " is called." + // << std::endl; + + double BMIvalue, timediffsecond; + + timediffsecond = abs( difftime( m_result.m_timepstampW,m_result.m_timepstampH)); + + + if( timediffsecond > DIFFTIME ){ + + BMIvalue= 0; + m_result.m_BMIresult=UNKOWNBMI; + std::cout << "[BMISensor] : OUTOFDATEBMI: "<< m_result.m_BMIresult << std::endl; + } + else if( (m_result.m_height > 0) && (m_result.m_weight > 0 )){ + BMIvalue= m_result.m_weight / (m_result.m_height * m_result.m_height); + + + std::cout << "[BMISensor] height : " << m_result.m_height << " weight : " << m_result.m_weight << " BMIvalue : "<< BMIvalue <<" timediff : "<= OVERWEIGHT_VAL) + { + m_result.m_BMIresult = (int) OBESE; + std::cout << "[BMISensor] : BMIresult:" << m_result.m_BMIresult<< " OBESE "<< std::endl; + } + else if (BMIvalue >= NORMALRANGE_VAL ) + { + m_result.m_BMIresult = (int) OVERWEIGHT; + std::cout << "[BMISensor] : BMIresult:" << m_result.m_BMIresult<< " OVERWEIGHT "<< std::endl; + + } + else if (BMIvalue >= UNDERWEIGHT_VAL ) + { + m_result.m_BMIresult = (int) NORMALRANGE; + std::cout << "[BMISensor] : BMIresult:" << m_result.m_BMIresult<< " NORMALRANGE "<< std::endl; + + } + else + { + m_result.m_BMIresult = (int) UNDERWEIGHT; + std::cout << "[BMISensor] : BMIresult:" << m_result.m_BMIresult<< " UNDERWEIGHT "<< std::endl; + } + + } + else { + BMIvalue= -1; + m_result.m_BMIresult=UNKOWNBMI; + std::cout << "[BMISensor] : UNKOWNBMI: "<< m_result.m_BMIresult << std::endl; + } + + + + return SUCCESS; + + +} + + +ContextData BMISensor::setOutput(int property_count) +{ + ContextData out; + + std::map < std::string, std::string > output_property; + + +// std::cout << "[BMISensor] BMISensor::" << __func__ << " is called." +// << std::endl; + + out.rootName = SENSOR_NAME; + out.outputPropertyCount = property_count; + + + std::ostringstream convert; + convert << m_result.m_BMIresult; + + + output_property.insert(std::make_pair("name", "BMIresult")); + output_property.insert(std::make_pair("type", "int")); + output_property.insert(std::make_pair("value", convert.str() )); + out.outputProperty.push_back(output_property); + + + + return out; +} + diff --git a/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/SysTimer.cpp b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/SysTimer.cpp new file mode 100644 index 0000000..5387a53 --- /dev/null +++ b/service/soft-sensor-manager/SoftSensorPlugin/BMISensor/src/SysTimer.cpp @@ -0,0 +1,56 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include +#include +#include +#include +#include + +#include "SysTimer.h" + +#include +std::string SysTimer::MilliSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + long long val = tv.tv_sec * (long long) 1000 + tv.tv_usec / 1000; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} + +std::string SysTimer::UTCSecondAsString() +{ + std::stringstream ss; + struct timeval tv; + + gettimeofday(&tv, NULL); + unsigned long val = tv.tv_sec; + + ss << val; + std::string strTime = ss.str(); + + return strTime; +} diff --git a/service/soft-sensor-manager/SoftSensorPlugin/SoftSensorDescription.xml b/service/soft-sensor-manager/SoftSensorPlugin/SoftSensorDescription.xml index bdf8018..e1deecd 100644 --- a/service/soft-sensor-manager/SoftSensorPlugin/SoftSensorDescription.xml +++ b/service/soft-sensor-manager/SoftSensorPlugin/SoftSensorDescription.xml @@ -95,4 +95,31 @@ + + BMISensor + + + version + string + 1.0 + + + lifetime + int + 60 + + + + + BMIresult + int + + + + Thing_WeightSensor + Thing_HeightSensor + + + + -- 2.7.4