-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneonGUI.py
More file actions
74 lines (56 loc) · 2.35 KB
/
neonGUI.py
File metadata and controls
74 lines (56 loc) · 2.35 KB
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
"""
Main body of code. Execute to start program.
"""
import json
import threading
import time
import pathlib
from api import Api, ApiRecv, InfoApi
from app import App
from entities import Match
def get_config(config_file=None):
if config_file:
config = json.loads(open(config_file, 'r').read())
else:
config = json.loads(open('config.json', 'r').read())
return config
class NeonFCGUI(object):
def __init__(self, config_file=None):
self.main_thread = None
self.update_thread = None
# Log file for the last session shall be emptied
if not pathlib.Path("files/last_session_log.txt").is_file():
open("files/last_session_log.txt", 'a').close()
log_file = open("files/last_session_log.txt", "w")
log_file.write("Last session started at: ")
log_file.write(str(time.ctime(time.time())) + "\n")
log_file.close()
self.match = Match()
self.app = App(self)
self.config = get_config(config_file)
self.api_address = self.config.get("network").get("api_address")
self.api_port = self.config.get("network").get("api_port")
self.api_recv_port = self.config.get("network").get("api_recv_port")
self.api = Api(self.api_address, self.api_port)
self.api_recv = ApiRecv(self.match, self.api_address, self.api_recv_port)
self.info_api = InfoApi(self.match, self.match.robots, self.match.opposites, self.match.ball,
self.match.control_parameters, self.match.coach_name)
def start(self):
# Create a thread targeting the `start` method of the API's instance
self.api_thread = threading.Thread(target=self.api.start)
# self.api.start()
# Start the API's thread
self.api_thread.start()
self.api_recv.connect_info(self.info_api)
self.api_recv.start()
self.main_thread = threading.current_thread()
self.update_thread = threading.Thread(target=self.update)
self.update_thread.start()
self.app.start()
def update(self):
while self.main_thread.is_alive():
self.api.send_data(self.info_api)
time.sleep(0.001) # Necessary pause of 1ms to avoid busy waiting (https://superfastpython.com/thread-busy-waiting-in-python/)
# self.api.send_gui_info()
gui = NeonFCGUI()
gui.start()