2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SensorRecorderTypes.h>
19 #include "../TypesInternal.h"
20 #include "../ClientInfo.h"
21 #include "PressureLogger.h"
23 #define INSERTION_THRESHOLD 20000
24 #define SAMPLING_INTERVAL 60000
25 #define BATCH_LATENCY INT_MAX
26 #define MAX_QUERY_LENGTH 1000
30 PressureLogger::PressureLogger()
32 setSensor(PRESSURE_SENSOR);
34 setSamplingInterval(SAMPLING_INTERVAL);
35 setBatchLatency(BATCH_LATENCY);
37 /* Create the log table */
39 "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \
40 KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \
41 KEY_PRESSURE " REAL NOT NULL" \
44 ClientInfo clientInfo;
45 if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
49 PressureLogger::~PressureLogger()
53 bool PressureLogger::start()
55 if (SensorProxy::isRunning())
58 _I(GREEN("Start to record"));
60 __lastInsertionTime = getTime();
61 __resetInsertionQuery();
63 return SensorProxy::start();
66 void PressureLogger::stop()
68 _I(GREEN("Stop recording"));
73 void PressureLogger::onEvent(sensor_data_t *eventData)
75 uint64_t receivedTime = getTime();
76 __record(eventData, receivedTime);
77 removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME);
80 void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime)
83 g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),",
84 getTime(eventData->timestamp), eventData->values[0]);
86 __insertionQuery += buffer;
88 if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH)
91 __insertionQuery.resize(__insertionQuery.size() - 1);
92 if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
93 executeQuery(__insertionQuery.c_str());
95 __lastInsertionTime = receivedTime;
96 __resetInsertionQuery();
99 void PressureLogger::__resetInsertionQuery()
102 "INSERT INTO " PRESSURE_RECORD \
103 " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES ";