Move modules back to threadpool

This commit is contained in:
Gregor Reitzenstein 2020-09-17 14:32:53 +02:00
parent 267ff63016
commit c943e78cc6
5 changed files with 13 additions and 21 deletions

View File

@ -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);

View File

@ -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<S: LocalSpawn>(log: Logger, config: &Settings, spawner: &S, registries: Registries) -> Result<()> {
pub fn init<S: Spawn>(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(())
}

View File

@ -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<ActBox> {
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 })
}
}

View File

@ -10,9 +10,7 @@ pub struct Actuators {
inner: Arc<RwLock<Inner>>,
}
unsafe impl Send for Actuators { }
pub type ActBox = Box<dyn Actuator>;
pub type ActBox = Box<dyn Actuator + Sync + Send>;
type Inner = HashMap<String, ActBox>;

View File

@ -20,7 +20,7 @@ impl Sensors {
}
}
pub type SensBox = Box<dyn Sensor>;
pub type SensBox = Box<dyn Sensor + Send + Sync>;
type Inner = HashMap<String, SensBox>;