-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainGui.py
218 lines (177 loc) · 7.6 KB
/
MainGui.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
# This Python file uses the following encoding: utf-8
import os
import sys
import time
import socket
from pathlib import Path
from ast import literal_eval
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import Signal, QObject, Slot, QPointF, QThread
from PySide6 import QtCharts
class gui():
def __init__(self,
HOST='127.0.0.1',
PORT=7000,
ChartBoundaryMaxY=1000,
ChartBoundaryMinY=-1000,
MaxSpeed=None,
MinSpeed=None,
MaxTTC=None,
MinTTC=None):
self.app = QApplication(sys.argv)
# app.setWindowIcon(QIcon("/src/icon/icon_app.ico"))
self.engine = QQmlApplicationEngine()
self.chart_component = self._ChartComponent(ChartBoundaryMaxY,
ChartBoundaryMinY)
self.speed_display = self._SpeedDisplay(MaxSpeed, MinSpeed)
self.ttc_display = self._TimeToCollisionDisplay(MaxTTC, MinTTC)
self.data_receive = self._DataReceiveThread(HOST, PORT)
self.data_receive.speed.connect(self.chart_component.get_data)
self.data_receive.speed.connect(self.speed_display.get_data)
self.data_receive.is_connected.connect(
self.speed_display.get_udp_status)
self.data_receive.ttc.connect(self.ttc_display.get_data)
self.data_receive.fcw.connect(self.ttc_display.get_fcw)
self.engine.rootContext().setContextProperty("chart_component",
self.chart_component)
self.engine.rootContext().setContextProperty("speed_display",
self.speed_display)
self.engine.rootContext().setContextProperty("ttc_display",
self.ttc_display)
self.engine.load(os.fspath(
Path(__file__).resolve().parent / "main.qml"))
def run(self):
if not self.engine.rootObjects():
return -1
self.data_receive.start()
ret = self.app.exec()
self.data_receive.requestInterruption()
self.data_receive.wait()
return ret
class _DataReceiveThread(QThread):
speed = Signal(float)
ttc = Signal(float)
fcw = Signal(bool)
# strr = Signal(str)
is_connected = Signal(bool)
def __init__(self, HOST='127.0.0.1', PORT=7000, parent=None):
super().__init__(parent)
self.HOST = HOST
self.PORT = PORT
os.system('cls' if os.name == 'nt' else 'clear')
def run(self):
time.sleep(1)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind((self.HOST, self.PORT))
s.setblocking(0)
s.settimeout(1.1)
while not self.isInterruptionRequested():
try:
# print('before recvfrom')
data, addr = s.recvfrom(225)
# print('after recvfrom')
data_dict = literal_eval(data.decode())
self.speed.emit(float(data_dict['VEL']))
self.ttc.emit(float(data_dict['TTC']))
self.fcw.emit(bool(data_dict['FCW']))
self.is_connected.emit(True)
# print(float(data_dict['VEL']))
except socket.timeout:
# print("closing socket: " + str(e))
self.speed.emit(0.0)
self.ttc.emit(0.0)
self.fcw.emit(False)
self.is_connected.emit(False)
# s.close()
except SyntaxError:
self.speed.emit(0.0)
self.ttc.emit(0.0)
self.fcw.emit(False)
self.is_connected.emit(False)
print("Error: received message format error")
except Exception:
self.speed.emit(0.0)
self.ttc.emit(0.0)
self.fcw.emit(False)
self.is_connected.emit(False)
print("Error: Unknown Error")
s.close()
class _ChartComponent(QObject):
def __init__(self,
BoundaryMaxY=1000,
BoundaryMinY=-1000,
parent=None):
super().__init__(parent)
self.maxY = BoundaryMaxY
self.minY = BoundaryMinY
self.XPoints = [0]*12
self.XYQPoints = [QPointF(i, self.XPoints[i])
for i in range(0, len(self.XPoints))]
# print("hello")
@Slot(QtCharts.QAbstractSeries, QtCharts.QAbstractAxis)
def update_series(self, series, axisY):
series.replace(self.XYQPoints)
if(self.XPoints and self.XPoints):
range = max(self.XPoints) - min(self.XPoints)
if range == 0:
range = 0.001
axisY.setMax(max(self.XPoints) + range*0.15)
axisY.setMin(min(self.XPoints) - range*0.15)
@Slot(float)
def get_data(self, speed):
if(speed > self.maxY):
speed = self.maxY
elif(speed < self.minY):
speed = self.minY
if len(self.XPoints) > 11:
self.XPoints.pop(0)
self.XPoints.append(speed)
self.XYQPoints = [QPointF(i, self.XPoints[i])
for i in range(0, len(self.XPoints))]
# print(self.XYQPoints)
class _SpeedDisplay(QObject):
speedSignal = Signal(float)
udpStatusSignal = Signal(bool)
def __init__(self,
MaxSpeed=None,
MinSpeed=None,
parent=None):
super().__init__(parent)
self.maxSpeed = MaxSpeed
self.minSpeed = MinSpeed
@Slot(float)
def get_data(self, speed):
if(self.maxSpeed is not None):
if(speed > self.maxSpeed):
speed = self.maxSpeed
if(self.minSpeed is not None):
if(speed < self.minSpeed):
speed = self.minSpeed
self.speedSignal.emit(speed)
@Slot(bool)
def get_udp_status(self, status):
self.udpStatusSignal.emit(status)
class _TimeToCollisionDisplay(QObject):
timeToCollisonSignal = Signal(float)
fcwSignal = Signal(bool)
def __init__(self,
MaxTTC=None,
MinTTC=None,
parent=None):
super().__init__(parent)
self.maxTTC = MaxTTC
self.minTTC = MinTTC
@Slot(float)
def get_data(self, ttc):
# print(ttc)
if(self.maxTTC is not None):
if(ttc > self.maxTTC):
ttc = self.maxTTC
if(self.minTTC is not None):
if(ttc < self.minTTC):
ttc = self.minTTC
self.timeToCollisonSignal.emit(ttc)
@Slot(bool)
def get_fcw(self, fcw):
self.fcwSignal.emit(fcw)