server-test

This commit is contained in:
derlole
2025-05-06 14:33:02 +00:00
parent 85d9e82f8f
commit 46736f619b
4 changed files with 30 additions and 29 deletions

View File

@@ -15,6 +15,15 @@ def fetch_command():
shared.reset_command() shared.reset_command()
return jsonify(pCd) return jsonify(pCd)
@esp.route('/online', methods=['POST'])
def esp_online():
data = request.get_json()
sender_ip = request.remote_addr
esp_ip = data.get("ip", "unknown")
print(f"ESP ONLINE von IP: {esp_ip}, roher IP: {sender_ip}")
return jsonify({"status": "ok"})
@esp.route('/toggle-machine', methods=['GET']) @esp.route('/toggle-machine', methods=['GET'])
def toggle_machine(): def toggle_machine():

View File

@@ -20,6 +20,6 @@ def send_command():
return f"Befehl '{pCd}' gespeichert." return f"Befehl '{pCd}' gespeichert."
return "Kein Befehl angegeben.", 400 return "Kein Befehl angegeben.", 400
@unsecure.route('/live') # @unsecure.route('/live')
def test(): # def test():
return render_template('live.html', users=[{'name': 'Max'}, {'name': 'Moritz'}, {'name': 'Hans'}]) # return render_template('live.html', users=[{'name': 'Max'}, {'name': 'Moritz'}, {'name': 'Hans'}])

View File

@@ -8,7 +8,8 @@ import paho.mqtt.client as mqtt
MQTT_BROKER = "localhost" MQTT_BROKER = "localhost"
MQTT_PORT = 1883 MQTT_PORT = 1883
MQTT_TOPIC = "coffee/status" MQTT_TOPIC_SUB = "coffee/status"
MQTT_TOPIC_SEND = "coffee/command"
app = Flask(__name__, static_url_path='/unsecure/static') app = Flask(__name__, static_url_path='/unsecure/static')
app.config['SECRET_KEY'] = 'super-secret-key' app.config['SECRET_KEY'] = 'super-secret-key'
@@ -21,8 +22,8 @@ app.register_blueprint(esp)
# MQTT Callback-Funktionen # MQTT Callback-Funktionen
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
print(f"[MQTT] Verbunden mit Code {rc}") print(f"[MQTT] Verbunden mit Code {rc}")
client.subscribe(MQTT_TOPIC) client.subscribe(MQTT_TOPIC_SUB)
print(f"[MQTT] Subscribed to topic: {MQTT_TOPIC}") print(f"[MQTT] Subscribed to topic: {MQTT_TOPIC_SUB}")
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}") print(f"[MQTT] Nachricht empfangen: {msg.topic} -> {msg.payload.decode()}")
@@ -41,20 +42,22 @@ def mqtt_thread():
client.loop_forever() client.loop_forever()
# Dummy-Daten-Thread # Dummy-Daten-Thread
def send_data(): # def send_data():
counter = 0 # counter = 0
while True: # while True:
data = { # data = {
'test': 'Live-Daten', # 'test': 'Live-Daten',
'status': 'OK', # 'status': 'OK',
'counter': counter # 'counter': counter
} # }
socketio.emit('update_data', data) # socketio.emit('update_data', data)
counter += 1 # counter += 1
time.sleep(2) # time.sleep(2)
# Beide Threads starten # Beide Threads starten
threading.Thread(target=send_data, daemon=True).start() #threading.Thread(target=send_data, daemon=True).start()
threading.Thread(target=mqtt_thread, daemon=True).start() threading.Thread(target=mqtt_thread, daemon=True).start()
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -80,17 +80,6 @@
function toggleMachine() { function toggleMachine() {
document.getElementById("machine-status").innerText = "PENDING"; document.getElementById("machine-status").innerText = "PENDING";
document.getElementById("machine-status-butt").classList.add("blink-orange"); document.getElementById("machine-status-butt").classList.add("blink-orange");
// fetch('/unsecure/esp/toggle-machine', { method: 'POST' })
// .then(res => res.json())
// .then(data => {
// //console.log(data);
// const inner = JSON.parse(data.status);
// if (inner.command === 'machineToggle') {
// } else {
// document.getElementById("machine-status").innerText = "SOME_ERR";
// }
// });
} }
</script> </script>
</body> </body>