Graph.py 2.66 KB
Newer Older
1
2
from time import sleep
from PyQt5.QtCore import QThread
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
3
from pyqtgraph import AxisItem, InfiniteLine, mkColor, mkPen, setConfigOption
4
from interfaces import *
5
from config import Config
6
7

class Graph(QThread):
8
    def __init__(self, graph, interval, logger, parent=None):
9
10
        super(Graph, self).__init__(parent)
        self.running = True
11
        self.logger = logger
12
        self.interval = interval/1000.0
13
14
15
16
17
18
19
20
        self.dataX = []
        self.dataY = []
        self.graph = graph
        self.plots = []
        self.axis = []

        self.config = Config().data
        self._configurePlots()
21
        device = self.config["device"]
22
23
24
25
        try:
            self.interface = INTERFACES[device["type"]](logger, device["location"], device["channels"])
        except FileNotFoundError:
            self.running = False
26

Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
27
28
        graph.setBackground((240, 240, 240))

29
30
    def run(self):
        while self.running:
31
            data = self.interface.readAll()
32
            self.graph.getPlotItem().getViewBox().setRange(xRange=(self.dataX[0][-1] - 10, self.dataX[0][-1])) # Força atualização do range do x com span de 5 segundos
33
34
            for i, channel in enumerate(self.plots):
                self.dataX[i].append(self.dataX[i][-1] + self.interval)
35
                self.dataY[i].append(data[i])
36
                channel.setData(self.dataX[i], self.dataY[i])
37
38
39
40
41
42
            sleep(self.interval)

    def setInterval(self, interval):
        self.interval = interval/1000.0

    def stop(self):
43
44
45
46
47
        self.running = False

    def addMarker(self, label):
        marker = InfiniteLine(angle=90, pos=self.dataX[0][-1], label=label)
        self.graph.getPlotItem().addItem(marker)
48
49

    def _configurePlots(self):
50
        first = True
51
52
53
54
55
56
        pltItem = self.graph.getPlotItem()
        pltItem.setClipToView(True)
        pltItem.showGrid(True)
        pltItem.addLegend()
        pltItem.getViewBox().enableAutoRange()
        pltItem.getViewBox().setAutoPan(x=True)
57
58
59
60
61
        self.logger("[GRAPH]", "Found new device {}".format(self.config["device"]["type"]))
        for channel in self.config["device"]["channels"]:
            self.logger("[GRAPH]", "Added channel {}".format(channel["name"]))
            self.dataX.append([0])
            self.dataY.append([0])
62
63
64
            plot = self.graph.plot(pen=mkPen(mkColor(channel["color"]), width=2), name=channel["name"])
            self.plots.append(plot)
            if first:
65
                pltItem.setLabel("left", text=channel["name"], units=channel["unit"])
66
67
                first = False
            else:
68
                pltItem.setLabel("right", text=channel["name"], units=channel["unit"])
69
        
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
70
        pltItem.setLabel("bottom", text="Tempo", units="s")