pub fn discover() -> Result<HashMap<IpAddr, DeviceKind>> {
    let query = json!({
        "system": {"get_sysinfo": {}},
        "emeter": {"get_realtime": {}},
        "smartlife.iot.dimmer": {"get_dimmer_parameters": {}},
        "smartlife.iot.common.emeter": {"get_realtime": {}},
        "smartlife.iot.smartbulb.lightingservice": {"get_light_state": {}},
    });
    let request = serde_json::to_vec(&query).unwrap();
    let proto = proto::Builder::new(([255, 255, 255, 255], 9999))
        .broadcast(true)
        .read_timeout(Duration::from_secs(3))
        .write_timeout(Duration::from_secs(3))
        .tolerance(3)
        .build();
    let responses = proto.discover(&request)?;

    let mut devices = HashMap::new();
    for (ip, response) in responses {
        let value = serde_json::from_slice::<Value>(&response).unwrap();
        let device = device_from(ip, &value)?;
        devices.entry(ip).or_insert(device);
    }

    Ok(devices)
}
/// Discover existing TP-Link Smart Home devices on the network.
///
/// # Examples
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     for (ip, device) in tplink::discover()? {
///         match device {
///             tplink::DeviceKind::Plug(mut plug) => {
///                 // .. do something with the plug
///             },
///             tplink::DeviceKind::Bulb(mut bulb) => {
///                 // .. do something with the bulb
///             },
///             _ => println!("unrecognised device on the network: {}", ip),
///         }
///     }
///     Ok(())
/// }
/// ```
Discover existing TP-Link Smart Home devices on the network.
Discover existing TP - Link Smart Home devices on the network .
pub fn discover() -> Result<HashMap<IpAddr, DeviceKind>> {
    let query = json!({
        "system": {"get_sysinfo": {}},
        "emeter": {"get_realtime": {}},
        "smartlife.iot.dimmer": {"get_dimmer_parameters": {}},
        "smartlife.iot.common.emeter": {"get_realtime": {}},
        "smartlife.iot.smartbulb.lightingservice": {"get_light_state": {}},
    });
    let request = serde_json::to_vec(&query).unwrap();
    let proto = proto::Builder::new(([255, 255, 255, 255], 9999))
        .broadcast(true)
        .read_timeout(Duration::from_secs(3))
        .write_timeout(Duration::from_secs(3))
        .tolerance(3)
        .build();
    let responses = proto.discover(&request)?;
    let mut devices = HashMap::new();
    for (ip, response) in responses {
        let value = serde_json::from_slice::<Value>(&response).unwrap();
        let device = device_from(ip, &value)?;
        devices.entry(ip).or_insert(device);
    }
    Ok(devices)
}
pub fn discover ( ) -> Result < HashMap < IpAddr , DeviceKind > > { let query = json ! ( { " " { " " { } } " " { " " { } } " " { " " { } } " " { " " { } } " " { " " { } } } ) ; let request = serde_json :: to_vec ( & query ) . unwrap ( ) ; let proto = proto :: Builder :: new ( ( [ 255 , 255 , 255 , 255 ] , 9999 ) ) . broadcast ( true ) . read_timeout ( Duration :: from_secs ( 3 ) ) . write_timeout ( Duration :: from_secs ( 3 ) ) . tolerance ( 3 ) . build ( ) ; let responses = proto . discover ( & request ) ? ; let mut devices = HashMap :: new ( ) ; for ( ip , response ) in responses { let value = serde_json :: from_slice :: < Value > ( & response ) . unwrap ( ) ; let device = device_from ( ip , & value ) ? ; devices . entry ( ip ) . or_insert ( device ) ; } Ok ( devices ) }
Discover existing TP-Link Smart Home devices on the network.
Discover existing TP - Link Smart Home devices on the network .
examples
```no_run
fn main() -> Result<(), Box> {
for (ip, device) in tplink::discover(). {
match device {
tplink::DeviceKind::Plug(mut plug) => {
do something with the plug
},
tplink::DeviceKind::Bulb(mut bulb) => {
do something with the bulb
},
_ => println!("unrecognised device on the network: {}", ip),
}
}
Ok(())
}
```
` ` ` no_run fn main () - > Result< () Box > { for ( ip device ) in tplink :: discover () . { match device { tplink :: DeviceKind :: Plug ( mut plug ) = > { do something with the plug } tplink :: DeviceKind :: Bulb ( mut bulb ) = > { do something with the bulb } _ = > println! ( " unrecognised device on the network : {} " ip ) } } Ok (( )) } ` ` `