use super::box_handle;
pub use crate::wire::{WifiAccessPoint, WifiConnection, WifiInterface};
pub(crate) mod ops {
use std::time::Duration;
use serde_json::json;
use crate::error::Result;
use crate::wire::{
box_command, unit, value_as, value_list_field, CommandResponse, Op, Timeout,
WifiAccessPoint, WifiConnection, WifiInterface,
};
const PATH: &str = "/wifi/command";
fn parse_interfaces(resp: CommandResponse) -> Result<Vec<WifiInterface>> {
value_list_field(resp, "interfaces")
}
fn parse_access_points(resp: CommandResponse) -> Result<Vec<WifiAccessPoint>> {
value_list_field(resp, "access_points")
}
fn budget() -> Timeout {
Timeout::After(Duration::from_secs(90))
}
pub(crate) fn status() -> Op<Vec<WifiInterface>> {
Op {
req: box_command(PATH, "status", json!({}), budget()),
parse: parse_interfaces,
}
}
pub(crate) fn scan(interface: &str) -> Op<Vec<WifiAccessPoint>> {
Op {
req: box_command(PATH, "scan", json!({ "interface": interface }), budget()),
parse: parse_access_points,
}
}
pub(crate) fn connect(ssid: &str, password: &str) -> Op<WifiConnection> {
Op {
req: box_command(
PATH,
"connect",
json!({ "ssid": ssid, "password": password }),
budget(),
),
parse: value_as::<WifiConnection>,
}
}
pub(crate) fn delete(ssid: &str) -> Op<()> {
Op {
req: box_command(PATH, "delete", json!({ "ssid": ssid }), budget()),
parse: unit,
}
}
}
box_handle! {
sync: Wifi,
async: AsyncWifi,
methods: {
fn status() -> Vec<WifiInterface> = ops::status;
fn scan(interface: &str) -> Vec<WifiAccessPoint> = ops::scan;
fn connect(ssid: &str, password: &str) -> WifiConnection = ops::connect;
fn delete(ssid: &str) -> () = ops::delete;
}
}