Skip to content

Commit

Permalink
Add multi-device support
Browse files Browse the repository at this point in the history
This closes #18
  • Loading branch information
bikeshedder committed Dec 10, 2024
1 parent 1da6d2a commit 81230c1
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.8.0] - 2024-12-10

### Added

- Add multi device support

### Changed

- Update `tokio-modbus` to version `0.16`
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use sunspec::{
models::{model1::Model1, model103::Model103},
};
use tokio::time::sleep;
use tokio_modbus::{client::tcp::connect_slave, Slave};
use tokio_modbus::client::tcp::connect;
#[derive(Parser)]
struct Args {
Expand All @@ -62,12 +62,10 @@ struct Args {
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let mut client = AsyncClient::new(
connect_slave(args.addr, Slave(args.device_id)).await?,
Config::default(),
).await?;
let client = AsyncClient::new(connect(args.addr).await?, Config::default());
let device = client.device(args.device_id).await?;
let m1: Model1 = client.read_model().await?;
let m1: Model1 = device.read_model().await?;
println!("Manufacturer: {}", m1.mn);
println!("Model: {}", m1.md);
Expand All @@ -76,7 +74,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!(
"Supported models: {}",
client
device
.models
.supported_model_ids()
.iter()
Expand All @@ -85,7 +83,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);
loop {
let m103: Model103 = client.read_model().await?;
let m103: Model103 = device.read_model().await?;
let w = m103.w as f32 * 10f32.powf(m103.w_sf.into());
let wh = m103.wh as f32 * 10f32.powf(m103.wh_sf.into());
println!("{:12.3} kWh {:9.3} kW", wh / 1000.0, w / 1000.0,);
Expand Down
16 changes: 8 additions & 8 deletions examples/model103/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sunspec::{
models::model1::Model1,
};
use tokio::time::sleep;
use tokio_modbus::{client::tcp::connect_slave, Slave};
use tokio_modbus::client::tcp::connect;

#[derive(Parser)]
struct Args {
Expand Down Expand Up @@ -37,18 +37,18 @@ struct Args {
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

let mut client = AsyncClient::new(
connect_slave(args.addr, Slave(args.device_id)).await?,
let client = AsyncClient::new(
connect(args.addr).await?,
Config {
discovery_addresses: args.discovery_addresses,
read_timeout: (args.read_timeout != 0.0)
.then(|| Duration::from_secs_f32(args.read_timeout)),
..Default::default()
},
)
.await?;
);

let m1: Model1 = client.read_model().await?;
let device = client.device(args.device_id).await?;
let m1: Model1 = device.read_model().await?;

println!("Manufacturer: {}", m1.mn);
println!("Model: {}", m1.md);
Expand All @@ -57,7 +57,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

println!(
"Supported models: {}",
client
device
.models
.supported_model_ids()
.iter()
Expand All @@ -66,7 +66,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);

loop {
let m103: Model103 = client.read_model().await?;
let m103: Model103 = device.read_model().await?;
let w = m103.w as f32 * 10f32.powf(m103.w_sf.into());
let wh = m103.wh as f32 * 10f32.powf(m103.wh_sf.into());
println!("{:12.3} kWh {:9.3} kW", wh / 1000.0, w / 1000.0,);
Expand Down
15 changes: 6 additions & 9 deletions examples/readme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sunspec::{
models::{model1::Model1, model103::Model103},
};
use tokio::time::sleep;
use tokio_modbus::{client::tcp::connect_slave, Slave};
use tokio_modbus::client::tcp::connect;

#[derive(Parser)]
struct Args {
Expand All @@ -19,13 +19,10 @@ struct Args {
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

let mut client = AsyncClient::new(
connect_slave(args.addr, Slave(args.device_id)).await?,
Config::default(),
)
.await?;
let client = AsyncClient::new(connect(args.addr).await?, Config::default());
let device = client.device(args.device_id).await?;

let m1: Model1 = client.read_model().await?;
let m1: Model1 = device.read_model().await?;

println!("Manufacturer: {}", m1.mn);
println!("Model: {}", m1.md);
Expand All @@ -34,7 +31,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

println!(
"Supported models: {}",
client
device
.models
.supported_model_ids()
.iter()
Expand All @@ -43,7 +40,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);

loop {
let m103: Model103 = client.read_model().await?;
let m103: Model103 = device.read_model().await?;
let w = m103.w as f32 * 10f32.powf(m103.w_sf.into());
let wh = m103.wh as f32 * 10f32.powf(m103.wh_sf.into());
println!("{:12.3} kWh {:9.3} kW", wh / 1000.0, w / 1000.0,);
Expand Down
Loading

0 comments on commit 81230c1

Please sign in to comment.