load-controller/load_controller/gui.py

125 lines
3.9 KiB
Python
Raw Normal View History

2023-01-03 22:57:18 +01:00
import enum
import os.path
import datetime
import csv
from PyQt6.QtCore import QDir
from PyQt6.QtWidgets import (
QWidget,
QGridLayout,
QHBoxLayout,
QVBoxLayout,
QLabel,
QPushButton,
QFileDialog,
QMessageBox,
)
from PyQt6.QtCharts import QChart, QChartView, QLineSeries
class ProfileState(enum.Enum):
STOPPED = 0
RUNNING = 1
PAUSED = 2
class GUI(QWidget):
def __init__(self):
super().__init__(None)
profile_label = QLabel("Profile:")
self.profile = QLabel("None")
self.profile.setStyleSheet("font-style: italic")
profile_chooser = QPushButton("Choose")
profile_chooser.clicked.connect(self._choose_profile)
profile_choice = QHBoxLayout()
profile_choice.addWidget(profile_label)
profile_choice.addWidget(self.profile)
profile_choice.addWidget(profile_chooser)
chart_view = QChartView()
self.chart = QChart()
chart_view.setChart(self.chart)
self.profile_state = ProfileState.STOPPED
self.start_pause = QPushButton("START")
self.start_pause.setDisabled(True)
self.start_pause.clicked.connect(self._start_pause)
self.stop = QPushButton("STOP")
self.stop.setDisabled(True)
self.stop.clicked.connect(self._stop)
profile_buttons = QHBoxLayout()
profile_buttons.addWidget(self.start_pause)
profile_buttons.addWidget(self.stop)
profile_layout = QVBoxLayout()
profile_layout.addLayout(profile_choice)
profile_layout.addWidget(chart_view)
profile_layout.addLayout(profile_buttons)
current_label = QLabel("Current [A]:")
self.current = QLabel("0")
time_label = QLabel("Time [min]:")
self.time = QLabel("00:00 / 00:00")
self.time.setStyleSheet("font-family: monospace")
voltage_label = QLabel("Voltage [V]:")
self.voltage = QLabel("[0.00, 0.00]")
temperature_label = QLabel("Temperature [°C]:")
self.temperature = QLabel("[00.0, 00.0]")
info_layout = QGridLayout()
info_layout.addWidget(current_label, 0, 0)
info_layout.addWidget(self.current, 0, 1)
info_layout.addWidget(time_label, 1, 0)
info_layout.addWidget(self.time, 1, 1)
info_layout.addWidget(voltage_label, 2, 0)
info_layout.addWidget(self.voltage, 2, 1)
info_layout.addWidget(temperature_label, 3, 0)
info_layout.addWidget(self.temperature, 3, 1)
layout = QHBoxLayout()
layout.addLayout(profile_layout)
layout.addLayout(info_layout)
self.setLayout(layout)
self.setWindowTitle("Load Controller")
def _choose_profile(self):
dlg = QFileDialog(parent=self)
dlg.setFileMode(QFileDialog.FileMode.ExistingFile)
dlg.setNameFilter("Profiles (*.csv)")
if not dlg.exec():
return
profile_path = dlg.selectedFiles()[0]
print(f"Profile: {profile_path}")
self.profile.setText(profile_path)
self.profile.setStyleSheet("font-style: normal")
series = QLineSeries()
with open(profile_path) as fh:
t = 0
for l in csv.reader(fh):
i = float(l[0])
series.append(t, i)
t += float(l[1])
series.append(t, i)
self.chart.removeAllSeries()
self.chart.addSeries(series)
self.chart.createDefaultAxes()
def _start_pause(self):
if self.profile_state == ProfileState.RUNNING:
self.profile_state = ProfileState.PAUSED
self.start_pause.setText("RESUME")
else:
self.profile_state = ProfileState.RUNNING
self.start_pause.setText("PAUSE")
self.stop.setDisabled(False)
# TODO: Start logging if we were stopped
def _stop(self):
self.profile_state = ProfileState.STOPPED
self.start_pause.setText("START")
self.stop.setDisabled(True)