Skip to main content

Simple application framework for MQTT based applications

Project description

Python MQTT Framework

License: MIT Latest release CI

Simple application framework for MQTT based applications. Purpose of the library is to simplify the application and minimize the boilerplate code.

Features

  • Relaiable MQTT connection and simple data publish and subscribe functionality
  • Interval and cron scheduler to e.g. update data to MQTT periodically
  • Environment variable based configuration
  • REST interface (e.g. /healtcheck)
  • Prometheus metrics (/metrics)
  • Easy logging
  • And much more ...

Environament variables

Following environment varibles are supported by the framework. Application can extend variables by the configuration.

Variable Default Descrition
CFG_APP_NAME Name of the app.
CFG_CONFIG_FILE None Name of the configuration file. See details from section Configuration files.
CFG_LOG_LEVEL INFO Logging level: CRITICAL, ERROR, WARNING, INFO or DEBUG.
CFG_UPDATE_CRON_SCHEDULE Update interval in cron format. Both Unix (5 elements) and Spring (6 elements) formats are supported.
CFG_UPDATE_INTERVAL 60 Update interval in seconds. 0 = disabled
CFG_DELAY_BEFORE_FIRST_TRY 5 Delay before first try in seconds.
CFG_MQTT_CLIENT_ID <CFG_APP_NAME> the unique client id string used when connecting to the broker.
CFG_MQTT_BROKER_URL 127.0.0.1 MQTT broker URL that should be used for the connection.
CFG_MQTT_BROKER_PORT 1883 MQTT broker port that should be used for the connection.
CFG_MQTT_USERNAME None MQTT broker username used for authentication. If none is provided authentication is disabled.
CFG_MQTT_PASSWORD None MQTT broker password used for authentication.
CFG_MQTT_TLS_ENABLED False Enable TLS.
CFG_MQTT_TLS_CA_CERTS None A string path to the Certificate Authority certificate files that are to be treated as trusted by this client.
CFG_MQTT_TLS_CERTFILE None String pointing to the PEM encoded client certificate.
CFG_MQTT_TLS_KEYFILE None String pointing to the PEM encoded client private key.
CFG_MQTT_TLS_INSECURE False Configure verification of the server hostname in the server certificate.
CFG_MQTT_TOPIC_PREFIX <CFG_APP_NAME>/ MQTT topic prefix.
CFG_WEB_STATIC_DIR /web/static Directory name for static pages.
CFG_WEB_TEMPLATE_DIR /web/templates Directory name for templates.

Configuration files

Configuration file name can be given from framework run/start function. If file name is not given, framework tries to read environment variable CFG_CONFIG_FILE. If configuration file is defined, configuration variables are loaded from the file and then overrided by the environment variables if exists. In files, variable names should be without CFG_ prefix. Py, toml and json formatted files are supported. Format is recognized from the file type suffix. If file type is not toml or json, file is loaded as py file.

MQTT topics

Following MQTT topics are available by default from the framework.

Topic Descrition
/updateNow Do immidiate update. Call do_update method from the app.
/setLogLevel Set log level. Supported values: TRACE, DEBUG, INFO, WARNING, ERROR or CRITICAL.

REST interface

Following default API is provided by the framework.

Path Method Descrition
host:port/healthy GET Do healthy check.
host:port/update GET Call app do_update function immidiately.
host:port/jobs GET Return job sceduling in json format.

Prometheus metrics

Prometheus metrics are available in <host:port>/metrics.

Usage

Simple test application (app.py). See more features from integration test app in tests/integration/testapp folder.

from pymqttframework import Framework
from pymqttframework import Config
from pymqttframework.callbacks import Callbacks
from pymqttframework.app import TriggerSource

class MyConfig(Config):

    def __init__(self):
        super().__init__(self.APP_NAME)

    APP_NAME = 'test'

    # App specific variables

    TEST_VARIABLE = 123456

class MyApp:

    def init(self, callbacks: Callbacks) -> None:
        self.logger = callbacks.get_logger()
        self.config = callbacks.get_config()
        self.metrics_registry = callbacks.get_metrics_registry()
        self.add_url_rule = callbacks.add_url_rule
        self.publish_value_to_mqtt_topic = callbacks.publish_value_to_mqtt_topic
        self.subscribe_to_mqtt_topic = callbacks.subscribe_to_mqtt_topic
        self.counter = 0

    def get_version(self) -> str:
        return '1.0.0'

    def stop(self) -> None:
        self.logger.debug('Exit')

    def subscribe_to_mqtt_topics(self) -> None:
        self.logger.debug('Subscribe to test topic')
        self.subscribe_to_mqtt_topic('test')

    def mqtt_message_received(self, topic: str, message: str) -> None:
        if topic == 'test':
            self.logger.debug('Received data %s for topic %s', message, topic)

    def do_healthy_check(self) -> bool:
        self.logger.debug('do_healthy_check called')
        return True

    def do_update(self, trigger_source: TriggerSource) -> None:
        self.logger.debug('update called, trigger_source=%s', trigger_source)
        self.logger.debug(f'TEST_VARIABLE from config: {self.config["TEST_VARIABLE"]}')
        self.counter += 1
        self.publish_value_to_mqtt_topic('counter', self.counter)

if __name__ == '__main__':
    Framework().run(MyApp(), MyConfig())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymqttframework-3.0.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymqttframework-3.0.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file pymqttframework-3.0.1.tar.gz.

File metadata

  • Download URL: pymqttframework-3.0.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.9

File hashes

Hashes for pymqttframework-3.0.1.tar.gz
Algorithm Hash digest
SHA256 1fe192d6bc5dfaf15d69daad1da7466448b9053acf0007fd387dbe41ab0cd043
MD5 989e29de4d38ba1d72448cca40ddb6bd
BLAKE2b-256 e9a7b8ed1ae2d572019d615b96825c8ca3e4e5f801b41f8a10d4bf17f9c9341b

See more details on using hashes here.

File details

Details for the file pymqttframework-3.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pymqttframework-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bec000935ac922374f1075222598c6d1e73ca55ad03ae3918dbc4f1b7bafa289
MD5 ea52a35e577a0036a0284d16d5f6af51
BLAKE2b-256 d8f5e05a9a77f439762dd80f471e8ec64a0119af3f1dfec5a0e3bb0ac4f5b7b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page