From 10f31ea90dd19467d1f422b572c305d97c725031 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Thu, 1 Jan 2026 14:54:35 +0100 Subject: [PATCH] feat(settings): add modbus and relay configuration structs Add ModbusSettings with host, port, slave_id, and timeout_secs fields. Add RelaySettings with label_max_length field. Integrate both into Settings struct and load from settings/base.yaml with test Modbus TCP configuration. Ref: T003 (specs/001-modbus-relay-control) --- settings/base.yaml | 9 +++++ specs/001-modbus-relay-control/tasks.md | 8 ++-- src/settings.rs | 51 ++++++++++++++++++++++++- src/startup.rs | 2 + 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/settings/base.yaml b/settings/base.yaml index 4b7f6fa..ad2fe6b 100644 --- a/settings/base.yaml +++ b/settings/base.yaml @@ -6,3 +6,12 @@ rate_limit: enabled: true burst_size: 10 per_seconds: 60 + +modbus: + host: "192.168.0.200" + port: 502 + slave_id: 0 + timeout_secs: 5 + +relay: + label_max_length: 8 diff --git a/specs/001-modbus-relay-control/tasks.md b/specs/001-modbus-relay-control/tasks.md index 346076b..10bd34c 100644 --- a/specs/001-modbus-relay-control/tasks.md +++ b/specs/001-modbus-relay-control/tasks.md @@ -22,16 +22,16 @@ - **Test**: Module declarations compile without errors - **Complexity**: Low | **Uncertainty**: Low -- [ ] **T003** [P] [Setup] [TDD] Update settings.rs with Modbus configuration +- [x] **T003** [P] [Setup] [TDD] Update settings.rs with Modbus configuration - Add ModbusSettings struct with `host`, `port`, `slave_id`, `timeout_secs` fields - Add RelaySettings struct with `label_max_length` field - Update Settings struct to include modbus and relay fields - **Test**: Settings loads from settings/base.yaml with test Modbus config - **Complexity**: Low | **Uncertainty**: Low -- [ ] **T004** [P] [Setup] [TDD] Create settings/base.yaml with Modbus defaults - - Add modbus section: host: "192.168.0.200", port: 502, slave_id: 0, timeout_secs: 5 - - Add relay section: label_max_length: 8 +- [x] **T004** [P] [Setup] [TDD] Create settings/base.yaml with Modbus defaults + - Add modbus section: `host: "192.168.0.200"`, `port: 502`, `slave_id: 0`, `timeout_secs: 5` + - Add relay section: `label_max_length: 8` - **Test**: Settings::new() loads config without errors - **Complexity**: Low | **Uncertainty**: Low diff --git a/src/settings.rs b/src/settings.rs index f90f6ce..9eb14a9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -4,7 +4,8 @@ //! - YAML configuration files (base.yaml and environment-specific files) //! - Environment variables (prefixed with APP__) //! -//! Settings include application details, email server configuration, and environment settings. +//! Settings include application details, Modbus connection parameters, relay configuration, +//! rate limiting, and environment settings. /// Application configuration settings. /// @@ -20,6 +21,10 @@ pub struct Settings { /// Rate limiting configuration #[serde(default)] pub rate_limit: RateLimitSettings, + /// Modbus configuration + pub modbus: ModbusSettings, + /// Relay configuration + pub relay: RelaySettings, } impl Settings { @@ -156,6 +161,50 @@ const fn default_per_seconds() -> u64 { 60 } +/// Modbus TCP connection configuration. +/// +/// Configures the connection parameters for communicating with the Modbus relay device +/// using Modbus RTU over TCP protocol. +#[derive(Debug, serde::Deserialize, Clone)] +pub struct ModbusSettings { + /// IP address or hostname of the Modbus device + pub host: String, + /// TCP port for Modbus communication (standard Modbus TCP port is 502) + pub port: u16, + /// Modbus slave/device ID (unit identifier) + pub slave_id: u8, + /// Operation timeout in seconds + pub timeout_secs: u8, +} + +impl Default for ModbusSettings { + fn default() -> Self { + Self { + host: "192.168.0.200".to_string(), + port: 502, + slave_id: 0, + timeout_secs: 5, + } + } +} + +/// Relay control configuration. +/// +/// Configures parameters for relay management and labeling. +#[derive(Debug, serde::Deserialize, Clone)] +pub struct RelaySettings { + /// Maximum length for custom relay labels (in characters) + pub label_max_length: u8, +} + +impl Default for RelaySettings { + fn default() -> Self { + Self { + label_max_length: 8, + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/startup.rs b/src/startup.rs index af3598e..7234226 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -181,6 +181,8 @@ mod tests { burst_size: 100, per_seconds: 60, }, + modbus: crate::settings::ModbusSettings::default(), + relay: crate::settings::RelaySettings::default(), } }