"""Defines all needed parameters for the supported communication."""
from dataclasses import dataclass, field
from multiprocessing import Event, Queue
from multiprocessing.synchronize import Event as EventClass
from pathlib import Path
@dataclass
class ComControl:
"""Data class for managing inter-process communication and control signals.
Contains input/output queues, events, and logging facilities for processes.
"""
input: Queue = field(default_factory=Queue)
output: Queue = field(default_factory=Queue)
ready: EventClass = field(default_factory=Event)
shutdown: EventClass = field(default_factory=Event)
logger: Queue = field(default_factory=Queue)
log_level: int = 50
@dataclass(slots=True, frozen=True)
class ModbusParameter:
"""Immutable configuration for a Modbus TCP connection.
Holds connection parameters used by a Modbus client.
Attributes:
host: IP address or hostname of the Modbus server.
port: TCP port of the Modbus server. Defaults to 502.
timeout: Request/response timeout in seconds. Defaults to 1.
retries: Number of retry attempts on transient I/O errors. Defaults to 3.
ignore: If True, specific exceptions are ignored.
Notes:
Instances are immutable (frozen=True) and use __slots__ for reduced memory footprint.
"""
host: str
port: int = 502
timeout: int = 1
retries: int = 3
ignore: bool = False
@dataclass(slots=True, frozen=True)
class MQTTParameter:
"""Data class for MQTT connection parameters.
Attributes:
broker (str): MQTT broker address.
port (int): Port number for MQTT broker.
subscribe (list[str]): List of topics to subscribe to.
tls_cert (str | None): Path to TLS certificate (optional).
username (str | None): Username for authentication (optional).
password (str | None): Password for authentication (optional).
"""
broker: str
port: int
subscribe: list[str]
tls_cert: str | None = None
username: str | None = None
password: str | None = None
@dataclass
class CANLoggerParameter:
"""Data class for CAN logger configuration parameters.
Attributes:
max_bytes (int): Maximum size of the log file in bytes before rollover.
rollover_count (int): Number of rollover log files to keep.
log_can_files (bool): Whether to enable CAN logging to files.
"""
max_bytes: int = 65536
rollover_count: int = 0
log_can_files: bool = False
@dataclass
class FileParameter:
"""Data class for file-based communication configuration.
Attributes:
input_file (Path | None): Path to the input file (optional).
output_file (Path | None): Path to the output file (optional).
encoding (str): File encoding (default "utf-8").
can_logger (CANLoggerParameter): CAN logger parameters.
"""
input_file: Path | None = None
output_file: Path | None = None
encoding: str = "utf-8"
can_logger: CANLoggerParameter = field(default_factory=CANLoggerParameter)
def __post_init__(self) -> None:
"""Ensures that input_file and output_file are Path objects after initialization."""
if self.input_file:
self.input_file = Path(self.input_file)
if self.output_file:
self.output_file = Path(self.output_file)