MainThread.py 2.74 KB
Newer Older
1 2
from datetime import datetime
from time import sleep
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
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 11 12 13 14

class MainThread(QThread):
    def __init__(self, ui, parent=None):
        super(MainThread, self).__init__(parent)
        self.ui = ui
        self.data = []
15
        self.graph = Graph(self.ui.mainGraph, 100, self._log)
16 17 18 19 20 21
        self.upTimer = Timer()
        self.downTimer = Timer(delay=10)
        try:
            self.config = Config()
        except FileNotFoundError:
            self._fatal("Arquivo de configuração não encontrado!")
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
22 23

    def run(self):
24
        self.ui.samplingCBox.currentIndexChanged.connect(self._updateSamplingRate)
25 26 27 28
        self.ui.startTimerButton.pressed.connect(self._startTimer)
        self.ui.stopTimerButton.pressed.connect(self._stopTimer)
        self.ui.startRegTimerButton.pressed.connect(self._startTimer)
        self.ui.stopRegTimerButton.pressed.connect(self._stopTimer)
29 30
        self.graph.start()

31 32 33
        while True:
            self._updateTimers()
            sleep(1/30.0) # Updates at 30FPS
34 35 36 37 38
        
    def _updateSamplingRate(self):
        MAP = [100, 200, 500, 1000, 1500, 2000, 2500, 5000, 10000]
        self._log("[DEBUG]", self.ui.samplingCBox.currentText())
        self.graph.setInterval(MAP[self.ui.samplingCBox.currentIndex()])
39 40 41 42 43 44 45 46
    
    def _fatal(self, msg):
        error = QMessageBox()
        error.setText(msg)
        error.setWindowTitle("Erro")
        error.exec()
        exit(-1)
    
47
    def _log(self, tag, msg):
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        self.ui.consoleView.setText("{} {}\n{}".format(tag, msg, self.ui.consoleView.text()))

    def _updateTimers(self):
        if self.upTimer.active:
            self.upTimer.update()
        if self.downTimer.active:
            self.downTimer.update()
        self.ui.currentTimeLabel.setText(datetime.now().strftime("%H:%M:%S"))
        self.ui.timerLabel.setText(self.upTimer.getTimeString())
        self.ui.regTimerLabel.setText(self.downTimer.getTimeString())
        
    def _startTimer(self):
        btn = self.sender()
        timer = self.upTimer if btn == self.ui.startTimerButton else self.downTimer
        if btn.text() == "Iniciar":
63
            self.ui.startTimeLabel.setText(datetime.now().strftime("%H:%M:%S"))
64 65 66 67 68
            btn.setText("Parar")
            timer.start()
        else:
            btn.setText("Iniciar")
            timer.stop()
69
        self.graph.addMarker(datetime.now().strftime("%H:%M:%S"))
70 71 72 73 74 75 76 77
    
    def _stopTimer(self):
        if self.sender == self.ui.stopTimerButton:
            self.upTimer.stop()
            self.upTimer.clear()
        else:
            self.downTimer.stop()
            self.downTimer.clear()