From c943e78cc6b918392c9c368bcfd4850208ead65f Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Thu, 17 Sep 2020 14:32:53 +0200 Subject: [PATCH] Move modules back to threadpool --- src/main.rs | 2 +- src/modules.rs | 6 +++--- src/modules/shelly.rs | 20 +++++++------------- src/registries/actuators.rs | 4 +--- src/registries/sensors.rs | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 25bc642..2cd09e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,7 +229,7 @@ fn main() -> Result<(), Error> { // without warning. let modlog = log.clone(); let regs = Registries::new(); - match modules::init(modlog.new(o!("system" => "modules")), &config, &local_spawn, regs) { + match modules::init(modlog.new(o!("system" => "modules")), &config, &pool, regs) { Ok(()) => {} Err(e) => { error!(modlog, "Module startup failed: {}", e); diff --git a/src/modules.rs b/src/modules.rs index 8faaf35..8bb06bb 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -10,16 +10,16 @@ use slog::Logger; mod shelly; use futures::prelude::*; -use futures::task::LocalSpawn; +use futures::task::Spawn; use crate::config::Settings; use crate::error::Result; use crate::registries::Registries; // spawner is a type that allows 'tasks' to be spawned on it, running them to completion. -pub fn init(log: Logger, config: &Settings, spawner: &S, registries: Registries) -> Result<()> { +pub fn init(log: Logger, config: &Settings, spawner: &S, registries: Registries) -> Result<()> { let f = Box::new(shelly::run(log.clone(), config.clone(), registries.clone())); - spawner.spawn_local_obj(f.into())?; + spawner.spawn_obj(f.into())?; Ok(()) } diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index 732782c..c51ceb8 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -15,33 +15,27 @@ use paho_mqtt as mqtt; // entirety. This works reasonably enough for this static modules here but if we do dynamic loading // via dlopen(), lua API, python API etc it will not. pub async fn run(log: Logger, config: Settings, registries: Registries) { - let shelly_r = Shelly::new(config).await; - if let Err(e) = shelly_r { - error!(log, "Shelly module errored: {}", e); - return; - } + let shelly = Shelly::new(config).await; - let r = registries.actuators.register( - "shelly".to_string(), - shelly_r.unwrap() - ).await; + let r = registries.actuators.register("shelly".to_string(), shelly).await; } /// An actuator for all Shellies connected listening on one MQTT broker /// /// This actuator can power toggle an arbitrariy named shelly on the broker it is connected to. If /// you need to toggle shellies on multiple brokers you need multiple instanced of this actuator. +#[derive(Clone)] struct Shelly { client: mqtt::AsyncClient, } impl Shelly { - pub async fn new(config: Settings) -> Result { - let client = mqtt::AsyncClient::new(config.shelly.unwrap().mqtt_url)?; + pub async fn new(config: Settings) -> ActBox { + let client = mqtt::AsyncClient::new(config.shelly.unwrap().mqtt_url).unwrap(); - client.connect(mqtt::ConnectOptions::new()).await?; + client.connect(mqtt::ConnectOptions::new()).await.unwrap(); - Ok(Box::new(Shelly { client }) as ActBox) + Box::new(Shelly { client }) } } diff --git a/src/registries/actuators.rs b/src/registries/actuators.rs index 890fc1e..3110ada 100644 --- a/src/registries/actuators.rs +++ b/src/registries/actuators.rs @@ -10,9 +10,7 @@ pub struct Actuators { inner: Arc>, } -unsafe impl Send for Actuators { } - -pub type ActBox = Box; +pub type ActBox = Box; type Inner = HashMap; diff --git a/src/registries/sensors.rs b/src/registries/sensors.rs index b926989..2b7819e 100644 --- a/src/registries/sensors.rs +++ b/src/registries/sensors.rs @@ -20,7 +20,7 @@ impl Sensors { } } -pub type SensBox = Box; +pub type SensBox = Box; type Inner = HashMap;