mod json_serial;
use crate::config;
use crate::world_view::{self, WorldView};
use crate::print;
use std::collections::HashMap;
use tokio::sync::{mpsc, watch};
use tokio::time::sleep;
pub async fn start_manager(
wv_watch_rx: watch::Receiver<WorldView>,
delegated_tasks_tx: mpsc::Sender<HashMap<u8, Vec<[bool; 2]>>>
)
{
let mut wv = world_view::get_wv(wv_watch_rx.clone());
loop
{
if world_view::update_wv(wv_watch_rx.clone(), &mut wv).await
{
if world_view::is_master(&wv)
{
let _ = delegated_tasks_tx.send(get_elev_tasks(&wv).await).await;
} else
{
sleep(config::SLAVE_TIMEOUT).await;
}
}
sleep(config::POLL_PERIOD).await;
}
}
async fn get_elev_tasks(
wv: &WorldView
) -> HashMap<u8, Vec<[bool; 2]>>
{
let json_str = json_serial::create_hall_request_json(wv).await;
if let Some(str) = json_str
{
let json_cost_str = json_serial::run_cost_algorithm(str.clone()).await;
if json_cost_str.trim().is_empty()
{
print::err(format!(
"run_cost_algorithm returned an empty string"
));
return HashMap::new();
}
return serde_json::from_str(&json_cost_str).unwrap_or_else(|e| {
print::err(format!("Failed to parse JSON from cost algorithm: {}", e));
HashMap::new()
});
}
print::err("create_hall_request_json returned None.".to_string());
HashMap::new()
}