pub struct DeviceDiscovery { /* private fields */ }
Expand description
Allows discovery of VBus-over-TCP devices in a local network.
All VBus-over-TCP devices listen for UDPv4 broadcast messages on port 7053. If such a message with the correct payload is received, the device sends a unicast message back to the sender of the broadcast to identify itself.
The DeviceDiscovery
type allows to send such broadcasts and collect all
associated replies.
Implementations§
Source§impl DeviceDiscovery
impl DeviceDiscovery
Sourcepub fn new() -> DeviceDiscovery
pub fn new() -> DeviceDiscovery
Create a new DeviceDiscovery
instance using default values.
§Examples
use async_resol_vbus::DeviceDiscovery;
let discovery = DeviceDiscovery::new();
let addresses = discovery.discover_device_addresses().await?;
Examples found in repository?
13fn main() -> Result<()> {
14 async_std::task::block_on(async {
15 let mut discovery = DeviceDiscovery::new();
16 // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17 discovery.set_fetch_port(3000);
18
19 let mut known_devices = Vec::<DeviceInformation>::new();
20 loop {
21 println!("---- Discovering... ----");
22
23 let mut found_devices = discovery.discover_devices().await?;
24 found_devices.sort_by(|l, r| compare_devices(l, r));
25
26 known_devices.sort_by(|l, r| compare_devices(l, r));
27
28 for found_device in found_devices.iter() {
29 let is_known_device = known_devices
30 .iter()
31 .any(|known_device| known_device.address == found_device.address);
32
33 if !is_known_device {
34 let name = match &found_device.name {
35 Some(name) => name.as_str(),
36 None => "???",
37 };
38
39 println!("FOUND: {} {}", found_device.address, name);
40 known_devices.push(found_device.clone());
41 }
42 }
43
44 let mut lost_device_addresses = Vec::new();
45
46 for known_device in known_devices.iter() {
47 let is_found_device = found_devices
48 .iter()
49 .any(|found_device| known_device.address == found_device.address);
50
51 if !is_found_device {
52 let name = match &known_device.name {
53 Some(name) => name.as_str(),
54 None => "???",
55 };
56
57 println!("LOST: {} {}", known_device.address, name);
58 lost_device_addresses.push(known_device.address);
59 }
60 }
61
62 for lost_device_address in lost_device_addresses {
63 let pos = known_devices
64 .iter()
65 .position(|known_device| known_device.address == lost_device_address);
66
67 if let Some(idx) = pos {
68 known_devices.remove(idx);
69 }
70 }
71
72 async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73 }
74 })
75}
Sourcepub fn set_broadcast_addr(&mut self, addr: SocketAddr)
pub fn set_broadcast_addr(&mut self, addr: SocketAddr)
Set the broadcast address.
Sourcepub fn set_rounds(&mut self, rounds: u8)
pub fn set_rounds(&mut self, rounds: u8)
Set the number of discovery rounds.
Sourcepub fn set_broadcast_timeout(&mut self, timeout: Duration)
pub fn set_broadcast_timeout(&mut self, timeout: Duration)
Set the timeout used to wait for replies after each round’s broadcast.
Sourcepub fn set_fetch_port(&mut self, port: u16)
pub fn set_fetch_port(&mut self, port: u16)
Set the port number used for fetching the device information.
Examples found in repository?
13fn main() -> Result<()> {
14 async_std::task::block_on(async {
15 let mut discovery = DeviceDiscovery::new();
16 // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17 discovery.set_fetch_port(3000);
18
19 let mut known_devices = Vec::<DeviceInformation>::new();
20 loop {
21 println!("---- Discovering... ----");
22
23 let mut found_devices = discovery.discover_devices().await?;
24 found_devices.sort_by(|l, r| compare_devices(l, r));
25
26 known_devices.sort_by(|l, r| compare_devices(l, r));
27
28 for found_device in found_devices.iter() {
29 let is_known_device = known_devices
30 .iter()
31 .any(|known_device| known_device.address == found_device.address);
32
33 if !is_known_device {
34 let name = match &found_device.name {
35 Some(name) => name.as_str(),
36 None => "???",
37 };
38
39 println!("FOUND: {} {}", found_device.address, name);
40 known_devices.push(found_device.clone());
41 }
42 }
43
44 let mut lost_device_addresses = Vec::new();
45
46 for known_device in known_devices.iter() {
47 let is_found_device = found_devices
48 .iter()
49 .any(|found_device| known_device.address == found_device.address);
50
51 if !is_found_device {
52 let name = match &known_device.name {
53 Some(name) => name.as_str(),
54 None => "???",
55 };
56
57 println!("LOST: {} {}", known_device.address, name);
58 lost_device_addresses.push(known_device.address);
59 }
60 }
61
62 for lost_device_address in lost_device_addresses {
63 let pos = known_devices
64 .iter()
65 .position(|known_device| known_device.address == lost_device_address);
66
67 if let Some(idx) = pos {
68 known_devices.remove(idx);
69 }
70 }
71
72 async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73 }
74 })
75}
Sourcepub fn set_fetch_timeout(&mut self, timeout: Duration)
pub fn set_fetch_timeout(&mut self, timeout: Duration)
Set the timeout used for fetching the device information.
Sourcepub async fn discover_devices(&self) -> Result<Vec<DeviceInformation>>
pub async fn discover_devices(&self) -> Result<Vec<DeviceInformation>>
Discover all VBus-over-TCP devices and return their device information.
§Examples
use async_resol_vbus::DeviceDiscovery;
let discovery = DeviceDiscovery::new();
let devices = discovery.discover_devices().await?;
Examples found in repository?
13fn main() -> Result<()> {
14 async_std::task::block_on(async {
15 let mut discovery = DeviceDiscovery::new();
16 // discovery.set_broadcast_addr("192.168.15.255:7053".parse().unwrap());
17 discovery.set_fetch_port(3000);
18
19 let mut known_devices = Vec::<DeviceInformation>::new();
20 loop {
21 println!("---- Discovering... ----");
22
23 let mut found_devices = discovery.discover_devices().await?;
24 found_devices.sort_by(|l, r| compare_devices(l, r));
25
26 known_devices.sort_by(|l, r| compare_devices(l, r));
27
28 for found_device in found_devices.iter() {
29 let is_known_device = known_devices
30 .iter()
31 .any(|known_device| known_device.address == found_device.address);
32
33 if !is_known_device {
34 let name = match &found_device.name {
35 Some(name) => name.as_str(),
36 None => "???",
37 };
38
39 println!("FOUND: {} {}", found_device.address, name);
40 known_devices.push(found_device.clone());
41 }
42 }
43
44 let mut lost_device_addresses = Vec::new();
45
46 for known_device in known_devices.iter() {
47 let is_found_device = found_devices
48 .iter()
49 .any(|found_device| known_device.address == found_device.address);
50
51 if !is_found_device {
52 let name = match &known_device.name {
53 Some(name) => name.as_str(),
54 None => "???",
55 };
56
57 println!("LOST: {} {}", known_device.address, name);
58 lost_device_addresses.push(known_device.address);
59 }
60 }
61
62 for lost_device_address in lost_device_addresses {
63 let pos = known_devices
64 .iter()
65 .position(|known_device| known_device.address == lost_device_address);
66
67 if let Some(idx) = pos {
68 known_devices.remove(idx);
69 }
70 }
71
72 async_std::task::sleep(std::time::Duration::from_secs(10)).await;
73 }
74 })
75}
Sourcepub async fn discover_device_addresses(&self) -> Result<Vec<SocketAddr>>
pub async fn discover_device_addresses(&self) -> Result<Vec<SocketAddr>>
Discover all VBus-over-TCP devices and return their addresses.
§Examples
use async_resol_vbus::DeviceDiscovery;
let discovery = DeviceDiscovery::new();
let addresses = discovery.discover_device_addresses().await?;