forked from JustReit/Spotlight_Storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
491 lines (395 loc) · 15.3 KB
/
db.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import json
import os
import shutil
import sqlite3
from collections import Counter
# Define the path for the combined database
COMBINED_DATABASE = 'data/combined_data.db'
def create_combined_db():
# Ensure the 'data' directory exists
if not os.path.exists(os.path.dirname(COMBINED_DATABASE)):
os.makedirs(os.path.dirname(COMBINED_DATABASE)) # Connect to the combined database
conn_combined = sqlite3.connect(COMBINED_DATABASE)
conn_combined.row_factory = sqlite3.Row
# Create items table in the combined database
conn_combined.execute('''
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
link TEXT,
image TEXT,
position TEXT,
quantity INTEGER,
ip TEXT,
tags TEXT
)
''')
# Create esp table in the combined database
conn_combined.execute('''
CREATE TABLE IF NOT EXISTS esp (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
esp_ip TEXT,
rows INTEGER,
cols INTEGER,
start_top TEXT,
start_left TEXT,
serpentine_direction TEXT
)
''')
# Create settings table in the combined database
conn_combined.execute('''
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
brightness INTEGER DEFAULT 100,
timeout INTEGER DEFAULT 5,
lightMode TEXT DEFAULT 'light',
colors TEXT DEFAULT '[#00ff00, #00ff00]',
language TEXT DEFAULT en
)
''')
# Commit the changes
conn_combined.commit()
# Check and add new columns if they don't exist
cursor = conn_combined.cursor()
# Check for the existence of 'colors' column
cursor.execute("PRAGMA table_info(settings)")
columns = [column[1] for column in cursor.fetchall()]
if 'colors' not in columns:
cursor.execute("ALTER TABLE settings ADD COLUMN colors TEXT DEFAULT '[#00ff00, #00ff00]'")
conn_combined.commit()
if 'language' not in columns:
cursor.execute("ALTER TABLE settings ADD COLUMN language TEXT DEFAULT 'en'")
conn_combined.commit()
return conn_combined
# Function to read the data from the database
def read_items():
conn = create_combined_db()
items = conn.execute('SELECT * FROM items').fetchall()
conn.close()
return [dict(item) for item in items]
def write_item(item):
conn = create_combined_db()
cursor = conn.cursor()
cursor.execute('INSERT INTO items (name, link, image, position, quantity, ip, tags) VALUES (?, ?, ?, ?, ?, ?, ?)',
[item['name'], item['link'], item['image'], item['position'], item['quantity'], item['ip'],
item['tags']])
lastId = cursor.lastrowid
conn.commit()
conn.close()
return lastId
def update_item_image(item_id, new_image_url):
conn = create_combined_db()
try:
cursor = conn.cursor()
# Update the image of the item with the specified item_id
cursor.execute('UPDATE items SET image = ? WHERE id = ?', [new_image_url['image'], item_id])
conn.commit()
except sqlite3.Error as e:
conn.rollback()
print(e)
finally:
conn.close()
# Function to update data in the database
def update_item(id, data):
conn = create_combined_db()
try:
conn.execute(
'UPDATE items SET name = ?, link = ?, image = ?, position = ?, quantity = ?, ip = ?, tags = ? WHERE id = ?',
[data['name'], data['link'], data['image'], data['position'], data['quantity'], data['ip'], data['tags'],
id])
conn.commit()
except sqlite3.Error as e:
conn.rollback()
finally:
conn.close()
def update_item_quantity(id, data):
conn = create_combined_db()
try:
conn.execute(
'UPDATE items SET quantity = ? WHERE id = ?',
[data['quantity'], id])
conn.commit()
except sqlite3.Error as e:
conn.rollback()
print(e)
finally:
conn.close()
def get_item(id):
conn = create_combined_db()
item = conn.execute('SELECT * FROM items WHERE id = ?', [id]).fetchone()
conn.close()
return dict(item) if item else None
def delete_item(id):
conn = create_combined_db()
conn.execute('DELETE FROM items WHERE id = ?', [id])
conn.commit()
conn.close()
# Function to write ESP settings to the database
def write_esp_settings(esp_settings):
required_fields = ['name', 'esp_ip', 'rows', 'cols', 'startTop', 'startLeft', 'serpentineDirection']
if not all(field in esp_settings for field in required_fields):
print("Missing required fields in esp_settings")
return None
conn = create_combined_db()
try:
cursor = conn.cursor()
cursor.execute(
'INSERT INTO esp (name, esp_ip, rows, cols, start_top, start_left, serpentine_direction) VALUES (?, ?, ?, ?, ?, ?, ?)',
[
esp_settings['name'],
esp_settings['esp_ip'],
esp_settings['rows'],
esp_settings['cols'],
esp_settings['startTop'],
esp_settings['startLeft'],
esp_settings['serpentineDirection']
])
lastId = cursor.lastrowid
conn.commit()
except Exception as e:
print(f"Database error: {e}")
conn.rollback()
lastId = None
finally:
conn.close()
return lastId
# Function to update ESP settings in the database
# Function to update ESP settings in the database
def update_esp_settings(id, esp_settings):
conn = create_combined_db()
try:
conn.execute(
'UPDATE esp SET name = ?, esp_ip = ?, rows = ?, cols = ?, start_top = ?, start_left = ?, serpentine_direction = ? WHERE id = ?',
[
esp_settings['name'],
esp_settings['esp_ip'],
esp_settings['rows'],
esp_settings['cols'],
esp_settings['startTop'],
esp_settings['startLeft'],
esp_settings['serpentineDirection'],
id
])
conn.commit()
except sqlite3.Error as e:
conn.rollback()
finally:
conn.close()
# Function to get ESP settings from the database by ID
def get_esp_settings(id):
conn = create_combined_db()
esp_settings = conn.execute('SELECT * FROM esp WHERE id = ?', [id]).fetchone()
conn.close()
if esp_settings:
return dict(esp_settings)
else:
return None # Return None if no matching settings are found
def read_esp():
conn = create_combined_db()
esps = conn.execute('SELECT * FROM esp').fetchall()
conn.close()
return [dict(esp) for esp in esps]
# Function to delete ESP settings from the database by ID
def delete_esp_settings(id):
conn = create_combined_db()
try:
conn.execute('DELETE FROM esp WHERE id = ?', [id])
conn.commit()
except sqlite3.Error as e:
conn.rollback()
finally:
conn.close()
def get_esp_settings_by_id(id):
conn = create_combined_db() # Get a database connection
try:
cursor = conn.cursor()
cursor.execute('SELECT * FROM esp WHERE id = ?', (id,))
row = cursor.fetchone()
if row is None:
return None # No record found for the given IP
# Convert the row to a dictionary
esp_settings = {col[0]: row[idx] for idx, col in enumerate(cursor.description)}
return esp_settings
except Exception as e:
print(f"Database error: {e}")
return None
finally:
conn.close()
def get_esp_settings_by_ip(ip):
conn = create_combined_db() # Get a database connection
try:
cursor = conn.cursor()
cursor.execute('SELECT * FROM esp WHERE esp_ip = ?', (ip,))
row = cursor.fetchone()
if row is None:
return None # No record found for the given IP
# Convert the row to a dictionary
esp_settings = {col[0]: row[idx] for idx, col in enumerate(cursor.description)}
return esp_settings
except Exception as e:
print(f"Database error: {e}")
return None
finally:
conn.close()
def get_ip_by_name(esp_name):
conn = create_combined_db()
esp = conn.execute('SELECT esp_ip FROM esp WHERE name = ?', (esp_name,)).fetchone()
conn.close()
return esp['esp_ip'] if esp else None
# Function to read settings from the database
# Function to read settings from the database
def read_settings():
conn = create_combined_db()
try:
cursor = conn.cursor()
cursor.execute('SELECT * FROM settings')
settings = cursor.fetchone()
if settings is None:
print("No settings found in the database.")
return {}
else:
# Convert the settings row to a dictionary
settings_dict = dict(zip([column[0] for column in cursor.description], settings))
# Deserialize the colors field if it exists
if 'colors' in settings_dict:
settings_dict['colors'] = json.loads(settings_dict['colors'])
else:
print("No 'colors' field found in the settings.")
return settings_dict
except sqlite3.Error as e:
print(f"SQLite error while reading settings: {e}")
return {}
finally:
conn.close()
# Function to update settings in the database
def update_settings(settings):
try:
# Serialize the colors list to a JSON string
settings['colors'] = json.dumps(settings['colors'])
conn = create_combined_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM settings') # Clear existing settings
cursor.execute('''
INSERT INTO settings (brightness, timeout, lightMode, colors, language)
VALUES (?, ?, ?, ?, ?)
''', [settings['brightness'], settings['timeout'], settings['lightMode'], settings['colors'], settings['language']])
conn.commit()
except sqlite3.Error as e:
print(f"SQLite error while updating settings: {e}")
finally:
conn.close()
def get_all_tags():
conn = create_combined_db()
cursor = conn.cursor()
try:
# Fetch all distinct tags from the items table
cursor.execute('SELECT tags FROM items')
raw_tags = [tag['tags'] for tag in cursor.fetchall() if tag['tags']]
# Parse the JSON strings representing lists
tags = [tag for raw_tag in raw_tags for tag in json.loads(raw_tag)]
# Count the occurrences of each tag
tag_counts = Counter(tags)
unique_tags_with_count = [{'tag': tag, 'count': count} for tag, count in tag_counts.items()]
unique_tags_with_count.sort(key=lambda x: x['count'], reverse=True)
return unique_tags_with_count
finally:
conn.close()
# Migration only needed if you are coming from an older version.
DATABASE = 'data.db'
DATABASE_ESP = 'esp.db'
DATABASE_SETTING = 'settings.db'
def get_db_connection(database_name):
"""Function to get a database connection."""
conn = sqlite3.connect(database_name)
conn.row_factory = sqlite3.Row
return conn
def migrate_items():
"""Migrate items from data.db to combined_data.db."""
conn_data = get_db_connection(DATABASE)
items = conn_data.execute('SELECT * FROM items').fetchall()
conn_combined = sqlite3.connect(COMBINED_DATABASE)
for item in items:
# Extract only the first 7 columns plus the 'tags' column
columns_to_insert = [
item['name'], item['link'], item['image'],
item['position'], item['quantity'], item['ip'], item['tags']
]
conn_combined.execute(
'INSERT INTO items (name, link, image, position, quantity, ip, tags) VALUES (?, ?, ?, ?, ?, ?, ?)',
columns_to_insert
)
conn_combined.commit()
conn_data.close()
conn_combined.close()
def migrate_esp_settings():
"""Migrate ESP settings from esp.db to combined_data.db."""
conn_esp = get_db_connection(DATABASE_ESP)
esp_settings_list = conn_esp.execute('SELECT * FROM esp').fetchall()
conn_combined = sqlite3.connect(COMBINED_DATABASE)
for esp_settings in esp_settings_list:
conn_combined.execute(
'INSERT INTO esp (name, esp_ip, rows, cols, start_top, start_left, serpentine_direction) VALUES (?, ?, ?, ?, ?, ?, ?)',
[esp_settings['name'], esp_settings['esp_ip'], esp_settings['rows'], esp_settings['cols'],
esp_settings['start_top'], esp_settings['start_left'], esp_settings['serpentine_direction']]
)
conn_combined.commit()
conn_esp.close()
conn_combined.close()
def migrate_settings():
"""Migrate general settings from settings.db to combined_data.db."""
conn_settings = get_db_connection(DATABASE_SETTING)
settings = conn_settings.execute('SELECT * FROM settings').fetchone()
conn_combined = sqlite3.connect(COMBINED_DATABASE)
conn_combined.execute(
'INSERT INTO settings (brightness, timeout, lightMode) VALUES (?, ?, ?)',
[settings['brightness'], settings['timeout'], settings['lightMode']]
)
conn_combined.commit()
conn_settings.close()
conn_combined.close()
def is_database_empty(database_name, table_name):
"""Check if a table in a database is empty."""
conn = get_db_connection(database_name)
cursor = conn.cursor()
cursor.execute(f'SELECT COUNT(*) FROM {table_name}')
count = cursor.fetchone()[0]
conn.close()
return count
def should_perform_migration(database_path, table_name):
"""Check if migration should be performed for a specific database and table."""
# Check if the individual database exists
if os.path.exists(database_path):
# Check if the combined database is not empty for the specified table
return not is_database_empty(COMBINED_DATABASE, table_name)
return False
def perform_migration():
"""Perform migration."""
create_combined_db()
# Check and migrate items
if should_perform_migration(DATABASE, 'items'):
migrate_items()
print("Items migration successful.")
# Check and migrate ESP settings
if should_perform_migration(DATABASE_ESP, 'esp'):
migrate_esp_settings()
print("ESP settings migration successful.")
# Check and migrate general settings
if should_perform_migration(DATABASE_SETTING, 'settings'):
migrate_settings()
print("General settings migration successful.")
# Call the function to perform the check and move
move_db_to_data_dir()
def move_db_to_data_dir():
main_dir_db = 'combined_data.db' # The original path in the main directory
data_dir_db = 'data/combined_data.db' # The new path inside the data directory
# Check if the database exists in the main directory
if os.path.exists(main_dir_db):
# Ensure the data directory exists, create if it doesn't
if not os.path.exists('data'):
os.makedirs('data')
# Move the database to the data directory
shutil.move(main_dir_db, data_dir_db)
print(f"Database moved to {data_dir_db}")
# Perform migration
perform_migration()