5f2f45963463a5a9a1846e3f84462b7fd599d6ec
[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 "../TimeUtil.h"
22 #include "PressureLogger.h"
23
24 #define INSERTION_THRESHOLD     20000
25 #define SAMPLING_INTERVAL       60000
26 #define BATCH_LATENCY           INT_MAX
27 #define MAX_QUERY_LENGTH        1000
28
29 using namespace ctx;
30
31 PressureLogger::PressureLogger()
32 {
33         setSensor(PRESSURE_SENSOR);
34         setPowerSave(false);
35         setSamplingInterval(SAMPLING_INTERVAL);
36         setBatchLatency(BATCH_LATENCY);
37
38         /* Create the log table */
39         executeQuery(
40                         "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \
41                                 KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \
42                                 KEY_PRESSURE " REAL NOT NULL" \
43                         ")");
44
45         ClientInfo clientInfo;
46         if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
47                 start();
48 }
49
50 PressureLogger::~PressureLogger()
51 {
52         stop();
53 }
54
55 bool PressureLogger::start()
56 {
57         IF_FAIL_RETURN_TAG(!isRunning(), true, _D, "Started already");
58         _I(GREEN("Start to record"));
59
60         __lastInsertionTime = TimeUtil::getTime();
61         __resetInsertionQuery();
62
63         return listen();
64 }
65
66 void PressureLogger::stop()
67 {
68         IF_FAIL_VOID_TAG(isRunning(), _D, "Stopped already");
69         _I(GREEN("Stop recording"));
70
71         unlisten();
72 }
73
74 void PressureLogger::onEvent(sensor_data_t *eventData)
75 {
76         uint64_t receivedTime = TimeUtil::getTime();
77         __record(eventData, receivedTime);
78         removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME);
79 }
80
81 void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime)
82 {
83         char buffer[64];
84         g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),",
85                         TimeUtil::getTime(eventData->timestamp), eventData->values[0]);
86
87         __insertionQuery += buffer;
88
89         if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH)
90                 return;
91
92         __insertionQuery.resize(__insertionQuery.size() - 1);
93         if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
94                 executeQuery(__insertionQuery.c_str());
95
96         __lastInsertionTime = receivedTime;
97         __resetInsertionQuery();
98 }
99
100 void PressureLogger::__resetInsertionQuery()
101 {
102         __insertionQuery =
103                 "INSERT INTO " PRESSURE_RECORD \
104                         " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES ";
105 }