| 1234567891011121314151617181920212223242526272829303132333435363738 |
- # -*- coding: utf-8 -*-
- """
- Basis-Klasse für Netzwerk-Befehle.
- """
- import uuid
- from dataclasses import dataclass, field
- from datetime import datetime
- from typing import Any
- @dataclass
- class CommandMessage:
- """
- Basisklasse für alle Netzwerk-Befehle.
- Jeder Befehl hat eine eindeutige ID und einen Zeitstempel
- für Tracking und Debugging.
- """
- message_id: str = field(default_factory=lambda: str(uuid.uuid4()))
- timestamp: datetime = field(default_factory=datetime.now)
- def to_dict(self) -> dict[str, Any]:
- """Konvertiert die Nachricht in ein Dictionary."""
- result: dict[str, Any] = {}
- for key, value in self.__dict__.items():
- if isinstance(value, datetime):
- result[key] = value.isoformat()
- else:
- result[key] = value
- return result
- @classmethod
- def from_dict(cls, data: dict[str, Any]) -> "CommandMessage":
- """Erstellt eine Nachricht aus einem Dictionary."""
- if "timestamp" in data and isinstance(data["timestamp"], str):
- data["timestamp"] = datetime.fromisoformat(data["timestamp"])
- return cls(**data)
|