aranet-service 0.1.13

Background collector and HTTP REST API for Aranet sensors
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Background data collector for polling Aranet devices.
//!
//! This module provides a background collector that polls configured devices at their
//! specified intervals and stores readings in the database.
//!
//! # Concurrency Model
//!
//! The collector uses a "task per device" model:
//!
//! - Each configured device gets its own Tokio task
//! - Tasks run independently with their own polling intervals
//! - Tasks share access to the application state via `Arc<AppState>`
//!
//! ## Lock Acquisition
//!
//! Device polling tasks acquire locks in this order:
//!
//! 1. **`device_stats` write lock** - Brief lock to update polling status
//! 2. **BLE device communication** - No Rust locks, but exclusive Bluetooth access
//! 3. **`store` mutex** - Brief lock to insert the reading
//! 4. **`device_stats` write lock** - Brief lock to update success/failure counts
//!
//! ## Graceful Shutdown
//!
//! The collector uses a `watch` channel for graceful shutdown:
//!
//! - [`Collector::stop()`] sends a stop signal to all tasks
//! - Each task checks for the stop signal between poll cycles
//! - Tasks complete their current operation before stopping
//!
//! ## Error Handling
//!
//! Connection and read errors are tracked per-device and logged with progressive
//! quieting: errors are logged at WARN level for the first 3 failures, then at
//! ERROR level once, then silently retried. This prevents log spam for devices
//! that are temporarily unavailable.
//!
//! # Example
//!
//! ```ignore
//! let collector = Collector::new(Arc::clone(&state));
//! collector.start().await;  // Returns immediately, collection is background
//!
//! // Later...
//! collector.stop();  // Signal all tasks to stop
//! ```

use std::sync::Arc;
use std::time::Duration;

use time::OffsetDateTime;
use tokio::sync::watch;
use tokio::task::JoinSet;
use tokio::time::interval;
use tracing::{debug, error, info, warn};

use aranet_core::Device;
use aranet_store::StoredReading;

use crate::config::DeviceConfig;
use crate::state::{AppState, DeviceCollectionStats, ReadingEvent};

/// Background collector that polls devices on their configured intervals.
pub struct Collector {
    state: Arc<AppState>,
    /// JoinSet to track spawned device polling tasks.
    tasks: JoinSet<()>,
}

impl Collector {
    /// Create a new collector.
    pub fn new(state: Arc<AppState>) -> Self {
        Self {
            state,
            tasks: JoinSet::new(),
        }
    }

    /// Start collecting data from all configured devices.
    ///
    /// This spawns a separate task for each device that polls at the configured interval.
    /// Returns immediately; collection happens in the background.
    ///
    /// Also spawns a reload watcher task that will restart the collector when
    /// the device configuration changes via the API.
    pub async fn start(&mut self) {
        // Reset stop signal if previously stopped
        self.state.collector.reset_stop();

        let config = self.state.config.read().await;
        let devices = config.devices.clone();
        drop(config);

        if devices.is_empty() {
            info!("No devices configured for collection");
            return;
        }

        info!("Starting collector for {} device(s)", devices.len());

        // Initialize device stats
        {
            let mut stats = self.state.collector.device_stats.write().await;
            stats.clear();
            for device in &devices {
                stats.push(DeviceCollectionStats {
                    device_id: device.address.clone(),
                    alias: device.alias.clone(),
                    poll_interval: device.poll_interval,
                    last_poll_at: None,
                    last_error_at: None,
                    last_error: None,
                    success_count: 0,
                    failure_count: 0,
                    polling: false,
                });
            }
        }

        // Mark as running
        self.state.collector.set_running(true);

        // Spawn device tasks into the shared JoinSet on CollectorState
        // This allows the reload watcher to also spawn tasks that are properly tracked
        for device_config in devices {
            let state = Arc::clone(&self.state);
            let stop_rx = self.state.collector.subscribe_stop();
            self.state
                .collector
                .spawn_device_task(async move {
                    collect_device(state, device_config, stop_rx).await;
                })
                .await;
        }

        // Spawn reload watcher task
        let state = Arc::clone(&self.state);
        self.tasks.spawn(async move {
            watch_for_reload(state).await;
        });
    }

