Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c4999e79

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

Re #8566 - add params passed to start subscriber

Zobrazit rozdíly:

aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
12 12
        self.callback.onConnected()
13 13
        # Subscribing in on_connect() means that if we lose the connection and
14 14
        # reconnect then subscriptions will be renewed.
15
        client.subscribe("#")
15
        for topic in self.params.topics:
16
            print("Subscribed to topic: " + topic)
17
            client.subscribe(topic)
16 18

  
17 19
    # The callback for when a PUBLISH message is received from the server.
18 20
    def on_message(self, client, userdata, message):
......
29 31
        client.on_message = self.on_message
30 32
        client.on_disconnect = self.on_disconnect
31 33

  
34
        if not self.params.anonymous:
35
            client.tls_set()
36
            client.username_pw_set(self.params.username, self.params.password)
37

  
32 38
        try:
33
            client.connect("localhost", 1883, 60)
39
            client.connect(
40
                self.params.connection.host,
41
                self.params.connection.port,
42
                self.params.connection.timeout
43
            )
34 44
        except Exception as error:
35
            self.error.emit(error)
45
            self.callback.onError()
36 46
            return
37 47

  
38 48
        client.loop_forever()
aswi2021vochomurka/service/subscriber.py
1
from PyQt5.QtCore import pyqtSignal, QObject
2

  
3 1
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback
2
from aswi2021vochomurka.service.subscriber_params import SubscriberParams
4 3

  
5 4

  
6 5
class Subscriber:
7 6
    callback: SubscriberCallback
7
    params: SubscriberParams
8 8

  
9
    def __init__(self, callback: SubscriberCallback):
9
    def __init__(self, callback: SubscriberCallback, params: SubscriberParams):
10 10
        self.callback = callback
11
        self.params = params
11 12

  
12 13
    def start(self): raise NotImplementedError
aswi2021vochomurka/service/subscriber_params.py
1
from recordclass import RecordClass
2
from typing import List
3

  
4

  
5
class ConnectionParams(RecordClass):
6
    host: str
7
    port: int
8
    timeout: int
9

  
10

  
11
class SubscriberParams(RecordClass):
12
    # list of topics to subscribe
13
    topics: List[str]
14
    # close limit in seconds
15
    closeLimit: int
16
    # connection params
17
    connection: ConnectionParams
18
    # connect anonymously to broker, otherwise provide username and password
19
    anonymous: bool
20
    username: str = ""
21
    password: str = ""
aswi2021vochomurka/view/main_view.py
5 5
from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber
6 6
from aswi2021vochomurka.service.subscriber import Subscriber
7 7
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback
8
from aswi2021vochomurka.service.subscriber_params import SubscriberParams, ConnectionParams
8 9

  
9 10

  
10 11
class Worker(QObject, SubscriberCallback):
......
14 15
    newMessage = pyqtSignal(str)
15 16
    subscriber: Subscriber = None
16 17

  
18
    params = SubscriberParams(
19
        ["/home/1", "/home/2"],
20
        120,
21
        ConnectionParams("localhost", 1883, 60),
22
        True
23
    )
24

  
17 25
    def start(self):
18
        self.subscriber = MQTTSubscriber(self)
26
        self.subscriber = MQTTSubscriber(self, self.params)
19 27
        self.subscriber.start()
20 28

  
21 29
    def onConnected(self):

Také k dispozici: Unified diff