-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfdriver.py
418 lines (393 loc) · 17.7 KB
/
fdriver.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
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
from main import *
from sheets import *
from MST import *
from clientss import *
import folium
from numpy import add, datetime64
from pyasn1_modules.rfc2459 import PrivateDomainName
import webview
import folium.plugins as plugins
import datetime
from fclient import *
import threading
import sys
pickDistUpdate=[]
pickTimeUpdate=[]
pickFromToUpdate=[]
dropDistUpdate=[]
dropTimeUpdate=[]
dropFromToUpdate=[]
address =[]
cnt =0
#THis is the driver side app
#GUI Main Window Setup
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(858, 858)
MainWindow.setStyleSheet("")
MainWindow.move(50,50)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lblDriverName = QtWidgets.QLabel(self.centralwidget)
self.lblDriverName.setGeometry(QtCore.QRect(110, 30, 291, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(16)
self.lblDriverName.setFont(font)
self.lblDriverName.setAlignment(QtCore.Qt.AlignCenter)
self.lblDriverName.setObjectName("lblDriverName")
self.txtDriverName = QtWidgets.QLineEdit(self.centralwidget)
self.txtDriverName.setGeometry(QtCore.QRect(120, 80, 271, 51))
font = QtGui.QFont()
font.setPointSize(12)
self.txtDriverName.setFont(font)
self.txtDriverName.setStyleSheet("border-radius:10px;")
self.txtDriverName.setObjectName("txtDriverName")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 150, 271, 61))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(16)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color:#4287f5;\n"
"border-radius: 25px;\n"
"color: white;")
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.submitDriverInfo)
self.lblKmDriven = QtWidgets.QLabel(self.centralwidget)
self.lblKmDriven.setGeometry(QtCore.QRect(30, 390, 131, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblKmDriven.setFont(font)
self.lblKmDriven.setObjectName("lblKmDriven")
self.lblTimeElapsed = QtWidgets.QLabel(self.centralwidget)
self.lblTimeElapsed.setGeometry(QtCore.QRect(30, 440, 141, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblTimeElapsed.setFont(font)
self.lblTimeElapsed.setObjectName("lblTimeElapsed")
self.lblGoingFrom = QtWidgets.QLabel(self.centralwidget)
self.lblGoingFrom.setGeometry(QtCore.QRect(30, 490, 131, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblGoingFrom.setFont(font)
self.lblGoingFrom.setObjectName("lblGoingFrom")
self.lblGoingTo = QtWidgets.QLabel(self.centralwidget)
self.lblGoingTo.setGeometry(QtCore.QRect(30, 540, 131, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblGoingTo.setFont(font)
self.lblGoingTo.setObjectName("lblGoingTo")
self.lblDashboard = QtWidgets.QLabel(self.centralwidget)
self.lblDashboard.setGeometry(QtCore.QRect(110, 310, 281, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(16)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.lblDashboard.setFont(font)
self.lblDashboard.setScaledContents(False)
self.lblDashboard.setAlignment(QtCore.Qt.AlignCenter)
self.lblDashboard.setObjectName("lblDashboard")
self.showKmDriven = QtWidgets.QLabel(self.centralwidget)
self.showKmDriven.setGeometry(QtCore.QRect(190, 390, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.showKmDriven.setFont(font)
self.showKmDriven.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showKmDriven.setAlignment(QtCore.Qt.AlignCenter)
self.showKmDriven.setObjectName("showKmDriven")
self.showTimeElapsed = QtWidgets.QLabel(self.centralwidget)
self.showTimeElapsed.setGeometry(QtCore.QRect(190, 440, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.showTimeElapsed.setFont(font)
self.showTimeElapsed.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showTimeElapsed.setAlignment(QtCore.Qt.AlignCenter)
self.showTimeElapsed.setObjectName("showTimeElapsed")
self.showGoingFrom = QtWidgets.QLabel(self.centralwidget)
self.showGoingFrom.setGeometry(QtCore.QRect(190, 490, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(8)
self.showGoingFrom.setFont(font)
self.showGoingFrom.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showGoingFrom.setAlignment(QtCore.Qt.AlignCenter)
self.showGoingFrom.setObjectName("showGoingFrom")
self.showGoingTo = QtWidgets.QLabel(self.centralwidget)
self.showGoingTo.setGeometry(QtCore.QRect(190, 540, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(8)
self.showGoingTo.setFont(font)
self.showGoingTo.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showGoingTo.setAlignment(QtCore.Qt.AlignCenter)
self.showGoingTo.setObjectName("showGoingTo")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(540, 40, 291, 751))
self.textBrowser.setStyleSheet("background-color:black;\n"
"color:white;")
self.textBrowser.setObjectName("textBrowser")
self.lblResults = QtWidgets.QLabel(self.centralwidget)
self.lblResults.setGeometry(QtCore.QRect(130, 620, 281, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(16)
self.lblResults.setFont(font)
self.lblResults.setObjectName("lblResults")
self.lblGasSaved = QtWidgets.QLabel(self.centralwidget)
self.lblGasSaved.setGeometry(QtCore.QRect(30, 690, 121, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblGasSaved.setFont(font)
self.lblGasSaved.setObjectName("lblGasSaved")
self.lblTimeSaved = QtWidgets.QLabel(self.centralwidget)
self.lblTimeSaved.setGeometry(QtCore.QRect(30, 750, 131, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.lblTimeSaved.setFont(font)
self.lblTimeSaved.setObjectName("lblTimeSaved")
self.showGasSaved = QtWidgets.QLabel(self.centralwidget)
self.showGasSaved.setGeometry(QtCore.QRect(190, 690, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.showGasSaved.setFont(font)
self.showGasSaved.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showGasSaved.setAlignment(QtCore.Qt.AlignCenter)
self.showGasSaved.setObjectName("showGasSaved")
self.showTimeSaved = QtWidgets.QLabel(self.centralwidget)
self.showTimeSaved.setGeometry(QtCore.QRect(190, 750, 311, 41))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(14)
self.showTimeSaved.setFont(font)
self.showTimeSaved.setStyleSheet("background-color: #4287f5;\n"
"color: white;\n"
"border-radius:10px;")
self.showTimeSaved.setAlignment(QtCore.Qt.AlignCenter)
self.showTimeSaved.setObjectName("showTimeSaved")
self.btnUpdate = QtWidgets.QPushButton(self.centralwidget)
self.btnUpdate.setGeometry(QtCore.QRect(120, 230, 271, 61))
font = QtGui.QFont()
font.setFamily("Product Sans")
font.setPointSize(16)
self.btnUpdate.setFont(font)
self.btnUpdate.setStyleSheet("background-color:#4287f5;\n"
"border-radius: 25px;\n"
"color: white;")
self.btnUpdate.setObjectName("btnUpdate")
self.btnUpdate.clicked.connect(self.updateInfo)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 858, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Part of GUI setup
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Driver Side Client"))
self.lblDriverName.setText(_translate("MainWindow", "Enter your Driver Name"))
self.pushButton.setText(_translate("MainWindow", "Submit"))
self.lblKmDriven.setText(_translate("MainWindow", "KM Driven:"))
self.lblTimeElapsed.setText(_translate("MainWindow", "Time Elapsed:"))
self.lblGoingFrom.setText(_translate("MainWindow", "Going From:"))
self.lblGoingTo.setText(_translate("MainWindow", "Going From:"))
self.lblDashboard.setText(_translate("MainWindow", "Dashboard"))
self.showKmDriven.setText(_translate("MainWindow", "0 km"))
self.showTimeElapsed.setText(_translate("MainWindow", "0 m"))
self.showGoingFrom.setText(_translate("MainWindow", "TBD"))
self.showGoingTo.setText(_translate("MainWindow", "TBD"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.lblResults.setText(_translate("MainWindow", "Sustainability Results:"))
self.lblGasSaved.setText(_translate("MainWindow", "Gas Saved:"))
self.lblTimeSaved.setText(_translate("MainWindow", "Time Saved:"))
self.showGasSaved.setText(_translate("MainWindow", "0"))
self.showTimeSaved.setText(_translate("MainWindow", "0"))
self.btnUpdate.setText(_translate("MainWindow", "Update"))
#Submits Driver info, selects suitable clients, finds distances and routes, and plots them on map
def submitDriverInfo(self):
global pickDistUpdate
global pickTimeUpdate
global pickFromToUpdate
global dropDistUpdate
global dropTimeUpdate
global dropFromToUpdate
global address
driverName = self.txtDriverName.text()
initSheets()
self.pText("Submitting...")
self.pText("Fetching data from Sheets Database API...")
[long,lat,address,destLat,destLong,destAddress] = getClients(driverName)
self.pText("Client Addresses...")
for i in address:
self.pText("-> "+str(i))
self.pText("Client Destinations...")
for i in destAddress:
self.pText("-> "+str(i))
self.pText("Fetching Client Locations and Data from database...")
lat.insert(0,'43.578842')
long.insert(0,'-79.683668')
address.insert(0,'1240 Eglinton Ave W, Mississauga, ON L5V 1N3')
destLat.insert(0,'43.578842')
destLong.insert(0,'-79.683668')
destAddress.insert(0,'1240 Eglinton Ave W, Mississauga, ON L5V 1N3')
initDistances(address,destAddress)
self.pText("Fetching data Google Maps Distance Matrix API...")
self.pText("Initialized client distances...")
pickUpGraph = createGraph(address,0)
self.pText("Creating Graph data structure using client locations...")
pickUpMst = minSpanningTree(pickUpGraph,len(address))
self.pText("Creating a Minimum Spanning Tree using Client Network...")
[pickUpRoute,pickDistUpdate,pickTimeUpdate,pickFromToUpdate] = getTravelRoute(pickUpMst,len(address),address,0)
self.pText("Client Pick Up Route:")
for i in pickUpRoute:
self.pText("-> "+ str(i))
self.pText("Finding Optimal Travel Route using depth-first search...")
mstClear()
print()
dropOffGraph = createGraph(destAddress,1)
self.pText("Creating graph for client destinations...")
dropOffMst = minSpanningTree(dropOffGraph,len(destAddress))
self.pText("Creating minimum spanning tree for client destinations...")
[dropOffRoute,dropDistUpdate,dropTimeUpdate,dropFromToUpdate] = getTravelRoute(dropOffMst,len(destAddress),destAddress,1)
self.pText("Client Drop Off Route:")
for i in dropOffRoute:
self.pText("-> "+ str(i))
self.pText("Finding Optimal Travel Route Using Client Destinations...")
mstClear()
#print(lat,"\n",long)
print(pickUpRoute,"\n",pickDistUpdate,"\n",pickTimeUpdate,"\n",pickFromToUpdate)
self.pText("Creating Interactive Map to show Travel Routes...")
self.drawRoute(lat = lat,long = long,travelRoute = pickUpRoute)
#Updates the realtime distance and time travelled info on the GUI
def updateInfo(self):
#threading.Timer(2.0, self.updateInfo).start()
global pickDistUpdate
global pickTimeUpdate
global pickFromToUpdate
global dropDistUpdate
global dropTimeUpdate
global dropFromToUpdate
global cnt
self.pText("Updating Realtime Data...")
curDist = float(self.showKmDriven.text()[:-2])
curTime = float(self.showTimeElapsed.text()[:-1])
if cnt >=len(dropDistUpdate):
[slowDist,slowTime] = calculateSlowCost(len(address))
timeSaved = abs(curTime-slowTime)
distSaved = abs(curDist-slowDist)
self.showTimeSaved.setText(str("{:.2f}".format(timeSaved))+" min")
self.showGasSaved.setText(str("{:.2f}".format(distSaved))+" km")
return 0
curDist += pickDistUpdate[cnt]
curTime += float(pickTimeUpdate[cnt][:-4])
curFrom = pickFromToUpdate[cnt][0]
curTo = pickFromToUpdate[cnt][1]
self.showKmDriven.setText(str("{:.2f}".format(curDist))+" km")
self.showTimeElapsed.setText(str("{:.2f}".format(curTime))+" m")
self.showGoingFrom.setText(curFrom)
self.showGoingTo.setText(curTo)
cnt+=1
#Draws the route taken by the driver on the map
def drawRoute(self,lat,long,travelRoute):
x = datetime.datetime.now()
lines = []
colors = ["red","blue","yellow","green","orange"]
for i in range(1,len(travelRoute)):
s = ""
idx = i%5
s = colors[idx]
a = x.strftime("%Y-%m-%d %H:%M:%S")
x+=datetime.timedelta(minutes=10)
b = x.strftime("%Y-%m-%d %H:%M:%S")
lines.append({
"coordinates":[
[float(long[travelRoute[i-1]]),float(lat[travelRoute[i-1]])],
[float(long[travelRoute[i]]),float(lat[travelRoute[i]])],
],
"dates": [a,b],
"color":s,
})
#pprint(lines)
m = folium.Map(location= [float(lat[travelRoute[0]]),float(long[travelRoute[0]])], zoom_start=12)
# Lon, Lat order.
features = [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": line["coordinates"],
},
"properties": {
"times": line["dates"],
"style": {
"color": line["color"],
"weight": line["weight"] if "weight" in line else 5,
},
},
}
for line in lines
]
print()
#lines[i]['coordinates'][0] = [lat,long]
#lines[i]['color']= 'red'
#lines[i]['dates'][0/1] = '2017-06-02T00:00:00'
plugins.TimestampedGeoJson(
{
"type": "FeatureCollection",
"features": features,
},
period="PT1M",
add_last_point=True,
).add_to(m)
m.save('x.html')
file = open("x.html", "r")
code = file.read()
webview.create_window('Interactive Map View', html=code)
webview.start()
#threading.Timer(2.0, self.updateInfo).start()
#Sets the console text (black box)
def pText(self,text):
s = self.textBrowser.toPlainText() + "\n" + text
self.textBrowser.setText(s)
#GUI main method
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())