    /// Stop the collector and wait for all tasks to complete.
    pub async fn stop(&mut self) {
        info!("Stopping collector");
        self.state.collector.signal_stop();

        // Wait for device tasks in the shared JoinSet (with timeout)
        let stopped_cleanly = self
            .state
            .collector
            .wait_for_device_tasks(Duration::from_secs(10))
            .await;

        if !stopped_cleanly {
            warn!("Device tasks did not stop within timeout, aborted");
        }

        // Also wait for the reload watcher task in our local JoinSet
        let timeout = tokio::time::timeout(Duration::from_secs(2), async {
            while self.tasks.join_next().await.is_some() {}
        });

        if timeout.await.is_err() {
            warn!("Reload watcher did not stop within timeout, aborting");
            self.tasks.abort_all();
        }
    }

    /// Check if the collector is running.
    pub fn is_running(&self) -> bool {
        self.state.collector.is_running()
    }

    /// Get the number of active collection tasks.
    pub fn task_count(&self) -> usize {
        self.tasks.len()
    }
}

/// Watch for configuration reload signals and restart collection tasks.
async fn watch_for_reload(state: Arc<AppState>) {
    let mut reload_rx = state.collector.subscribe_reload();
    let mut stop_rx = state.collector.subscribe_stop();

    loop {
        tokio::select! {
            result = reload_rx.changed() => {
                if result.is_err() {
                    // Sender dropped, exit
                    break;
                }

                info!("Configuration reload requested, restarting device tasks");

                // Signal current tasks to stop
                state.collector.signal_stop();

                // Wait a moment for tasks to notice the stop signal
                tokio::time::sleep(Duration::from_millis(100)).await;

                // Reset stop signal
                state.collector.reset_stop();

                // Read new config
                let config = state.config.read().await;
                let devices = config.devices.clone();
                drop(config);

                // Re-initialize device stats
                {
                    let mut stats = state.collector.device_stats.write().await;
                    stats.clear();
                    for device in &devices {
                        stats.push(DeviceCollectionStats {
                            device_id: device.address.clone(),
                            alias: device.alias.clone(),
                            poll_interval: device.poll_interval,
                            last_poll_at: None,
                            last_error_at: None,
                            last_error: None,
                            success_count: 0,
                            failure_count: 0,
                            polling: false,
                        });
                    }
                }

                if devices.is_empty() {
                    info!("No devices configured after reload");
                    continue;
                }

                info!("Restarting collector for {} device(s)", devices.len());

                // Spawn new device tasks into the shared JoinSet
                for device_config in devices {
                    let state_clone = Arc::clone(&state);
                    let stop_rx = state.collector.subscribe_stop();
                    state
                        .collector
                        .spawn_device_task(async move {
                            collect_device(state_clone, device_config, stop_rx).await;
                        })
                        .await;
                }
            }
            _ = stop_rx.changed() => {
                if *stop_rx.borrow() {
                    info!("Reload watcher received stop signal");
                    break;
                }
            }
        }
    }
}

/// Collect readings from a single device.
async fn collect_device(
    state: Arc<AppState>,
    config: DeviceConfig,
    mut stop_rx: watch::Receiver<bool>,
) {
    let device_id = config.address.clone();
    let alias = config.alias.as_deref().unwrap_or(&device_id);
    let poll_interval = Duration::from_secs(config.poll_interval);

    info!(
        "Starting collector for {} (alias: {}, interval: {}s)",
        device_id, alias, config.poll_interval
    );

    let mut interval_timer = interval(poll_interval);
    let mut consecutive_failures = 0u32;

    loop {
        tokio::select! {
            _ = interval_timer.tick() => {
                // Update stats: mark as polling
                update_device_stat(&state, &device_id, |stat| {
                    stat.polling = true;
                }).await;

                match poll_device(&state, &device_id).await {
                    Ok(reading) => {
                        consecutive_failures = 0;
                        debug!("Collected reading from {}: CO2={}", device_id, reading.co2);

                        // Update stats
                        update_device_stat(&state, &device_id, |stat| {
                            stat.last_poll_at = Some(OffsetDateTime::now_utc());
                            stat.success_count += 1;
                            stat.polling = false;
                        }).await;

                        // Broadcast the reading to WebSocket clients
                        let event = ReadingEvent {
                            device_id: device_id.clone(),
                            reading,
                        };
                        if state.readings_tx.send(event).is_err() {
                            debug!("No active WebSocket subscribers for reading broadcast");
                        }
                    }
                    Err(e) => {
                        consecutive_failures += 1;

                        // Update stats
                        update_device_stat(&state, &device_id, |stat| {
                            stat.last_error_at = Some(OffsetDateTime::now_utc());
                            stat.last_error = Some(e.to_string());
                            stat.failure_count += 1;
                            stat.polling = false;
                        }).await;

                        if consecutive_failures <= 3 {
                            warn!(
                                "Failed to poll {}: {} (attempt {})",
                                device_id, e, consecutive_failures
                            );
                        } else if consecutive_failures == 4 {
                            error!(
                                "Failed to poll {} after {} attempts, reducing log frequency",
                                device_id, consecutive_failures
                            );
                        } else if consecutive_failures.is_multiple_of(100) {
                            error!(
                                "Failed to poll {} ({} consecutive failures): {}",
                                device_id, consecutive_failures, e
                            );
                        }
                        // Continue trying - the device may come back online
                    }
                }
            }
            _ = stop_rx.changed() => {
                if *stop_rx.borrow() {
                    info!("Collector for {} received stop signal", device_id);
                    break;
                }
            }
        }
    }

    info!("Collector for {} stopped", device_id);
}

