aranet-cli 0.2.0

Command-line interface for Aranet environmental 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
//! Native desktop GUI for Aranet environmental sensors.
//!
//! This module provides a cross-platform GUI application built with [egui](https://www.egui.rs/).
//!
//! # Usage
//!
//! Run directly:
//! ```bash
//! aranet gui
//! ```
//!
//! Or via the standalone binary:
//! ```bash
//! aranet-gui
//! ```

mod app;
mod components;
pub mod demo;
mod export;
mod helpers;
mod menu;
mod panels;
mod readings;
mod theme;
mod tray;
mod types;
mod worker;

use std::path::PathBuf;
use std::sync::mpsc as std_mpsc;
use std::sync::{Arc, Mutex};

use anyhow::Result;
use aranet_store::default_db_path;
use eframe::egui::{self, IconData};
use tokio::sync::mpsc;
use tracing::{debug, info, warn};

use aranet_core::messages::{Command, SensorEvent};

use crate::config::Config;

/// Embedded icon PNG data (64x64 RGBA)
pub(crate) const ICON_PNG: &[u8] = include_bytes!("../../assets/aranet-icon.png");

/// Load the application icon from embedded PNG data.
fn load_icon() -> Option<Arc<IconData>> {
    let img = image::load_from_memory(ICON_PNG).ok()?.into_rgba8();
    let (width, height) = img.dimensions();
    Some(Arc::new(IconData {
        rgba: img.into_raw(),
        width,
        height,
    }))
}

pub use app::AranetApp;
pub use menu::{MenuCommand, MenuManager};
pub use theme::{Theme, ThemeMode};
pub use tray::{
    TrayCommand, TrayError, TrayManager, TrayState, check_co2_threshold, hide_dock_icon,
    set_egui_context, show_dock_icon,
};
pub use types::{
    AlertEntry, AlertSeverity, AlertType, Co2Level, ConnectionFilter, ConnectionState, DeviceState,
    DeviceTypeFilter, HistoryFilter, RadonLevel, Tab, Trend,
};
pub use worker::SensorWorker;

/// Options for running the GUI application.
#[derive(Debug, Default, Clone)]
pub struct GuiOptions {
    /// Run in demo mode with mock data (for screenshots).
    pub demo: bool,
    /// Take a screenshot and save to this path, then exit.
    pub screenshot: Option<PathBuf>,
    /// Number of frames to wait before taking screenshot (default: 3).
    pub screenshot_delay_frames: u32,
}

impl GuiOptions {
    /// Create new options with demo mode enabled.
    pub fn demo() -> Self {
        Self {
            demo: true,
            ..Default::default()
        }
    }

    /// Set screenshot output path.
    pub fn with_screenshot(mut self, path: impl Into<PathBuf>) -> Self {
        self.screenshot = Some(path.into());
        self
    }
}

