Revize 70b83aa2
Přidáno uživatelem Jan Rach před téměř 4 roky(ů)
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
1 | 1 |
import logging |
2 | 2 |
import math |
3 |
import random |
|
4 | 3 |
|
5 | 4 |
from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal |
6 |
from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit, QDialog, QHBoxLayout |
|
7 |
from numpy import pi, sin, cos, tan, exp |
|
8 |
from matplotlib.pyplot import subplot |
|
5 |
from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit, QDialog, QHBoxLayout, QGridLayout |
|
9 | 6 |
|
10 | 7 |
from aswi2021vochomurka.model.Message import Message |
11 | 8 |
from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber |
... | ... | |
13 | 10 |
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback |
14 | 11 |
from aswi2021vochomurka.service.subscriber_params import SubscriberParams, ConnectionParams |
15 | 12 |
|
16 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
|
17 |
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar |
|
18 |
import matplotlib.pyplot as plt |
|
19 |
|
|
20 |
import sys |
|
21 | 13 |
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout |
22 | 14 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
23 | 15 |
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar |
24 | 16 |
import matplotlib.pyplot as plt |
25 |
import random |
|
26 | 17 |
|
27 | 18 |
from aswi2021vochomurka.view.logger_view import LoggerView |
28 | 19 |
|
... | ... | |
31 | 22 |
connected = pyqtSignal() |
32 | 23 |
disconnected = pyqtSignal() |
33 | 24 |
error = pyqtSignal(Exception) |
34 |
newMessage = pyqtSignal(str)
|
|
25 |
newMessage = pyqtSignal(Message)
|
|
35 | 26 |
subscriber: Subscriber = None |
36 | 27 |
|
37 | 28 |
params = SubscriberParams( |
38 |
["/home/1", "/home/2"], |
|
29 |
["/home/1", "/home/2", "/home/3", "/home/4", "/home/5", "/home/6", "/home/7", "/home/8"],
|
|
39 | 30 |
10, |
40 | 31 |
ConnectionParams("localhost", 1883, 60), |
41 | 32 |
True |
... | ... | |
55 | 46 |
self.error.emit() |
56 | 47 |
|
57 | 48 |
def onMessage(self, message: Message): |
58 |
self.newMessage.emit(message.topic)
|
|
59 |
self.window.plot(message) |
|
49 |
self.newMessage.emit(message) |
|
50 |
#self.window.plot(message)
|
|
60 | 51 |
|
61 | 52 |
def onCloseTopic(self, topic: str): |
62 | 53 |
pass |
... | ... | |
75 | 66 |
self.dataIndex = 0 |
76 | 67 |
self.dataDict = {} |
77 | 68 |
|
78 |
self.figure = plt.figure(figsize=([500,500])) |
|
69 |
self.canvasDict = {} |
|
70 |
self.figureDict = {} |
|
79 | 71 |
|
80 |
self.canvas = FigureCanvas(self.figure) |
|
81 |
self.toolbar = NavigationToolbar(self.canvas, self) |
|
72 |
#self.toolbar = NavigationToolbar(self.canvas, self) |
|
82 | 73 |
|
83 |
self.setMinimumSize(QSize(440, 240)) |
|
74 |
#self.setMinimumSize(QSize(440, 240)) |
|
75 |
self.setMinimumSize(QSize(1200, 800)) |
|
84 | 76 |
self.setWindowTitle("MQTT demo") |
85 | 77 |
|
86 | 78 |
# Add logger text field |
... | ... | |
92 | 84 |
|
93 | 85 |
layout = QVBoxLayout() |
94 | 86 |
layout.addWidget(logger.widget) |
95 |
layout.addWidget(self.toolbar) |
|
96 |
layout.addWidget(self.canvas) |
|
87 |
#layout.addWidget(self.toolbar) |
|
97 | 88 |
|
98 | 89 |
self.setLayout(layout) |
99 | 90 |
|
100 |
self.initSubscriber()
|
|
91 |
self.grid = QGridLayout()
|
|
101 | 92 |
|
102 |
def plot(self, message: Message): |
|
103 |
self.figure.clear() |
|
93 |
layout.addLayout(self.grid) |
|
94 |
|
|
95 |
self.init_subscriber() |
|
104 | 96 |
|
97 |
def plot(self, message: Message): |
|
105 | 98 |
if message.topic in self.dataDict: |
106 | 99 |
self.dataDict[message.topic].append(message.value) |
100 |
|
|
101 |
figure = self.figureDict[message.topic] |
|
102 |
figure.clear() |
|
103 |
|
|
104 |
plt.figure(figure.number) |
|
105 |
plt.plot(self.dataDict[message.topic]) |
|
106 |
|
|
107 |
self.canvasDict[message.topic].draw() |
|
107 | 108 |
else: |
108 | 109 |
self.dataDict[message.topic] = [message.value] |
109 |
self.chartsNum += 1 |
|
110 | 110 |
|
111 |
rows = math.ceil(self.chartsNum / 2) |
|
111 |
figure = plt.figure(figsize=[500, 500]) |
|
112 |
canvas = FigureCanvas(figure) |
|
112 | 113 |
|
113 |
b = 0 |
|
114 |
for a in self.dataDict.values(): |
|
115 |
self.figure.add_subplot(rows, 2, b + 1) |
|
116 |
b += 1 |
|
117 |
plt.plot(a) |
|
114 |
plt.plot(self.dataDict[message.topic]) |
|
118 | 115 |
|
119 |
self.canvas.draw() |
|
116 |
self.canvasDict[message.topic] = canvas |
|
117 |
self.figureDict[message.topic] = figure |
|
118 |
|
|
119 |
self.grid.addWidget(canvas, int(self.chartsNum / 2), self.chartsNum % 2) |
|
120 |
|
|
121 |
self.chartsNum += 1 |
|
120 | 122 |
|
121 |
def initSubscriber(self):
|
|
123 |
def init_subscriber(self):
|
|
122 | 124 |
self.workerThread = QThread() |
123 | 125 |
self.worker = Worker() |
124 | 126 |
self.worker.moveToThread(self.workerThread) |
125 | 127 |
self.workerThread.started.connect(self.worker.start) |
126 |
# self.worker.newMessage.connect(
|
|
127 |
# lambda message: self.b.insertPlainText(message + "\n")
|
|
128 |
# )
|
|
128 |
self.worker.newMessage.connect( |
|
129 |
lambda message: self.plot(message)
|
|
130 |
) |
|
129 | 131 |
self.worker.window = self |
130 | 132 |
self.workerThread.start() |
133 |
#lambda message: self.b.insertPlainText(message + "\n") |
Také k dispozici: Unified diff
Re #8923 - Graphs displayed separately in a grid layout.