|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Authors: Tom Kralidis <[email protected]> |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Tom Kralidis |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person |
| 8 | +# obtaining a copy of this software and associated documentation |
| 9 | +# files (the "Software"), to deal in the Software without |
| 10 | +# restriction, including without limitation the rights to use, |
| 11 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the |
| 13 | +# Software is furnished to do so, subject to the following |
| 14 | +# conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be |
| 17 | +# included in all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +# |
| 28 | +# ================================================================= |
| 29 | + |
| 30 | +import logging |
| 31 | + |
| 32 | +from paho.mqtt import client as mqtt_client |
| 33 | + |
| 34 | +from pycsw.broker.base import BasePubSubClient |
| 35 | + |
| 36 | +LOGGER = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +class MQTTPubSubClient(BasePubSubClient): |
| 40 | + """MQTT client""" |
| 41 | + |
| 42 | + def __init__(self, broker_url): |
| 43 | + """ |
| 44 | + Initialize object |
| 45 | +
|
| 46 | + :param publisher_def: provider definition |
| 47 | +
|
| 48 | + :returns: pycsw.pubsub.mqtt.MQTTClient |
| 49 | + """ |
| 50 | + |
| 51 | + super().__init__(broker_url) |
| 52 | + self.type = 'mqtt' |
| 53 | + self.port = self.broker_url.port |
| 54 | + |
| 55 | + self.userdata = {} |
| 56 | + |
| 57 | + msg = f'Connecting to broker {self.broker_safe_url} with id {self.client_id}' # noqa |
| 58 | + LOGGER.debug(msg) |
| 59 | + self.conn = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2, |
| 60 | + client_id=self.client_id) |
| 61 | + |
| 62 | + self.conn.enable_logger(logger=LOGGER) |
| 63 | + |
| 64 | + if None not in [self.broker_url.username, self.broker_url.password]: |
| 65 | + LOGGER.debug('Setting credentials') |
| 66 | + self.conn.username_pw_set( |
| 67 | + self.broker_url.username, |
| 68 | + self.broker_url.password) |
| 69 | + |
| 70 | + if self.port is None: |
| 71 | + if self.broker_url.scheme == 'mqtts': |
| 72 | + self.port = 8883 |
| 73 | + else: |
| 74 | + self.port = 1883 |
| 75 | + |
| 76 | + if self.broker_url.scheme == 'mqtts': |
| 77 | + self.conn.tls_set(tls_version=2) |
| 78 | + |
| 79 | + def connect(self) -> None: |
| 80 | + """ |
| 81 | + Connect to an MQTT broker |
| 82 | + :returns: None |
| 83 | + """ |
| 84 | + |
| 85 | + self.conn.connect(self.broker_url.hostname, self.port) |
| 86 | + LOGGER.debug('Connected to broker') |
| 87 | + |
| 88 | + def pub(self, topic: str, message: str, qos: int = 1) -> bool: |
| 89 | + """ |
| 90 | + Publish a message to a broker/topic |
| 91 | + :param topic: `str` of topic |
| 92 | + :param message: `str` of message |
| 93 | + :returns: `bool` of publish result |
| 94 | + """ |
| 95 | + |
| 96 | + LOGGER.debug(f'Publishing to broker {self.broker_safe_url}') |
| 97 | + LOGGER.debug(f'Topic: {topic}') |
| 98 | + LOGGER.debug(f'Message: {message}') |
| 99 | + |
| 100 | + result = self.conn.publish(topic, message, qos) |
| 101 | + LOGGER.debug(f'Result: {result}') |
| 102 | + |
| 103 | + # TODO: investigate implication |
| 104 | + # result.wait_for_publish() |
| 105 | + |
| 106 | + if result.is_published: |
| 107 | + LOGGER.debug('Message published') |
| 108 | + return True |
| 109 | + else: |
| 110 | + msg = f'Publishing error code: {result[1]}' |
| 111 | + LOGGER.warning(msg) |
| 112 | + return False |
| 113 | + |
| 114 | + def __repr__(self): |
| 115 | + return f'<MQTTPubSubClient> {self.broker_safe_url}' |
0 commit comments