esp_routes_basic and server comm

This commit is contained in:
derlole
2025-04-17 12:33:11 +00:00
parent 58f9d3b451
commit f8cf645325
5 changed files with 32 additions and 16 deletions

10
routes/esp_routes.py Normal file
View File

@@ -0,0 +1,10 @@
from flask import Blueprint, render_template, request, jsonify
import routes.shared as shared
esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp')
@esp.route('/')
def fetch_command():
pCd = shared.pending_command
shared.reset_command()
return jsonify(pCd)

10
routes/shared.py Normal file
View File

@@ -0,0 +1,10 @@
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-URL': None
}

View File

@@ -1,8 +1,8 @@
from flask import Blueprint, render_template, request, jsonify from flask import Blueprint, render_template, request, jsonify
import routes.shared as shared
unsecure = Blueprint('unsecure', __name__, url_prefix='/unsecure') unsecure = Blueprint('unsecure', __name__, url_prefix='/unsecure')
pending_command = None
@unsecure.route('/') @unsecure.route('/')
def index(): def index():
@@ -10,21 +10,16 @@ def index():
@unsecure.route('/send') @unsecure.route('/send')
def send_command(): def send_command():
global pending_command pCd = shared.pending_command
befehl = request.args.get('befehl') befehl = request.args.get('befehl')
if befehl: if befehl:
pending_command = befehl pCd['command'] = befehl
return f"Befehl '{befehl}' gespeichert." pCd['command-URL'] = '/unsecure/esp/someURI'
pCd.update({'extra': 'test'})
print(pCd)
return f"Befehl '{pCd}' gespeichert."
return "Kein Befehl angegeben.", 400 return "Kein Befehl angegeben.", 400
@unsecure.route('/fetch')
def fetch_command():
global pending_command
if pending_command:
cmd = pending_command
pending_command = None
return jsonify({'befehl': cmd})
return jsonify({'befehl': None})
@unsecure.route('/test') @unsecure.route('/test')
def test(): def test():
return render_template('test.html') return render_template('test.html')

View File

@@ -37,6 +37,7 @@
from flask import Flask from flask import Flask
from flask_socketio import SocketIO from flask_socketio import SocketIO
from routes.unsecure_routes import unsecure from routes.unsecure_routes import unsecure
from routes.esp_routes import esp
@@ -47,6 +48,7 @@ socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading')
# Blueprint registrieren # Blueprint registrieren
app.register_blueprint(unsecure) app.register_blueprint(unsecure)
app.register_blueprint(esp)
# Simuliere regelmäßige Daten-Updates # Simuliere regelmäßige Daten-Updates
import threading import threading
@@ -55,7 +57,7 @@ import time
def send_data(): def send_data():
counter = 0 counter = 0
while True: while True:
print(f"Sending data: {counter}") #print(f"Sending data: {counter}")
data = { data = {
'test': 'Live-Daten', 'test': 'Live-Daten',
'status': 'OK', 'status': 'OK',
@@ -71,4 +73,4 @@ thread.daemon = True
thread.start() thread.start()
if __name__ == '__main__': if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5000) socketio.run(app, host='0.0.0.0', port=3060)

View File

@@ -11,7 +11,6 @@
<p><strong>Status:</strong> <span id="status"></span></p> <p><strong>Status:</strong> <span id="status"></span></p>
<p><strong>Counter:</strong> <span id="counter"></span></p> <p><strong>Counter:</strong> <span id="counter"></span></p>
<script src="https://cdn.socket.io/4.6.1/socket.io.min.js"></script>
<script> <script>
const socket = io(); const socket = io();