-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
251 lines (211 loc) · 8.43 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
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 getInstance():
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 getCarColorsList(self):
command = "select distinct(color) from cars"
rows = self.con.cursor().execute(command).fetchall()
return [row[0] for row in rows]
def getLicenseList(self):
command = "select license_number from cars"
rows = self.con.cursor().execute(command).fetchall()
return [row[0] for row in rows]
def insertIntoCars(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 getMaxCarId(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 insertIntoViolations(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.convertTimeToDB(time)))
cur.close()
self.con.commit()
def insertIntoRules(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 insertIntoCamera(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.convertTimeToDB(time[0])) + " and violations.time <= " + str(self.convertTimeToDB(time[1]))
cur.execute(command)
rows = cur.fetchall()
ret = []
for row in rows:
dict = {}
dict[KEYS.LOCATION] = row[0]
dict[KEYS.CARID] = row[1]
dict[KEYS.CARCOLOR] = row[2]
dict[KEYS.FIRSTSIGHTED] = row[3]
carimage = QPixmap("car_images/" + row[4])
dict[KEYS.CARIMAGE] = carimage
dict[KEYS.LICENSENUMBER] = row[5]
licenseimage = QPixmap("license_images/" + row[6])
dict[KEYS.LICENSEIMAGE] = licenseimage
dict[KEYS.NUMRULESBROKEN] = row[7]
dict[KEYS.CAROWNER] = row[8]
dict[KEYS.RULENAME] = row[9]
dict[KEYS.RULEFINE] = row[10]
dict[KEYS.TIME] = row[11]
dict[KEYS.RULEID] = row[12]
ret.append(dict)
cur.close()
return ret
def getViolationsFromCam(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) + "'"
cur.execute(command)
rows = cur.fetchall()
ret = []
for row in rows:
dict = {}
dict[KEYS.LOCATION] = row[0]
dict[KEYS.CARID] = row[1]
dict[KEYS.CARCOLOR] = row[2]
dict[KEYS.FIRSTSIGHTED] = row[3]
carImagePath = "car_images/" + row[6]
carimage = QPixmap(carImagePath)
dict[KEYS.CARIMAGE] = carimage
dict[KEYS.LICENSENUMBER] = row[5]
licenseimage = QPixmap("license_images/" + row[6])
dict[KEYS.LICENSEIMAGE] = licenseimage
dict[KEYS.NUMRULESBROKEN] = row[7]
dict[KEYS.CAROWNER] = row[8]
dict[KEYS.RULENAME] = row[9]
dict[KEYS.RULEFINE] = row[10]
dict[KEYS.TIME] = row[11]
dict[KEYS.RULEID] = row[12]
ret.append(dict)
cur.close()
return ret
def deleteViolation(self, carid, ruleid, time):
cur = self.con.cursor()
command = "update violations set cleared = true " \
"where car = " + str(carid) + " and rule = " + str(ruleid) + " and time = " + str(time)
rowcount = cur.execute(command).rowcount
print("Deleted " + str(rowcount) + " rows")
cur.close()
self.con.commit()
def getCamDetails(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()
location = None
feed = None
location, feed = res[0]
cur.close()
return count, location, feed
def deleteAllCars(self):
commad = "delete from cars"
cur = self.con.cursor()
cur.execute(commad)
cur.close()
self.con.commit()
def deleteAllViolations(self):
commad = "delete from violations"
cur = self.con.cursor()
cur.execute(commad)
cur.close()
self.con.commit()
def getCamList(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 getCamGroupList(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 clearCamLog(self):
command = "update violations set cleared = true"
cur = self.con.cursor()
cur.execute(command)
cur.close()
self.con.commit()
def convertTimeToDB(self, time):
pass
def convertTimeToGUI(self, time):
pass