mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2024-11-11 01:53:23 +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.
|
// without warning.
|
||||||
let modlog = log.clone();
|
let modlog = log.clone();
|
||||||
let regs = Registries::new();
|
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(()) => {}
|
Ok(()) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(modlog, "Module startup failed: {}", e);
|
error!(modlog, "Module startup failed: {}", e);
|
||||||
|
@ -10,16 +10,16 @@ use slog::Logger;
|
|||||||
mod shelly;
|
mod shelly;
|
||||||
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use futures::task::LocalSpawn;
|
use futures::task::Spawn;
|
||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::registries::Registries;
|
use crate::registries::Registries;
|
||||||
|
|
||||||
// spawner is a type that allows 'tasks' to be spawned on it, running them to completion.
|
// 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()));
|
let f = Box::new(shelly::run(log.clone(), config.clone(), registries.clone()));
|
||||||
spawner.spawn_local_obj(f.into())?;
|
spawner.spawn_obj(f.into())?;
|
||||||
|
|
||||||
Ok(())
|
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
|
// 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.
|
// via dlopen(), lua API, python API etc it will not.
|
||||||
pub async fn run(log: Logger, config: Settings, registries: Registries) {
|
pub async fn run(log: Logger, config: Settings, registries: Registries) {
|
||||||
let shelly_r = Shelly::new(config).await;
|
let shelly = Shelly::new(config).await;
|
||||||
if let Err(e) = shelly_r {
|
|
||||||
error!(log, "Shelly module errored: {}", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let r = registries.actuators.register(
|
let r = registries.actuators.register("shelly".to_string(), shelly).await;
|
||||||
"shelly".to_string(),
|
|
||||||
shelly_r.unwrap()
|
|
||||||
).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An actuator for all Shellies connected listening on one MQTT broker
|
/// 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
|
/// 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.
|
/// you need to toggle shellies on multiple brokers you need multiple instanced of this actuator.
|
||||||
|
#[derive(Clone)]
|
||||||
struct Shelly {
|
struct Shelly {
|
||||||
client: mqtt::AsyncClient,
|
client: mqtt::AsyncClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shelly {
|
impl Shelly {
|
||||||
pub async fn new(config: Settings) -> Result<ActBox> {
|
pub async fn new(config: Settings) -> ActBox {
|
||||||
let client = mqtt::AsyncClient::new(config.shelly.unwrap().mqtt_url)?;
|
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>>,
|
inner: Arc<RwLock<Inner>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Actuators { }
|
pub type ActBox = Box<dyn Actuator + Sync + Send>;
|
||||||
|
|
||||||
pub type ActBox = Box<dyn Actuator>;
|
|
||||||
|
|
||||||
type Inner = HashMap<String, ActBox>;
|
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>;
|
type Inner = HashMap<String, SensBox>;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user