Skip to content

Commit

Permalink
implement gateway reset over modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
liamkinne committed Jun 11, 2024
1 parent a601d60 commit 3942884
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
net::{IpAddr, SocketAddr},
path::PathBuf,
};
use tokio_modbus::client::tcp::connect;
use tokio_modbus::{client::tcp::connect, slave::SlaveContext, Slave};

#[derive(Subcommand)]
pub enum Commands {
Expand Down Expand Up @@ -36,14 +36,16 @@ impl Cmd {
let output = std::io::stdout().lock();

let socket_addr = SocketAddr::new(self.ip, 502);
let ctx = connect(socket_addr).await?;
let mut ctx = connect(socket_addr).await?;

ctx.set_slave(Slave(0));

match self.subcommand {
Commands::Status => status::command(output, ctx).await,
Commands::Update(options) => {
update::command(output, options, self.ip).await
}
Commands::Restart => restart::command(output).await,
Commands::Restart => restart::command(output, ctx).await,
Commands::Reset => reset::command(output).await,
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/gateway/restart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
use std::time::Duration;
use tokio_modbus::client::{Context as ModbusContext, Writer};

#[allow(unused_variables, unused_mut)]
pub async fn command(mut output: impl std::io::Write) -> anyhow::Result<()> {
unimplemented!();
pub async fn command(
mut output: impl std::io::Write,
mut ctx: ModbusContext,
) -> anyhow::Result<()> {
writeln!(output, "Resetting gateway...")?;

// write reset coil
let _ = tokio::time::timeout(
Duration::from_secs(1),
ctx.write_single_coil(1, true),
)
.await;

writeln!(output, "Done")?;

Ok(())
}

0 comments on commit 3942884

Please sign in to comment.