Revize 3b3b9e9b
Přidáno uživatelem Martin Forejt před téměř 4 roky(ů)
aswi2021vochomurka/service/mqtt/mqtt_subscriber.py | ||
---|---|---|
49 | 49 |
|
50 | 50 |
if not self.params.anonymous: |
51 | 51 |
logging.info('Using credentials, username=' + self.params.username + ', password=' + self.params.password) |
52 |
client.tls_set() |
|
53 | 52 |
client.username_pw_set(self.params.username, self.params.password) |
54 | 53 |
|
55 | 54 |
try: |
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
3 | 3 |
|
4 | 4 |
import matplotlib.pyplot as plt |
5 | 5 |
from PyQt5 import QtGui |
6 |
from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal |
|
6 |
from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal, QSettings
|
|
7 | 7 |
from PyQt5.QtWidgets import QMainWindow, QWidget, QMenuBar, QAction |
8 | 8 |
from PyQt5.QtWidgets import QVBoxLayout |
9 | 9 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
... | ... | |
17 | 17 |
from aswi2021vochomurka.view.about import AboutDialog |
18 | 18 |
from aswi2021vochomurka.view.help import HelpDialog |
19 | 19 |
from aswi2021vochomurka.view.logger_view import LoggerView |
20 |
from aswi2021vochomurka.view.settings import SettingsDialog |
|
20 |
from aswi2021vochomurka.view.settings import SettingsDialog, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_KEEPALIVE, \ |
|
21 |
DEFAULT_ANONYMOUS, DEFAULT_USERNAME, DEFAULT_TIMEOUT, DEFAULT_TOPICS |
|
21 | 22 |
|
22 | 23 |
|
23 | 24 |
class Worker(QObject, SubscriberCallback): |
24 | 25 |
connected = pyqtSignal() |
25 | 26 |
disconnected = pyqtSignal() |
26 |
error = pyqtSignal(Exception) |
|
27 | 27 |
newMessage = pyqtSignal(Message) |
28 | 28 |
subscriber: Subscriber = None |
29 |
params: SubscriberParams |
|
29 | 30 |
|
30 |
params = SubscriberParams( |
|
31 |
["/home/1", "/home/2"], |
|
32 |
10, |
|
33 |
ConnectionParams("localhost", 1883, 60), |
|
34 |
True |
|
35 |
) |
|
31 |
def __init__(self, params: SubscriberParams) -> None: |
|
32 |
super().__init__() |
|
33 |
self.params = params |
|
36 | 34 |
|
37 | 35 |
def start(self): |
38 | 36 |
self.subscriber = MQTTSubscriber(self, self.params) |
... | ... | |
48 | 46 |
self.disconnected.emit() |
49 | 47 |
|
50 | 48 |
def onError(self): |
51 |
self.error.emit()
|
|
49 |
pass
|
|
52 | 50 |
|
53 | 51 |
def onMessage(self, message: Message): |
54 | 52 |
self.newMessage.emit(message) |
... | ... | |
71 | 69 |
self.dataIndex = 0 |
72 | 70 |
self.dataDict = {} |
73 | 71 |
|
74 |
self.figure = plt.figure(figsize=([500,500])) |
|
72 |
self.figure = plt.figure(figsize=([500, 500]))
|
|
75 | 73 |
|
76 | 74 |
self.canvas = FigureCanvas(self.figure) |
77 | 75 |
self.toolbar = NavigationToolbar(self.canvas, self) |
... | ... | |
156 | 154 |
|
157 | 155 |
def reconnect(self): |
158 | 156 |
self.disconnect() |
157 |
self.worker.params = self.getConfigParams() |
|
159 | 158 |
self.workerThread.start() |
160 | 159 |
|
161 | 160 |
def initSubscriber(self): |
162 | 161 |
self.workerThread = QThread() |
163 |
self.worker = Worker() |
|
162 |
self.worker = Worker(self.getConfigParams())
|
|
164 | 163 |
self.worker.moveToThread(self.workerThread) |
165 | 164 |
self.workerThread.started.connect(self.worker.start) |
166 | 165 |
self.worker.newMessage.connect( |
167 | 166 |
lambda message: self.plot(message) |
168 | 167 |
) |
169 | 168 |
self.workerThread.start() |
169 |
|
|
170 |
def getConfigParams(self) -> SubscriberParams: |
|
171 |
settings = QSettings("Vochomurka", "MQTTClient") |
|
172 |
|
|
173 |
connection = ConnectionParams( |
|
174 |
settings.value("connection_host", DEFAULT_HOST, str), |
|
175 |
settings.value("connection_port", DEFAULT_PORT, int), |
|
176 |
settings.value("connection_keepalive", DEFAULT_KEEPALIVE, int) |
|
177 |
) |
|
178 |
|
|
179 |
params = SubscriberParams( |
|
180 |
settings.value("topics_items", DEFAULT_TOPICS), |
|
181 |
settings.value("topics_timeout", DEFAULT_TIMEOUT), |
|
182 |
connection, |
|
183 |
settings.value("connection_anonymous", DEFAULT_ANONYMOUS, bool), |
|
184 |
settings.value("connection_username", DEFAULT_USERNAME, str), |
|
185 |
settings.value("connection_password", DEFAULT_USERNAME, str), |
|
186 |
) |
|
187 |
|
|
188 |
return params |
aswi2021vochomurka/view/settings.py | ||
---|---|---|
1 | 1 |
from PyQt5 import QtCore |
2 | 2 |
from PyQt5.QtCore import QSettings, QSize |
3 |
from PyQt5.QtGui import QStandardItemModel, QStandardItem |
|
4 | 3 |
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox, QGroupBox, QFormLayout, QLabel, QLineEdit, QSpinBox, \ |
5 |
QCheckBox, QListView, QWidget, QPushButton, QHBoxLayout, QListWidget, QListWidgetItem, QTableWidget
|
|
4 |
QCheckBox, QPushButton, QListWidget, QListWidgetItem
|
|
6 | 5 |
|
6 |
DEFAULT_HOST = "localhost" |
|
7 |
DEFAULT_PORT = 1883 |
|
8 |
DEFAULT_KEEPALIVE = 60 |
|
9 |
DEFAULT_ANONYMOUS = True |
|
10 |
DEFAULT_USERNAME = "" |
|
11 |
DEFAULT_PASSWORD = "" |
|
12 |
DEFAULT_TOPICS = ["/home/1", "/home/2"] |
|
13 |
DEFAULT_TIMEOUT = 60 |
|
7 | 14 |
|
8 | 15 |
class SettingsDialog(QDialog): |
9 |
topics = ['/home/1', '/home/2']
|
|
16 |
topics = DEFAULT_TOPICS
|
|
10 | 17 |
|
11 | 18 |
def __init__(self): |
12 | 19 |
super(SettingsDialog, self).__init__() |
... | ... | |
16 | 23 |
|
17 | 24 |
connectionGroupBox = QGroupBox("Connection") |
18 | 25 |
connectionLayout = QFormLayout() |
19 |
self.hostInput = QLineEdit(self.settings.value("connection_host", "localhost", str))
|
|
26 |
self.hostInput = QLineEdit(self.settings.value("connection_host", DEFAULT_HOST, str))
|
|
20 | 27 |
connectionLayout.addRow(QLabel("Host:"), self.hostInput) |
21 | 28 |
self.portInput = QSpinBox() |
22 | 29 |
self.portInput.setMaximum(65535) |
23 |
self.portInput.setValue(self.settings.value("connection_port", 1883, int))
|
|
30 |
self.portInput.setValue(self.settings.value("connection_port", DEFAULT_PORT, int))
|
|
24 | 31 |
connectionLayout.addRow(QLabel("Port:"), self.portInput) |
25 | 32 |
self.keepaliveInput = QSpinBox() |
26 | 33 |
self.keepaliveInput.setMaximum(1000) |
27 |
self.keepaliveInput.setValue(self.settings.value("connection_keepalive", 60, int))
|
|
34 |
self.keepaliveInput.setValue(self.settings.value("connection_keepalive", DEFAULT_KEEPALIVE, int))
|
|
28 | 35 |
connectionLayout.addRow(QLabel("Keepalive(s):"), self.keepaliveInput) |
29 | 36 |
self.anonymousInput = QCheckBox() |
30 |
self.anonymousInput.setChecked(self.settings.value("connection_anonymous", True, bool))
|
|
37 |
self.anonymousInput.setChecked(self.settings.value("connection_anonymous", DEFAULT_ANONYMOUS, bool))
|
|
31 | 38 |
self.anonymousInput.stateChanged.connect(self.anonymousChanged) |
32 | 39 |
connectionLayout.addRow(QLabel("Anonymous:"), self.anonymousInput) |
33 |
self.usernameInput = QLineEdit(self.settings.value("connection_username", "", str))
|
|
40 |
self.usernameInput = QLineEdit(self.settings.value("connection_username", DEFAULT_USERNAME, str))
|
|
34 | 41 |
connectionLayout.addRow(QLabel("Username:"), self.usernameInput) |
35 |
self.passwordInput = QLineEdit(self.settings.value("connection_password", "", str))
|
|
42 |
self.passwordInput = QLineEdit(self.settings.value("connection_password", DEFAULT_PASSWORD, str))
|
|
36 | 43 |
connectionLayout.addRow(QLabel("Password:"), self.passwordInput) |
37 | 44 |
self.anonymousChanged() |
38 | 45 |
connectionGroupBox.setLayout(connectionLayout) |
... | ... | |
40 | 47 |
topicsGroupBox = QGroupBox("Topics") |
41 | 48 |
topicsLayout = QFormLayout() |
42 | 49 |
|
50 |
self.topics = self.settings.value("topics_items", DEFAULT_TOPICS, list) |
|
43 | 51 |
self.topicsListWidget = QListWidget() |
44 | 52 |
for topic in self.topics: |
45 | 53 |
item = QListWidgetItem() |
... | ... | |
63 | 71 |
timeoutLabel = QLabel("Topic timeout(s):") |
64 | 72 |
timeoutLabel.setToolTip("Unsubscribe topic and close file when there is not new message after this " |
65 | 73 |
"timeout (in seconds) expires") |
66 |
self.timeoutInput.setValue(self.settings.value("topic_timeout", 60, int))
|
|
74 |
self.timeoutInput.setValue(self.settings.value("topics_timeout", DEFAULT_TIMEOUT, int))
|
|
67 | 75 |
topicsLayout.addRow(timeoutLabel, self.timeoutInput) |
68 | 76 |
|
69 | 77 |
topicsGroupBox.setLayout(topicsLayout) |
... | ... | |
96 | 104 |
|
97 | 105 |
def accept(self) -> None: |
98 | 106 |
super().accept() |
99 |
self.topics = self.topicsListWidget.item(0).text() |
|
100 |
print(self.topics) |
|
107 |
self.topics = [] |
|
108 |
for index in range(self.topicsListWidget.count()): |
|
109 |
self.topics.append(self.topicsListWidget.item(index).text()) |
|
110 |
|
|
111 |
self.settings.setValue("topics_items", self.topics) |
|
112 |
self.settings.setValue("topics_timeout", self.timeoutInput.value()) |
|
101 | 113 |
self.settings.setValue("connection_host", self.hostInput.text()) |
102 | 114 |
self.settings.setValue("connection_port", self.portInput.value()) |
103 | 115 |
self.settings.setValue("connection_keepalive", self.keepaliveInput.value()) |
Také k dispozici: Unified diff
Re: #8921 - use configuration from QSettings