-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
278 lines (233 loc) · 9.53 KB
/
database.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import ast
import json
import mysql.connector.cursor
import mysql.connector.errors
from dbutils.pooled_db import PooledDB
import config
import time
class Database:
def __init__(self):
self.connection_pool = PooledDB(mysql.connector, 5,
host=config.MySql.host,
port=config.MySql.port,
user=config.MySql.user,
password=config.MySql.password,
db=config.MySql.db,
buffered=True
)
self.connection_pool.connection().cursor().execute("SET NAMES UTF8")
@staticmethod
def get_sql_from_file():
"""
Reads and parses SQL queries from provided .sql file.
"""
from os import path
file = "database.sql"
# File did not exists
if path.isfile(file) is False:
print("Unable to open sql file '{}'.".format(file))
return False
else:
with open(file, "r") as sql_file:
query = sql_file.read().split(';')
query.pop()
return query
def prepare_database(self) -> None:
"""
Utilizes SQL from 'database.sql' to create all needed
tables automatically.
"""
connection = mysql.connector.connect(
host=config.MySql.host,
port=config.MySql.port,
user=config.MySql.user,
password=config.MySql.password,
buffered=True
)
with connection.cursor() as cursor:
try:
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {config.MySql.db}")
except mysql.connector.Error as e:
raise e
connection.commit()
connection.close()
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
# SQL queries as a list
queries = self.get_sql_from_file()
for query in queries:
cursor.execute(query)
# Commit changes and close connection
con.commit()
con.close()
return
def get_data(self, from_date, to_date):
"""
Reads the data records of specified time range.
Parameters
----------
from_date : str
datetime
Specifies starting point of query
to_date : str
datetime
Specifies end point of query
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
cursor.execute("SELECT * FROM `data` WHERE DATE(`measured`) BETWEEN '%s' AND '%s'" % (from_date, to_date))
result = cursor.fetchall()
con.close()
return result
def insert_data(self, temperature, weight, humidity):
"""
Inserts a new dataset into the database.
Parameters
----------
temperature : str
float
Current temperature
weight : str
float
Current weight
humidity : str
float
Current humidity
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
cursor.execute("SELECT * FROM data ORDER BY number DESC LIMIT 1")
res = cursor.fetchone()
if res is not None and res["weight"] is None:
sql = "UPDATE data SET `temperature` = '%s', `weight` = '%s', `humidity` = '%s' WHERE number = '%s'" % (float(temperature), float(weight) * config.correction[0] - config.real_tare[0], float(humidity), res["number"])
else:
sql = "INSERT INTO `data` (`number`, `temperature`, `weight`, `humidity`, `measured`) VALUES (0, %s, %s, %s, '%s')" % (float(temperature), float(weight) * config.correction[0] - config.real_tare[0], float(humidity), date)
log = open("logs/insert.log", mode="a")
log.write("\n[%s] - %s" % (time.asctime(), sql))
log.close()
cursor.execute(sql)
con.commit()
con.close()
return
def scales(self, number, weight):
"""
Inserts a new dataset for a specific scale into the database.
A collumn with the name of "number" must exist!
Parameters
----------
number : str
The unique number of the scale
weight : str
float
Current weight
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
cursor.execute("SELECT * FROM data ORDER BY number DESC LIMIT 1")
res = cursor.fetchone()
cursor.execute("DESCRIBE data")
fields = cursor.fetchall()
fields = [str(x["Field"]) for x in fields]
if number not in fields:
return "No column in database for this scale"
if res is not None and res[number] is None:
sql = "UPDATE data SET `%s` = '%s' WHERE number = '%s'" % (number, weight, res['number'])
else:
sql = "INSERT INTO `data` (`%s`, `measured`) VALUES (%s, '%s')" % (number, weight, date)
print(sql)
cursor.execute(str(sql))
con.commit()
con.close()
log = open("logs/insert.log", mode="a")
log.write("\n[%s] - %s" % (time.asctime(), sql))
log.close()
return
def insert_feed(self, feed_name, data):
"""
Inserts a new feed item into the database.
Parameters
----------
feed_name : str
Name of the feed.
Currently only "data", "admin", or "warning".
data : dict
The data that should get inserted.
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
insert = json.dumps(data, ensure_ascii=False).encode('utf8')
cursor.execute(f"INSERT INTO `notifications` (`feed`, `data`) VALUES (%s, %s)", [feed_name, insert])
con.commit()
con.close()
return True
def get_feed(self, feed_name):
"""
Gets a feed from the database.
Parameters
----------
feed_name : str
Name of the feed.
Currently only "data", "admin", or "warning".
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
cursor.execute(f"SELECT `id`, `data` FROM `notifications` WHERE `feed`='{feed_name}'")
feed = []
for item in cursor.fetchall():
item["data"] = json.loads(item["data"])
feed.append({
"time": item["data"]["time"],
"title": bytes(item["data"]["title"], "utf8").decode("utf8"),
"text": bytes(item["data"]["text"], "utf8").decode("utf8"),
"id": item["id"]
})
con.close()
feed.reverse()
return feed
def set_telegram_subscription(self, chat_id, feed_name, subscribe):
"""
Changes the feed subscriptions of a telegram chat.
Parameters
----------
chat_id : str
Chat ID of telegram chat. message.chat.id
feed_name : str
Name of the feed to change subscription. "data", "admin", "warning"
subscribe : bool
Specify whether to recieve updates on that feed.
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
cursor.execute(f"SELECT * FROM `subscriptions` WHERE `telegram_id`='{chat_id}'")
if cursor.fetchone() is None:
cursor.execute(f"INSERT INTO `subscriptions` (`telegram_id`) VALUES ({chat_id})")
cursor.execute(f"UPDATE `subscriptions` SET `{feed_name}_feed`='{1 if subscribe else 0}' WHERE `telegram_id`='{chat_id}'")
con.commit()
con.close()
return True
def check_telegram_subscription(self, chat_id, feed_name):
"""
Checks the feed subscriptions of a telegram chat.
Parameters
----------
chat_id : str
Chat ID of telegram chat. message.chat.id
feed_name : str
Name of the feed to check subscription for. "data", "admin", "warning"
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
cursor.execute(f"SELECT * FROM `subscriptions` WHERE `telegram_id`='{chat_id}'")
if cursor.fetchone()[f"{feed_name}_feed"] == 1:
con.close()
return True
else:
con.close()
return False
def get_telegram_subscriptions(self, feed_name):
"""
Gets all chats who have subscribed to a feed.
Parameters
----------
feed_name : str
Name of the feed to check subscription for. "data", "admin", "warning"
"""
with self.connection_pool.connection() as con, con.cursor(dictionary=True) as cursor:
cursor.execute(f"SELECT * FROM `subscriptions` WHERE `{feed_name}_feed`='1'")
res = cursor.fetchall()
con.close()
return [x["telegram_id"] for x in res]