1use crate::signals::{notify_memory_pressure, sync_signals_from_mock};
4use crate::types::*;
5use std::sync::{Arc, Mutex, OnceLock};
6
7#[derive(Debug, Clone, PartialEq, Default)]
9pub struct DeviceTelemetrySnapshot {
10 pub battery: BatteryInfo,
11 pub thermal: ThermalState,
12 pub network: NetworkStatus,
13 pub screen: ScreenGeometry,
14 pub display_refresh: DisplayRefreshInfo,
15 pub gpu: GpuInfo,
16 pub geolocation: GeolocationInfo,
17 pub compass: CompassReading,
18 pub imu: ImuReading,
19 pub barometer: BarometerReading,
20 pub ambient_light: AmbientLightReading,
21}
22
23static GLOBAL_MOCK: OnceLock<Arc<Mutex<DeviceTelemetrySnapshot>>> = OnceLock::new();
24
25fn get_global_mock() -> &'static Arc<Mutex<DeviceTelemetrySnapshot>> {
26 GLOBAL_MOCK.get_or_init(|| Arc::new(Mutex::new(DeviceTelemetrySnapshot::default())))
27}
28
29#[derive(Debug, Clone)]
31pub struct DeviceMockController {
32 state: Arc<Mutex<DeviceTelemetrySnapshot>>,
33}
34
35impl Default for DeviceMockController {
36 fn default() -> Self {
37 Self::new()
38 }
39}
40
41impl DeviceMockController {
42 pub fn new() -> Self {
44 Self {
45 state: Arc::clone(get_global_mock()),
46 }
47 }
48
49 pub fn reset_to_defaults(&self) {
51 let mut guard = self.state.lock().unwrap();
52 *guard = DeviceTelemetrySnapshot::default();
53 drop(guard);
54 sync_signals_from_mock(self);
55 }
56
57 pub fn battery(&self) -> BatteryInfo {
59 self.state.lock().unwrap().battery
60 }
61
62 pub fn set_battery(&self, info: BatteryInfo) {
64 self.state.lock().unwrap().battery = info;
65 sync_signals_from_mock(self);
66 }
67
68 pub fn set_battery_level(&self, level: f32) {
70 let mut guard = self.state.lock().unwrap();
71 guard.battery.level = level.clamp(0.0, 1.0);
72 drop(guard);
73 sync_signals_from_mock(self);
74 }
75
76 pub fn thermal(&self) -> ThermalState {
78 self.state.lock().unwrap().thermal
79 }
80
81 pub fn set_thermal(&self, state: ThermalState) {
83 self.state.lock().unwrap().thermal = state;
84 sync_signals_from_mock(self);
85 }
86
87 pub fn network(&self) -> NetworkStatus {
89 self.state.lock().unwrap().network
90 }
91
92 pub fn set_network(&self, status: NetworkStatus) {
94 self.state.lock().unwrap().network = status;
95 sync_signals_from_mock(self);
96 }
97
98 pub fn set_online(&self, is_online: bool) {
100 let mut guard = self.state.lock().unwrap();
101 guard.network.is_online = is_online;
102 drop(guard);
103 sync_signals_from_mock(self);
104 }
105
106 pub fn screen(&self) -> ScreenGeometry {
108 self.state.lock().unwrap().screen
109 }
110
111 pub fn set_screen(&self, screen: ScreenGeometry) {
113 self.state.lock().unwrap().screen = screen;
114 sync_signals_from_mock(self);
115 }
116
117 pub fn set_safe_area(&self, insets: SafeAreaInsets) {
119 let mut guard = self.state.lock().unwrap();
120 guard.screen.safe_area = insets;
121 drop(guard);
122 sync_signals_from_mock(self);
123 }
124
125 pub fn display_refresh(&self) -> DisplayRefreshInfo {
127 self.state.lock().unwrap().display_refresh
128 }
129
130 pub fn set_display_refresh(&self, info: DisplayRefreshInfo) {
132 self.state.lock().unwrap().display_refresh = info;
133 sync_signals_from_mock(self);
134 }
135
136 pub fn gpu(&self) -> GpuInfo {
138 self.state.lock().unwrap().gpu.clone()
139 }
140
141 pub fn set_gpu(&self, gpu: GpuInfo) {
143 self.state.lock().unwrap().gpu = gpu;
144 }
145
146 pub fn geolocation(&self) -> GeolocationInfo {
148 self.state.lock().unwrap().geolocation.clone()
149 }
150
151 pub fn set_geolocation(&self, info: GeolocationInfo) {
153 self.state.lock().unwrap().geolocation = info;
154 sync_signals_from_mock(self);
155 }
156
157 pub fn set_coordinates(
159 &self,
160 latitude: f64,
161 longitude: f64,
162 altitude_m: Option<f64>,
163 accuracy_m: f64,
164 ) {
165 let fix = GeolocationInfo::available(
166 latitude,
167 longitude,
168 accuracy_m,
169 GeolocationSource::MockOverride,
170 )
171 .with_altitude(altitude_m);
172 self.set_geolocation(fix);
173 }
174
175 pub fn clear_geolocation(&self) {
177 self.set_geolocation(GeolocationInfo::default());
178 }
179
180 pub fn compass(&self) -> CompassReading {
182 self.state.lock().unwrap().compass
183 }
184
185 pub fn set_compass(&self, reading: CompassReading) {
187 self.state.lock().unwrap().compass = reading;
188 sync_signals_from_mock(self);
189 }
190
191 pub fn imu(&self) -> ImuReading {
193 self.state.lock().unwrap().imu
194 }
195
196 pub fn set_imu(&self, reading: ImuReading) {
198 self.state.lock().unwrap().imu = reading;
199 sync_signals_from_mock(self);
200 }
201
202 pub fn barometer(&self) -> BarometerReading {
204 self.state.lock().unwrap().barometer
205 }
206
207 pub fn set_barometer(&self, reading: BarometerReading) {
209 self.state.lock().unwrap().barometer = reading;
210 sync_signals_from_mock(self);
211 }
212
213 pub fn ambient_light(&self) -> AmbientLightReading {
215 self.state.lock().unwrap().ambient_light
216 }
217
218 pub fn set_ambient_light(&self, reading: AmbientLightReading) {
220 self.state.lock().unwrap().ambient_light = reading;
221 sync_signals_from_mock(self);
222 }
223
224 pub fn trigger_memory_pressure(&self, level: MemoryPressure) {
226 notify_memory_pressure(level);
227 }
228}