Files
PC-Monitor/plugins/base.py
2026-03-24 07:17:48 +01:00

152 lines
4.8 KiB
Python

"""
Classe de base pour tous les plugins de monitoring
"""
from abc import ABC, abstractmethod
from typing import Optional, List, Dict, Any
class BasePlugin(ABC):
"""Classe abstraite définissant l'interface des plugins"""
def __init__(self, config: dict):
"""
Initialise le plugin avec sa configuration
Args:
config: Configuration du plugin (host, port, etc.)
"""
self.host = config.get('host', '127.0.0.1')
self.port = config.get('port', self.get_default_port())
self.config = config
@abstractmethod
def get_id(self) -> str:
"""Retourne l'identifiant unique du plugin"""
pass
@abstractmethod
def get_name(self) -> str:
"""Retourne le nom affichable du plugin"""
pass
@abstractmethod
def get_default_port(self) -> int:
"""Retourne le port par défaut"""
pass
@abstractmethod
def test_connection(self) -> Dict[str, Any]:
"""
Teste la connexion au logiciel de monitoring
Returns:
dict avec 'success' (bool), 'message' (str), et optionnellement 'version'
"""
pass
@abstractmethod
def get_data(self) -> Optional[dict]:
"""
Récupère les données brutes du logiciel de monitoring
Returns:
Données brutes ou None si erreur
"""
pass
@abstractmethod
def get_hierarchy(self) -> List[dict]:
"""
Récupère la hiérarchie des capteurs (pour l'admin)
Returns:
Liste de hardware avec leurs capteurs
"""
pass
@abstractmethod
def parse_sensors(self, data: dict) -> List[dict]:
"""
Parse les données brutes en liste standardisée de capteurs
Args:
data: Données brutes du logiciel
Returns:
Liste de capteurs au format standardisé:
[
{
'id': str, # Identifiant unique
'name': str, # Nom du capteur
'value': str, # Valeur formatée (ex: "45 °C")
'raw_value': float, # Valeur numérique
'type': str, # Type (temperature, load, voltage, etc.)
'unit': str, # Unité (°C, %, MHz, etc.)
'hardware': str, # Nom du hardware parent
'hardware_type': str # Type de hardware (CPU, GPU, etc.)
},
...
]
"""
pass
def get_base_url(self) -> str:
"""Retourne l'URL de base pour les requêtes"""
return f"http://{self.host}:{self.port}"
def get_sensor_type(self, unit: str, name: str = '') -> str:
"""
Détermine le type de capteur à partir de l'unité et du nom
Args:
unit: Unité du capteur
name: Nom du capteur (pour affiner la détection)
Returns:
Type standardisé du capteur
"""
unit_lower = unit.lower() if unit else ''
name_lower = name.lower() if name else ''
# Détection par unité
if '°c' in unit_lower or '°f' in unit_lower:
return 'temperature'
elif '%' in unit_lower:
if 'load' in name_lower or 'usage' in name_lower:
return 'load'
return 'percentage'
elif 'mhz' in unit_lower or 'ghz' in unit_lower:
return 'frequency'
elif 'rpm' in unit_lower:
return 'fan'
elif 'w' in unit_lower and 'wh' not in unit_lower:
return 'power'
elif 'v' in unit_lower and 'mv' not in unit_lower:
return 'voltage'
elif 'mv' in unit_lower:
return 'voltage'
elif 'mb' in unit_lower or 'gb' in unit_lower or 'kb' in unit_lower:
return 'data'
elif 'mb/s' in unit_lower or 'kb/s' in unit_lower or 'gb/s' in unit_lower:
return 'throughput'
elif 'a' in unit_lower:
return 'current'
elif 'wh' in unit_lower:
return 'energy'
# Détection par nom si unité non reconnue
if 'temp' in name_lower or 'temperature' in name_lower:
return 'temperature'
elif 'fan' in name_lower:
return 'fan'
elif 'clock' in name_lower or 'freq' in name_lower:
return 'frequency'
elif 'load' in name_lower or 'usage' in name_lower:
return 'load'
elif 'power' in name_lower:
return 'power'
elif 'voltage' in name_lower or 'vcore' in name_lower:
return 'voltage'
return 'generic'