backend rework- full test 1
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
import routes.shared as shared
|
||||
from flask import Flask, jsonify, request
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
import random
|
||||
import sqlite3
|
||||
import os
|
||||
from modules.persistence import esp_conn_infos
|
||||
import datetime
|
||||
from modules.socketio import resend_static_data
|
||||
|
||||
esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp')
|
||||
|
||||
@@ -15,11 +17,6 @@ MQTT_BROKER = "localhost" # oder IP/Domain
|
||||
MQTT_PORT = 1883
|
||||
MQTT_TOPIC = "coffee/command"
|
||||
|
||||
@esp.route('/')
|
||||
def fetch_command():
|
||||
pCd = shared.pending_command
|
||||
shared.reset_command()
|
||||
return jsonify(pCd)
|
||||
|
||||
@esp.route('/online', methods=['POST'])
|
||||
def esp_online():
|
||||
@@ -27,6 +24,12 @@ def esp_online():
|
||||
sender_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
|
||||
esp_ip = data.get("ip", "unknown")
|
||||
|
||||
esp_conn_infos["ip_local"] = esp_ip
|
||||
esp_conn_infos["ip_global"] = sender_ip
|
||||
esp_conn_infos["last_seen"] = datetime.now()
|
||||
esp_conn_infos["connection_valid"] = True
|
||||
resend_static_data()
|
||||
|
||||
print(f"ESP ONLINE von IP: {esp_ip}, roher IP: {sender_ip}")
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
pending_command = {'command': None, 'command-URL': None, 'command-expected': None, 'command-expected-URL': None}
|
||||
|
||||
def reset_command():
|
||||
global pending_command
|
||||
pending_command = {
|
||||
'command': None,
|
||||
'command-URL': None,
|
||||
'command-expected': None,
|
||||
'command-expected-topic': None
|
||||
}
|
||||
@@ -1,25 +1,30 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
import routes.shared as shared
|
||||
unsecure = Blueprint('unsecure', __name__, url_prefix='/unsecure')
|
||||
from modules.persistence import load_dict, save_dict
|
||||
from modules.persistence import esp_conn_infos
|
||||
# from flask_socketio import SocketIO
|
||||
from modules.socketio import resend_static_data
|
||||
|
||||
|
||||
# def resend_static_data():
|
||||
# water = load_dict("water")
|
||||
# beans = load_dict("beans")
|
||||
# machine = load_dict("machine")
|
||||
# socketio.emit('static_data', {
|
||||
# 'water': water,
|
||||
# 'beans': beans,
|
||||
# 'machine': machine,
|
||||
# 'esp_conn_infos': esp_conn_infos
|
||||
# })
|
||||
|
||||
@unsecure.route('/')
|
||||
def index():
|
||||
return render_template('index.html', title='gimmiCoffee')
|
||||
water = load_dict("water")
|
||||
beans = load_dict("beans")
|
||||
machine = load_dict("machine")
|
||||
print(f"Water: {water}, Beans: {beans}, Machine: {machine}")
|
||||
return render_template('index.html', title='gimmiCoffee', water=water, beans=beans, machine=machine, esp_conn_infos=esp_conn_infos)
|
||||
|
||||
@unsecure.route('/send')
|
||||
def send_command():
|
||||
pCd = shared.pending_command
|
||||
befehl = request.args.get('befehl')
|
||||
if befehl:
|
||||
pCd['command'] = befehl
|
||||
pCd['command-URL'] = '/unsecure/esp/someURI'
|
||||
pCd.update({'extra': 'test'})
|
||||
print(pCd)
|
||||
return f"Befehl '{pCd}' gespeichert."
|
||||
return "Kein Befehl angegeben.", 400
|
||||
|
||||
# @unsecure.route('/live')
|
||||
# def test():
|
||||
# return render_template('live.html', users=[{'name': 'Max'}, {'name': 'Moritz'}, {'name': 'Hans'}])
|
||||
@unsecure.route('/update')
|
||||
def update():
|
||||
resend_static_data()
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
Reference in New Issue
Block a user