Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1c9b8c16

Přidáno uživatelem Martin Forejt před téměř 4 roky(ů)

Re #8489 - move subscriber to service package

Zobrazit rozdíly:

aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
1
import paho.mqtt.client as mqtt
2

  
3
from aswi2021vochomurka.service.subscriber import Subscriber
4

  
5

  
6
class MQTTSubscriber(Subscriber):
7

  
8
    # The callback for when the client receives a CONNACK response from the server.
9
    def on_connect(self, client, userdata, flags, rc, properties=None):
10
        print("Connected with result code " + str(rc))
11
        self.connected.emit()
12
        # Subscribing in on_connect() means that if we lose the connection and
13
        # reconnect then subscriptions will be renewed.
14
        client.subscribe("#")
15

  
16
    # The callback for when a PUBLISH message is received from the server.
17
    def on_message(self, client, userdata, message):
18
        print(message.topic + " " + str(message.payload.decode("utf-8")))
19
        self.newMessage.emit(message.topic + " " + str(message.payload.decode("utf-8")))
20

  
21
    def on_disconnect(self, client, userdata, rc):
22
        self.disconnected.emit()
23

  
24
    def start(self):
25
        client = mqtt.Client()
26
        client.on_connect = self.on_connect
27
        client.on_message = self.on_message
28
        client.on_disconnect = self.on_disconnect
29

  
30
        try:
31
            client.connect("localhost", 1884, 60)
32
        except Exception as error:
33
            self.error.emit(error)
34
            return
35

  
36
        client.loop_forever()
aswi2021vochomurka/service/subscriber.py
1
import paho.mqtt.client as mqtt
1
from PyQt5.QtCore import pyqtSignal, QObject
2 2

  
3 3

  
4
class MQTTSubscriber:
4
class Subscriber(QObject):
5
    connected = pyqtSignal()
6
    disconnected = pyqtSignal()
7
    error = pyqtSignal(Exception)
8
    newMessage = pyqtSignal(str)
5 9

  
6
    # The callback for when the client receives a CONNACK response from the server.
7
    def on_connect(self, client, userdata, flags, rc, properties=None):
8
        print("Connected with result code " + str(rc))
9
        # Subscribing in on_connect() means that if we lose the connection and
10
        # reconnect then subscriptions will be renewed.
11
        client.subscribe("#")
12

  
13
    # The callback for when a PUBLISH message is received from the server.
14
    def on_message(self, client, userdata, message):
15
        print(message.topic + " " + str(message.payload.decode("utf-8")))
16

  
17
    def start(self):
18
        client = mqtt.Client()
19
        client.on_connect = self.on_connect
20
        client.on_message = self.on_message
21

  
22
        client.connect("localhost", 1883, 60)
23

  
24
        # Blocking call that processes network traffic, dispatches callbacks and
25
        # handles reconnecting.
26
        # Other loop*() functions are available that give a threaded interface and a
27
        # manual interface.
28
        client.loop_forever()
10
    def start(self): raise NotImplementedError
aswi2021vochomurka/view/main_view.py
1
from PyQt5.QtCore import QSize, QObject, pyqtSignal, QThread
1
from PyQt5.QtCore import QSize, QThread
2 2
from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit
3
import paho.mqtt.client as mqtt
4 3

  
5

  
6
class Worker(QObject):
7
    connected = pyqtSignal()
8
    newMessage = pyqtSignal(str)
9

  
10
    def on_connect(self, client, userdata, flags, rc, properties=None):
11
        print("Connected with result code " + str(rc))
12
        self.connected.emit()
13
        # Subscribing in on_connect() means that if we lose the connection and
14
        # reconnect then subscriptions will be renewed.
15
        client.subscribe("#")
16

  
17
    # The callback for when a PUBLISH message is received from the server.
18
    def on_message(self, client, userdata, message):
19
        print(message.topic + " " + str(message.payload.decode("utf-8")))
20
        self.newMessage.emit(message.topic + " " + str(message.payload.decode("utf-8")))
21

  
22
    def start(self):
23
        client = mqtt.Client()
24
        client.on_connect = self.on_connect
25
        client.on_message = self.on_message
26

  
27
        client.connect("localhost", 1883, 60)
28

  
29
        # Blocking call that processes network traffic, dispatches callbacks and
30
        # handles reconnecting.
31
        # Other loop*() functions are available that give a threaded interface and a
32
        # manual interface.
33
        client.loop_forever()
4
from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber
34 5

  
35 6

  
36 7
class MainView(QMainWindow):
8
    worker = None
9
    workerThread = None
37 10

  
38 11
    def __init__(self):
39 12
        QMainWindow.__init__(self)
......
47 20
        self.b.move(10, 10)
48 21
        self.b.resize(400, 200)
49 22

  
50
        self.thread = QThread()
51
        self.worker = Worker()
52
        self.worker.moveToThread(self.thread)
53
        self.thread.started.connect(self.worker.start)
23
        self.initSubscriber()
24

  
25
    def initSubscriber(self):
26
        self.workerThread = QThread()
27
        self.worker = MQTTSubscriber()
28
        self.worker.moveToThread(self.workerThread)
29
        self.workerThread.started.connect(self.worker.start)
54 30
        self.worker.newMessage.connect(
55 31
            lambda message: self.b.insertPlainText(message + "\n")
56 32
        )
57

  
58
        self.thread.start()
33
        self.workerThread.start()

Také k dispozici: Unified diff