mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2024-11-25 16:17:56 +01:00
Move modules back to threadpool
This commit is contained in:
parent
267ff63016
commit
c943e78cc6
@ -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);
|
||||
|
@ -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(())
|
||||
}
|
||||
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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>;
|
||||
|
||||
|
@ -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>;
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user