sdm72_lib/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! A library for controlling the SDM72 series energy meters via Modbus.
3//!
4//! This crate provides two main ways to interact with the SDM72 energy meters:
5//!
6//! 1.  **High-Level, Safe Clients**: Stateful, thread-safe clients that are easy
7//!     to share and use in concurrent applications. This is the recommended
8//!     approach for most users. See [`tokio_sync_safe_client::SafeClient`] (blocking)
9//!     and [`tokio_async_safe_client::SafeClient`] (`async`).
10//!
11//! 2.  **Low-Level, Stateless Functions**: A set of stateless functions that
12//!     directly map to the device's Modbus commands. This API offers maximum
13//!     flexibility but requires manual management of the Modbus context. See
14//!     the [`tokio_sync`] and [`tokio_async`] modules.
15//!
16//! ## Features
17//!
18//! - **Protocol Implementation**: Complete implementation of the SDM72 Modbus protocol.
19//! - **Stateful, Thread-Safe Clients**: For easy and safe concurrent use.
20//! - **Stateless, Low-Level Functions**: For maximum flexibility and control.
21//! - **Synchronous and Asynchronous APIs**: Both blocking and `async/await` APIs are available.
22//! - **Strongly-Typed API**: Utilizes Rust's type system for protocol correctness.
23//!
24//! ## Quick Start
25//!
26//! This example shows how to use the recommended high-level, synchronous `SafeClient`.
27//!
28//! ```no_run
29//! use sdm72_lib::{
30//!     protocol::Address,
31//!     tokio_sync_safe_client::SafeClient,
32//! };
33//! use tokio_modbus::client::sync::tcp;
34//! use tokio_modbus::Slave;
35//! use std::time::Duration;
36//!
37//! fn main() -> Result<(), Box<dyn std::error::Error>> {
38//!     // Connect to the device and create a stateful, safe client
39//!     let socket_addr = "192.168.1.100:502".parse()?;
40//!     let ctx = tcp::connect_slave(socket_addr, Slave(*Address::default()))?;
41//!     let mut client = SafeClient::new(ctx);
42//!
43//!     // Use the client to interact with the device
44//!     let values = client.read_all(&Duration::from_millis(100))?;
45//!
46//!     println!("Successfully read values: {:#?}", values);
47//!
48//!     Ok(())
49//! }
50//! ```
51//!
52//! For more details, see the documentation for the specific client you wish to use.
53
54pub mod protocol;
55
56#[cfg_attr(docsrs, doc(cfg(feature = "tokio-rtu-sync")))]
57#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tcp-sync")))]
58#[cfg_attr(docsrs, doc(cfg(feature = "tokio-rtu")))]
59#[cfg_attr(docsrs, doc(cfg(feature = "tokio-tcp")))]
60#[cfg(any(
61    feature = "tokio-rtu-sync",
62    feature = "tokio-tcp-sync",
63    feature = "tokio-rtu",
64    feature = "tokio-tcp"
65))]
66pub mod tokio_common;
67
68#[cfg_attr(
69    docsrs,
70    doc(cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")))
71)]
72#[cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync"))]
73pub mod tokio_sync;
74
75#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))))]
76#[cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))]
77pub mod tokio_async;
78
79#[cfg_attr(
80    docsrs,
81    doc(cfg(all(
82        feature = "safe-client-sync",
83        any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
84    )))
85)]
86#[cfg(all(
87    feature = "safe-client-sync",
88    any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
89))]
90pub mod tokio_sync_safe_client;
91
92#[cfg_attr(
93    docsrs,
94    doc(cfg(all(
95        feature = "safe-client-async",
96        any(feature = "tokio-rtu", feature = "tokio-tcp")
97    )))
98)]
99#[cfg(all(
100    feature = "safe-client-async",
101    any(feature = "tokio-rtu", feature = "tokio-tcp")
102))]
103pub mod tokio_async_safe_client;