-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_bme_680_sqlite3.hpp
128 lines (108 loc) · 4.45 KB
/
ble_bme_680_sqlite3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* BLE Sensor Service
*
* Copyright (C) 2020 Patrick K. Moffitt
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef SQLITE3_BLE_BME_680_SQLITE3_HPP
#define SQLITE3_BLE_BME_680_SQLITE3_HPP
#include <iostream>
#include <cstring>
#include <chrono>
#include <ctime>
#include <cmath>
#include <sqlite3.h>
#include "boost/filesystem.hpp"
#include "jsonxx/jsonxx.h"
#define EXIT_DB_DIRECTORY_ERROR 101
#define EXIT_DB_HANDLE_ERROR 102
#define EXIT_DB_PREP_STMT_SENSOR_DATA_TBL_COUNT 103
#define EXIT_DB_EXEC_STMT_SENSOR_TABLE_EXISTS 104
#define EXIT_DB_PREP_STMT_CREATE_SENSOR_TABLE 105
#define EXIT_DB_EXEC_STMT_CREATE_SENSOR_TABLE 106
#define EXIT_DB_PREP_STMT_CREATE_SENSOR_TABLE_IDX 107
#define EXIT_DB_EXEC_STMT_CREATE_SENSOR_TABLE_IDX 108
#define EXIT_DB_PREP_STMT_SENSOR_DATA_INSERT 109
#define EXIT_DB_EXEC_STMT_SENSOR_DATA_INSERT 110
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_E 111
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_T 112
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_H 113
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_P 114
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_G 115
#define EXIT_DB_PREP_STMT_SENSOR_DATA_BIND_B 116
#define EXIT_DB_PREP_STMT_GET_SENSOR_DATA_RANGE 117
#define EXIT_DB_EXEC_STMT_GET_SENSOR_DATA_RANGE 118
#define EXIT_DB_PREP_STMT_GET_SENSOR_DATA_RANGE_BIND_B 119
#define EXIT_DB_PREP_STMT_GET_SENSOR_DATA_RANGE_BIND_E 120
using namespace std;
using namespace chrono;
namespace std {
namespace filesystem = boost::filesystem;
}
namespace fs = std::filesystem;
using namespace jsonxx;
/*
* @brief Factory for sensor data operations.
*/
class Ble_bme_680_db {
public:
sqlite3 *db_h{nullptr};
Ble_bme_680_db(const char *sqlite3_db_dir,
const char *sqlite3_db_file);
~Ble_bme_680_db();
const char *sensor_table_exists_stmt =
R"(SELECT COUNT(*)
FROM sqlite_master
WHERE type='table'
AND name='sensor_data')";
const char *create_sensor_data_table_stmt =
R"(CREATE TABLE "main"."sensor_data" (
"epoch" integer NOT NULL,
"temperature_c" real,
"humidity_rh" real,
"pressure_kpa" real,
"gas_ohms" integer,
"battery_volts" real,
UNIQUE (epoch COLLATE BINARY DESC) ))";
const char *create_sensor_data_table_idx_stmt =
R"(CREATE INDEX "main"."epoch_idx"
ON "sensor_data"
("epoch" COLLATE BINARY DESC))";
const char *sensor_data_insert_stmt =
R"(INSERT OR REPLACE
INTO 'sensor_data'
('epoch', 'temperature_c',
'humidity_rh', 'pressure_kpa',
'gas_ohms', 'battery_volts')
VALUES (?, ?, ?, ?, ?, ?))";
const char *get_sensor_data_range_stmt =
R"(SELECT * FROM sensor_data
WHERE epoch <= ?
AND epoch >= ?
ORDER BY epoch ASC)";
bool sensor_table_exists() const;
void create_sensor_data_table() const;
void create_sensor_data_table_idx() const;
void sensor_data_insert(int32_t t, int8_t t_exp,
int32_t h, int8_t h_exp,
int32_t p, int8_t p_exp,
int32_t g, int8_t g_exp,
int32_t b, int8_t b_exp) const;
string get_sensor_data_range(int32_t epoch1, int32_t epoch2) const;
void on_error_exit(int error, int condition, int exit_code) const;
};
#endif //SQLITE3_BLE_BME_680_SQLITE3_HPP