Skip to main content

wifi_densepose_wasm/
lib.rs

1//! WiFi-DensePose WebAssembly bindings
2//!
3//! This crate provides WebAssembly bindings for browser-based applications using
4//! WiFi-DensePose technology. It includes:
5//!
6//! - **mat**: WiFi-Mat disaster response dashboard module for browser integration
7//!
8//! # Features
9//!
10//! - `mat` - Enable WiFi-Mat disaster detection WASM bindings
11//! - `console_error_panic_hook` - Better panic messages in browser console
12//!
13//! # Building for WASM
14//!
15//! ```bash
16//! # Build with wasm-pack
17//! wasm-pack build --target web --features mat
18//!
19//! # Or with cargo
20//! cargo build --target wasm32-unknown-unknown --features mat
21//! ```
22//!
23//! # Example Usage (JavaScript)
24//!
25//! ```javascript
26//! import init, { MatDashboard, initLogging } from './wifi_densepose_wasm.js';
27//!
28//! async function main() {
29//!     await init();
30//!     initLogging('info');
31//!
32//!     const dashboard = new MatDashboard();
33//!
34//!     // Create a disaster event
35//!     const eventId = dashboard.createEvent('earthquake', 37.7749, -122.4194, 'Bay Area Earthquake');
36//!
37//!     // Add scan zones
38//!     dashboard.addRectangleZone('Building A', 50, 50, 200, 150);
39//!     dashboard.addCircleZone('Search Area B', 400, 200, 80);
40//!
41//!     // Subscribe to events
42//!     dashboard.onSurvivorDetected((survivor) => {
43//!         console.log('Survivor detected:', survivor);
44//!         updateUI(survivor);
45//!     });
46//!
47//!     dashboard.onAlertGenerated((alert) => {
48//!         showNotification(alert);
49//!     });
50//!
51//!     // Render to canvas
52//!     const canvas = document.getElementById('map');
53//!     const ctx = canvas.getContext('2d');
54//!
55//!     function render() {
56//!         ctx.clearRect(0, 0, canvas.width, canvas.height);
57//!         dashboard.renderZones(ctx);
58//!         dashboard.renderSurvivors(ctx);
59//!         requestAnimationFrame(render);
60//!     }
61//!     render();
62//! }
63//!
64//! main();
65//! ```
66
67use wasm_bindgen::prelude::*;
68
69// WiFi-Mat module for disaster response dashboard
70pub mod mat;
71pub use mat::*;
72
73/// Initialize the WASM module.
74/// Call this once at startup before using any other functions.
75#[wasm_bindgen(start)]
76pub fn init() {
77    // Set panic hook for better error messages in browser console
78    #[cfg(feature = "console_error_panic_hook")]
79    console_error_panic_hook::set_once();
80}
81
82/// Initialize logging with specified level.
83///
84/// @param {string} level - Log level: "trace", "debug", "info", "warn", "error"
85#[wasm_bindgen(js_name = initLogging)]
86pub fn init_logging(level: &str) {
87    let log_level = match level.to_lowercase().as_str() {
88        "trace" => log::Level::Trace,
89        "debug" => log::Level::Debug,
90        "info" => log::Level::Info,
91        "warn" => log::Level::Warn,
92        "error" => log::Level::Error,
93        _ => log::Level::Info,
94    };
95
96    let _ = wasm_logger::init(wasm_logger::Config::new(log_level));
97    log::info!("WiFi-DensePose WASM initialized with log level: {}", level);
98}
99
100/// Get the library version.
101///
102/// @returns {string} Version string
103#[wasm_bindgen(js_name = getVersion)]
104pub fn get_version() -> String {
105    env!("CARGO_PKG_VERSION").to_string()
106}
107
108/// Check if the MAT feature is enabled.
109///
110/// @returns {boolean} True if MAT module is available
111#[wasm_bindgen(js_name = isMatEnabled)]
112pub fn is_mat_enabled() -> bool {
113    true
114}
115
116/// Get current timestamp in milliseconds (for performance measurements).
117///
118/// @returns {number} Timestamp in milliseconds
119#[wasm_bindgen(js_name = getTimestamp)]
120pub fn get_timestamp() -> f64 {
121    let window = web_sys::window().expect("no global window");
122    let performance = window.performance().expect("no performance object");
123    performance.now()
124}
125
126// Re-export all public types from mat module for easy access
127pub mod types {
128    pub use super::mat::{
129        JsAlert, JsAlertPriority, JsDashboardStats, JsDisasterType, JsScanZone, JsSurvivor,
130        JsTriageStatus, JsZoneStatus,
131    };
132}