/// Run the GUI application.
///
/// This is the main entry point for the GUI. It:
/// 1. Sets up the tokio runtime in a background thread
/// 2. Creates communication channels between UI and worker
/// 3. Spawns the background sensor worker
/// 4. Runs the egui/eframe main loop
pub fn run() -> Result<()> {
    tracing_subscriber::fmt::init();

    // Load config to get service URL
    let config = Config::load_or_default()?;
    let service_url = config.gui.service_url.clone();
    let service_api_key = config.gui.service_api_key.clone();

    // Get store path (shared database location)
    let store_path = default_db_path();
    info!("Using database at: {:?}", store_path);

    // Create tokio runtime in a separate thread
    let (command_tx, command_rx) = mpsc::channel::<Command>(32);
    let (event_tx, event_rx_tokio) = mpsc::channel::<SensorEvent>(32);

    // Bridge from tokio mpsc to std mpsc for sync access in egui
    let (std_tx, std_rx) = std_mpsc::channel::<SensorEvent>();

    // Clone command_tx for sending initial load command
    let startup_command_tx = command_tx.clone();

    // Spawn tokio runtime thread
    std::thread::spawn(move || {
        let rt = match tokio::runtime::Runtime::new() {
            Ok(rt) => rt,
            Err(e) => {
                tracing::error!("Failed to create tokio runtime: {}", e);
                return;
            }
        };
        rt.block_on(async {
            let worker = SensorWorker::with_service_config(
                command_rx,
                event_tx,
                store_path,
                &service_url,
                service_api_key,
            );

            // Send startup commands: load cached data and fetch service status
            if let Err(e) = startup_command_tx.send(Command::LoadCachedData).await {
                tracing::error!("Failed to send LoadCachedData command at startup: {}", e);
            }
            if let Err(e) = startup_command_tx.send(Command::RefreshServiceStatus).await {
                tracing::warn!(
                    "Failed to send RefreshServiceStatus command at startup: {}",
                    e
                );
            }

            // Forward events from worker to std channel
            let mut event_rx = event_rx_tokio;
            let forward_handle = tokio::spawn(async move {
                while let Some(event) = event_rx.recv().await {
                    if std_tx.send(event).is_err() {
                        break; // GUI closed
                    }
                }
            });

            // Run the worker
            worker.run().await;
            forward_handle.abort();
        });
    });

    // Reuse config loaded earlier for GUI settings
    let gui_config = &config.gui;
    let default_width = 800.0;
    let default_height = 600.0;
    let window_width = gui_config.window_width.unwrap_or(default_width);
    let window_height = gui_config.window_height.unwrap_or(default_height);

    // Create system tray icon (must be on main thread before event loop)
    // We need to create a temporary tray state first
    let tray_state_temp = Arc::new(Mutex::new(TrayState {
        window_visible: true, // Will be updated below
        ..Default::default()
    }));

    let tray_manager = match TrayManager::new(tray_state_temp.clone()) {
        Ok(manager) => Some(manager),
        Err(e) => {
            warn!(
                "Failed to create system tray: {}. Continuing without tray.",
                e
            );
            None
        }
    };

    // Check if we should start minimized (requires tray to be available)
    let start_minimized = gui_config.start_minimized && tray_manager.is_some();
    if start_minimized {
        info!("Starting minimized to system tray");
        // Update tray state to reflect hidden window
        if let Ok(mut state) = tray_state_temp.lock() {
            state.window_visible = false;
        }
        // Hide the dock icon on macOS
        hide_dock_icon();
    }

    // Use the properly initialized tray state
    let tray_state = tray_state_temp;

    // Build viewport with icon, saved size, and close-to-tray behavior
    let mut viewport = egui::ViewportBuilder::default()
        .with_inner_size([window_width, window_height])
        .with_min_inner_size([600.0, 400.0])
        .with_close_button(true)
        .with_visible(!start_minimized); // Start hidden if start_minimized is enabled

    // Restore window position if saved
    if let (Some(x), Some(y)) = (gui_config.window_x, gui_config.window_y) {
        // Validate the position is reasonable
        if x >= -500.0 && y >= -500.0 && x < 5000.0 && y < 5000.0 {
            debug!("Restoring window position: ({}, {})", x, y);
            viewport = viewport.with_position([x, y]);
        }
    }

    if let Some(icon) = load_icon() {
        viewport = viewport.with_icon(icon);
    }

    let native_options = eframe::NativeOptions {
        viewport,
        ..Default::default()
    };

    eframe::run_native(
        "Aranet",
        native_options,
        Box::new(move |cc| {
            // Set the egui context for tray event handling.
            // This allows tray events to wake up the event loop when the window is hidden.
            set_egui_context(cc.egui_ctx.clone());

            // Create app first without menu
            let mut app = AranetApp::new(cc, command_tx, std_rx, tray_state, tray_manager, None);

            // Create native menu bar AFTER eframe has initialized NSApp (required for macOS)
            let menu_manager = match MenuManager::new() {
                Ok(manager) => {
                    // Initialize for macOS - now safe because NSApp is ready
                    manager.init_for_macos();
                    Some(manager)
                }
                Err(e) => {
                    warn!(
                        "Failed to create native menu: {}. Continuing without menu.",
                        e
                    );
                    None
                }
            };

            // Set the menu manager on the app
            app.set_menu_manager(menu_manager);

            Ok(Box::new(app))
        }),
    )
    .map_err(|e| anyhow::anyhow!("Failed to run eframe: {}", e))?;

    Ok(())
}

