sensor: reorganize the folder structure
[platform/core/context/context-provider.git] / src / sensor / pressure / PressureLogger.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sqlite3.h>
18 #include <SensorRecorderTypes.h>
19 #include "../TypesInternal.h"
20 #include "../ClientInfo.h"
21 #include "PressureLogger.h"
22
23 #define INSERTION_THRESHOLD     20000
24 #define SAMPLING_INTERVAL       60000
25 #define BATCH_LATENCY           INT_MAX
26 #define MAX_QUERY_LENGTH        1000
27
28 using namespace ctx;
29
30 PressureLogger::PressureLogger()
31 {
32         setSensor(PRESSURE_SENSOR);
33         setPowerSave(false);
34         setSamplingInterval(SAMPLING_INTERVAL);
35         setBatchLatency(BATCH_LATENCY);
36
37         /* Create the log table */
38         executeQuery(
39                         "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \
40                                 KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \
41                                 KEY_PRESSURE " REAL NOT NULL" \
42                         ")");
43
44         ClientInfo clientInfo;
45         if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
46                 start();
47 }
48
49 PressureLogger::~PressureLogger()
50 {
51 }
52
53 bool PressureLogger::start()
54 {
55         if (SensorProxy::isRunning())
56                 return true;
57
58         _I(GREEN("Start to record"));
59
60         __lastInsertionTime = getTime();
61         __resetInsertionQuery();
62
63         return SensorProxy::start();
64 }
65
66 void PressureLogger::stop()
67 {
68         _I(GREEN("Stop recording"));
69
70         SensorProxy::stop();
71 }
72
73 void PressureLogger::onEvent(sensor_data_t *eventData)
74 {
75         uint64_t receivedTime = getTime();
76         __record(eventData, receivedTime);
77         removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME);
78 }
79
80 void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime)
81 {
82         char buffer[64];
83         g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),",
84                         getTime(eventData->timestamp), eventData->values[0]);
85
86         __insertionQuery += buffer;
87
88         if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH)
89                 return;
90
91         __insertionQuery.resize(__insertionQuery.size() - 1);
92         if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
93                 executeQuery(__insertionQuery.c_str());
94
95         __lastInsertionTime = receivedTime;
96         __resetInsertionQuery();
97 }
98
99 void PressureLogger::__resetInsertionQuery()
100 {
101         __insertionQuery =
102                 "INSERT INTO " PRESSURE_RECORD \
103                         " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES ";
104 }