README.md 2.06 KB
Newer Older
Anton Malmrot's avatar
Anton Malmrot committed
1
# LibMQTT
Magnus Knutas's avatar
Magnus Knutas committed
2
[![pipeline status](https://code.tf/components/libmqtt/badges/master/pipeline.svg)](https://code.tf/components/libmqtt/commits/master)
Magnus Knutas's avatar
Magnus Knutas committed
3
[![coverage report](https://code.tf/components/libmqtt/badges/master/coverage.svg)](http://components.pages.code.tf/libmqtt/)
Magnus Knutas's avatar
Magnus Knutas committed
4

5
Lib for connecting to mosquitto in services
Anton Malmrot's avatar
Anton Malmrot committed
6

Magnus Knutas's avatar
Magnus Knutas committed
7
8
9
10
11
## Installation
```bash
pip install https://code.tf/components/libmqtt/repository/master/archive.zip
```

Anton Malmrot's avatar
Anton Malmrot committed
12
13
## Testing
```bash
Magnus Knutas's avatar
Magnus Knutas committed
14
./setup.py flake8
Anton Malmrot's avatar
Anton Malmrot committed
15
16
```
```bash
Magnus Knutas's avatar
Magnus Knutas committed
17
./setup.py lint
Anton Malmrot's avatar
Anton Malmrot committed
18
19
```
```bash
Magnus Knutas's avatar
Magnus Knutas committed
20
./setup.py test
Anton Malmrot's avatar
Anton Malmrot committed
21
22
```

Magnus Knutas's avatar
Magnus Knutas committed
23
24
25
26
27
28
29
## Usage
### Add routes
```bash
mkdir transport
```
create file transport/mqtt_router.py
```python
Bilbo Brovall's avatar
Bilbo Brovall committed
30
31
32
33
34
35
36
37
38
"""
The dispatch function passes the following kwargs
endpoint
client
user_data
message
context: is the named parameters in the path
?payload_data: is message parsed as json
"""
Magnus Knutas's avatar
Magnus Knutas committed
39
40
41
42
43
44
45
from libmqtt.router import hook_route

@hook_route(('import/data',))  # hooks to given topic/topics
def import_data(*args, **kwargs):  # pylint: disable=unused-argument
    """Importing data."""
    csv = kwargs.get('message').payload.decode("utf-8")
    ImportCsv(data=csv)
Magnus Knutas's avatar
Magnus Knutas committed
46
    kwargs['client'].publish('hello/world', 'message can be string or json')
Bilbo Brovall's avatar
Bilbo Brovall committed
47
48
49
50
51
52
53
54


# sending {"gender": "man"} to hello/bilbo results in
@hook_route(('hello/(?P<name>+)',))  # hooks to given hello/#
def import_data(*args, **kwargs):  # pylint: disable=unused-argument
    """sending {"gender": "man} to hello/bilbo results in"""
    context = kwargs.get('context', {})  # is {"name": "bilbo"}
    data = kwargs.get('payload_data', {})  # is {"gender": "man"}
Magnus Knutas's avatar
Magnus Knutas committed
55
56
```

Magnus Knutas's avatar
Magnus Knutas committed
57
### Send messages
Magnus Knutas's avatar
Magnus Knutas committed
58
```python
Magnus Knutas's avatar
Magnus Knutas committed
59
60
61
>>> from libmqtt.sender import get_client
>>> client.publish('/hello/world', 'message can be string or json')
>>> client.loop_stop()
Magnus Knutas's avatar
Magnus Knutas committed
62
63
64
65
66
67
68
69
```

### Start application
```python
>>> from libmqtt.application import Application
>>> Application('ip', 'username', 'pwd')  # Will start an application and run "forever"
```

Anton Malmrot's avatar
Anton Malmrot committed
70
71
72
73
74
## External packages used
* [Paho-mqtt](https://www.eclipse.org/paho/)

### Testing
* [flake8](http://flake8.pycqa.org/en/latest/)
Magnus Knutas's avatar
Magnus Knutas committed
75
* [pylint](https://docs.pylint.org)
Bilbo Brovall's avatar
Bilbo Brovall committed
76
* [setuptools-lint](https://pypi.python.org/pypi/setuptools-lint)