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 SAMPLING_INTERVAL 60000
25 #define BATCH_LATENCY INT_MAX
27 #define RESAMPLING_INTERVAL 20000
28 #define CACHE_LIMIT 20
32 PressureLogger::PressureLogger()
34 setSensor(PRESSURE_SENSOR);
36 setSamplingInterval(SAMPLING_INTERVAL);
37 setBatchLatency(BATCH_LATENCY);
39 /* Create the log table */
41 "CREATE TABLE IF NOT EXISTS " PRESSURE_RECORD " (" \
42 KEY_UNIV_TIME " INTEGER NOT NULL PRIMARY KEY, " \
43 KEY_PRESSURE " REAL NOT NULL" \
46 ClientInfo clientInfo;
47 if (clientInfo.exist(SUBJ_SENSOR_PRESSURE))
51 PressureLogger::~PressureLogger()
56 bool PressureLogger::start()
58 IF_FAIL_RETURN_TAG(!isRunning(), true, _D, "Started already");
59 _I(GREEN("Start to record"));
63 __resetInsertionQuery();
68 void PressureLogger::stop()
70 IF_FAIL_VOID_TAG(isRunning(), _D, "Stopped already");
71 _I(GREEN("Stop recording"));
77 void PressureLogger::flushCache(bool force)
79 IF_FAIL_VOID(force || __cacheCount > CACHE_LIMIT);
81 __insertionQuery.resize(__insertionQuery.size() - 1);
82 if (__insertionQuery.at(__insertionQuery.size() - 1) == ')')
83 executeQuery(__insertionQuery.c_str());
86 __resetInsertionQuery();
89 void PressureLogger::onEvent(sensor_data_t *eventData)
91 uint64_t eventTime = TimeUtil::getTime(eventData->timestamp);
92 __record(eventData->values[0], eventTime);
93 removeExpired(SUBJ_SENSOR_PRESSURE, PRESSURE_RECORD, KEY_UNIV_TIME);
96 void PressureLogger::__record(float pressure, uint64_t eventTime)
98 IF_FAIL_VOID(eventTime - __lastEventTime >= RESAMPLING_INTERVAL);
101 g_snprintf(buffer, sizeof(buffer), "(%llu, %.5f),", eventTime, pressure);
102 _D("[%u] %s", __cacheCount, buffer);
104 __insertionQuery += buffer;
105 __lastEventTime = eventTime;
111 void PressureLogger::__resetInsertionQuery()
114 "INSERT INTO " PRESSURE_RECORD \
115 " (" KEY_UNIV_TIME ", " KEY_PRESSURE ") VALUES ";