私はvery simple interface between MQTT broker and ASGIを実装しました。まだ実験的であり、いくつかの制限がありますが、MQTTブローカーで公開されたメッセージをフェッチするために使用できます(またはコードを例として使用します)。 (消費者を実装
$ asgimqtt my_django_project.asgi:channels_layer
がmy_django_project/routing.py
from channels import route
from my_django_app.consumers import on_mqtt_message
channels_routing = [
route("mqtt.sub", on_mqtt_message),
]
にルートを定義し(daphneと同様)
$ systemctl start mosquitto
実行MQTT-ASGIインターフェース:
MQTTブローカーを実行MQTTメッセージをデータベースに格納するなど)my_django_app/consumers.py
from .models import MqttMessage
def on_mqtt_message(message):
# do something with the message
MqttMessage(topic=message["topic"],
payload=message["payload"],
qos=message["qos",
host=message["host"],
port=message["port"]).save()