Init
This commit is contained in:
+202
@@ -0,0 +1,202 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
import threading
|
||||
from flask import Flask, render_template
|
||||
import os
|
||||
|
||||
|
||||
# ==========================================================
|
||||
# KONFIGURATION & DATEIEN
|
||||
# ==========================================================
|
||||
|
||||
CHIP_NAMES_FILE = "data.json"
|
||||
STATE_SAVE_FILE = "current_status.json" # Die Datei, in die der Zustand geschrieben wird
|
||||
MQTT_TOPIC = None
|
||||
MQTT_BROKER = None
|
||||
MQTT_PORT = None
|
||||
|
||||
# Global State Management: Schlüssel ist die eindeutige ID.
|
||||
RFID_STATUS = {}
|
||||
status_lock = threading.Lock()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def load_config():
|
||||
"""Liest alle Konfigurationsdaten (MQTT-Settings und Chip-Status) aus data.json."""
|
||||
global RFID_STATUS, MQTT_BROKER, MQTT_TOPIC, MQTT_PORT
|
||||
print("--- Starte Konfigurationsladen ---")
|
||||
try:
|
||||
with open(CHIP_NAMES_FILE, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# 1. MQTT Konfiguration auslesen
|
||||
if "mqtt_config" not in data or not isinstance(data["mqtt_config"], dict):
|
||||
raise ValueError("Die JSON-Struktur ist falsch. Fehlendes 'mqtt_config' Key.")
|
||||
|
||||
mqtt_cfg = data["mqtt_config"]
|
||||
MQTT_BROKER = str(mqtt_cfg.get('broker', 'localhost'))
|
||||
MQTT_PORT = int(mqtt_cfg.get('port', 1883))
|
||||
MQTT_TOPIC = str(mqtt_cfg.get('topic', 'Gruppe 14'))
|
||||
|
||||
print(f"✅ MQTT-Einstellungen geladen: Broker={MQTT_BROKER}, Topic={MQTT_TOPIC}")
|
||||
|
||||
|
||||
# 2. Chip-Status initialisieren
|
||||
if "chips" not in data or not isinstance(data["chips"], list):
|
||||
raise ValueError("Die JSON-Struktur ist falsch. Fehlendes 'chips' Key.")
|
||||
|
||||
for chip_data in data["chips"]:
|
||||
chip_id = str(chip_data.get('id', 'UNKNOWN'))
|
||||
chip_name = str(chip_data.get('name', 'N/A'))
|
||||
initial_status = int(str(chip_data.get('status', '0')))
|
||||
|
||||
RFID_STATUS[chip_id] = {
|
||||
"Name": chip_name,
|
||||
"ID": chip_id,
|
||||
"Status": initial_status
|
||||
}
|
||||
|
||||
print(f"✅ Starte mit {len(RFID_STATUS)} Chips, geladen aus {CHIP_NAMES_FILE}.")
|
||||
return True
|
||||
|
||||
except FileNotFoundError:
|
||||
print("❌ KRITISCHER FEHLER: Die Datei data.json wurde nicht gefunden!")
|
||||
print("Bitte erstellen Sie diese Datei und verwenden Sie die korrekte Struktur.")
|
||||
return False
|
||||
except json.JSONDecodeError:
|
||||
print(f"❌ KRITISCHER FEHLER: Die Datei {CHIP_NAMES_FILE} ist kein gültiges JSON!")
|
||||
return False
|
||||
except ValueError as e:
|
||||
print(f"❌ KONFIGURIERUNGSFEHLER: {e}")
|
||||
return False
|
||||
|
||||
# ==========================================================
|
||||
# 💾 Funktion zur Zustandsspeicherung (Korrigierter Block)
|
||||
# ==========================================================
|
||||
|
||||
def save_state():
|
||||
"""Speichert den gesamten aktuellen RFID_STATUS in die Konfigurationsdatei."""
|
||||
global RFID_STATUS
|
||||
data_to_save = {
|
||||
"chips": [
|
||||
{
|
||||
"name": data['Name'],
|
||||
"id": data['ID'],
|
||||
"status": str(data['Status'])
|
||||
} for data in RFID_STATUS.values()
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
with status_lock:
|
||||
# Innerstes Level der Indentation ist hier entscheidend!
|
||||
with open(STATE_SAVE_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(data_to_save, f, indent=4)
|
||||
print(f"\n[💾 SPEICHERT] Status erfolgreich in {STATE_SAVE_FILE} gespeichert.")
|
||||
except Exception as e:
|
||||
# Dieser Block muss korrekt unter dem 'try' stehen und einen Fehler abfangen.
|
||||
print(f"[FEHLER!] Konnte den Zustand nicht speichern: {e}")
|
||||
|
||||
|
||||
# ==========================================================
|
||||
# ⚙️ Angepasste Funktion (Status-Update und Logging)
|
||||
# ==========================================================
|
||||
|
||||
def update_status(chip_id):
|
||||
"""Aktualisiert den Status, loggt die Änderung UND speichert den Zustand."""
|
||||
global RFID_STATUS
|
||||
|
||||
if chip_id not in RFID_STATUS:
|
||||
print(f"[WARNUNG] Unbekannte oder ungültige ID erkannt: '{chip_id}'. Ignoriere den Statuswechsel.")
|
||||
return
|
||||
|
||||
with status_lock:
|
||||
old_status = RFID_STATUS[chip_id]['Status']
|
||||
new_status = 1 - old_status
|
||||
RFID_STATUS[chip_id]['Status'] = new_status
|
||||
|
||||
# Logging der Änderung und Speichern des Zustands
|
||||
if old_status == 0 and new_status == 1:
|
||||
logging_message = "✅ AKTIVIERUNG DETEKTIERT! (Von Inaktiv zu Aktiv)"
|
||||
print(f"\n[!!! STATUS GEÄNDERT !!!] {logging_message} Chip ID '{chip_id}'. Neuer Status: 1.")
|
||||
elif old_status == 1 and new_status == 0:
|
||||
logging_message = "⚠️ DEAKTIVIERUNG DETEKTIERT! (Von Aktiv zu Inaktiv)"
|
||||
print(f"\n[!!! STATUS GEÄNDERT !!!] {logging_message} Chip ID '{chip_id}'. Neuer Status: 0.")
|
||||
|
||||
# Speichern des Zustandes nach jeder Änderung
|
||||
save_state()
|
||||
|
||||
|
||||
# ==========================================================
|
||||
# MQTT LOGIK (Verwendet die globalen Variablen)
|
||||
# ==========================================================
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
"""Callback bei erfolgreicher Verbindung."""
|
||||
if rc == 0:
|
||||
print("\n✅ MQTT verbunden. Abonniere Topic:", MQTT_TOPIC)
|
||||
client.subscribe(MQTT_TOPIC)
|
||||
else:
|
||||
print(f"\n❌ MQTT Verbindung fehlgeschlagen mit Code {rc}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
"""Callback bei empfangener Nachricht (Plain Text Payload = Chip ID)."""
|
||||
payload = msg.payload.decode().strip()
|
||||
|
||||
if not payload:
|
||||
print("[WARNUNG] Empfangen leerer Payload.")
|
||||
return
|
||||
|
||||
chip_id = payload
|
||||
|
||||
print(f"\n[MQTT] Plain Text ID empfangen: {chip_id}")
|
||||
update_status(chip_id)
|
||||
|
||||
|
||||
def run_mqtt_client():
|
||||
"""Startet den MQTT-Client im Hintergrundthread mit den konfigurierten Daten."""
|
||||
client = mqtt.Client("RFID_Tracker")
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
print("\n[MQTT] Starte Verbindung zum Broker...")
|
||||
try:
|
||||
# Nutzung der globalen Variablen (die aus data.json kommen)
|
||||
client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
||||
client.loop_start()
|
||||
except Exception as e:
|
||||
print(f"\n!!! KRITISCHER FEHLER !!! Broker-Verbindung fehlgeschlagen ({e}). Prüfen Sie die Einstellungen in data.json.")
|
||||
|
||||
# ==========================================================
|
||||
# WEB-SERVER LOGIK (Flask) - Bleibt unverändert
|
||||
# ==========================================================
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
"""Die Hauptseite, die den aktuellen Status aller RFID Karten anzeigt."""
|
||||
global RFID_STATUS
|
||||
with status_lock:
|
||||
current_status = dict(RFID_STATUS)
|
||||
return render_template('index.html', rfid_data=current_status, mqtt_topic=MQTT_TOPIC)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Initialisierung des Chip-Zustandes UND der MQTT Konfiguration
|
||||
if not load_config():
|
||||
print("\n[🛑 ABBRUCH] Das Programm kann wegen eines Fehlers bei der Konfiguration nicht gestartet werden.")
|
||||
exit()
|
||||
|
||||
mqtt_thread = threading.Thread(target=run_mqtt_client)
|
||||
mqtt_thread.start()
|
||||
|
||||
# Speichere den initialen Zustand, um die JSON-Datenbank zu befüllen
|
||||
save_state()
|
||||
|
||||
print("\n============================================================")
|
||||
print(" *** RFID STATUS TRACKER AKTIV ***")
|
||||
print("============================================================")
|
||||
print(f"Statusseite läuft unter: http://0.0.0.0:{MQTT_PORT}/")
|
||||
|
||||
# Start des Webservers auf allen Interfaces (für externen Zugriff)
|
||||
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"chips": [
|
||||
{
|
||||
"name": "1",
|
||||
"id": "729558387180",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "2",
|
||||
"id": "987572218311",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "3",
|
||||
"id": "842310768930",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "4",
|
||||
"id": "773910059391",
|
||||
"status": "0"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"mqtt_config": {
|
||||
"broker": "192.168.213.12",
|
||||
"port": 1883,
|
||||
"topic": "RFID"
|
||||
},
|
||||
"chips": [
|
||||
{
|
||||
"name": "1",
|
||||
"id": "729558387180",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "2",
|
||||
"id": "987572218311",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "3",
|
||||
"id": "842310768930",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "4",
|
||||
"id": "773910059391",
|
||||
"status": "0"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>RFID Status Tracker</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
margin: 20px;
|
||||
background-color: #f4f7fa; /* Heller Hintergrund */
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
.container {
|
||||
width: 100%;
|
||||
max-width: 900px;
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
color: #333;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
p.connection-info {
|
||||
text-align: center;
|
||||
font-size: 1em;
|
||||
color: #555;
|
||||
border-bottom: 1px solid #eee;
|
||||
padding-bottom: 20px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
/* Statusanzeige */
|
||||
.card-status {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 18px 20px;
|
||||
margin-bottom: 15px;
|
||||
border-radius: 8px;
|
||||
background-color: #ffffff; /* Weißer Hintergrund für jeden Chip */
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
|
||||
border-left: 5px solid #007bff; /* Akzentstreifen links */
|
||||
}
|
||||
|
||||
.chip-info {
|
||||
flex-grow: 2;
|
||||
font-size: 1.3em;
|
||||
color: #0056b3;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.chip-details span {
|
||||
font-size: 0.9em;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.status-container {
|
||||
text-align: center;
|
||||
margin-right: 20px;
|
||||
}
|
||||
|
||||
/* Status-Indikator (Das Quadrat) */
|
||||
.status-indicator {
|
||||
padding: 10px 25px;
|
||||
border-radius: 30px;
|
||||
font-weight: bold;
|
||||
font-size: 1.4em;
|
||||
min-width: 90px; /* Stellt sicher, dass die Breite konstant ist */
|
||||
text-align: center;
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
/* Status Styling basierend auf der Variable 'Status' (1 oder 0) */
|
||||
.status-active {
|
||||
background-color: #e6ffe9; /* Hellgrün für Hintergrund */
|
||||
color: #28a745; /* Dunkelgrüner Text */
|
||||
border: 1px solid #28a745;
|
||||
}
|
||||
.status-inactive {
|
||||
background-color: #fdecec; /* Hellrot für Hintergrund */
|
||||
color: #dc3545; /* Dunkelroter Text */
|
||||
border: 1px solid #dc3545;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>RFID Chip Status Übersicht</h1>
|
||||
<p class="connection-info">Daten werden über MQTT von Topic "{{ mqtt_topic }}" empfangen.</p>
|
||||
|
||||
{% for chip_id, data in rfid_data.items() %}
|
||||
<div class="card-status">
|
||||
<!-- Name und ID -->
|
||||
<span class="chip-info">
|
||||
{{ data.Name }}
|
||||
<div class="chip-details"><span>(ID: {{ data.ID }})</span></div>
|
||||
</span>
|
||||
|
||||
<!-- Statusbereich -->
|
||||
<div class="status-container">
|
||||
<p style="font-size: 0.9em; color: #555;">Status:</p>
|
||||
<!-- Dynamisches Styling basierend auf dem Status -->
|
||||
{% set status_class = 'status-active' if data.Status == 1 else 'status-inactive' %}
|
||||
<span class="status-indicator {{ status_class }}">
|
||||
{{ data.Status }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"chips": [
|
||||
{
|
||||
"name": "1",
|
||||
"id": "729558387180",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "2",
|
||||
"id": "987572218311",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "3",
|
||||
"id": "842310768930",
|
||||
"status": "0"
|
||||
},
|
||||
{
|
||||
"name": "4",
|
||||
"id": "773910059391",
|
||||
"status": "0"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import time
|
||||
import socket
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
from mfrc522 import SimpleMFRC522
|
||||
reader = SimpleMFRC522()
|
||||
|
||||
log = logging.getLogger('werkzeug')
|
||||
log.setLevel(logging.ERROR)
|
||||
#try:
|
||||
# while True:
|
||||
# (status, TagType) = reader.MFRC522_Request(MFRC522.PICC_REQIDL)
|
||||
# if status == MFRC522.MI_OK:
|
||||
# (status, uid) = reader.MFRC522_Anticoll()
|
||||
# if status == MFRC522.MI_OK:
|
||||
# uid_str = "-".join([str(x) for x in uid[:4]])
|
||||
# print(f"Tag erkannt! UID: {uid_str}")
|
||||
# time.sleep(0.1)
|
||||
|
||||
#state 0 = nicht da?
|
||||
#state 1 = ist da?
|
||||
|
||||
|
||||
try:
|
||||
while True:
|
||||
id, text = reader.read()
|
||||
idz = str(id)
|
||||
import subprocess
|
||||
subprocess.run([
|
||||
"mosquitto_pub",
|
||||
"-h", "192.168.213.12", # -h -> Host; IP des Brokers
|
||||
"-t", "RFID", # -t -> Topic; "Gruppe"
|
||||
"-m", idz # -m -> Message; sendende Nachricht
|
||||
])
|
||||
|
||||
print("RFID",id)
|
||||
time.sleep(10)
|
||||
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
print("\nProgramm beendet.")
|
||||
Reference in New Issue
Block a user