From cc7eaef635558f245368b7766a0df710d205b116 Mon Sep 17 00:00:00 2001 From: Clavier Paul Date: Tue, 10 May 2022 15:02:41 +0200 Subject: [PATCH] version flask proxy + htmx --- .gitignore | 1 + main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ requirement.txt | 3 ++- templates/index.html | 17 +++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f21b54 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/venv/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..8bf3367 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +from flask import Flask, render_template, request +from flask_mqtt import Mqtt + +app = Flask(__name__) +app.config['MQTT_BROKER_URL'] = 'localhost' +app.config['MQTT_KEEPALIVE'] = 5 +app.config['MQTT_TLS_ENABLED'] = False +mqtt = Mqtt(app) + +DATA = [] + +@mqtt.on_connect() +def connect(client, userdata, flags, rc): + mqtt.subscribe('main') + +@mqtt.on_message() +def message(client, userdata, message): + data = dict( + topic=message.topic, + payload=message.payload + ) + DATA.append(data) + +@app.route('/get') +def get(): + if not DATA: + return '' + return '' + +@app.route('/send', methods=["POST"]) +def send(): + message = request.form.get('msg') + mqtt.publish('main', message) + return 'OK' + +@app.route('/') +def home(): + return render_template("index.html") + +app.run() \ No newline at end of file diff --git a/requirement.txt b/requirement.txt index d173f65..78435cb 100644 --- a/requirement.txt +++ b/requirement.txt @@ -1 +1,2 @@ -paho-mqtt \ No newline at end of file +flask +flask-mqtt \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..ba580ec --- /dev/null +++ b/templates/index.html @@ -0,0 +1,17 @@ + + + + + Client MQTT avec proxy flask + + + +

Derniers messages:

+ +

Envoyer un message

+
+ + +
+ + \ No newline at end of file