/// Update stats for a specific device.
async fn update_device_stat<F>(state: &AppState, device_id: &str, update_fn: F)
where
    F: FnOnce(&mut DeviceCollectionStats),
{
    let mut stats = state.collector.device_stats.write().await;
    if let Some(stat) = stats.iter_mut().find(|s| s.device_id == device_id) {
        update_fn(stat);
    }
}

/// Poll a single device and store the reading.
async fn poll_device(state: &AppState, device_id: &str) -> Result<StoredReading, CollectorError> {
    // Connect to the device
    let device = Device::connect(device_id)
        .await
        .map_err(CollectorError::Connect)?;

    // Read current values
    let reading = device.read_current().await.map_err(CollectorError::Read)?;

    // Disconnect
    let _ = device.disconnect().await;

    // Store the reading
    {
        let store = state.store.lock().await;
        store
            .insert_reading(device_id, &reading)
            .map_err(CollectorError::Store)?;
    }

    // Return the stored reading
    Ok(StoredReading::from_reading(device_id, &reading))
}

/// Collector errors.
#[derive(Debug, thiserror::Error)]
pub enum CollectorError {
    #[error("Failed to connect: {0}")]
    Connect(aranet_core::Error),
    #[error("Failed to read: {0}")]
    Read(aranet_core::Error),
    #[error("Failed to store: {0}")]
    Store(aranet_store::Error),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;

    fn create_test_state() -> Arc<AppState> {
        let store = aranet_store::Store::open_in_memory().unwrap();
        let config = Config::default();
        AppState::new(store, config)
    }

    #[test]
    fn test_collector_new() {
        let state = create_test_state();
        let collector = Collector::new(Arc::clone(&state));
        assert!(!collector.is_running());
    }

    #[test]
    fn test_collector_is_running_initially_false() {
        let state = create_test_state();
        let collector = Collector::new(state);
        assert!(!collector.is_running());
    }

    #[tokio::test]
    async fn test_collector_start_no_devices() {
        let state = create_test_state();
        let mut collector = Collector::new(Arc::clone(&state));

        // Start with no devices configured
        collector.start().await;

        // Should not be running since there are no devices
        // (the collector returns early if no devices are configured)
        // But it would still set running=true briefly; let's test the stats are empty
        let stats = state.collector.device_stats.read().await;
        assert!(stats.is_empty());
    }

