Revize 2bef6e38
Přidáno uživatelem Martin Forejt před téměř 4 roky(ů)
aswi2021vochomurka/model/Message.py | ||
---|---|---|
1 |
from recordclass import RecordClass |
|
2 |
|
|
3 |
|
|
4 |
class Message(RecordClass): |
|
5 |
topic: str |
|
6 |
index: int |
|
7 |
date: str |
|
8 |
time: str |
|
9 |
value: float |
aswi2021vochomurka/service/mqtt/mqtt_subscriber.py | ||
---|---|---|
1 | 1 |
import paho.mqtt.client as mqtt |
2 | 2 |
|
3 | 3 |
from aswi2021vochomurka.service.subscriber import Subscriber |
4 |
from aswi2021vochomurka.model.Message import Message |
|
4 | 5 |
|
5 | 6 |
|
6 | 7 |
class MQTTSubscriber(Subscriber): |
... | ... | |
8 | 9 |
# The callback for when the client receives a CONNACK response from the server. |
9 | 10 |
def on_connect(self, client, userdata, flags, rc, properties=None): |
10 | 11 |
print("Connected with result code " + str(rc)) |
11 |
self.connected.emit()
|
|
12 |
self.callback.onConnected()
|
|
12 | 13 |
# Subscribing in on_connect() means that if we lose the connection and |
13 | 14 |
# reconnect then subscriptions will be renewed. |
14 | 15 |
client.subscribe("#") |
... | ... | |
16 | 17 |
# The callback for when a PUBLISH message is received from the server. |
17 | 18 |
def on_message(self, client, userdata, message): |
18 | 19 |
print(message.topic + " " + str(message.payload.decode("utf-8"))) |
19 |
self.newMessage.emit(message.topic + " " + str(message.payload.decode("utf-8"))) |
|
20 |
m = Message(message.topic, 0, '', '', 1) |
|
21 |
self.callback.onMessage(m) |
|
20 | 22 |
|
21 | 23 |
def on_disconnect(self, client, userdata, rc): |
22 |
self.disconnected.emit()
|
|
24 |
self.callback.onDisconnected()
|
|
23 | 25 |
|
24 | 26 |
def start(self): |
25 | 27 |
client = mqtt.Client() |
aswi2021vochomurka/service/subscriber.py | ||
---|---|---|
1 | 1 |
from PyQt5.QtCore import pyqtSignal, QObject |
2 | 2 |
|
3 |
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback |
|
3 | 4 |
|
4 |
class Subscriber(QObject): |
|
5 |
connected = pyqtSignal() |
|
6 |
disconnected = pyqtSignal() |
|
7 |
error = pyqtSignal(Exception) |
|
8 |
newMessage = pyqtSignal(str) |
|
5 |
|
|
6 |
class Subscriber: |
|
7 |
callback: SubscriberCallback |
|
8 |
|
|
9 |
def __init__(self, callback: SubscriberCallback): |
|
10 |
self.callback = callback |
|
9 | 11 |
|
10 | 12 |
def start(self): raise NotImplementedError |
aswi2021vochomurka/service/subscriber_callback.py | ||
---|---|---|
1 |
from aswi2021vochomurka.model.Message import Message |
|
2 |
|
|
3 |
|
|
4 |
class SubscriberCallback: |
|
5 |
def onConnected(self): raise NotImplementedError |
|
6 |
|
|
7 |
def onDisconnected(self): raise NotImplementedError |
|
8 |
|
|
9 |
def onError(self): raise NotImplementedError |
|
10 |
|
|
11 |
def onMessage(self, message: Message): raise NotImplementedError |
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
1 |
from PyQt5.QtCore import QSize, QThread |
|
1 |
from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal
|
|
2 | 2 |
from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit |
3 | 3 |
|
4 |
from aswi2021vochomurka.model.Message import Message |
|
4 | 5 |
from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber |
6 |
from aswi2021vochomurka.service.subscriber import Subscriber |
|
7 |
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback |
|
8 |
|
|
9 |
|
|
10 |
class Worker(QObject, SubscriberCallback): |
|
11 |
connected = pyqtSignal() |
|
12 |
disconnected = pyqtSignal() |
|
13 |
error = pyqtSignal(Exception) |
|
14 |
newMessage = pyqtSignal(str) |
|
15 |
subscriber: Subscriber = None |
|
16 |
|
|
17 |
def start(self): |
|
18 |
self.subscriber = MQTTSubscriber(self) |
|
19 |
self.subscriber.start() |
|
20 |
|
|
21 |
def onConnected(self): |
|
22 |
self.connected.emit() |
|
23 |
|
|
24 |
def onDisconnected(self): |
|
25 |
self.disconnected.emit() |
|
26 |
|
|
27 |
def onError(self): |
|
28 |
self.error.emit() |
|
29 |
|
|
30 |
def onMessage(self, message: Message): |
|
31 |
self.newMessage.emit(message.topic) |
|
5 | 32 |
|
6 | 33 |
|
7 | 34 |
class MainView(QMainWindow): |
8 |
worker = None |
|
9 |
workerThread = None |
|
35 |
worker: Worker = None
|
|
36 |
workerThread: QThread = None
|
|
10 | 37 |
|
11 | 38 |
def __init__(self): |
12 | 39 |
QMainWindow.__init__(self) |
... | ... | |
24 | 51 |
|
25 | 52 |
def initSubscriber(self): |
26 | 53 |
self.workerThread = QThread() |
27 |
self.worker = MQTTSubscriber()
|
|
54 |
self.worker = Worker()
|
|
28 | 55 |
self.worker.moveToThread(self.workerThread) |
29 | 56 |
self.workerThread.started.connect(self.worker.start) |
30 | 57 |
self.worker.newMessage.connect( |
poetry.lock | ||
---|---|---|
26 | 26 |
optional = false |
27 | 27 |
python-versions = ">=3.5" |
28 | 28 |
|
29 |
[[package]] |
|
30 |
name = "recordclass" |
|
31 |
version = "0.14.3" |
|
32 |
description = "Mutable variants of tuple (mutabletuple) and collections.namedtuple (recordclass), which support assignments and more memory saving variants (dataobject, structclass, litelist, ...)." |
|
33 |
category = "main" |
|
34 |
optional = false |
|
35 |
python-versions = "*" |
|
36 |
|
|
29 | 37 |
[metadata] |
30 | 38 |
lock-version = "1.1" |
31 | 39 |
python-versions = "^3.8" |
32 |
content-hash = "095d3d22c000ec45381dc27326be154edd194e5d574bc0af2941f0cf8a385159"
|
|
40 |
content-hash = "2880a04cfc48e9d672fd4b9e4a1f82906e5f77f098ecbd615194242e0ee76ad0"
|
|
33 | 41 |
|
34 | 42 |
[metadata.files] |
35 | 43 |
pyqt5 = [ |
... | ... | |
68 | 76 |
{file = "PyQt5_sip-12.8.1-cp39-cp39-win_amd64.whl", hash = "sha256:f168f0a7f32b81bfeffdf003c36f25d81c97dee5eb67072a5183e761fe250f13"}, |
69 | 77 |
{file = "PyQt5_sip-12.8.1.tar.gz", hash = "sha256:30e944db9abee9cc757aea16906d4198129558533eb7fadbe48c5da2bd18e0bd"}, |
70 | 78 |
] |
79 |
recordclass = [ |
|
80 |
{file = "recordclass-0.14.3-cp27-cp27m-win32.whl", hash = "sha256:016a70b9e1d93e184a065a23d9bd15387c154375a166fd8868db96f362d77d94"}, |
|
81 |
{file = "recordclass-0.14.3-cp27-cp27m-win_amd64.whl", hash = "sha256:c9941edde67077f95e83dfd69795b2bc6cff13e05ef537b4a57cddfb8fac55f9"}, |
|
82 |
{file = "recordclass-0.14.3-cp36-cp36m-win32.whl", hash = "sha256:867f94a6c96474e2b52d50359adbcd1eaf18202c11654b662022a700831dfb7d"}, |
|
83 |
{file = "recordclass-0.14.3-cp36-cp36m-win_amd64.whl", hash = "sha256:3ced6c8d7d34d573fd7f926b47b5cdb2875b986841f1464eb9d41777edb63f7e"}, |
|
84 |
{file = "recordclass-0.14.3-cp37-cp37m-win32.whl", hash = "sha256:2c7dd437b2d766954e0468bdd49e444f24fc82bb080f57a4901e3718c678fcf6"}, |
|
85 |
{file = "recordclass-0.14.3-cp37-cp37m-win_amd64.whl", hash = "sha256:3c885d6d92c3306846c36797a03712e7c9dea6cc01b61f45305a815c87ea5a0c"}, |
|
86 |
{file = "recordclass-0.14.3-cp38-cp38-win32.whl", hash = "sha256:b5c25065b5d2825122fd67ac43053033bd85c209b261264ca3e2460b8308336d"}, |
|
87 |
{file = "recordclass-0.14.3-cp38-cp38-win_amd64.whl", hash = "sha256:57ba80fe2b3fc61cd878b3cd90ca1445a48292baf5d154055d671dc76c521bc1"}, |
|
88 |
{file = "recordclass-0.14.3-cp39-cp39-win32.whl", hash = "sha256:8a99639842291349667133a0c4076b2e5e483c4628da342715072037902d5884"}, |
|
89 |
{file = "recordclass-0.14.3-cp39-cp39-win_amd64.whl", hash = "sha256:176af306e9b3a13a97b3e8e87228a2975f87173048ab5eafbd0ba9f2838cadb0"}, |
|
90 |
{file = "recordclass-0.14.3.tar.gz", hash = "sha256:f2a0a42ec1128be81e28b8ba82ef1a1dccc41efbec6ecf97866915649645222f"}, |
|
91 |
] |
pyproject.toml | ||
---|---|---|
7 | 7 |
[tool.poetry.dependencies] |
8 | 8 |
python = "^3.8" |
9 | 9 |
PyQt5 = "^5.15.4" |
10 |
recordclass = "^0.14.3" |
|
10 | 11 |
|
11 | 12 |
[tool.poetry.dev-dependencies] |
12 | 13 |
|
Také k dispozici: Unified diff
Re #8566 - setup callback