Fix tibber handler

This commit is contained in:
hodasemi 2023-10-16 14:19:36 +02:00
parent 38cc9facc6
commit 51bfe0ab4b

View file

@ -65,63 +65,36 @@ impl TibberHandler {
} }
pub async fn current_prices(&self) -> Result<Vec<(House, PriceInfo)>> { pub async fn current_prices(&self) -> Result<Vec<(House, PriceInfo)>> {
let mut v = Vec::new(); self.get_data(|session, home_id| {
session.get_current_price(home_id).map_err(|err| {
for (home_id, house) in self.homes.iter() {
v.push((
house.clone(),
tokio::task::spawn_blocking(move || {
self.session.get_current_price(home_id).map_err(|err| {
Error::msg(format!( Error::msg(format!(
"TibberHandler: failed getting current price: {err:?}" "TibberHandler: failed getting current price: {err:?}"
)) ))
}) })
}) })
.await?, .await
));
}
Ok(v)
} }
pub async fn prices_today(&self) -> Result<Vec<(House, Vec<PriceInfo>)>> { pub async fn prices_today(&self) -> Result<Vec<(House, Vec<PriceInfo>)>> {
let mut v = Vec::new(); self.get_data(|session, home_id| {
session.get_prices_today(home_id).map_err(|err| {
for (home_id, house) in self.homes.iter() {
v.push((
house.clone(),
tokio::task::spawn_blocking(move || {
self.session.get_prices_today(home_id).map_err(|err| {
Error::msg(format!( Error::msg(format!(
"TibberHandler: failed getting prices of today: {err:?}" "TibberHandler: failed getting prices of today: {err:?}"
)) ))
}) })
}) })
.await?, .await
));
}
Ok(v)
} }
pub async fn prices_tomorrow(&self) -> Result<Vec<(House, Vec<PriceInfo>)>> { pub async fn prices_tomorrow(&self) -> Result<Vec<(House, Vec<PriceInfo>)>> {
let mut v = Vec::new(); self.get_data(|session, home_id| {
session.get_prices_tomorrow(home_id).map_err(|err| {
for (home_id, house) in self.homes.iter() {
v.push((
house.clone(),
tokio::task::spawn_blocking(move || {
self.session.get_prices_tomorrow(home_id).map_err(|err| {
Error::msg(format!( Error::msg(format!(
"TibberHandler: failed getting prices for tomorrow: {err:?}" "TibberHandler: failed getting prices for tomorrow: {err:?}"
)) ))
}) })
}) })
.await?, .await
));
}
Ok(v)
} }
pub async fn consumption( pub async fn consumption(
@ -166,7 +139,10 @@ mod test {
#[tokio::test] #[tokio::test]
async fn test_connection() -> Result<()> { async fn test_connection() -> Result<()> {
TibberHandler::new(fs::read_to_string("tibber_token.txt")?).await?; let tibber = TibberHandler::new(fs::read_to_string("tibber_token.txt")?).await?;
let current_prices = tibber.current_prices().await?;
println!("{current_prices:?}");
Ok(()) Ok(())
} }