-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_handler.py
212 lines (149 loc) · 4.28 KB
/
db_handler.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sqlite3
from sqlite3 import Error
import time
import os
import csv
db_file_location = os.path.join(os.getcwd(), "database.db")
def connect_db():
""" create a database connection to the SQLite database
specified by the db_file
"""
conn = None
try:
conn = sqlite3.connect(db_file_location)
except Error as e:
print(e)
return conn
def read_db(conn, query_str):
"""
Query all rows in the tasks table
"""
cur = conn.cursor()
cur.execute(query_str)
rows = cur.fetchall()
return rows
def write_db(conn, sql, values):
""" Write to database """
cur = conn.cursor()
cur.execute(sql, values)
conn.commit()
return cur.lastrowid
def create_table(conn, create_table_sql):
""" create a table from the create_table_sql statement """
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print("could not create a table")
print(e)
def create_beacons_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS beacons "
"( id integer PRIMARY KEY, "
"device_id text NOT NULL, "
"time_stamp int, "
"rssi int)"
)
create_table(conn, sql)
conn.close()
def create_pir_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS pir "
"( id integer PRIMARY KEY, "
"sensor_id int, "
"time_stamp int, "
"presence int)"
)
create_table(conn, sql)
conn.close()
def create_button_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS button "
"( id integer PRIMARY KEY, "
"time_stamp int, "
"status int)"
)
create_table(conn, sql)
conn.close()
def create_ultrasonic_calibration_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS calibration_ultrasonic "
"( id integer PRIMARY KEY, "
"time_stamp int, "
"distance real, "
"mean real, "
"std real)"
)
create_table(conn, sql)
conn.close()
def create_ultrasonic_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS ultrasonic "
"( id integer PRIMARY KEY, "
"time_stamp int, "
"distance real, "
"mean real, "
"std real)"
)
create_table(conn, sql)
conn.close()
def create_controller_table():
conn = connect_db()
sql = (
"CREATE TABLE IF NOT EXISTS control_signals "
"( id integer PRIMARY KEY, "
"time_stamp int, "
"sensor text, "
"light_type text, "
"signal int)"
)
create_table(conn, sql)
conn.close()
def add_fake_beacons_reading(_id, rssi):
sql = "INSERT INTO beacons(device_id, time_stamp, rssi) VALUES(?,?,?) "
reading = (_id, int(time.time()), rssi)
conn = connect_db()
ix = write_db(conn, sql, reading)
conn.close()
print(ix)
def main():
conn = connect_db()
# # query all data
# query = "SELECT * from beacons"
# rows = read_db(conn, query)
# # query only last entry by beacon id
# query_last_entry_by_id = (
# "SELECT device_id, rssi, MAX(time_stamp) " "FROM beacons " "GROUP BY device_id;"
# )
# rows = read_db(conn, query_last_entry_by_id)
# # query last entries rpi
# now = int(time.time()) - 60
# query_last_entry_by_id = f"SELECT * FROM pir WHERE time_stamp > {now}"
# rows = read_db(conn, query_last_entry_by_id)
# # query ultrasonic data
# query = f"SELECT * FROM calibration_ultrasonic"
# rows = read_db(conn, query)
#
# with open("ultrasonic_calibration.csv", "w", newline="") as csv_file:
# writer = csv.writer(
# csv_file, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
# )
# for row in rows:
# writer.writerow(row)
# query ultrasonic data
query = f"SELECT * FROM control_signals"
rows = read_db(conn, query)
with open("./control_signals_analysis/control_signals.csv", "w", newline="") as csv_file:
writer = csv.writer(
csv_file, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
)
for row in rows:
writer.writerow(row)
conn.close()
if __name__ == "__main__":
main()