/// Run the GUI application with custom options.
///
/// This allows running in demo mode with mock data for screenshots.
pub fn run_with_options(options: GuiOptions) -> Result<()> {
    tracing_subscriber::fmt::init();

    if options.demo {
        info!("Running in demo mode with mock data");
    }

    // Load config to get service URL and GUI settings
    let config = Config::load_or_default()?;
    let service_url = config.gui.service_url.clone();
    let service_api_key = config.gui.service_api_key.clone();

    // Get store path (shared database location) - not used in demo mode
    let store_path = default_db_path();
    if !options.demo {
        info!("Using database at: {:?}", store_path);
    }

    // Create tokio runtime in a separate thread
    let (command_tx, command_rx) = mpsc::channel::<Command>(32);
    let (event_tx, event_rx_tokio) = mpsc::channel::<SensorEvent>(32);

    // Bridge from tokio mpsc to std mpsc for sync access in egui
    let (std_tx, std_rx) = std_mpsc::channel::<SensorEvent>();

    // Clone command_tx for sending initial load command
    let startup_command_tx = command_tx.clone();
    let is_demo = options.demo;

    // Spawn tokio runtime thread
    std::thread::spawn(move || {
        let rt = match tokio::runtime::Runtime::new() {
            Ok(rt) => rt,
            Err(e) => {
                tracing::error!("Failed to create tokio runtime: {}", e);
                return;
            }
        };
        rt.block_on(async {
            let worker = SensorWorker::with_service_config(
                command_rx,
                event_tx,
                store_path,
                &service_url,
                service_api_key,
            );

            // Send startup commands (skip in demo mode)
            if !is_demo {
                if let Err(e) = startup_command_tx.send(Command::LoadCachedData).await {
                    tracing::error!("Failed to send LoadCachedData command at startup: {}", e);
                }
                if let Err(e) = startup_command_tx.send(Command::RefreshServiceStatus).await {
                    tracing::warn!(
                        "Failed to send RefreshServiceStatus command at startup: {}",
                        e
                    );
                }
            }

            // Forward events from worker to std channel
            let mut event_rx = event_rx_tokio;
            let forward_handle = tokio::spawn(async move {
                while let Some(event) = event_rx.recv().await {
                    if std_tx.send(event).is_err() {
                        break; // GUI closed
                    }
                }
            });

            // Run the worker
            worker.run().await;
            forward_handle.abort();
        });
    });
    let gui_config = &config.gui;
    let default_width = 800.0;
    let default_height = 600.0;
    // Use 900x600 for screenshots to match VHS tape dimensions
    let screenshot_width = 900.0;
    let screenshot_height = 600.0;
    let (window_width, window_height) = if options.demo {
        (screenshot_width, screenshot_height)
    } else {
        (
            gui_config.window_width.unwrap_or(default_width),
            gui_config.window_height.unwrap_or(default_height),
        )
    };

    // Create shared tray state
    let tray_state_temp = Arc::new(Mutex::new(TrayState {
        window_visible: true, // Will be updated below if start_minimized
        ..Default::default()
    }));

    // Create system tray icon (must be on main thread before event loop)
    // Skip tray in demo mode for cleaner screenshots
    let tray_manager = if options.demo {
        None
    } else {
        match TrayManager::new(tray_state_temp.clone()) {
            Ok(manager) => Some(manager),
            Err(e) => {
                warn!(
                    "Failed to create system tray: {}. Continuing without tray.",
                    e
                );
                None
            }
        }
    };

    // Check if we should start minimized (requires tray and not in demo mode)
    let start_minimized = !options.demo && gui_config.start_minimized && tray_manager.is_some();
    if start_minimized {
        info!("Starting minimized to system tray");
        // Update tray state to reflect hidden window
        if let Ok(mut state) = tray_state_temp.lock() {
            state.window_visible = false;
        }
        // Hide the dock icon on macOS
        hide_dock_icon();
    }

    // Use the properly initialized tray state
    let tray_state = tray_state_temp;

    // Build viewport with icon, saved size, and close-to-tray behavior
    let mut viewport = egui::ViewportBuilder::default()
        .with_inner_size([window_width, window_height])
        .with_min_inner_size([600.0, 400.0])
        .with_close_button(true)
        .with_visible(!start_minimized); // Start hidden if start_minimized is enabled

    // Restore window position if saved (skip in demo mode)
    if !options.demo
        && let (Some(x), Some(y)) = (gui_config.window_x, gui_config.window_y)
        && x >= -500.0
        && y >= -500.0
        && x < 5000.0
        && y < 5000.0
    {
        debug!("Restoring window position: ({}, {})", x, y);
        viewport = viewport.with_position([x, y]);
    }

    if let Some(icon) = load_icon() {
        viewport = viewport.with_icon(icon);
    }

    let native_options = eframe::NativeOptions {
        viewport,
        ..Default::default()
    };

    let screenshot_path = options.screenshot.clone();
    let screenshot_delay = options.screenshot_delay_frames;
    let demo_mode = options.demo;

    eframe::run_native(
        "Aranet",
        native_options,
        Box::new(move |cc| {
            // Set the egui context for tray event handling.
            // This allows tray events to wake up the event loop when the window is hidden.
            set_egui_context(cc.egui_ctx.clone());

            // Create app first without menu
            let mut app = AranetApp::new_with_options(
                cc,
                command_tx,
                std_rx,
                tray_state,
                tray_manager,
                None,
                demo_mode,
                screenshot_path,
                screenshot_delay,
            );

            // Create native menu bar AFTER eframe has initialized NSApp (required for macOS)
            // Skip menu in demo mode for cleaner screenshots
            let menu_manager = if demo_mode {
                None
            } else {
                match MenuManager::new() {
                    Ok(manager) => {
                        // Initialize for macOS - now safe because NSApp is ready
                        manager.init_for_macos();
                        Some(manager)
                    }
                    Err(e) => {
                        warn!(
                            "Failed to create native menu: {}. Continuing without menu.",
                            e
                        );
                        None
                    }
                }
            };

            // Set the menu manager on the app
            app.set_menu_manager(menu_manager);

            Ok(Box::new(app))
        }),
    )
    .map_err(|e| anyhow::anyhow!("Failed to run eframe: {}", e))?;

    Ok(())
}