MQTT Endpoint Library
Project description
Endpointlib
MQTT Endpoint library
Links
How to install
From your virtual environment run:
pip install endpointlib
How to use
Basic example
This example just creates an enpoint to publish some data to the mqtt broker:
import asyncio
import random
from endpointlib.endpoint_factory import EndpointFactory
from endpointlib.helpers.loggers.logger_level import LoggerLevel
from endpointlib.helpers.loggers.logger_manager import LoggerManager
from endpointlib.helpers.loggers.logger_settings import LoggerSettings
LoggerManager.create(LoggerSettings.get_debug_settings(level=LoggerLevel.INFO))
logger = LoggerManager.get_async_logger(name='demo')
async def demo():
endpoint = EndpointFactory.basic_endpoint(('test.mosquitto.org', 1883), main_callback=main_entry_point)
await endpoint.run_forever()
async def main_entry_point(client):
await logger.info('[main_entry_point]')
#async loop
while True:
#call other async services
data = random.randrange(10, 52, 1)
data_topic = 'endpoint/data/sample'
await client.publish(data_topic, data, qos=1, retain=True)
await logger.info('Data published')
await asyncio.sleep(5)
asyncio.run(demo(), debug=True)
Advanced example
This example creates an endpoint that monitors a tcp socket device. This implementation uses a simple echo tcp server to simulate the responses, this server is not included in the package.
import asyncio
from endpointlib.endpoint_factory import EndpointFactory
from endpointlib.helpers.loggers.logger_level import LoggerLevel
from endpointlib.helpers.loggers.logger_manager import LoggerManager
from endpointlib.helpers.loggers.logger_settings import LoggerSettings
LoggerManager.create(LoggerSettings.get_debug_settings(level=LoggerLevel.INFO))
logger = LoggerManager.get_async_logger(name='demo')
async def demo():
myCallbacks = dict()
myCallbacks['endpoint/control/device1/operation01'] = on_operation_01
myCallbacks['endpoint/control/device1/operation02'] = on_operation_02
broker = ('test.mosquitto.org', 1883)
cmd = ':MONITOR1234!'
# ('host_to_monitor', 'port', 'monitor_interval', 'command_to_monitor', 'on_monitor_callback')
monitor = ('localhost', 10001, 10, cmd, on_monitor)
endpoint = EndpointFactory.socket_monitor_endpoint(mqtt_connection=broker, socket_monitor=monitor, handlers=myCallbacks)
await endpoint.run_forever()
async def on_monitor(status, client):
await logger.info('[on_monitor]: ' + status)
await client.publish('endpoint/control/device1/response/01', '1234')
async def on_operation_01(topic, payload, device):
response = await device.send_command(':M11!')
await logger.info('[on_operation_01]Response from device: ' + response)
async def on_operation_02(topic, payload, device):
response = await device.send_command(':R00!')
await logger.info('[on_operation_02]Response from device: ' + response)
asyncio.run(demo(), debug=True)
Development setup
[TODO: Development setup instructions]
Built With
- Pyhton
Author
Alfonso Franco
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for endpointlib-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0891b4c6f39355bd7b7ee2211d4577be3ffaa5a3579fba1928ba184860f617a |
|
MD5 | 89b55a13ccc93092e26650c47b2d772b |
|
BLAKE2b-256 | 24ae0b7952d3761c5e8288ce20f3ec261b5dce553a4c0028cde8a91614617380 |