1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
use std::time::Duration;
use anyhow::Result;
use camper_widget_refresh::dbus::{
PropertyType, start_service_at, update_connected_status, update_single_property,
};
use futures_util::StreamExt;
use tokio::time::timeout;
use zbus::{Connection, connection, proxy};
const TIMEOUT: Duration = Duration::from_secs(2);
// ---------- Client-side proxy definitions ----------
#[proxy(
interface = "org.craftknight.CamperWidget.Status",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget"
)]
trait Status {
#[zbus(property)]
fn connected(&self) -> zbus::Result<bool>;
}
#[proxy(
interface = "org.craftknight.CamperWidget.Battery",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget/Battery"
)]
trait Battery {
#[zbus(property)]
fn soc(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn power(&self) -> zbus::Result<f64>;
#[zbus(property)]
fn state(&self) -> zbus::Result<String>;
}
#[proxy(
interface = "org.craftknight.CamperWidget.Solar",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget/Solar"
)]
trait Solar {
#[zbus(property)]
fn power(&self) -> zbus::Result<f64>;
}
#[proxy(
interface = "org.craftknight.CamperWidget.AcInput",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget/AcInput"
)]
trait AcInput {
#[zbus(property)]
fn power(&self) -> zbus::Result<f64>;
}
#[proxy(
interface = "org.craftknight.CamperWidget.AcLoad",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget/AcLoad"
)]
trait AcLoad {
#[zbus(property)]
fn power(&self) -> zbus::Result<f64>;
}
#[proxy(
interface = "org.craftknight.CamperWidget.Config",
default_service = "org.craftknight.CamperWidget",
default_path = "/org/craftknight/camper_widget"
)]
trait Config {
fn reload(&self) -> zbus::Result<()>;
}
// ---------- Test fixture ----------
struct DbusTestFixture {
_daemon: dbus_launch::Daemon,
address: String,
}
impl DbusTestFixture {
fn new() -> Self {
let daemon = dbus_launch::Launcher::daemon()
.launch()
.expect("failed to launch dbus-daemon — is dbus-daemon installed?");
let address = daemon.address().to_string();
Self {
_daemon: daemon,
address,
}
}
fn address(&self) -> &str {
&self.address
}
async fn client_connection(&self) -> Result<Connection> {
let conn = connection::Builder::address(self.address())?
.build()
.await?;
Ok(conn)
}
}
// ---------- Tests ----------
#[tokio::test]
async fn test_default_property_values() -> Result<()> {
let fixture = DbusTestFixture::new();
let (_conn, _state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let status = StatusProxy::new(&client).await?;
let battery = BatteryProxy::new(&client).await?;
let solar = SolarProxy::new(&client).await?;
let ac_input = AcInputProxy::new(&client).await?;
let ac_load = AcLoadProxy::new(&client).await?;
assert!(!status.connected().await?);
assert_eq!(battery.soc().await?, 0.0);
assert_eq!(battery.power().await?, 0.0);
assert_eq!(battery.state().await?, "idle");
assert_eq!(solar.power().await?, 0.0);
assert_eq!(ac_input.power().await?, 0.0);
assert_eq!(ac_load.power().await?, 0.0);
Ok(())
}
#[tokio::test]
async fn test_connected_status_update() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let status = StatusProxy::new(&client).await?;
assert!(!status.connected().await?);
// Subscribe to changes before mutating
let mut stream = status.receive_connected_changed().await;
update_connected_status(&conn, &state, true).await?;
// Wait for a property-changed signal with the expected value.
// The proxy may deliver a cached/initial signal first, so loop until
// we see the value we expect or timeout.
let deadline = tokio::time::Instant::now() + TIMEOUT;
loop {
let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
if remaining.is_zero() {
panic!("timed out waiting for connected=true signal");
}
let signal = timeout(remaining, stream.next()).await?;
let changed = signal.expect("stream ended unexpectedly");
if changed.get().await? {
break; // got the expected value
}
}
Ok(())
}
#[tokio::test]
async fn test_battery_soc_update() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let battery = BatteryProxy::new(&client).await?;
let mut stream = battery.receive_soc_changed().await;
update_single_property(&conn, &state, &PropertyType::BatterySoc, 85.5).await?;
let signal = timeout(TIMEOUT, stream.next()).await?;
assert!(signal.is_some());
assert!((signal.unwrap().get().await? - 85.5).abs() < f64::EPSILON);
Ok(())
}
#[tokio::test]
async fn test_battery_power_and_state_derivation() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let battery = BatteryProxy::new(&client).await?;
let mut state_stream = battery.receive_state_changed().await;
// Positive power above threshold -> charging
update_single_property(&conn, &state, &PropertyType::BatteryPower, 12.0).await?;
update_single_property(&conn, &state, &PropertyType::BatteryState, 12.0).await?;
let signal = timeout(TIMEOUT, state_stream.next()).await?;
assert!(signal.is_some());
assert_eq!(signal.unwrap().get().await?, "charging");
// Negative power below threshold -> discharging
update_single_property(&conn, &state, &PropertyType::BatteryPower, -12.0).await?;
update_single_property(&conn, &state, &PropertyType::BatteryState, -12.0).await?;
let signal = timeout(TIMEOUT, state_stream.next()).await?;
assert!(signal.is_some());
assert_eq!(signal.unwrap().get().await?, "discharging");
// Small power within threshold -> idle
update_single_property(&conn, &state, &PropertyType::BatteryPower, 3.0).await?;
update_single_property(&conn, &state, &PropertyType::BatteryState, 3.0).await?;
let signal = timeout(TIMEOUT, state_stream.next()).await?;
assert!(signal.is_some());
assert_eq!(signal.unwrap().get().await?, "idle");
Ok(())
}
#[tokio::test]
async fn test_solar_power_update() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let solar = SolarProxy::new(&client).await?;
let mut stream = solar.receive_power_changed().await;
update_single_property(&conn, &state, &PropertyType::SolarPower, 250.0).await?;
let signal = timeout(TIMEOUT, stream.next()).await?;
assert!(signal.is_some());
assert!((signal.unwrap().get().await? - 250.0).abs() < f64::EPSILON);
Ok(())
}
#[tokio::test]
async fn test_ac_input_power_update() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let ac_input = AcInputProxy::new(&client).await?;
let mut stream = ac_input.receive_power_changed().await;
update_single_property(&conn, &state, &PropertyType::AcInputPower, 82.0).await?;
let signal = timeout(TIMEOUT, stream.next()).await?;
assert!(signal.is_some());
assert!((signal.unwrap().get().await? - 82.0).abs() < f64::EPSILON);
Ok(())
}
#[tokio::test]
async fn test_ac_load_power_update() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let ac_load = AcLoadProxy::new(&client).await?;
let mut stream = ac_load.receive_power_changed().await;
update_single_property(&conn, &state, &PropertyType::AcLoadPower, 77.0).await?;
let signal = timeout(TIMEOUT, stream.next()).await?;
assert!(signal.is_some());
assert!((signal.unwrap().get().await? - 77.0).abs() < f64::EPSILON);
Ok(())
}
#[tokio::test]
async fn test_config_reload_method() -> Result<()> {
let fixture = DbusTestFixture::new();
let (_conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let config = ConfigProxy::new(&client).await?;
// Spawn a listener for the notify signal
let notify = state.config_reload_notify.clone();
let listener = tokio::spawn(async move { notify.notified().await });
// Call reload via D-Bus
config.reload().await?;
// The listener should complete within the timeout
timeout(TIMEOUT, listener).await??;
Ok(())
}
#[tokio::test]
async fn test_property_change_signal_emitted() -> Result<()> {
let fixture = DbusTestFixture::new();
let (conn, state) = start_service_at(fixture.address()).await?;
let client = fixture.client_connection().await?;
let battery = BatteryProxy::new(&client).await?;
// Subscribe to soc property changes before making updates
let mut stream = battery.receive_soc_changed().await;
// Update the property
update_single_property(&conn, &state, &PropertyType::BatterySoc, 50.0).await?;
// Should receive the change signal
let signal = timeout(TIMEOUT, stream.next()).await?;
assert!(signal.is_some());
let changed = signal.unwrap();
assert!((changed.get().await? - 50.0).abs() < f64::EPSILON);
Ok(())
}
|