Skip to content

Commit

Permalink
Semi-working mock sender using configurable address:port for the udp …
Browse files Browse the repository at this point in the history
…listener
  • Loading branch information
rorynugent committed May 29, 2024
1 parent 16c16ba commit 708dfe3
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,15 @@ impl Tempest {
/// Returns a Tokio receiver containing a weather event as an `EventType`.
/// The `Tempest` instance is disregarded in this use case.
pub async fn listen_udp() -> Receiver<EventType> {
let (_, rx) = Tempest::listen_udp_internal(false, None).await;
let (_, rx) = Tempest::listen_udp_internal(None, None, false, None).await;
rx
}

/// Listen to UDP packets sent from the WeatherFlow Tempest hub and cache data about hubs and stations reporting events
///
/// Returns a `Tempest` instance along with a Tokio receiver containining a weather event as an `EventType`
pub async fn listen_udp_with_cache() -> (Tempest, Receiver<EventType>) {
Tempest::listen_udp_internal(true, None).await
Tempest::listen_udp_internal(None, None, true, None).await
}

/// Listen to UDP packets sent from the WeatherFlow Tempest hub and only share events that match the provided serial number.
Expand All @@ -549,7 +549,7 @@ impl Tempest {
.map(|&station| station.to_string())
.collect();

let (_, rx) = Tempest::listen_udp_internal(false, Some(station_filter)).await;
let (_, rx) = Tempest::listen_udp_internal(None, None, false, Some(station_filter)).await;
rx
}

Expand All @@ -566,10 +566,12 @@ impl Tempest {
/// This function returns both an instance of `Tempest` for further weather data retrieval (air temperature, wind, etc)
/// and `rx` is an mpsc receiver for accepting weather event data as it arrives.
async fn listen_udp_internal(
address: Option<Ipv4Addr>,
port: Option<u64>,
caching: bool,
station_filter: Option<Vec<String>>,
) -> (Tempest, Receiver<EventType>) {
let mut tempest = Tempest::bind(None, None).await;
let mut tempest = Tempest::bind(address, port).await;
let (tx, rx) = mpsc::channel(16);

let tempest_clone: Tempest = tempest.clone();
Expand Down Expand Up @@ -798,6 +800,8 @@ impl Tempest {

#[cfg(test)]
mod test {
use serde_json::json;

use super::*;
use std::net::UdpSocket;

Expand All @@ -806,31 +810,45 @@ mod test {
}

impl MockSender {
fn bind() -> Self{
fn bind() -> Self {
let socket = UdpSocket::bind("127.0.0.1:0").expect("Unable to bind to address");

MockSender {
socket,
}
MockSender { socket }
}

fn send(&self) {
self.socket.send_to(&[0; 10], format!("127.0.0.1:{DEFAULT_PORT}")).expect("couldn't send data");
fn send(&self, buffer: Vec<u8>) {
self.socket
.send_to(&buffer, "127.0.0.1:50223")
.expect("couldn't send data");
}
}

#[tokio::test]
async fn hub_count() {
let mock = MockSender::bind();

let mut receiver = Tempest::listen_udp().await;
let (_, mut receiver) = Tempest::listen_udp_internal(
Some(Ipv4Addr::new(127, 0, 0, 1)),
Some(50223),
false,
None,
)
.await;

let payload = json!({
"serial_number": "SK-00008453",
"type":"rapid_wind",
"hub_sn": "HB-00000001",
"ob": [1493322445, 2.3, 128]
});
let payload_vec = serde_json::to_vec(&payload).expect("Failured to convert JSON to vector");

mock.send();
mock.send(payload_vec);

let data = receiver.recv().await;

println!("Data: {data:?}");

assert_eq!(0,0);
assert_eq!(0, 0);
}
}
}

0 comments on commit 708dfe3

Please sign in to comment.