hippox-drivers 0.3.5

🦛All indivisible atomic driver units in Hippox.
//! WiFi analyze quality skill - analyze WiFi quality and recommend channel
use super::common::{get_wifi_status, scan_wifi_networks};
use crate::{
    DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
    types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
/// Driver for analyzing WiFi quality and recommending optimal channel
#[derive(Debug)]
pub struct WifiAnalyzeQualityDriver;
#[async_trait::async_trait]
impl Driver for WifiAnalyzeQualityDriver {
    fn name(&self) -> &str {
        "wifi_analyze_quality"
    }
    fn description(&self) -> &str {
        "Analyze WiFi quality and recommend the best channel to use"
    }
    fn usage_hint(&self) -> &str {
        "Use this skill to diagnose WiFi interference and get recommendations for improving connection quality."
    }
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![];
    }
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "wifi_analyze_quality"
        }));
    }
    fn example_output(&self) -> String {
        return "Quality Analysis:\n- Score: 75/100\n- Current Channel: 6\n- Recommended Channel: 11\n- Recommendations: Switch to channel 11 to avoid interference from 3 nearby networks".to_string();
    }
    fn category(&self) -> DriverCategory {
        return DriverCategory::Wifi;
    }
    async fn execute(
        &self,
        _parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing wifi_analyze_quality driver");
        // Get current WiFi status
        let status = get_wifi_status().map_err(|e| {
            debug!("Failed to get WiFi status: {}", e);
            return DriverError::execution(format!("Failed to get WiFi status: {}", e));
        })?;
        // Scan for nearby networks
        let networks = scan_wifi_networks().map_err(|e| {
            debug!("Failed to scan WiFi networks: {}", e);
            return DriverError::execution(format!("Failed to scan WiFi networks: {}", e));
        })?;
        let current_channel = status.channel.unwrap_or(6);
        // Count networks per channel
        let mut channel_counts: HashMap<u32, u32> = HashMap::new();
        for network in &networks {
            if let Some(channel) = network.channel {
                *channel_counts.entry(channel).or_insert(0) += 1;
            }
        }
        // Find best channel (least congested)
        let mut best_channel = current_channel;
        let mut min_count = u32::MAX;
        for channel in [1, 6, 11, 3, 8, 2, 4, 5, 7, 9, 10] {
            let count = *channel_counts.entry(channel).or_insert(0);
            if count < min_count {
                min_count = count;
                best_channel = channel;
            }
        }
        let signal = status.signal_strength.unwrap_or(0);
        let score = if signal > 80 {
            90
        } else if signal > 60 {
            70
        } else if signal > 40 {
            50
        } else {
            30
        };
        let mut recommendations = Vec::new();
        if best_channel != current_channel {
            recommendations.push(format!("Switch to channel {} to avoid interference from {} nearby networks", best_channel, min_count));
        }
        if signal < 50 {
            recommendations.push("Move closer to the router for better signal strength".to_string());
        }
        let mut result = format!(
            "Quality Analysis:\n- Score: {}/100\n- Current Channel: {}\n- Recommended Channel: {}\n- Interfering Networks: {}\n",
            score, current_channel, best_channel, min_count
        );
        if !recommendations.is_empty() {
            result.push_str("- Recommendations:\n");
            for rec in recommendations {
                result.push_str(&format!("  * {}\n", rec));
            }
        }
        info!("WiFi quality analysis completed, score: {}/100", score);
        return Ok(result);
    }
}