sdm72_lib/
tokio_sync_safe_client.rs1use crate::{
31 protocol as proto,
32 tokio_common::{AllSettings, AllValues, Result},
33 tokio_sync::SDM72,
34};
35use std::sync::{Arc, Mutex};
36use tokio_modbus::{client::sync::Context, prelude::SlaveContext};
37
38#[derive(Clone)]
40pub struct SafeClient {
41 ctx: Arc<Mutex<Context>>,
42}
43
44macro_rules! read_holding {
45 ($func_name:ident, $ty:ident) => {
46 paste::item! {
47 #[doc = "Reads the [`proto::" $ty "`] value from the Modbus holding register."]
48 pub fn $func_name(&mut self) -> Result<proto::$ty> {
49 let mut ctx = self.ctx.lock().unwrap();
50 SDM72::$func_name(&mut ctx)
51 }
52 }
53 };
54}
55
56macro_rules! write_holding {
57 ($func_name:ident, $ty:ident) => {
58 paste::item! {
59 #[doc = "Writes the [`proto::" $ty "`] value to the Modbus holding register."]
60 pub fn [< set_ $func_name >](&mut self, value: proto::$ty) -> Result<()> {
61 let mut ctx = self.ctx.lock().unwrap();
62 SDM72::[< set_ $func_name >](&mut ctx, value)
63 }
64 }
65 };
66}
67
68impl SafeClient {
69 pub fn new(ctx: Context) -> Self {
75 Self {
76 ctx: Arc::new(Mutex::new(ctx)),
77 }
78 }
79
80 pub fn from_shared(ctx: Arc<Mutex<Context>>) -> Self {
85 Self { ctx }
86 }
87
88 pub fn clone_shared(&self) -> Arc<Mutex<Context>> {
93 self.ctx.clone()
94 }
95
96 read_holding!(system_type, SystemType);
97 write_holding!(system_type, SystemType);
98 read_holding!(pulse_width, PulseWidth);
99 write_holding!(pulse_width, PulseWidth);
100 read_holding!(kppa, KPPA);
101
102 pub fn set_kppa(&mut self, password: proto::Password) -> Result<()> {
106 let mut ctx = self.ctx.lock().unwrap();
107 SDM72::set_kppa(&mut ctx, password)
108 }
109
110 read_holding!(parity_and_stop_bit, ParityAndStopBit);
111 write_holding!(parity_and_stop_bit, ParityAndStopBit);
112 read_holding!(address, Address);
113
114 pub fn set_address(&mut self, value: proto::Address) -> Result<()> {
115 let mut ctx = self.ctx.lock().unwrap();
116 SDM72::set_address(&mut ctx, value)?;
117 ctx.set_slave(tokio_modbus::Slave(*value));
118 Ok(())
119 }
120
121 read_holding!(pulse_constant, PulseConstant);
122 write_holding!(pulse_constant, PulseConstant);
123 read_holding!(password, Password);
124 write_holding!(password, Password);
125 read_holding!(baud_rate, BaudRate);
126 write_holding!(baud_rate, BaudRate);
127 read_holding!(auto_scroll_time, AutoScrollTime);
128 write_holding!(auto_scroll_time, AutoScrollTime);
129 read_holding!(backlight_time, BacklightTime);
130 write_holding!(backlight_time, BacklightTime);
131 read_holding!(pulse_energy_type, PulseEnergyType);
132 write_holding!(pulse_energy_type, PulseEnergyType);
133
134 pub fn reset_historical_data(&mut self) -> Result<()> {
138 let mut ctx = self.ctx.lock().unwrap();
139 SDM72::reset_historical_data(&mut ctx)
140 }
141
142 read_holding!(serial_number, SerialNumber);
143 read_holding!(meter_code, MeterCode);
144 read_holding!(software_version, SoftwareVersion);
145
146 pub fn read_all_settings(&mut self, delay: &std::time::Duration) -> Result<AllSettings> {
148 let mut ctx = self.ctx.lock().unwrap();
149 SDM72::read_all_settings(&mut ctx, delay)
150 }
151
152 pub fn read_all(&mut self, delay: &std::time::Duration) -> Result<AllValues> {
154 let mut ctx = self.ctx.lock().unwrap();
155 SDM72::read_all(&mut ctx, delay)
156 }
157}