Add sensor recorder provides (pedometer & pressure supported)
[platform/core/context/context-provider.git] / src / sensor / PedometerQuerier.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 "PedometerQuerier.h"
21
22 #define PROJECTION \
23         "SUM(" KEY_WALK_STEPS " + " KEY_RUN_STEPS ") AS " KEY_STEPS ", " \
24         "SUM(" KEY_WALK_STEPS ") AS " KEY_WALK_STEPS ", " \
25         "SUM(" KEY_RUN_STEPS ") AS " KEY_RUN_STEPS ", " \
26         "SUM(" KEY_DISTANCE ") AS " KEY_DISTANCE ", " \
27         "SUM(" KEY_CALORIES ") AS " KEY_CALORIES ", " \
28         "MIN(" KEY_START_TIME ") AS " KEY_START_TIME ", " \
29         "MAX(" KEY_END_TIME ") AS " KEY_END_TIME
30
31 using namespace ctx;
32
33 PedometerQuerier::PedometerQuerier(ContextProvider *provider, Json option) :
34         Querier(provider, option)
35 {
36 }
37
38 PedometerQuerier::~PedometerQuerier()
39 {
40 }
41
42 int PedometerQuerier::query(int startTime, int endTime)
43 {
44         char *sql = sqlite3_mprintf(
45                         "SELECT " PROJECTION \
46                         " FROM " PEDOMETER_RECORD \
47                         " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu",
48                         SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)));
49
50         int ret = Querier::query(sql);
51         sqlite3_free(sql);
52
53         return ret;
54 }
55
56 int PedometerQuerier::query(int startTime, int endTime, int anchor, int interval)
57 {
58         char *sql = sqlite3_mprintf(
59                         "SELECT " PROJECTION \
60                         " FROM " PEDOMETER_RECORD \
61                         " WHERE " KEY_END_TIME " > %llu AND " KEY_END_TIME " <= %llu" \
62                         " GROUP BY ROUND((" KEY_END_TIME " - %llu) / %llu + 0.5)" \
63                         " ORDER BY " KEY_END_TIME " ASC",
64                         SEC_TO_MS(static_cast<uint64_t>(startTime)), SEC_TO_MS(static_cast<uint64_t>(endTime)),
65                         SEC_TO_MS(static_cast<uint64_t>(anchor)), SEC_TO_MS(static_cast<uint64_t>(interval)));
66
67         int ret = Querier::query(sql);
68         sqlite3_free(sql);
69
70         return ret;
71 }