Skip to content

Commit

Permalink
add more food logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrejb committed Feb 29, 2024
1 parent 6576a81 commit b611a20
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/food/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub async fn food_worker_loop(window: Weak<MainWindow>, rx: Receiver<Url>) {
loop {
match rx.try_recv() {
Ok(tracking_url) => {
//TODO: Use logging instead of println
info!("Got new tracking url: {}", tracking_url);
current_url = Some(tracking_url);
}
Expand All @@ -24,31 +23,37 @@ pub async fn food_worker_loop(window: Weak<MainWindow>, rx: Receiver<Url>) {
}

if let Some(url) = &current_url {
info!("Fetching tracking data for url: {}", url);
let tracking_response = get_tracking_data(&url).await;
let tracking_status = get_tracking_status(tracking_response);

if !tracking_status.active {
info!("Tracking status is not active. Resetting current url.");
current_url = None;
}

current_tracking = tracking_status;
}

info!("Displaying tracking information: {:?}", current_tracking);
display_tracking(&window, current_tracking.clone());
//TODO: Adjust timeout, maybe dynamic based on refresh_in_seconds
info!("Sleeping for 5 seconds");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}

async fn get_tracking_data(url: &Url) -> Result<WoltTracking, reqwest::Error> {
info!("Sending GET request to: {}", url);
let response = reqwest::get(url.clone()).await?;
let wolt_tracking_data = response.json::<WoltTracking>().await?;
info!("Received tracking data: {:?}", wolt_tracking_data);
Ok(wolt_tracking_data)
}

fn get_tracking_status(tracking_data: Result<WoltTracking, reqwest::Error>) -> FoodTracking {
match tracking_data {
Ok(tracking_data) => {
info!("Processing tracking data: {:?}", tracking_data);
let minutes_remaining: SharedString = match tracking_data.delivery_eta {
Some(eta) => ((eta) - Local::now()).num_minutes().to_string().into(),
None => "ukjent antall".into(),
Expand All @@ -70,6 +75,7 @@ fn get_tracking_status(tracking_data: Result<WoltTracking, reqwest::Error>) -> F
}

fn display_tracking(window_weak: &Weak<MainWindow>, food_tracking: FoodTracking) {
info!("Updating UI with tracking information: {:?}", food_tracking);
window_weak
.upgrade_in_event_loop(move |window: MainWindow| window.set_foodTracking(food_tracking))
.unwrap();
Expand Down

0 comments on commit b611a20

Please sign in to comment.