From: Mu-Woong Lee Date: Thu, 30 Jun 2016 01:18:02 +0000 (+0900) Subject: sensor: reorganize the folder structure X-Git-Tag: submit/tizen/20160705.023110~1^2~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F48%2F77448%2F1;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git sensor: reorganize the folder structure Sub-folders for pedometer & pressure recorders are created. Change-Id: Ic4adeb1d52b257f5228e4176886841ef88f95d4c Signed-off-by: Mu-Woong Lee --- diff --git a/src/sensor/CMakeLists.txt b/src/sensor/CMakeLists.txt index c00256a..5c489b3 100644 --- a/src/sensor/CMakeLists.txt +++ b/src/sensor/CMakeLists.txt @@ -4,7 +4,7 @@ SET(DEPS ${DEPS} sensor ) -FILE(GLOB SRCS *.cpp) +FILE(GLOB_RECURSE SRCS *.cpp) INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(PKG_SENSOR REQUIRED ${DEPS}) diff --git a/src/sensor/CreateProvider.cpp b/src/sensor/CreateProvider.cpp index 47d5ee9..141ea0e 100644 --- a/src/sensor/CreateProvider.cpp +++ b/src/sensor/CreateProvider.cpp @@ -16,8 +16,8 @@ #include #include -#include "Pedometer.h" -#include "Pressure.h" +#include "pedometer/Pedometer.h" +#include "pressure/Pressure.h" using namespace ctx; diff --git a/src/sensor/Pedometer.cpp b/src/sensor/Pedometer.cpp deleted file mode 100644 index cac1e89..0000000 --- a/src/sensor/Pedometer.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "PedometerLogger.h" -#include "PedometerQuerier.h" -#include "Pedometer.h" - -using namespace ctx; - -PedometerProvider::PedometerProvider() : - SensorProvider(SUBJ_SENSOR_PEDOMETER) -{ - IF_FAIL_VOID(isSupported()); - - sensorLogger = new(std::nothrow) PedometerLogger(); - IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed"); -} - -PedometerProvider::~PedometerProvider() -{ -} - -void PedometerProvider::getPrivilege(std::vector &privilege) -{ - privilege.push_back(PRIV_HEALTHINFO); -} - -bool PedometerProvider::isSupported() -{ - return util::getSystemInfoBool("tizen.org/feature/sensor.pedometer"); -} - -Querier* PedometerProvider::getQuerier(Json option) -{ - PedometerQuerier *querier = new(std::nothrow) PedometerQuerier(this, option); - IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed"); - return querier; -} diff --git a/src/sensor/Pedometer.h b/src/sensor/Pedometer.h deleted file mode 100644 index d4773fe..0000000 --- a/src/sensor/Pedometer.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PEDOMETER_PROVIDER_H__ -#define __CONTEXT_PEDOMETER_PROVIDER_H__ - -#include "SensorProvider.h" - -namespace ctx { - - class PedometerProvider : public SensorProvider { - public: - PedometerProvider(); - ~PedometerProvider(); - - bool isSupported(); - void getPrivilege(std::vector &privilege); - - protected: - Querier* getQuerier(Json option); - }; -} - -#endif /* _CONTEXT_PEDOMETER_PROVIDER_H_ */ diff --git a/src/sensor/PedometerLogger.cpp b/src/sensor/PedometerLogger.cpp deleted file mode 100644 index b091e58..0000000 --- a/src/sensor/PedometerLogger.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "ClientInfo.h" -#include "PedometerLogger.h" - -using namespace ctx; - -PedometerLogger::PedometerLogger() : - __firstEvent(true) -{ - setSensor(HUMAN_PEDOMETER_SENSOR); - setPowerSave(false); - - /* Create the log table */ - executeQuery( - "CREATE TABLE IF NOT EXISTS " PEDOMETER_RECORD " (" \ - KEY_START_TIME " INTEGER NOT NULL, " \ - KEY_END_TIME " INTEGER NOT NULL PRIMARY KEY, " \ - KEY_WALK_STEPS " INTEGER NOT NULL, " \ - KEY_RUN_STEPS " INTEGER NOT NULL, " \ - KEY_DISTANCE " REAL NOT NULL, " \ - KEY_CALORIES " REAL NOT NULL" \ - ")"); - - ClientInfo clientInfo; - if (clientInfo.exist(SUBJ_SENSOR_PEDOMETER)) - start(); -} - -PedometerLogger::~PedometerLogger() -{ -} - -bool PedometerLogger::start() -{ - if (SensorProxy::isRunning()) - return true; - - _I(GREEN("Start to record")); - - if (SensorProxy::start()) { - flush(); - return true; - } - - return false; -} - -void PedometerLogger::stop() -{ - _I(GREEN("Stop recording")); - - SensorProxy::stop(); - __firstEvent = true; -} - -void PedometerLogger::onEvent(sensor_data_t *eventData) -{ - sensor_pedometer_data_t *pedometerData = reinterpret_cast(eventData); - uint64_t timestamp = getTime(); - - if (__firstEvent) { - _D("Baseline event"); - __firstEvent = false; - } else if (pedometerData->diffs_count == 0) { - _D("Single event"); - __recordSingle(pedometerData, timestamp); - } else { - _D("Batch event"); - __recordBatch(pedometerData, timestamp); - } - - __baseline.timestamp = timestamp; - __baseline.walkSteps = eventData->values[1]; - __baseline.runSteps = eventData->values[2]; - __baseline.distance = eventData->values[3]; - __baseline.calories = eventData->values[4]; - - _D("Baseline: %u, %u, %.3f, %.3f", - __baseline.walkSteps, __baseline.runSteps, __baseline.distance, __baseline.calories); - - removeExpired(SUBJ_SENSOR_PEDOMETER, PEDOMETER_RECORD, KEY_END_TIME); -} - -void PedometerLogger::__recordSingle(sensor_pedometer_data_t *eventData, uint64_t timestamp) -{ - DataRecord record; - record.walkSteps = static_cast(eventData->values[1]) - __baseline.walkSteps; - record.runSteps = static_cast(eventData->values[2]) - __baseline.runSteps; - record.distance = eventData->values[3] - __baseline.distance; - record.calories = eventData->values[4] - __baseline.calories; - - IF_FAIL_VOID_TAG(record.walkSteps + record.runSteps > 0, _D, "Skipping zero-count event"); - - char *query = sqlite3_mprintf( - "INSERT INTO " PEDOMETER_RECORD "(" \ - KEY_START_TIME ", " \ - KEY_END_TIME ", " \ - KEY_WALK_STEPS ", " \ - KEY_RUN_STEPS ", " \ - KEY_DISTANCE ", " \ - KEY_CALORIES ") " \ - "VALUES (%llu, %llu, %u, %u, %.3f, %.3f)", - __baseline.timestamp, timestamp, record.walkSteps, record.runSteps, record.distance, record.calories); - executeQuery(query); - sqlite3_free(query); -} - -void PedometerLogger::__recordBatch(sensor_pedometer_data_t *eventData, uint64_t timestamp) -{ - std::string query("INSERT INTO " PEDOMETER_RECORD "(" \ - KEY_START_TIME ", " \ - KEY_END_TIME ", " \ - KEY_WALK_STEPS ", " \ - KEY_RUN_STEPS ", " \ - KEY_DISTANCE ", " \ - KEY_CALORIES ") VALUES "); - char buffer[256]; - - for (int i = 0; i < eventData->diffs_count; ++i) { - if (eventData->diffs[i].walk_steps + eventData->diffs[i].run_steps == 0) { - _D("Skipping zero-count event"); - continue; - } - - /* TODO: check the timestamps.. they look strange.. */ - g_snprintf(buffer, sizeof(buffer), "(%llu, %llu, %d, %d, %.3f, %.3f),", - i == 0 ? __baseline.timestamp : SEC_TO_MS(static_cast(eventData->diffs[i-1].timestamp)), - SEC_TO_MS(static_cast(eventData->diffs[i].timestamp)), - eventData->diffs[i].walk_steps, - eventData->diffs[i].run_steps, - eventData->diffs[i].distance, - eventData->diffs[i].calories); - query += buffer; - } - - query.resize(query.size() - 1); - IF_FAIL_VOID_TAG(query.at(query.size() - 1) == ')', _D, "No records"); - - executeQuery(query.c_str()); -} diff --git a/src/sensor/PedometerLogger.h b/src/sensor/PedometerLogger.h deleted file mode 100644 index e1d021b..0000000 --- a/src/sensor/PedometerLogger.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PEDOMETER_LOGGER_H__ -#define __CONTEXT_PEDOMETER_LOGGER_H__ - -#include "SensorLogger.h" -#include "SensorProxy.h" - -namespace ctx { - - class PedometerLogger : public SensorLogger, public SensorProxy { - public: - PedometerLogger(); - ~PedometerLogger(); - - bool start(); - void stop(); - - protected: - void onEvent(sensor_data_t *eventData); - - private: - struct DataRecord { - uint64_t timestamp; - unsigned int walkSteps; - unsigned int runSteps; - float distance; - float calories; - }; - - void __recordSingle(sensor_pedometer_data_t *eventData, uint64_t timestamp); - void __recordBatch(sensor_pedometer_data_t *eventData, uint64_t timestamp); - - bool __firstEvent; - DataRecord __baseline; - }; -} - -#endif /* __CONTEXT_PEDOMETER_LOGGER_H__ */ diff --git a/src/sensor/PedometerQuerier.cpp b/src/sensor/PedometerQuerier.cpp deleted file mode 100644 index 37fa7de..0000000 --- a/src/sensor/PedometerQuerier.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "PedometerQuerier.h" - -#define PROJECTION \ - "SUM(" KEY_WALK_STEPS " + " KEY_RUN_STEPS ") AS " KEY_STEPS ", " \ - "SUM(" KEY_WALK_STEPS ") AS " KEY_WALK_STEPS ", " \ - "SUM(" KEY_RUN_STEPS ") AS " KEY_RUN_STEPS ", " \ - "SUM(" KEY_DISTANCE ") AS " KEY_DISTANCE ", " \ - "SUM(" KEY_CALORIES ") AS " KEY_CALORIES ", " \ - "MIN(" KEY_START_TIME ") AS " KEY_START_TIME ", " \ - "MAX(" KEY_END_TIME ") AS " KEY_END_TIME - -using namespace ctx; - -PedometerQuerier::PedometerQuerier(ContextProvider *provider, Json option) : - Querier(provider, option) -{ -} - -PedometerQuerier::~PedometerQuerier() -{ -} - -int PedometerQuerier::query(int startTime, int endTime) -{ - char *sql = sqlite3_mprintf( - "SELECT " PROJECTION \ - " FROM " PEDOMETER_RECORD \ - " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu", - SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); - - int ret = Querier::query(sql); - sqlite3_free(sql); - - return ret; -} - -int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval) -{ - char *sql = sqlite3_mprintf( - "SELECT " PROJECTION \ - " FROM " PEDOMETER_RECORD \ - " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu" \ - " GROUP BY ROUND((" KEY_END_TIME " - %llu) / %llu + 0.5)" \ - " ORDER BY " KEY_END_TIME " ASC", - SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime)), - SEC_TO_MS(static_cast(anchor)), SEC_TO_MS(static_cast(interval))); - - int ret = Querier::query(sql); - sqlite3_free(sql); - - return ret; -} diff --git a/src/sensor/PedometerQuerier.h b/src/sensor/PedometerQuerier.h deleted file mode 100644 index 5cdc506..0000000 --- a/src/sensor/PedometerQuerier.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PEDOMETER_QUERIER_H__ -#define __CONTEXT_PEDOMETER_QUERIER_H__ - -#include "Querier.h" - -namespace ctx { - - class PedometerQuerier : public Querier { - public: - PedometerQuerier(ContextProvider *provider, Json option); - ~PedometerQuerier(); - - int query(int startTime, int endTime); - int query(int startTime, int endTime, int anchor, int interval); - }; -} - -#endif /* __CONTEXT_PEDOMETER_QUERIER_H__ */ diff --git a/src/sensor/Pressure.cpp b/src/sensor/Pressure.cpp deleted file mode 100644 index b50caa2..0000000 --- a/src/sensor/Pressure.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "PressureLogger.h" -#include "PressureQuerier.h" -#include "Pressure.h" - -using namespace ctx; - -PressureProvider::PressureProvider() : - SensorProvider(SUBJ_SENSOR_PRESSURE) -{ - IF_FAIL_VOID(isSupported()); - - sensorLogger = new(std::nothrow) PressureLogger(); - IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed"); -} - -PressureProvider::~PressureProvider() -{ -} - -bool PressureProvider::isSupported() -{ - return util::getSystemInfoBool("tizen.org/feature/sensor.barometer"); -} - -Querier* PressureProvider::getQuerier(Json option) -{ - PressureQuerier *querier = new(std::nothrow) PressureQuerier(this, option); - IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed"); - return querier; -} diff --git a/src/sensor/Pressure.h b/src/sensor/Pressure.h deleted file mode 100644 index d12efac..0000000 --- a/src/sensor/Pressure.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PRESSURE_PROVIDER_H__ -#define __CONTEXT_PRESSURE_PROVIDER_H__ - -#include "SensorProvider.h" - -namespace ctx { - - class PressureProvider : public SensorProvider { - public: - PressureProvider(); - ~PressureProvider(); - - bool isSupported(); - - protected: - Querier* getQuerier(Json option); - }; -} - -#endif /* _CONTEXT_PRESSURE_PROVIDER_H_ */ diff --git a/src/sensor/PressureLogger.cpp b/src/sensor/PressureLogger.cpp deleted file mode 100644 index 0dcaca9..0000000 --- a/src/sensor/PressureLogger.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "ClientInfo.h" -#include "PressureLogger.h" - -#define INSERTION_THRESHOLD 20000 -#define SAMPLING_INTERVAL 60000 -#define BATCH_LATENCY INT_MAX -#define MAX_QUERY_LENGTH 1000 - -using namespace ctx; - -PressureLogger::PressureLogger() -{ - setSensor(PRESSURE_SENSOR); - setPowerSave(false); - setSamplingInterval(SAMPLING_INTERVAL); - setBatchLatency(BATCH_LATENCY); - - /* Create the log table */ - executeQuery( - "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \ - KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \ - KEY_PRESSURE " REAL NOT NULL" \ - ")"); - - ClientInfo clientInfo; - if (clientInfo.exist(SUBJ_SENSOR_PRESSURE)) - start(); -} - -PressureLogger::~PressureLogger() -{ -} - -bool PressureLogger::start() -{ - if (SensorProxy::isRunning()) - return true; - - _I(GREEN("Start to record")); - - __lastInsertionTime = getTime(); - __resetInsertionQuery(); - - return SensorProxy::start(); -} - -void PressureLogger::stop() -{ - _I(GREEN("Stop recording")); - - SensorProxy::stop(); -} - -void PressureLogger::onEvent(sensor_data_t *eventData) -{ - uint64_t receivedTime = getTime(); - __record(eventData, receivedTime); - removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME); -} - -void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime) -{ - char buffer[64]; - g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),", - getTime(eventData->timestamp), eventData->values[0]); - - __insertionQuery += buffer; - - if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH) - return; - - __insertionQuery.resize(__insertionQuery.size() - 1); - if (__insertionQuery.at(__insertionQuery.size() - 1) == ')') - executeQuery(__insertionQuery.c_str()); - - __lastInsertionTime = receivedTime; - __resetInsertionQuery(); -} - -void PressureLogger::__resetInsertionQuery() -{ - __insertionQuery = - "INSERT INTO " PRESSURE_RECORD \ - " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES "; -} diff --git a/src/sensor/PressureLogger.h b/src/sensor/PressureLogger.h deleted file mode 100644 index 45d0c68..0000000 --- a/src/sensor/PressureLogger.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PRESSURE_LOGGER_H__ -#define __CONTEXT_PRESSURE_LOGGER_H__ - -#include "SensorLogger.h" -#include "SensorProxy.h" - -namespace ctx { - - class PressureLogger : public SensorLogger, public SensorProxy { - public: - PressureLogger(); - ~PressureLogger(); - - bool start(); - void stop(); - - protected: - void onEvent(sensor_data_t *eventData); - - private: - void __record(sensor_data_t *eventData, uint64_t receivedTime); - void __resetInsertionQuery(); - - uint64_t __lastInsertionTime; - std::string __insertionQuery; - }; -} - -#endif /* __CONTEXT_PRESSURE_LOGGER_H__ */ diff --git a/src/sensor/PressureQuerier.cpp b/src/sensor/PressureQuerier.cpp deleted file mode 100644 index 5e01662..0000000 --- a/src/sensor/PressureQuerier.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 "TypesInternal.h" -#include "PressureQuerier.h" - -#define PROJECTION \ - "AVG(" KEY_PRESSURE ") AS " KEY_PRESSURE ", " \ - "MIN(" KEY_PRESSURE ") AS " KEY_MIN_PRESSURE ", " \ - "MAX(" KEY_PRESSURE ") AS " KEY_MAX_PRESSURE ", " \ - "AVG(" KEY_PRESSURE ") AS " KEY_AVG_PRESSURE ", " \ - "MIN(" KEY_UNIV_TIME ") AS " KEY_START_TIME ", " \ - "MAX(" KEY_UNIV_TIME ") AS " KEY_END_TIME - -#define PROJECTION_RAW \ - KEY_PRESSURE ", " \ - KEY_PRESSURE " AS " KEY_MIN_PRESSURE ", " \ - KEY_PRESSURE " AS " KEY_MAX_PRESSURE ", " \ - KEY_PRESSURE " AS " KEY_AVG_PRESSURE ", " \ - KEY_UNIV_TIME " AS " KEY_START_TIME ", " \ - KEY_UNIV_TIME " AS " KEY_END_TIME - -using namespace ctx; - -PressureQuerier::PressureQuerier(ContextProvider *provider, Json option) : - Querier(provider, option) -{ -} - -PressureQuerier::~PressureQuerier() -{ -} - -int PressureQuerier::queryRaw(int startTime, int endTime) -{ - char *sql = sqlite3_mprintf( - "SELECT " PROJECTION_RAW \ - " FROM " PRESSURE_RECORD \ - " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu", - SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); - - int ret = Querier::query(sql); - sqlite3_free(sql); - - return ret; -} - -int PressureQuerier::query(int startTime, int endTime) -{ - char *sql = sqlite3_mprintf( - "SELECT " PROJECTION \ - " FROM " PRESSURE_RECORD \ - " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu", - SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); - - int ret = Querier::query(sql); - sqlite3_free(sql); - - return ret; -} - -int PressureQuerier::query(int startTime, int endTime, int anchor, int interval) -{ - char *sql = sqlite3_mprintf( - "SELECT " PROJECTION \ - " FROM " PRESSURE_RECORD \ - " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu" \ - " GROUP BY ROUND((" KEY_UNIV_TIME " - %llu) / %llu + 0.5)" \ - " ORDER BY " KEY_END_TIME " ASC", - SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime)), - SEC_TO_MS(static_cast(anchor)), SEC_TO_MS(static_cast(interval))); - - int ret = Querier::query(sql); - sqlite3_free(sql); - - return ret; -} diff --git a/src/sensor/PressureQuerier.h b/src/sensor/PressureQuerier.h deleted file mode 100644 index be1788c..0000000 --- a/src/sensor/PressureQuerier.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * 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 __CONTEXT_PRESSURE_QUERIER_H__ -#define __CONTEXT_PRESSURE_QUERIER_H__ - -#include "Querier.h" - -namespace ctx { - - class PressureQuerier : public Querier { - public: - PressureQuerier(ContextProvider *provider, Json option); - ~PressureQuerier(); - - int queryRaw(int startTime, int endTime); - int query(int startTime, int endTime); - int query(int startTime, int endTime, int anchor, int interval); - }; -} - -#endif /* __CONTEXT_PRESSURE_QUERIER_H__ */ diff --git a/src/sensor/pedometer/Pedometer.cpp b/src/sensor/pedometer/Pedometer.cpp new file mode 100644 index 0000000..8752754 --- /dev/null +++ b/src/sensor/pedometer/Pedometer.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "PedometerLogger.h" +#include "PedometerQuerier.h" +#include "Pedometer.h" + +using namespace ctx; + +PedometerProvider::PedometerProvider() : + SensorProvider(SUBJ_SENSOR_PEDOMETER) +{ + IF_FAIL_VOID(isSupported()); + + sensorLogger = new(std::nothrow) PedometerLogger(); + IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed"); +} + +PedometerProvider::~PedometerProvider() +{ +} + +void PedometerProvider::getPrivilege(std::vector &privilege) +{ + privilege.push_back(PRIV_HEALTHINFO); +} + +bool PedometerProvider::isSupported() +{ + return util::getSystemInfoBool("tizen.org/feature/sensor.pedometer"); +} + +Querier* PedometerProvider::getQuerier(Json option) +{ + PedometerQuerier *querier = new(std::nothrow) PedometerQuerier(this, option); + IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed"); + return querier; +} diff --git a/src/sensor/pedometer/Pedometer.h b/src/sensor/pedometer/Pedometer.h new file mode 100644 index 0000000..7fee01f --- /dev/null +++ b/src/sensor/pedometer/Pedometer.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PEDOMETER_PROVIDER_H__ +#define __CONTEXT_PEDOMETER_PROVIDER_H__ + +#include "../SensorProvider.h" + +namespace ctx { + + class PedometerProvider : public SensorProvider { + public: + PedometerProvider(); + ~PedometerProvider(); + + bool isSupported(); + void getPrivilege(std::vector &privilege); + + protected: + Querier* getQuerier(Json option); + }; +} + +#endif /* _CONTEXT_PEDOMETER_PROVIDER_H_ */ diff --git a/src/sensor/pedometer/PedometerLogger.cpp b/src/sensor/pedometer/PedometerLogger.cpp new file mode 100644 index 0000000..ed73b2a --- /dev/null +++ b/src/sensor/pedometer/PedometerLogger.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "../ClientInfo.h" +#include "PedometerLogger.h" + +using namespace ctx; + +PedometerLogger::PedometerLogger() : + __firstEvent(true) +{ + setSensor(HUMAN_PEDOMETER_SENSOR); + setPowerSave(false); + + /* Create the log table */ + executeQuery( + "CREATE TABLE IF NOT EXISTS " PEDOMETER_RECORD " (" \ + KEY_START_TIME " INTEGER NOT NULL, " \ + KEY_END_TIME " INTEGER NOT NULL PRIMARY KEY, " \ + KEY_WALK_STEPS " INTEGER NOT NULL, " \ + KEY_RUN_STEPS " INTEGER NOT NULL, " \ + KEY_DISTANCE " REAL NOT NULL, " \ + KEY_CALORIES " REAL NOT NULL" \ + ")"); + + ClientInfo clientInfo; + if (clientInfo.exist(SUBJ_SENSOR_PEDOMETER)) + start(); +} + +PedometerLogger::~PedometerLogger() +{ +} + +bool PedometerLogger::start() +{ + if (SensorProxy::isRunning()) + return true; + + _I(GREEN("Start to record")); + + if (SensorProxy::start()) { + flush(); + return true; + } + + return false; +} + +void PedometerLogger::stop() +{ + _I(GREEN("Stop recording")); + + SensorProxy::stop(); + __firstEvent = true; +} + +void PedometerLogger::onEvent(sensor_data_t *eventData) +{ + sensor_pedometer_data_t *pedometerData = reinterpret_cast(eventData); + uint64_t timestamp = getTime(); + + if (__firstEvent) { + _D("Baseline event"); + __firstEvent = false; + } else if (pedometerData->diffs_count == 0) { + _D("Single event"); + __recordSingle(pedometerData, timestamp); + } else { + _D("Batch event"); + __recordBatch(pedometerData, timestamp); + } + + __baseline.timestamp = timestamp; + __baseline.walkSteps = eventData->values[1]; + __baseline.runSteps = eventData->values[2]; + __baseline.distance = eventData->values[3]; + __baseline.calories = eventData->values[4]; + + _D("Baseline: %u, %u, %.3f, %.3f", + __baseline.walkSteps, __baseline.runSteps, __baseline.distance, __baseline.calories); + + removeExpired(SUBJ_SENSOR_PEDOMETER, PEDOMETER_RECORD, KEY_END_TIME); +} + +void PedometerLogger::__recordSingle(sensor_pedometer_data_t *eventData, uint64_t timestamp) +{ + DataRecord record; + record.walkSteps = static_cast(eventData->values[1]) - __baseline.walkSteps; + record.runSteps = static_cast(eventData->values[2]) - __baseline.runSteps; + record.distance = eventData->values[3] - __baseline.distance; + record.calories = eventData->values[4] - __baseline.calories; + + IF_FAIL_VOID_TAG(record.walkSteps + record.runSteps > 0, _D, "Skipping zero-count event"); + + char *query = sqlite3_mprintf( + "INSERT INTO " PEDOMETER_RECORD "(" \ + KEY_START_TIME ", " \ + KEY_END_TIME ", " \ + KEY_WALK_STEPS ", " \ + KEY_RUN_STEPS ", " \ + KEY_DISTANCE ", " \ + KEY_CALORIES ") " \ + "VALUES (%llu, %llu, %u, %u, %.3f, %.3f)", + __baseline.timestamp, timestamp, record.walkSteps, record.runSteps, record.distance, record.calories); + executeQuery(query); + sqlite3_free(query); +} + +void PedometerLogger::__recordBatch(sensor_pedometer_data_t *eventData, uint64_t timestamp) +{ + std::string query("INSERT INTO " PEDOMETER_RECORD "(" \ + KEY_START_TIME ", " \ + KEY_END_TIME ", " \ + KEY_WALK_STEPS ", " \ + KEY_RUN_STEPS ", " \ + KEY_DISTANCE ", " \ + KEY_CALORIES ") VALUES "); + char buffer[256]; + + for (int i = 0; i < eventData->diffs_count; ++i) { + if (eventData->diffs[i].walk_steps + eventData->diffs[i].run_steps == 0) { + _D("Skipping zero-count event"); + continue; + } + + /* TODO: check the timestamps.. they look strange.. */ + g_snprintf(buffer, sizeof(buffer), "(%llu, %llu, %d, %d, %.3f, %.3f),", + i == 0 ? __baseline.timestamp : SEC_TO_MS(static_cast(eventData->diffs[i-1].timestamp)), + SEC_TO_MS(static_cast(eventData->diffs[i].timestamp)), + eventData->diffs[i].walk_steps, + eventData->diffs[i].run_steps, + eventData->diffs[i].distance, + eventData->diffs[i].calories); + query += buffer; + } + + query.resize(query.size() - 1); + IF_FAIL_VOID_TAG(query.at(query.size() - 1) == ')', _D, "No records"); + + executeQuery(query.c_str()); +} diff --git a/src/sensor/pedometer/PedometerLogger.h b/src/sensor/pedometer/PedometerLogger.h new file mode 100644 index 0000000..57832f9 --- /dev/null +++ b/src/sensor/pedometer/PedometerLogger.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PEDOMETER_LOGGER_H__ +#define __CONTEXT_PEDOMETER_LOGGER_H__ + +#include "../SensorLogger.h" +#include "../SensorProxy.h" + +namespace ctx { + + class PedometerLogger : public SensorLogger, public SensorProxy { + public: + PedometerLogger(); + ~PedometerLogger(); + + bool start(); + void stop(); + + protected: + void onEvent(sensor_data_t *eventData); + + private: + struct DataRecord { + uint64_t timestamp; + unsigned int walkSteps; + unsigned int runSteps; + float distance; + float calories; + }; + + void __recordSingle(sensor_pedometer_data_t *eventData, uint64_t timestamp); + void __recordBatch(sensor_pedometer_data_t *eventData, uint64_t timestamp); + + bool __firstEvent; + DataRecord __baseline; + }; +} + +#endif /* __CONTEXT_PEDOMETER_LOGGER_H__ */ diff --git a/src/sensor/pedometer/PedometerQuerier.cpp b/src/sensor/pedometer/PedometerQuerier.cpp new file mode 100644 index 0000000..020e868 --- /dev/null +++ b/src/sensor/pedometer/PedometerQuerier.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "PedometerQuerier.h" + +#define PROJECTION \ + "SUM(" KEY_WALK_STEPS " + " KEY_RUN_STEPS ") AS " KEY_STEPS ", " \ + "SUM(" KEY_WALK_STEPS ") AS " KEY_WALK_STEPS ", " \ + "SUM(" KEY_RUN_STEPS ") AS " KEY_RUN_STEPS ", " \ + "SUM(" KEY_DISTANCE ") AS " KEY_DISTANCE ", " \ + "SUM(" KEY_CALORIES ") AS " KEY_CALORIES ", " \ + "MIN(" KEY_START_TIME ") AS " KEY_START_TIME ", " \ + "MAX(" KEY_END_TIME ") AS " KEY_END_TIME + +using namespace ctx; + +PedometerQuerier::PedometerQuerier(ContextProvider *provider, Json option) : + Querier(provider, option) +{ +} + +PedometerQuerier::~PedometerQuerier() +{ +} + +int PedometerQuerier::query(int startTime, int endTime) +{ + char *sql = sqlite3_mprintf( + "SELECT " PROJECTION \ + " FROM " PEDOMETER_RECORD \ + " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu", + SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); + + int ret = Querier::query(sql); + sqlite3_free(sql); + + return ret; +} + +int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval) +{ + char *sql = sqlite3_mprintf( + "SELECT " PROJECTION \ + " FROM " PEDOMETER_RECORD \ + " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu" \ + " GROUP BY ROUND((" KEY_END_TIME " - %llu) / %llu + 0.5)" \ + " ORDER BY " KEY_END_TIME " ASC", + SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime)), + SEC_TO_MS(static_cast(anchor)), SEC_TO_MS(static_cast(interval))); + + int ret = Querier::query(sql); + sqlite3_free(sql); + + return ret; +} diff --git a/src/sensor/pedometer/PedometerQuerier.h b/src/sensor/pedometer/PedometerQuerier.h new file mode 100644 index 0000000..0cce36b --- /dev/null +++ b/src/sensor/pedometer/PedometerQuerier.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PEDOMETER_QUERIER_H__ +#define __CONTEXT_PEDOMETER_QUERIER_H__ + +#include "../Querier.h" + +namespace ctx { + + class PedometerQuerier : public Querier { + public: + PedometerQuerier(ContextProvider *provider, Json option); + ~PedometerQuerier(); + + int query(int startTime, int endTime); + int query(int startTime, int endTime, int anchor, int interval); + }; +} + +#endif /* __CONTEXT_PEDOMETER_QUERIER_H__ */ diff --git a/src/sensor/pressure/Pressure.cpp b/src/sensor/pressure/Pressure.cpp new file mode 100644 index 0000000..76b60f4 --- /dev/null +++ b/src/sensor/pressure/Pressure.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "PressureLogger.h" +#include "PressureQuerier.h" +#include "Pressure.h" + +using namespace ctx; + +PressureProvider::PressureProvider() : + SensorProvider(SUBJ_SENSOR_PRESSURE) +{ + IF_FAIL_VOID(isSupported()); + + sensorLogger = new(std::nothrow) PressureLogger(); + IF_FAIL_VOID_TAG(sensorLogger, _E, "Memory allocation failed"); +} + +PressureProvider::~PressureProvider() +{ +} + +bool PressureProvider::isSupported() +{ + return util::getSystemInfoBool("tizen.org/feature/sensor.barometer"); +} + +Querier* PressureProvider::getQuerier(Json option) +{ + PressureQuerier *querier = new(std::nothrow) PressureQuerier(this, option); + IF_FAIL_RETURN_TAG(querier, NULL, _E, "Memory allocation failed"); + return querier; +} diff --git a/src/sensor/pressure/Pressure.h b/src/sensor/pressure/Pressure.h new file mode 100644 index 0000000..419cfba --- /dev/null +++ b/src/sensor/pressure/Pressure.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PRESSURE_PROVIDER_H__ +#define __CONTEXT_PRESSURE_PROVIDER_H__ + +#include "../SensorProvider.h" + +namespace ctx { + + class PressureProvider : public SensorProvider { + public: + PressureProvider(); + ~PressureProvider(); + + bool isSupported(); + + protected: + Querier* getQuerier(Json option); + }; +} + +#endif /* _CONTEXT_PRESSURE_PROVIDER_H_ */ diff --git a/src/sensor/pressure/PressureLogger.cpp b/src/sensor/pressure/PressureLogger.cpp new file mode 100644 index 0000000..e301165 --- /dev/null +++ b/src/sensor/pressure/PressureLogger.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "../ClientInfo.h" +#include "PressureLogger.h" + +#define INSERTION_THRESHOLD 20000 +#define SAMPLING_INTERVAL 60000 +#define BATCH_LATENCY INT_MAX +#define MAX_QUERY_LENGTH 1000 + +using namespace ctx; + +PressureLogger::PressureLogger() +{ + setSensor(PRESSURE_SENSOR); + setPowerSave(false); + setSamplingInterval(SAMPLING_INTERVAL); + setBatchLatency(BATCH_LATENCY); + + /* Create the log table */ + executeQuery( + "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \ + KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \ + KEY_PRESSURE " REAL NOT NULL" \ + ")"); + + ClientInfo clientInfo; + if (clientInfo.exist(SUBJ_SENSOR_PRESSURE)) + start(); +} + +PressureLogger::~PressureLogger() +{ +} + +bool PressureLogger::start() +{ + if (SensorProxy::isRunning()) + return true; + + _I(GREEN("Start to record")); + + __lastInsertionTime = getTime(); + __resetInsertionQuery(); + + return SensorProxy::start(); +} + +void PressureLogger::stop() +{ + _I(GREEN("Stop recording")); + + SensorProxy::stop(); +} + +void PressureLogger::onEvent(sensor_data_t *eventData) +{ + uint64_t receivedTime = getTime(); + __record(eventData, receivedTime); + removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME); +} + +void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime) +{ + char buffer[64]; + g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),", + getTime(eventData->timestamp), eventData->values[0]); + + __insertionQuery += buffer; + + if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH) + return; + + __insertionQuery.resize(__insertionQuery.size() - 1); + if (__insertionQuery.at(__insertionQuery.size() - 1) == ')') + executeQuery(__insertionQuery.c_str()); + + __lastInsertionTime = receivedTime; + __resetInsertionQuery(); +} + +void PressureLogger::__resetInsertionQuery() +{ + __insertionQuery = + "INSERT INTO " PRESSURE_RECORD \ + " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES "; +} diff --git a/src/sensor/pressure/PressureLogger.h b/src/sensor/pressure/PressureLogger.h new file mode 100644 index 0000000..3f8995a --- /dev/null +++ b/src/sensor/pressure/PressureLogger.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PRESSURE_LOGGER_H__ +#define __CONTEXT_PRESSURE_LOGGER_H__ + +#include "../SensorLogger.h" +#include "../SensorProxy.h" + +namespace ctx { + + class PressureLogger : public SensorLogger, public SensorProxy { + public: + PressureLogger(); + ~PressureLogger(); + + bool start(); + void stop(); + + protected: + void onEvent(sensor_data_t *eventData); + + private: + void __record(sensor_data_t *eventData, uint64_t receivedTime); + void __resetInsertionQuery(); + + uint64_t __lastInsertionTime; + std::string __insertionQuery; + }; +} + +#endif /* __CONTEXT_PRESSURE_LOGGER_H__ */ diff --git a/src/sensor/pressure/PressureQuerier.cpp b/src/sensor/pressure/PressureQuerier.cpp new file mode 100644 index 0000000..43d3457 --- /dev/null +++ b/src/sensor/pressure/PressureQuerier.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 "../TypesInternal.h" +#include "PressureQuerier.h" + +#define PROJECTION \ + "AVG(" KEY_PRESSURE ") AS " KEY_PRESSURE ", " \ + "MIN(" KEY_PRESSURE ") AS " KEY_MIN_PRESSURE ", " \ + "MAX(" KEY_PRESSURE ") AS " KEY_MAX_PRESSURE ", " \ + "AVG(" KEY_PRESSURE ") AS " KEY_AVG_PRESSURE ", " \ + "MIN(" KEY_UNIV_TIME ") AS " KEY_START_TIME ", " \ + "MAX(" KEY_UNIV_TIME ") AS " KEY_END_TIME + +#define PROJECTION_RAW \ + KEY_PRESSURE ", " \ + KEY_PRESSURE " AS " KEY_MIN_PRESSURE ", " \ + KEY_PRESSURE " AS " KEY_MAX_PRESSURE ", " \ + KEY_PRESSURE " AS " KEY_AVG_PRESSURE ", " \ + KEY_UNIV_TIME " AS " KEY_START_TIME ", " \ + KEY_UNIV_TIME " AS " KEY_END_TIME + +using namespace ctx; + +PressureQuerier::PressureQuerier(ContextProvider *provider, Json option) : + Querier(provider, option) +{ +} + +PressureQuerier::~PressureQuerier() +{ +} + +int PressureQuerier::queryRaw(int startTime, int endTime) +{ + char *sql = sqlite3_mprintf( + "SELECT " PROJECTION_RAW \ + " FROM " PRESSURE_RECORD \ + " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu", + SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); + + int ret = Querier::query(sql); + sqlite3_free(sql); + + return ret; +} + +int PressureQuerier::query(int startTime, int endTime) +{ + char *sql = sqlite3_mprintf( + "SELECT " PROJECTION \ + " FROM " PRESSURE_RECORD \ + " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu", + SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime))); + + int ret = Querier::query(sql); + sqlite3_free(sql); + + return ret; +} + +int PressureQuerier::query(int startTime, int endTime, int anchor, int interval) +{ + char *sql = sqlite3_mprintf( + "SELECT " PROJECTION \ + " FROM " PRESSURE_RECORD \ + " WHERE " KEY_UNIV_TIME " > %llu AND " KEY_UNIV_TIME " <= %llu" \ + " GROUP BY ROUND((" KEY_UNIV_TIME " - %llu) / %llu + 0.5)" \ + " ORDER BY " KEY_END_TIME " ASC", + SEC_TO_MS(static_cast(startTime)), SEC_TO_MS(static_cast(endTime)), + SEC_TO_MS(static_cast(anchor)), SEC_TO_MS(static_cast(interval))); + + int ret = Querier::query(sql); + sqlite3_free(sql); + + return ret; +} diff --git a/src/sensor/pressure/PressureQuerier.h b/src/sensor/pressure/PressureQuerier.h new file mode 100644 index 0000000..1bd9aa6 --- /dev/null +++ b/src/sensor/pressure/PressureQuerier.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * 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 __CONTEXT_PRESSURE_QUERIER_H__ +#define __CONTEXT_PRESSURE_QUERIER_H__ + +#include "../Querier.h" + +namespace ctx { + + class PressureQuerier : public Querier { + public: + PressureQuerier(ContextProvider *provider, Json option); + ~PressureQuerier(); + + int queryRaw(int startTime, int endTime); + int query(int startTime, int endTime); + int query(int startTime, int endTime, int anchor, int interval); + }; +} + +#endif /* __CONTEXT_PRESSURE_QUERIER_H__ */