    #[tokio::test]
    async fn test_collector_start_with_devices_initializes_stats() {
        let state = create_test_state();

        // Add a device to config
        {
            let mut config = state.config.write().await;
            config.devices.push(crate::config::DeviceConfig {
                address: "AA:BB:CC:DD:EE:FF".to_string(),
                alias: Some("Test Device".to_string()),
                poll_interval: 60,
            });
        }

        let mut collector = Collector::new(Arc::clone(&state));
        collector.start().await;

        // Wait a moment for async initialization
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Check that device stats were initialized
        let stats = state.collector.device_stats.read().await;
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].device_id, "AA:BB:CC:DD:EE:FF");
        assert_eq!(stats[0].alias, Some("Test Device".to_string()));
        assert_eq!(stats[0].poll_interval, 60);
        // Note: success_count, failure_count, and polling may have changed
        // due to async collector activity, so we only verify initialization happened
    }

    #[tokio::test]
    async fn test_collector_stop() {
        let state = create_test_state();
        state.collector.set_running(true);

        let mut collector = Collector::new(Arc::clone(&state));
        assert!(collector.is_running());

        collector.stop().await;
        assert!(!collector.is_running());
    }

    #[test]
    fn test_collector_error_display_connect() {
        let core_error = aranet_core::Error::NotConnected;
        let error = CollectorError::Connect(core_error);
        let display = format!("{}", error);
        assert!(display.contains("Failed to connect"));
    }

    #[test]
    fn test_collector_error_display_read() {
        let core_error = aranet_core::Error::NotConnected;
        let error = CollectorError::Read(core_error);
        let display = format!("{}", error);
        assert!(display.contains("Failed to read"));
    }

    #[test]
    fn test_collector_error_display_store() {
        let store_error = aranet_store::Error::DeviceNotFound("test".to_string());
        let error = CollectorError::Store(store_error);
        let display = format!("{}", error);
        assert!(display.contains("Failed to store"));
    }

    #[test]
    fn test_collector_error_debug() {
        let core_error = aranet_core::Error::NotConnected;
        let error = CollectorError::Connect(core_error);
        let debug = format!("{:?}", error);
        assert!(debug.contains("Connect"));
    }

    #[tokio::test]
    async fn test_device_collection_stats_initialization() {
        let stats = DeviceCollectionStats {
            device_id: "test-device".to_string(),
            alias: Some("Test Alias".to_string()),
            poll_interval: 120,
            last_poll_at: None,
            last_error_at: None,
            last_error: None,
            success_count: 0,
            failure_count: 0,
            polling: false,
        };

        assert_eq!(stats.device_id, "test-device");
        assert_eq!(stats.alias, Some("Test Alias".to_string()));
        assert_eq!(stats.poll_interval, 120);
        assert!(stats.last_poll_at.is_none());
        assert_eq!(stats.success_count, 0);
        assert_eq!(stats.failure_count, 0);
        assert!(!stats.polling);
    }

    #[tokio::test]
    async fn test_update_device_stat() {
        let state = create_test_state();

        // Initialize stats
        {
            let mut stats = state.collector.device_stats.write().await;
            stats.push(DeviceCollectionStats {
                device_id: "test-device".to_string(),
                alias: None,
                poll_interval: 60,
                last_poll_at: None,
                last_error_at: None,
                last_error: None,
                success_count: 0,
                failure_count: 0,
                polling: false,
            });
        }

        // Update the stat
        update_device_stat(&state, "test-device", |stat| {
            stat.success_count = 5;
            stat.polling = true;
        })
        .await;

        // Verify the update
        let stats = state.collector.device_stats.read().await;
        assert_eq!(stats[0].success_count, 5);
        assert!(stats[0].polling);
    }

    #[tokio::test]
    async fn test_update_device_stat_nonexistent_device() {
        let state = create_test_state();

        // Initialize stats with one device
        {
            let mut stats = state.collector.device_stats.write().await;
            stats.push(DeviceCollectionStats {
                device_id: "existing-device".to_string(),
                alias: None,
                poll_interval: 60,
                last_poll_at: None,
                last_error_at: None,
                last_error: None,
                success_count: 0,
                failure_count: 0,
                polling: false,
            });
        }

        // Try to update a nonexistent device - should not panic
        update_device_stat(&state, "nonexistent-device", |stat| {
            stat.success_count = 10;
        })
        .await;

        // Verify existing device wasn't changed
        let stats = state.collector.device_stats.read().await;
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].success_count, 0);
    }

    #[tokio::test]
    async fn test_collector_multiple_devices() {
        let state = create_test_state();

        // Add multiple devices
        {
            let mut config = state.config.write().await;
            config.devices.push(crate::config::DeviceConfig {
                address: "DEVICE-1".to_string(),
                alias: Some("First".to_string()),
                poll_interval: 30,
            });
            config.devices.push(crate::config::DeviceConfig {
                address: "DEVICE-2".to_string(),
                alias: Some("Second".to_string()),
                poll_interval: 60,
            });
            config.devices.push(crate::config::DeviceConfig {
                address: "DEVICE-3".to_string(),
                alias: None,
                poll_interval: 120,
            });
        }

        let mut collector = Collector::new(Arc::clone(&state));
        collector.start().await;

        // Wait for initialization
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Check all devices were initialized
        let stats = state.collector.device_stats.read().await;
        assert_eq!(stats.len(), 3);

        // Verify each device
        let device1 = stats.iter().find(|s| s.device_id == "DEVICE-1").unwrap();
        assert_eq!(device1.alias, Some("First".to_string()));
        assert_eq!(device1.poll_interval, 30);

        let device2 = stats.iter().find(|s| s.device_id == "DEVICE-2").unwrap();
        assert_eq!(device2.alias, Some("Second".to_string()));
        assert_eq!(device2.poll_interval, 60);

        let device3 = stats.iter().find(|s| s.device_id == "DEVICE-3").unwrap();
        assert!(device3.alias.is_none());
        assert_eq!(device3.poll_interval, 120);
    }
}