forked from OrionAerospaceYT/SideKick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.py
351 lines (281 loc) · 12.8 KB
/
Backend.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
from distutils.command.upload import upload
import numpy as np
from datetime import datetime
import random
import time
import sys
import os
import subprocess
import glob
import serial
import __main__
class DataHandler():
def __init__(self):
# Define default variables here, by default, xyz plots.
self.com_port = ""
self.com = False
self.device = None
self.labels =[[],[]]
self.num_of_top_plots = 0
self.num_of_bottom_plots = 0
self.top_plots_raw_data = []
self.top_plots_data = []
self.top_plots = []
self.bottom_plots_raw_data = []
self.bottom_plots_data = []
self.bottom_plots = []
self.terminal_data = []
self.buffer_string = ""
self.number_of_lines_displayed_on_terminal = 20
self.amount_of_data = 1000
self.skipped_loops = 0
self.html_header = """<h1><b><font color="#00f0c3">Terminal</b></h1><body>"""
self.html_footer = "</font></body>"
self.save_data = False
self.com_port_range = 0
self.html_terminal_text = self.html_header + self.html_footer
self.serial_port = []
self.errors = False
# Goes through raw terminal input and outputs the graphing values in a list.
# The list has a format of [name, 1 for top - 2 for bottom, data]
def decode_graph(self, input):
# edmpty list for output values
output = []
# decodes the input data from sidekick format to usable data
graph_data = input.split("g(")
# goes through each item and keeps breaking it down
for item in graph_data:
# splits into ended sections
item = item.replace(" ", "")
item = item.split(")")
# removes empty lists : checks that we dont add a terminal to graphs so terminal can be anywhere
if item != [''] and "t(" not in item[0] and "r(" not in item[0]:
output.append(item[0].split(","))
graph_data = []
for x in range(0, len(output)):
i = []
for item in output[x]:
value = item
## checks if the data is a number
if value.isnumeric():
item = float(item)
# assembles the array of data to add to graph_data
i.append(item)
graph_data.append(i)
# checks for all inputs to graphs to be numerical
for x in range(0,len(graph_data)):
for y in range(1, len(graph_data[0])):
try: # .isnumeric wont work on already numeric numbers
float(graph_data[x][y])
except:
del graph_data[x] # deletes item with unwanted data
return graph_data
# Returns terminal text as string.
def decode_terminal(self, input):
# terminal section of decoding
raw_list = input.split("t(")
terminal_output = []
for string in raw_list:
# finds which seperation the terminal is in
if string != '':
string = string.split(")")
for data in string:
if "g(" not in data and "r(" not in data:
if data != "" and "\r" not in data:
terminal_output.append(data)
#__main__.fileManager.save_terminal_data(terminal_output)
return terminal_output
# Returns list of [x, y, z] orientation
def decode_orientation(self, input):
# orientation section of decoding
orientation_output = input.split("r(")
for item in orientation_output:
# finds which seperation the orientation is in
if item != '':
item = item.split(")")
for item in item:
if "g(" not in item and "t(" not in item:
orientation_output = item
break
if len(orientation_output.split(",")) == 3:
return orientation_output.split(",")
else:
return [0,0,0]
def transpose(self, list):
list = np.array(list, dtype=object)
transpose = list.T
return transpose.tolist()
# Formats the terminal data as html for the PyQt5 textBox
def organise_terminal_data(self, terminal_parsed_data_string):
# Gets current date and time
now = datetime.now()
current_time_string = now.strftime("%H:%M:%S")
# Creates string of "time,data"
for item in terminal_parsed_data_string:
self.terminal_data.append(current_time_string + "," + item)
# Keeps terminal data string at length of data to be displayed as screen changes
while len(self.terminal_data) > self.number_of_lines_displayed_on_terminal:
self.terminal_data.pop(0)
# Terminal title for text box
self.html_terminal_text = self.html_header
# For each line, makes the time as white and data as #00f0c3
for mesage in self.terminal_data:
text_for_terminal_and_time = mesage.split(",")
self.html_terminal_text += (f"""<p><font color="white">{text_for_terminal_and_time[0]}>$ <font color="#00f0c3">{text_for_terminal_and_time[1]}</p>""")
# Ends the html
self.html_terminal_text += self.html_footer
# Returns a list of open serial ports
def serial_ports(self):
# Checks the supported OS: windows linux and darwin
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
start = time.time()
# Checks if the port is open, then appends to list
port = ports[self.com_port_range]
try:
s = serial.Serial(port)
s.close()
self.serial_port.append(port)
except (OSError, serial.SerialException):
if port in self.serial_port:
self.serial_port.remove(port)
if self.com_port_range == len(ports) - 1:
self.com_port_range = 0
else:
self.com_port_range += 1
return self.serial_port
# The function that gets called and uses the other functions.
def get_data(self):
# Checks if device has been defined and if it has, gets the data from the serial device
if self.device != None:
self.buffer_string = self.buffer_string + self.device.read(self.device.inWaiting()).decode().strip()
# Keeps buffer size small to save memory.
# Saves two lines as one is complete and the other may be incomplete.
if len(self.buffer_string.split("\n")) > 3:
raw_data = self.buffer_string.split("\n")[-3]
self.buffer_string = self.buffer_string.split("\n")[-3]
else:
raw_data = ""
# Keeps trying to connect to serial device if none is connected
try:
if self.device == None:
self.device = serial.Serial(self.com_port, __main__.eventHandler.graphing.ui.baud_rate.currentText(), rtscts=False)
except:
pass
# If there is raw_data then we need to display it.
if not len(raw_data) > 0:
self.skipped_loops += 1
return 0
self.skipped_loops = 0
# Parses all of the data seperately: graph, terminal and orientation.
graph_data = self.decode_graph(raw_data)
terminal_output = self.decode_terminal(raw_data)
orientation_output = self.decode_orientation(raw_data)
# Formats the html for the terminal data.
self.organise_terminal_data(terminal_output)
# Saves data to file if recording is meant to happen.
if self.save_data:
for item in terminal_output:
__main__.fileManager.save_terminal_data(item)
# Sets the orientation equal to the latest
self.x , self.y , self.z = orientation_output[0], orientation_output[1], orientation_output[2]
# Defines the temporary list that we append data to
top_temp , bottom_temp = [], []
# Starts off with no plots on top or bottom...
# We use this to ensure that the displayed number of plots equals
# the number of input plots.
self.num_of_top_plots, self.num_of_bottom_plots = 0, 0
self.labels = [[],[]]
# Goes through graph data and checks which graph, the label and values
for item in graph_data:
try:
if item[1] == 1:
top_temp.append(float(item[2]))
self.num_of_top_plots += 1
self.labels[0].append(item[0])
elif item[1] == 2:
bottom_temp.append(float(item[2]))
self.num_of_bottom_plots += 1
self.labels[1].append(item[0])
except:
pass
# Appends latest data to current data buffer of 200.
# If the buffer is too long, remove the first element.
self.top_plots_raw_data.append(top_temp)
if len(self.top_plots_raw_data) > self.amount_of_data:
self.top_plots_raw_data.pop(0)
self.top_plots_data = self.transpose(self.top_plots_raw_data)
self.bottom_plots_raw_data.append(bottom_temp)
if len(self.bottom_plots_raw_data) > self.amount_of_data:
self.bottom_plots_raw_data.pop(0)
self.bottom_plots_data = self.transpose(self.bottom_plots_raw_data)
# Uploads the selected project to the board.
def compile_and_upload(self, port, project):
# Gets the board from the board selected in drop down.
if __main__.eventHandler.graphing.ui.device.currentText() != "Select Board":
board = __main__.supported_devices[__main__.eventHandler.graphing.ui.device.currentText()]
cwd = os.getcwd()
# Compiles the coad before upload and gets errors:
compile_cmd = f'"{cwd}/Externals/arduino-cli.exe" compile --fqbn {board} {project}'
compile = subprocess.Popen(compile_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
self.compile_output, error = compile.communicate()
# Uploads the code and gets errors.
upload_cmd = f'"{cwd}/Externals/arduino-cli.exe" upload -p {port} --fqbn {board} {project}'
upload = subprocess.Popen(upload_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
self.upload_output, error = upload.communicate()
# If no board is selected, displays error.
else:
self.compile_output = b"error: No Board selected!"
# Returns the HTML for the errors to be displayed.
def process_errors(self):
if "error" in str(self.compile_output):
# Formats the output for html rather than python
output = self.compile_output.decode('utf-8').replace("\r\n", "<br>")
# Defines temporary variables
error = False
warning = False
self.compile_output = ""
# Defines colours
error_colour = "#ff003c"
warning_colour = "#ff7300"
default_colour = "#71d9c6"
# Iterates through each line to check for error
for item in output.split("<br>"):
# Checks for errors
if "error" in item:
item = f"""<font color={error_colour}>{item}<font color="#00f0c3">"""
error = True
elif error:
item = f"""<font color={error_colour}>{item}<font color="#00f0c3">"""
if "^" in item:
error = False
# Checks for warnings
elif "warning" in item:
item = f"""<font color={warning_colour}>{item}<font color="#00f0c3">"""
warning = True
elif warning:
item = f"""<font color={warning_colour}>{item}<font color="#00f0c3">"""
if "^" in item:
warning = False
# Information lines are default
else:
item = f"""<font color={default_colour}>{item}<font color="#00f0c3">"""
self.compile_output += item
return True
else:
return False
# This code runs upload on another thread to keep GUI on main thread
def upload(self, port, project):
# Compiles and Uploads the project.
self.compile_and_upload(port, project)
# Parses Errors for the GUI to display.
if self.process_errors():
self.errors = True
else:
__main__.eventHandler.update_com(port)