Converted more of the codebase to async

This commit is contained in:
2023-01-10 00:37:13 +01:00
parent 5b9d24e82f
commit 220c68cd65
13 changed files with 101 additions and 67 deletions

View File

@@ -15,6 +15,7 @@ use automation::{
use dotenvy::dotenv;
use rumqttc::{AsyncClient, MqttOptions, Transport};
use tracing::{debug, error, info, metadata::LevelFilter};
use futures::future::join_all;
use google_home::{GoogleHome, Request};
use tracing_subscriber::EnvFilter;
@@ -58,23 +59,22 @@ async fn main() {
// Create a mqtt client and wrap the eventloop
let (client, eventloop) = AsyncClient::new(mqttoptions, 10);
let mqtt = mqtt::start(eventloop);
let presence = presence::start(mqtt.clone(), config.presence.clone(), client.clone());
let light_sensor =
light_sensor::start(mqtt.clone(), config.light_sensor.clone(), client.clone());
let presence = presence::start(mqtt.clone(), config.presence.clone(), client.clone()).await;
let light_sensor = light_sensor::start(mqtt.clone(), config.light_sensor.clone(), client.clone()).await;
let devices = devices::start(mqtt, presence.clone(), light_sensor.clone());
config
.devices
.clone()
.into_iter()
.map(|(identifier, device_config)| {
// This can technically block, but this only happens during start-up, so should not be
// a problem
device_config.into(identifier, &config, client.clone())
})
.for_each(|device| {
devices.add_device(device);
});
join_all(
config
.devices
.clone()
.into_iter()
.map(|(identifier, device_config)| async {
// This can technically block, but this only happens during start-up, so should not be
// a problem
let device = device_config.into(identifier, &config, client.clone()).await;
devices.add_device(device).await;
})
).await;
// Start the ntfy service if it is configured
if let Some(ntfy_config) = config.ntfy {