implement functionality to delete clients in
clients and SetupMode components
This commit is contained in:
@@ -131,3 +131,70 @@ def get_client_group(uuid):
|
||||
group_id = client.group_id
|
||||
session.close()
|
||||
return jsonify({"group_id": group_id})
|
||||
|
||||
# Neue Route: Liefert alle Clients mit Alive-Status
|
||||
|
||||
|
||||
@clients_bp.route("/with_alive_status", methods=["GET"])
|
||||
def get_clients_with_alive_status():
|
||||
session = Session()
|
||||
clients = session.query(Client).all()
|
||||
result = []
|
||||
for c in clients:
|
||||
result.append({
|
||||
"uuid": c.uuid,
|
||||
"description": c.description,
|
||||
"ip": c.ip,
|
||||
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
||||
"is_active": c.is_active,
|
||||
"is_alive": bool(c.last_alive and c.is_active),
|
||||
})
|
||||
session.close()
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@clients_bp.route("/<uuid>/restart", methods=["POST"])
|
||||
def restart_client(uuid):
|
||||
"""
|
||||
Route to restart a specific client by UUID.
|
||||
Sends an MQTT message to the broker to trigger the restart.
|
||||
"""
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
|
||||
# MQTT broker configuration
|
||||
MQTT_BROKER = "mqtt"
|
||||
MQTT_PORT = 1883
|
||||
MQTT_TOPIC = f"clients/{uuid}/restart"
|
||||
|
||||
# Connect to the database to check if the client exists
|
||||
session = Session()
|
||||
client = session.query(Client).filter_by(uuid=uuid).first()
|
||||
if not client:
|
||||
session.close()
|
||||
return jsonify({"error": "Client nicht gefunden"}), 404
|
||||
session.close()
|
||||
|
||||
# Send MQTT message
|
||||
try:
|
||||
mqtt_client = mqtt.Client()
|
||||
mqtt_client.connect(MQTT_BROKER, MQTT_PORT)
|
||||
payload = {"action": "restart"}
|
||||
mqtt_client.publish(MQTT_TOPIC, json.dumps(payload))
|
||||
mqtt_client.disconnect()
|
||||
return jsonify({"success": True, "message": f"Restart signal sent to client {uuid}"}), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Failed to send MQTT message: {str(e)}"}), 500
|
||||
|
||||
|
||||
@clients_bp.route("/<uuid>", methods=["DELETE"])
|
||||
def delete_client(uuid):
|
||||
session = Session()
|
||||
client = session.query(Client).filter_by(uuid=uuid).first()
|
||||
if not client:
|
||||
session.close()
|
||||
return jsonify({"error": "Client nicht gefunden"}), 404
|
||||
session.delete(client)
|
||||
session.commit()
|
||||
session.close()
|
||||
return jsonify({"success": True})
|
||||
|
||||
@@ -1,13 +1,45 @@
|
||||
from models.models import Client
|
||||
# Neue Route: Liefert alle Gruppen mit zugehörigen Clients und deren Alive-Status
|
||||
|
||||
from database import Session
|
||||
from models.models import ClientGroup
|
||||
from flask import Blueprint, request, jsonify
|
||||
from sqlalchemy import func
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
sys.path.append('/workspace')
|
||||
|
||||
groups_bp = Blueprint("groups", __name__, url_prefix="/api/groups")
|
||||
|
||||
|
||||
def get_grace_period():
|
||||
"""Wählt die Grace-Periode abhängig von ENV."""
|
||||
env = os.environ.get("ENV", "production").lower()
|
||||
if env == "development" or env == "dev":
|
||||
return int(os.environ.get("HEARTBEAT_GRACE_PERIOD_DEV", "15"))
|
||||
return int(os.environ.get("HEARTBEAT_GRACE_PERIOD_PROD", "180"))
|
||||
|
||||
|
||||
def is_client_alive(last_alive, is_active):
|
||||
"""Berechnet, ob ein Client als alive gilt."""
|
||||
if not last_alive or not is_active:
|
||||
return False
|
||||
grace_period = get_grace_period()
|
||||
# last_alive kann ein String oder datetime sein
|
||||
if isinstance(last_alive, str):
|
||||
last_alive_str = last_alive[:-
|
||||
1] if last_alive.endswith('Z') else last_alive
|
||||
try:
|
||||
last_alive_dt = datetime.fromisoformat(last_alive_str)
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
last_alive_dt = last_alive
|
||||
return datetime.utcnow() - last_alive_dt <= timedelta(seconds=grace_period)
|
||||
|
||||
|
||||
@groups_bp.route("", methods=["POST"])
|
||||
def create_group():
|
||||
data = request.get_json()
|
||||
@@ -127,3 +159,31 @@ def rename_group_by_name(old_name):
|
||||
}
|
||||
session.close()
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@groups_bp.route("/with_clients", methods=["GET"])
|
||||
def get_groups_with_clients():
|
||||
session = Session()
|
||||
groups = session.query(ClientGroup).all()
|
||||
result = []
|
||||
for g in groups:
|
||||
clients = session.query(Client).filter_by(group_id=g.id).all()
|
||||
client_list = []
|
||||
for c in clients:
|
||||
client_list.append({
|
||||
"uuid": c.uuid,
|
||||
"description": c.description,
|
||||
"ip": c.ip,
|
||||
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
||||
"is_active": c.is_active,
|
||||
"is_alive": is_client_alive(c.last_alive, c.is_active),
|
||||
})
|
||||
result.append({
|
||||
"id": g.id,
|
||||
"name": g.name,
|
||||
"created_at": g.created_at.isoformat() if g.created_at else None,
|
||||
"is_active": g.is_active,
|
||||
"clients": client_list,
|
||||
})
|
||||
session.close()
|
||||
return jsonify(result)
|
||||
|
||||
Reference in New Issue
Block a user