-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataBase.py
More file actions
99 lines (78 loc) · 3.16 KB
/
Copy pathdataBase.py
File metadata and controls
99 lines (78 loc) · 3.16 KB
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
import psycopg2
from configparser import ConfigParser
from contextlib import contextmanager
@contextmanager
def get_db_connection():
try:
conn = psycopg2.connect(host="127.0.0.1",
user="postgres",
database="visionalarm",
password="root",
port="5432")
cursor = conn.cursor()
yield cursor, conn
except Exception as error:
print(f"Error while connecting to PostgreSQL: {error}")
finally:
conn.close()
def add_user(username, password):
with get_db_connection() as (cursor, conn):
query = "INSERT INTO users (username, password) VALUES (%s, %s)"
cursor.execute(query, (username, password))
conn.commit()
def add_camera(address, nom):
with get_db_connection() as (cursor, conn):
try:
query = "SELECT COUNT(*) FROM cameras"
cursor.execute(query)
number_cam = cursor.fetchone()[0]
if number_cam < 4:
query = "INSERT INTO cameras (address, nom) VALUES (%s, %s)"
cursor.execute(query, (address, nom))
conn.commit()
else:
print("Maximun number of cameras added already")
except Exception as e:
print(f"Error adding camera: {e}")
def remove_camera(id):
pass
def store_alert_data(alert_time, video_link, alert_class, alert_type):
query = f"INSERT INTO {alert_type}_alerts (alert_time, video_link, class) VALUES (%s, %s, %s)"
with get_db_connection() as (cursor, conn):
try:
cursor.execute(query, (alert_time, video_link, alert_class))
conn.commit()
except Exception as e:
print(f"Error inserting alert into {alert_type}_alerts: {e}")
def retrieve_alerts(alert_type):
query=f"SELECT * FROM {alert_type}_alerts"
with get_db_connection() as (cursor, conn):
cursor.execute(query)
alerts = cursor.fetchall()
print(f"{alert_type.capitalize()} alerts: \n ----------------------------------------------- \n")
for row in alerts:
print(f"ID: {row[0]} | Alert Time: {row[1]} | Video Link; {row[2]} | Class: {row[3]}")
def retrieve_all_alerts():
retrieve_alerts("fire")
retrieve_alerts("fall")
retrieve_alerts("robbery")
def retrieve_users():
# Establishing Connection to DB
with get_db_connection() as (cursor, conn):
try:
query = "select * from users"
cursor.execute(query)
result = cursor.fetchall()
for row in result:
print(f"ID: {row[0]} | Username: {row[1]}")
except Exception as e:
print(f"Error retrieving users: {e}")
def remove_camera(camera_id):
with get_db_connection() as (cursor, conn):
try:
query = "DELETE FROM cameras WHERE id = %s"
cursor.execute(query, (camera_id,))
conn.commit()
print(f"Camera with ID {camera_id} removed successfully.")
except Exception as e:
print(f"Error removing camera: {e}")