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//! ## Key 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//! ## Cargo Features
25//!
26//! This crate uses feature flags to enable different functionalities and to
27//! select the desired `tokio-modbus` backend.
28//!
29//! For library users, it is recommended to disable the default features and
30//! enable only the ones you need. For example, in your `Cargo.toml`:
31//!
32//! ```toml
33//! [dependencies.sdm72]
34//! version = "0.2"
35//! default-features = false
36//! features = ["tokio-tcp-sync", "safe-client-sync"]
37//! ```
38//!
39//! ### Available Features
40//!
41//! - `tokio-rtu-sync`: Enables the synchronous (`blocking`) RTU backend.
42//! - `tokio-tcp-sync`: Enables the synchronous (`blocking`) TCP backend.
43//! - `tokio-rtu`: Enables the asynchronous (`async`) RTU backend.
44//! - `tokio-tcp`: Enables the asynchronous (`async`) TCP backend.
45//! - `safe-client-sync`: Enables the high-level, thread-safe, synchronous [`tokio_sync_safe_client::SafeClient`].
46//! Requires either `tokio-rtu-sync` or `tokio-tcp-sync`.
47//! - `safe-client-async`: Enables the high-level, thread-safe, asynchronous [`tokio_async_safe_client::SafeClient`].
48//! Requires either `tokio-rtu` or `tokio-tcp`.
49//! - `serde`: Enables `serde` support for the `protocol` types.
50//! - `bin-dependencies`: Enables all dependencies required for the `sdm72`
51//! binary. This is not intended for library users.
52//!
53//! The `default` feature enables `bin-dependencies`.
54//!
55//! ## Quick Start
56//!
57//! This example shows how to use the recommended high-level, synchronous `SafeClient`.
58//!
59//! ```no_run
60//! use sdm72_lib::{
61//! protocol::Address,
62//! tokio_sync_safe_client::SafeClient,
63//! };
64//! use tokio_modbus::client::sync::tcp;
65//! use tokio_modbus::Slave;
66//! use std::time::Duration;
67//!
68//! fn main() -> Result<(), Box<dyn std::error::Error>> {
69//! // Connect to the device and create a stateful, safe client
70//! let socket_addr = "192.168.1.100:502".parse()?;
71//! let ctx = tcp::connect_slave(socket_addr, Slave(*Address::default()))?;
72//! let mut client = SafeClient::new(ctx);
73//!
74//! // Use the client to interact with the device
75//! let values = client.read_all(&Duration::from_millis(100))?;
76//!
77//! println!("Successfully read values: {:#?}", values);
78//!
79//! Ok(())
80//! }
81//! ```
82//!
83//! For more details, see the documentation for the specific client you wish to use.
84
85pub mod protocol;
86
87#[cfg_attr(
88 docsrs,
89 doc(cfg(any(
90 feature = "tokio-rtu-sync",
91 feature = "tokio-tcp-sync",
92 feature = "tokio-rtu",
93 feature = "tokio-tcp"
94 )))
95)]
96#[cfg(any(
97 feature = "tokio-rtu-sync",
98 feature = "tokio-tcp-sync",
99 feature = "tokio-rtu",
100 feature = "tokio-tcp"
101))]
102pub mod tokio_common;
103
104#[cfg_attr(
105 docsrs,
106 doc(cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")))
107)]
108#[cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync"))]
109pub mod tokio_sync;
110
111#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))))]
112#[cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))]
113pub mod tokio_async;
114
115#[cfg_attr(
116 docsrs,
117 doc(cfg(all(
118 feature = "safe-client-sync",
119 any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
120 )))
121)]
122#[cfg(all(
123 feature = "safe-client-sync",
124 any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
125))]
126pub mod tokio_sync_safe_client;
127
128#[cfg_attr(
129 docsrs,
130 doc(cfg(all(
131 feature = "safe-client-async",
132 any(feature = "tokio-rtu", feature = "tokio-tcp")
133 )))
134)]
135#[cfg(all(
136 feature = "safe-client-async",
137 any(feature = "tokio-rtu", feature = "tokio-tcp")
138))]
139pub mod tokio_async_safe_client;