use crate::protocol as proto;
use std::time::Duration;
use tokio_modbus::prelude::{SyncReader, SyncWriter};
pub struct R413D08 {
ctx: tokio_modbus::client::sync::Context,
}
impl R413D08 {
pub fn new(ctx: tokio_modbus::client::sync::Context) -> Self {
Self { ctx }
}
pub fn set_timeout(&mut self, timeout: Option<Duration>) {
self.ctx.set_timeout(timeout);
}
pub fn timeout(&self) -> Option<Duration> {
self.ctx.timeout()
}
pub fn read_ports(&mut self) -> tokio_modbus::Result<proto::PortStates> {
match self
.ctx
.read_holding_registers(proto::PortStates::ADDRESS, proto::PortStates::QUANTITY)
{
Ok(Ok(rsp)) => {
Ok(Ok(proto::PortStates::decode_from_holding_registers(&rsp)))
}
Ok(Err(err)) => {
Ok(Err(err))
}
Err(err) => {
Err(err)
}
}
}
pub fn set_port_open(&mut self, port: proto::Port) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::REG_DATA_SET_PORT_OPEN,
)
}
pub fn set_all_open(&mut self) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
proto::PortsAll::ADDRESS,
proto::PortsAll::REG_DATA_SET_ALL_OPEN,
)
}
pub fn set_port_close(&mut self, port: proto::Port) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::REG_DATA_SET_PORT_CLOSE,
)
}
pub fn set_all_close(&mut self) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
proto::PortsAll::ADDRESS,
proto::PortsAll::REG_DATA_SET_ALL_CLOSE,
)
}
pub fn set_port_toggle(&mut self, port: proto::Port) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::REG_DATA_SET_PORT_TOGGLE,
)
}
pub fn set_port_latch(&mut self, port: proto::Port) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::REG_DATA_SET_PORT_LATCH,
)
}
pub fn set_port_momentary(&mut self, port: proto::Port) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::REG_DATA_SET_PORT_MOMENTARY,
)
}
pub fn set_port_delay(&mut self, port: proto::Port, delay: u8) -> tokio_modbus::Result<()> {
self.ctx.write_single_register(
port.address_for_write_register(),
proto::Port::encode_delay_for_write_register(delay),
)
}
pub fn read_address(&mut self) -> tokio_modbus::Result<proto::Address> {
match self.ctx.read_holding_registers(
proto::Address::ADDRESS, proto::Address::QUANTITY, ) {
Ok(Ok(rsp)) => {
Ok(Ok(proto::Address::decode_from_holding_registers(&rsp)))
}
Ok(Err(err)) => {
Ok(Err(err))
}
Err(err) => {
Err(err)
}
}
}
pub fn set_address(&mut self, address: proto::Address) -> tokio_modbus::Result<()> {
self.ctx
.write_single_register(proto::Address::ADDRESS, address.encode_for_write_register())
}
}