MainThread.py 3.03 KB
Newer Older
1
from datetime import datetime
2
from time import sleep, time
3
from sys import exit
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
4
from PyQt5.QtCore import QThread, QTimer
5
from PyQt5.QtWidgets import QMessageBox, QApplication
6
from threads.Graph import Graph
7
from config import Config
8
from util.Timer import Timer
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
9

10
class MainThread():
11
    def __init__(self, ui, timer):
12
        #super(MainThread, self).__init__(parent)
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
13 14
        self.ui = ui
        self.data = []
15
        self.graph = Graph(self.ui, 500, self._log)
16 17
        #self.upTimer = Timer()
        #self.downTimer = Timer(delay=10)
18 19 20 21
        try:
            self.config = Config()
        except FileNotFoundError:
            self._fatal("Arquivo de configuração não encontrado!")
22
        self.graphTimer = timer
23 24 25
        self.graphTimer.timeout.connect(self.graph.updateGraphs)
        self.graphTimer.start(self.graph.interval)
        self.ui.samplingCBox.currentIndexChanged.connect(self._updateSamplingRate)
26
        self.ui.playPause.clicked.connect(self._playPause)
27

28 29 30
    def _updateSamplingRate(self):
        MAP = [100, 200, 500, 1000, 1500, 2000, 2500, 5000, 10000]
        self._log("[DEBUG]", self.ui.samplingCBox.currentText())
31 32 33
        self.graphTimer.stop()
        self.graphTimer.start(MAP[self.ui.samplingCBox.currentIndex()])
        self.graph.interval = MAP[self.ui.samplingCBox.currentIndex()]
34

35 36 37 38 39 40
    def _fatal(self, msg):
        error = QMessageBox()
        error.setText(msg)
        error.setWindowTitle("Erro")
        error.exec()
        exit(-1)
41

42
    def _log(self, tag, msg):
43
        self.ui.consoleView.setText("{} {}\n{}".format(tag, msg, self.ui.consoleView.text()))
44
        if "FATAL" in tag:
45
            self._fatal(msg)
46

47 48
    def _playPause(self):
        if self.graph.running:
49 50 51
            self.graphTimer.stop()
            self.graph.running = False
            self.ui.playPause.setText("REINICIAR")
52
        else:
53 54 55
            self.graph.running = True
            self.graphTimer.start(self.graph.interval)
            self.ui.playPause.setText("PAUSA")
56 57 58 59

    def _finalize(self):
        pass

60 61 62 63 64
    def _updateTimers(self):
        if self.upTimer.active:
            self.upTimer.update()
        if self.downTimer.active:
            self.downTimer.update()
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
65 66 67
        #self.ui.currentTimeLabel.setText(datetime.now().strftime("%H:%M:%S"))
        #self.ui.timerLabel.setText(self.upTimer.getTimeString())
        #self.ui.regTimerLabel.setText(self.downTimer.getTimeString())
68

69 70 71 72
    def _startTimer(self):
        btn = self.sender()
        timer = self.upTimer if btn == self.ui.startTimerButton else self.downTimer
        if btn.text() == "Iniciar":
73
            self.ui.startTimeLabel.setText(datetime.now().strftime("%H:%M:%S"))
74 75 76 77 78
            btn.setText("Parar")
            timer.start()
        else:
            btn.setText("Iniciar")
            timer.stop()
79
        self.graph.addMarker(datetime.now().strftime("%H:%M:%S"))
80

81 82 83 84 85 86
    def _stopTimer(self):
        if self.sender == self.ui.stopTimerButton:
            self.upTimer.stop()
            self.upTimer.clear()
        else:
            self.downTimer.stop()
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
87
            self.downTimer.clear()