Metadata-Version: 2.1
Name: mqtt_logger
Version: 0.3.5
Summary: Python based MQTT to SQLite3 logger
Home-page: https://github.com/Blake-Haydon/mqtt-logger
License: MIT
Keywords: MQTT,SQLite3
Author: Blake Haydon
Author-email: blake.a.haydon@gmail.com
Requires-Python: >=3.7,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: paho-mqtt (>=1.6.1,<2.0.0)
Requires-Dist: rich (>=12.0.0,<13.0.0)
Description-Content-Type: text/markdown

# MQTT to SQLite Logger

[![PyPI version](https://badge.fury.io/py/mqtt-logger.svg)](https://badge.fury.io/py/mqtt-logger)
[![Python package](https://github.com/Blake-Haydon/mqtt-logger/actions/workflows/python-package.yml/badge.svg)](https://github.com/Blake-Haydon/mqtt-logger/actions/workflows/python-package.yml)
[![Upload Python Package](https://github.com/Blake-Haydon/mqtt-logger/actions/workflows/python-publish.yml/badge.svg)](https://github.com/Blake-Haydon/mqtt-logger/actions/workflows/python-publish.yml)

## Table of Contents

- [MQTT to SQLite Logger](#mqtt-to-sqlite-logger)
  - [Table of Contents](#table-of-contents)
  - [Description](#description)
  - [Installation](#installation)
  - [Example Usage](#example-usage)
    - [Recording MQTT Messages](#recording-mqtt-messages)
    - [Playback Recorded MQTT Messages](#playback-recorded-mqtt-messages)
  - [Database](#database)
    - [`LOG` Table](#log-table)
    - [`RUN` Table](#run-table)

## Description

`mqtt-logger` allows for asynchronous data logging of MQTT messages to a SQLite database. It also allows for the playback of previously recorded MQTT messages.

## Installation

To install `mqtt-logger` you can simply use `pip`.

```bash
pip install mqtt-logger
```

## Example Usage

### Recording MQTT Messages

This example records messages to the `test/#` topic using a public MQTT broker. It will record for 10 seconds. If you
are using a private broker, you may need to set the `username` and `password` parameters.

<!-- poetry run python examples/10s_recording.py -->

_Example recorder taken from [examples/10s_recording.py](examples/10s_recording.py)_

```python
import mqtt_logger
import os
import time

# Initalise mqtt recorder object
rec = mqtt_logger.Recorder(
    sqlite_database_path=os.path.join(os.path.dirname(__file__), "MQTT_log.db"),
    topics=["test/#"],
    broker_address="broker.hivemq.com",
    verbose=True,
)

# Start the logger, wait 10 seconds and stop the logger
rec.start()
time.sleep(10)
rec.stop()
```

### Playback Recorded MQTT Messages

This example plays back previously recorded MQTT messages from `mqtt_logger.Recorder`. If you are using a private
broker, you may need to set the `username` and `password` parameters.

<!-- poetry run python examples/10s_playback.py -->

_Example recorder taken from [examples/10s_playback.py](examples/10s_playback.py)_

```python
import mqtt_logger
import os

# Initalise playback object
playback = mqtt_logger.Playback(
    sqlite_database_path=os.path.join(os.path.dirname(__file__), "MQTT_log.db"),
    broker_address="broker.hivemq.com",
    verbose=True,
)

# Start playback at 2x speed (twice as fast)
playback.play(speed=2)
```

## Database

The SQLite database has two tables called `LOG` and `RUN`. The `LOG` table contains the messages that are being logged. The `RUN` table contains the information about the current run of the program.

### `LOG` Table

| ROW NAME  | DESCRIPTION                                            |
| --------- | ------------------------------------------------------ |
| ID        | Unique number assigned to each message (ascending int) |
| RUN_ID    | ID of the current run (ascending int)                  |
| UNIX_TIME | Time when the message was received                     |
| TOPIC     | MQTT topic                                             |
| MESSAGE   | MQTT message received                                  |

---

### `RUN` Table

| ROW NAME        | DESCRIPTION                                   |
| --------------- | --------------------------------------------- |
| ID              | Unique number assigned to run (ascending int) |
| START_UNIX_TIME | Time when logger was started                  |
| END_UNIX_TIME   | Time when logger was stopped                  |

---

