-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
160 lines (128 loc) · 4.7 KB
/
main.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
import sys
import os
import json
import base64
import requests
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QVBoxLayout,
QWidget,
QLabel,
QLineEdit,
QPushButton,
QMessageBox,
QCheckBox,
QGridLayout,
)
import constants
from analyzer.analyzer import Analyzer
from uploader.uploader import Uploader
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.analyzer_thread = None
self.uploader_thread = None
self.setWindowTitle("Configuration App")
self.setGeometry(100, 100, 400, 200)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
self.check_config()
def closeEvent(self, event):
if self.analyzer_thread:
self.analyzer_thread.stop()
if self.uploader_thread:
self.uploader_thread.stop()
event.accept()
def check_config(self):
if os.path.exists(constants.CONFIG_PATH):
self.show_empty_ui()
else:
self.show_config_input()
def show_empty_ui(self):
layout = self.centralWidget().layout()
while layout.count():
child = layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
label = QLabel("Configuration already exists.")
layout.addWidget(label)
with open(constants.CONFIG_PATH, "r") as f:
config = json.load(f)
self.analyzer_thread = Analyzer(config)
self.uploader_thread = Uploader(config)
self.analyzer_thread.start()
self.uploader_thread.start()
def show_config_input(self):
label = QLabel("Enter Operator ID:")
self.centralWidget().layout().addWidget(label)
self.user_id_input = QLineEdit()
self.centralWidget().layout().addWidget(self.user_id_input)
button = QPushButton("Get Configs")
button.clicked.connect(self.get_configs)
self.centralWidget().layout().addWidget(button)
def get_configs(self):
user_id = self.user_id_input.text()
if not user_id:
QMessageBox.warning(self, "Warning", "Please enter Operator ID.")
return
try:
response = requests.get(
constants.GET_OPERATOR_DETAILS_URL, params={"operator_id": user_id}
)
response.raise_for_status()
configs = json.loads(base64.b64decode(response.content))
dialog = QWidget()
layout = QVBoxLayout(dialog)
checkboxes = {}
for hydrophone in configs["hydrophones"]:
checkbox = QCheckBox(hydrophone["id"], dialog)
layout.addWidget(checkbox)
checkboxes[hydrophone["id"]] = checkbox
button_box = QGridLayout()
save_button = QPushButton("Save Selected")
cancel_button = QPushButton("Cancel")
button_box.addWidget(save_button, 0, 0)
button_box.addWidget(cancel_button, 0, 1)
layout.addLayout(button_box)
save_button.clicked.connect(
lambda: self.save_selected_configs(checkboxes, configs, dialog)
)
cancel_button.clicked.connect(lambda: dialog.close())
dialog.setWindowTitle("Select Hydrophones to Monitor")
dialog.setLayout(layout)
dialog.show()
except requests.exceptions.RequestException as e:
QMessageBox.critical(None, "Error", f"Failed to retrieve configs: {e}")
def save_selected_configs(self, checkboxes, config, dialog):
selected_hydrophones = [
hydrophone_id
for hydrophone_id, checkbox in checkboxes.items()
if checkbox.isChecked()
]
filtered_hydrophones = [
hydrophone
for hydrophone in config["hydrophones"]
if hydrophone["id"] in selected_hydrophones
]
filtered_configs = config.copy()
filtered_configs["hydrophones"] = filtered_hydrophones
try:
with open(constants.CONFIG_PATH, "w") as f:
json.dump(filtered_configs, f)
QMessageBox.information(
None, "Success", "Selected configs retrieved and saved successfully."
)
self.show_empty_ui()
dialog.close()
except Exception as e:
QMessageBox.critical(None, "Error", f"Failed to save configs: {e}")
if __name__ == "__main__":
if not os.path.exists(constants.BASE_PATH):
os.makedirs(constants.BASE_PATH)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())