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 "../TimeUtil.h"
22 #include "PressureLogger.h"
24 #define INSERTION_THRESHOLD 20000
25 #define SAMPLING_INTERVAL 60000
26 #define BATCH_LATENCY INT_MAX
27 #define MAX_QUERY_LENGTH 1000
31 PressureLogger::PressureLogger()
33 setSensor(PRESSURE_SENSOR);
35 setSamplingInterval(SAMPLING_INTERVAL);
36 setBatchLatency(BATCH_LATENCY);
38 /* Create the log table */
40 "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \
41 KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \
42 KEY_PRESSURE " REAL NOT NULL" \
45 ClientInfo clientInfo;
46 if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
50 PressureLogger::~PressureLogger()
55 bool PressureLogger::start()
57 IF_FAIL_RETURN_TAG(!isRunning(), true, _D, "Started already");
58 _I(GREEN("Start to record"));
60 __lastInsertionTime = TimeUtil::getTime();
61 __resetInsertionQuery();
66 void PressureLogger::stop()
68 IF_FAIL_VOID_TAG(isRunning(), _D, "Stopped already");
69 _I(GREEN("Stop recording"));
74 void PressureLogger::onEvent(sensor_data_t *eventData)
76 uint64_t receivedTime = TimeUtil::getTime();
77 __record(eventData, receivedTime);
78 removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME);
81 void PressureLogger::__record(sensor_data_t *eventData, uint64_t receivedTime)
84 g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),",
85 TimeUtil::getTime(eventData->timestamp), eventData->values[0]);
87 __insertionQuery += buffer;
89 if (receivedTime - __lastInsertionTime < INSERTION_THRESHOLD && __insertionQuery.size() < MAX_QUERY_LENGTH)
92 __insertionQuery.resize(__insertionQuery.size() - 1);
93 if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
94 executeQuery(__insertionQuery.c_str());
96 __lastInsertionTime = receivedTime;
97 __resetInsertionQuery();
100 void PressureLogger::__resetInsertionQuery()
103 "INSERT INTO " PRESSURE_RECORD \
104 " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES ";