Files
Web2Mqtt/web2mqtt/views.py

48 lines
1.1 KiB
Python

from datetime import datetime
from flask import Flask, render_template,request
from . import app, client
@app.route("/")
def home():
return render_template("home.html")
@app.route("/about/")
def about():
return render_template("about.html")
@app.route("/contact/")
def contact():
return render_template("contact.html")
@app.route("/hello/")
@app.route("/hello/<name>")
def hello_there(name = None):
return render_template(
"hello_there.html",
name=name,
date=datetime.now()
)
@app.route("/api/data")
def get_data():
return app.send_static_file("data.json")
CMD = "web2mqtt/command"
@app.route("/cmnd")
def command():
opts = ""
command = CMD
if request.args.get('cmnd'):
command += "/" + request.args.get('cmnd')
if request.args.get('opts'):
opts = request.args.get('opts')
if not client.is_connected():
print("reconnection to mqtt server...")
client.connect("192.168.178.36",1884,keepalive=60)
client.publish(command,opts)
content = f"Command: {command} triggered with opts: {opts}"
return content