-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
245 lines (213 loc) · 8.5 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
import sqlite3 as lite
from enum import Enum
from PyQt5.QtGui import QPixmap
class KEYS(Enum):
LOCATION = 'location'
CARID = 'carid'
CARCOLOR = 'carcolor'
FIRSTSIGHTED = 'firstsighted'
CARIMAGE = 'carimage'
LICENSENUMBER = 'licensenumber'
LICENSEIMAGE = 'licenseimage'
NUMRULESBROKEN = 'numrulesbroken'
CAROWNER = 'carowner'
RULENAME = 'rulename'
RULEFINE = 'rulefine'
TIME = 'time'
RULEID = 'ruleid'
class Database:
__instance = None
@staticmethod
def get_instance():
if Database.__instance is None:
Database()
return Database.__instance
def __init__(self):
if Database.__instance is not None:
raise Exception("This class is a singleton!")
else:
Database.__instance = self
self.con = lite.connect("database/traffic.db")
def get_car_color_list(self):
command = "select distinct(color) from cars"
rows = self.con.cursor().execute(command).fetchall()
return [row[0] for row in rows]
def get_licenses(self):
command = "select license_number from cars"
rows = self.con.cursor().execute(command).fetchall()
return [row[0] for row in rows]
def insert_into_cars(self, car_id='', color='', lic_num='', lic_img='', car_img='', owner=''):
sql = '''INSERT INTO cars(id, color,license_image, license_number, car_image, owner)
VALUES(?,?,?,?,?,?) '''
car_img = car_img.split('/')[-1]
lic_img = lic_img.split('/')[-1]
cur = self.con.cursor()
cur.execute(sql, (car_id, color, lic_num, lic_img, car_img, owner))
cur.close()
self.con.commit()
def get_max_car_id(self):
sql = '''select max(id) from cars'''
carid = self.con.cursor().execute(sql).fetchall()[0][0]
if carid is None:
carid = 1
return carid
def insert_into_violations(self, camera, car, rule, time):
sql = '''INSERT INTO violations(camera, car, rule, time)
VALUES(?,?,?,?) '''
cur = self.con.cursor()
cur.execute(sql, (camera, car, rule, self.covert_time_to_bd(time)))
cur.close()
self.con.commit()
def insert_into_rules(self, rule, fine):
sql = '''INSERT INTO rules(name, fine)
VALUES(?,?) '''
cur = self.con.cursor()
cur.execute(sql, (rule, fine))
cur.close()
self.con.commit()
def insert_into_camera(self, id, location, x, y, group, file):
sql = '''INSERT INTO camera(id,location,coordinate_x, coordinate_y, feed, cam_group)
VALUES(?,?,?,?,?,?) '''
file = file.split('/')[-1]
cur = self.con.cursor()
cur.execute(sql, (id, location, x, y, file, group))
cur.close()
self.con.commit()
def search(self, cam=None, color=None, license=None, time=None):
cur = self.con.cursor()
command = "SELECT camera.location, cars.id, cars.color, cars.first_sighted, cars.license_image, " \
" cars.license_number, cars.car_image, cars.num_rules_broken, cars.owner," \
" rules.name, rules.fine, violations.time, rules.id" \
" FROM violations, rules, cars, camera" \
" where rules.id = violations.rule" \
" and violations.camera = camera.id" \
" and cars.id = violations.car"
if cam is not None:
command = command + " and violations.camera = '" + str(cam) + "'"
if color is not None:
command = command + " and cars.color = '" + str(color) + "'"
if time is not None:
command = command + " and violations.time >= " + str(
self.covert_time_to_bd(time[0])) + " and violations.time <= " + str(self.covert_time_to_bd(time[1]))
cur.execute(command)
rows = cur.fetchall()
ret = []
for row in rows:
ret.append({
KEYS.LOCATION: row[0],
KEYS.CARID: row[1],
KEYS.CARCOLOR: row[2],
KEYS.FIRSTSIGHTED: row[3],
KEYS.CARIMAGE: QPixmap("car_images/" + row[4]),
KEYS.LICENSENUMBER: row[5],
KEYS.LICENSEIMAGE: QPixmap("license_images/" + row[6]),
KEYS.NUMRULESBROKEN: row[7],
KEYS.CAROWNER: row[8],
KEYS.RULENAME: row[9],
KEYS.RULEFINE: row[10],
KEYS.TIME: row[11],
KEYS.RULEID: row[12],
})
cur.close()
return ret
def get_violations_from_cam(self, cam, cleared=False):
cur = self.con.cursor()
command = "SELECT camera.location, cars.id, cars.color, cars.first_sighted, cars.license_image, " \
" cars.license_number, cars.car_image, cars.num_rules_broken, cars.owner," \
" rules.name, rules.fine, violations.time, rules.id" \
" FROM violations, rules, cars, camera" \
" where rules.id = violations.rule" \
" and cars.id = violations.car" \
" and violations.camera = camera.id"
if cam is not None:
command = command + " and violations.camera = '" + str(cam) + "'"
if cleared:
command = command + " and violations.cleared = true"
else:
command = command + " and violations.cleared = false"
cur.execute(command)
rows = cur.fetchall()
ret = []
for row in rows:
ret.append({
KEYS.LOCATION: row[0],
KEYS.CARID: row[1],
KEYS.CARCOLOR: row[2],
KEYS.FIRSTSIGHTED: row[3],
KEYS.CARIMAGE: QPixmap("car_images/" + row[6]),
KEYS.LICENSENUMBER: row[5],
KEYS.LICENSEIMAGE: QPixmap("license_images/" + row[6]),
KEYS.NUMRULESBROKEN: row[7],
KEYS.CAROWNER: row[8],
KEYS.RULENAME: row[9],
KEYS.RULEFINE: row[10],
KEYS.TIME: row[11],
KEYS.RULEID: row[12],
})
cur.close()
return ret
def delete_violation(self, carid, ruleid, time):
cur = self.con.cursor()
command = "UPDATE violations SET cleared = true WHERE car = ? AND rule = ? AND time = ?"
rowcount = cur.execute(command, (carid, ruleid, time)).rowcount
print("Deleted " + str(rowcount) + " rows")
cur.close()
self.con.commit()
def get_cam_details(self, cam_id):
command = "select count(*) from violations where camera = '" + str(cam_id) + "'"
cur = self.con.cursor()
count = cur.execute(command).fetchall()[0][0]
cur.close()
command = "select location, feed from camera where id = '" + str(cam_id) + "'"
cur = self.con.cursor()
res = cur.execute(command).fetchall()
if not res:
# Handle the case when the result is empty
location = None
feed = None
else:
# Unpack the first tuple from the result and assign to location and feed
location, feed = res[0]
cur.close()
return count, location, feed
def delete_all_cars(self):
commad = "delete from cars"
cur = self.con.cursor()
cur.execute(commad)
cur.close()
self.con.commit()
def delete_all_violations(self):
commad = "delete from violations"
cur = self.con.cursor()
cur.execute(commad)
cur.close()
self.con.commit()
def get_cam_list(self, group):
if group is not None:
command = "select id, location, feed from camera where cam_group = '{}'".format(str(group))
else:
command = "select id, location, feed from camera"
cur = self.con.cursor()
cur.execute(command)
rows = cur.fetchall()
ret = [(row[0], row[1], row[2]) for row in rows]
cur.close()
return ret
def get_cam_group_list(self):
command = "select name from camera_group"
cur = self.con.cursor()
cur.execute(command)
rows = cur.fetchall()
ret = [row[0] for row in rows]
cur.close()
return ret
def clear_cam_log(self):
command = "update violations set cleared = true"
cur = self.con.cursor()
cur.execute(command)
cur.close()
self.con.commit()
def covert_time_to_bd(self, time):
pass
def convert_time_to_GUI(self, time):
pass