Esempio n. 1
0
        def __init__(self, app: FastAPI, mqtt_host: str, mqtt_port: int,
                     mqtt_client_name: str):
            logger.info(
                f'Creating MQTT client {mqtt_host}, {mqtt_port}, {mqtt_client_name}'
            )
            self.mqtt_client_name = mqtt_client_name
            self.mqtt_host = mqtt_host
            self.mqtt_port = mqtt_port
            self.app = app

            # SSL
            context = False

            if MQTT_BROKER_CERT_FILE is not None:
                context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
                context.load_verify_locations(MQTT_BROKER_CERT_FILE)

            # TODO XXX there is now way to pass client name...
            mqtt_config = MQTTConfig(host=self.mqtt_host,
                                     port=self.mqtt_port,
                                     username=MQTT_USERNAME,
                                     password=MQTT_PASSWORD,
                                     version=MQTTv311,
                                     ssl=context)
            self.fast_mqtt = FastMQTT(config=mqtt_config)
Esempio n. 2
0
def initialize_mqtt(fastapi_app):

    if not config.TESTING_NO_MQTT:
        logger.debug(
            f'MQTT host: {config.MQTT_HOST}:{config.MQTT_PORT} | user: {config.MQTT_USERNAME}'
        )
        context = False
        if config.MQTT_BROKER_CERT_FILE is not None:
            context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
            context.load_verify_locations(config.MQTT_BROKER_CERT_FILE)
        mqtt_config = MQTTConfig(host=config.MQTT_HOST,
                                 port=config.MQTT_PORT,
                                 username=config.MQTT_USERNAME,
                                 password=config.MQTT_PASSWORD,
                                 version=MQTTv311,
                                 ssl=context)
        mqtt = FastMQTT(config=mqtt_config)
        mqtt.init_app(fastapi_app)

    else:
        mqtt = DummyMQTT()

    return mqtt
Esempio n. 3
0
    class MQTTClient:
        def __init__(self, app: FastAPI, mqtt_host: str, mqtt_port: int,
                     mqtt_client_name: str):
            logger.info(
                f'Creating MQTT client {mqtt_host}, {mqtt_port}, {mqtt_client_name}'
            )
            self.mqtt_client_name = mqtt_client_name
            self.mqtt_host = mqtt_host
            self.mqtt_port = mqtt_port
            self.app = app

            # SSL
            context = False

            if MQTT_BROKER_CERT_FILE is not None:
                context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
                context.load_verify_locations(MQTT_BROKER_CERT_FILE)

            # TODO XXX there is now way to pass client name...
            mqtt_config = MQTTConfig(host=self.mqtt_host,
                                     port=self.mqtt_port,
                                     username=MQTT_USERNAME,
                                     password=MQTT_PASSWORD,
                                     version=MQTTv311,
                                     ssl=context)
            self.fast_mqtt = FastMQTT(config=mqtt_config)

        def publish(self, topic, message):
            logger.info(f' publishing to {topic}. message: {message}')
            return self.fast_mqtt.publish(topic, message)

        async def connect(self):
            logger.info("before connect")

            self.fast_mqtt.init_app(self.app)
            logger.info("after connect")
Esempio n. 4
0
logger.debug(f'MQTT host: {MQTT_HOST}:{MQTT_PORT} | user: {MQTT_USERNAME}')

context = False

if MQTT_BROKER_CERT_FILE is not None:
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    context.load_verify_locations(MQTT_BROKER_CERT_FILE)

mqtt_config = MQTTConfig(host=MQTT_HOST,
                         port=MQTT_PORT,
                         username=MQTT_USERNAME,
                         password=MQTT_PASSWORD,
                         version=MQTTv311,
                         ssl=context)

fast_mqtt = FastMQTT(config=mqtt_config)
fast_mqtt.init_app(app)


async def pulse():
    tick = True
    while True:
        message = "tick" if tick else "tack"
        tick = not tick
        logger.info(message)
        await asyncio.sleep(10)


@app.on_event("startup")
async def startup_event():
    app.state.customer_positions = {}
Esempio n. 5
0
            "example": {
                "host":
                "10.0.2.15",
                "portRange":
                "22-80",
                "description":
                "Scan the port from 22 to 80 of the ip address 10.0.2.15"
            }
        }


## for docker - compose using mqtt - broker name from docker compose.
## for normal leave that blank to fall off to localhost.
mqtt_config = MQTTConfig(host="mqtt")

mqtt = FastMQTT(config=mqtt_config)

mqtt.init_app(app)


@mqtt.on_connect()
def connect(client, flags, rc, properties):
    mqtt.client.subscribe(
        "/mqtt/toModel/#")  # subscribing mqtt topic wildcard- multi-level
    print("connected: ", client, flags, rc, properties)


@mqtt.on_message()
async def message(client, topic, payload, qos, properties):
    print("received message: ", topic, jsonpickle.decode(payload.decode()),
          qos, properties)
Esempio n. 6
0
 def get_mqtt_app(self):
     return FastMQTT(config=self.get_mqtt_config())
Esempio n. 7
0
from typing import Optional

from fastapi import FastAPI

from fastapi_mqtt import FastMQTT, MQQTConfig


app = FastAPI()

mqtt_config = MQQTConfig(host = "mqtt-broker")

mqtt = FastMQTT(
    config=mqtt_config
)


@app.on_event("startup")
async def startup():
    await mqtt.connection()


@app.on_event("shutdown")
async def shutdown():
    await mqtt.client.disconnect()


@app.post("/send/{payload}")
async def request_answer(payload: str):
    await mqtt.publish("/from-service-a", payload)