diff --git a/src/initiator.rs b/src/initiator.rs index c4cd16b..e63558a 100644 --- a/src/initiator.rs +++ b/src/initiator.rs @@ -13,44 +13,33 @@ use crate::machine::{Machine, ReturnToken}; use crate::db::machine::MachineState; use crate::db::user::{User, UserId, UserData}; +use crate::registries::sensors::Sensor; + use crate::error::Result; -pub struct Initiator<'a> { +pub struct Initiator { signal: MutableSignalCloned>, machine: Option, - future: Option, MachineState)>>, + future: Option, MachineState)>>, token: Option, - step: bool, + //state: Option, + sensor: Box, } -async fn producer(step: bool) -> (Option, MachineState) { - Timer::after(std::time::Duration::from_secs(1)).await; - if step { - return (None, MachineState::free()); - } else { - let user = User::new( - UserId::new("test".to_string(), None, None), - UserData::new(vec![], 0), - ); - let p = user.data.priority; - let id = user.id.clone(); - return (Some(user), MachineState::used(id, p)); - } -} - -impl<'a> Initiator<'a> { - pub fn new(signal: MutableSignalCloned>) -> Self { +impl Initiator { + pub fn new(sensor: Box, signal: MutableSignalCloned>) -> Self { Self { signal: signal, machine: None, future: None, token: None, - step: false, + //state: None, + sensor: sensor, } } } -impl<'a> Future for Initiator<'a> { +impl Future for Initiator { type Output = (); fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll { @@ -69,11 +58,11 @@ impl<'a> Future for Initiator<'a> { // If there is a future, poll it match this.future.as_mut().map(|future| Future::poll(Pin::new(future), cx)) { None => { - this.future = Some(Box::pin(producer(this.step))); - this.step = !this.step; + this.future = Some(this.sensor.run_sensor(None)); }, - Some(Poll::Ready((user, state))) => { + Some(Poll::Ready((fut_state, user, state))) => { this.future.take(); + //this.state.replace(fut_state); this.machine.as_mut().map(|machine| machine.request_state_change(user.as_ref(), state)); } Some(Poll::Pending) => return Poll::Pending, @@ -82,6 +71,35 @@ impl<'a> Future for Initiator<'a> { } } -pub fn load<'a>() -> Result> { +pub fn load() -> Result> { unimplemented!() } + +pub struct Dummy; + +impl Sensor for Dummy { + type State = bool; + + fn run_sensor(&mut self, state: Option) + -> BoxFuture<'static, (Self::State, Option, MachineState)> + { + let step = state.map(|b| !b).unwrap_or(false); + let f = async move { + Timer::after(std::time::Duration::from_secs(1)).await; + if step { + return (step, None, MachineState::free()); + } else { + let user = User::new( + UserId::new("test".to_string(), None, None), + UserData::new(vec![], 0), + ); + let p = user.data.priority; + let id = user.id.clone(); + return (step, Some(user), MachineState::used(id, p)); + } + }; + + Box::pin(f) + } +} + diff --git a/src/main.rs b/src/main.rs index 2120eb5..a5d6bad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,8 +47,6 @@ use error::Error; use slog::Logger; -use registries::Registries; - fn main() { use clap::{crate_version, crate_description, crate_name}; diff --git a/src/modules.rs b/src/modules.rs index 9f8a21e..f2decc8 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -14,11 +14,3 @@ 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 async fn init(log: Logger, config: Settings, spawner: S, registries: Registries) -> Result<()> { - shelly::run(log.clone(), config.clone(), registries.clone(), spawner.clone()).await; - - Ok(()) -} diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index c20cf1c..37a5075 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -3,7 +3,6 @@ use slog::Logger; use crate::config::Settings; use crate::error::Result; use crate::db::machine::Status; -use crate::registries::Registries; use std::pin::Pin; use futures::prelude::*; @@ -15,9 +14,3 @@ use futures_signals::signal::Signal; use paho_mqtt as mqtt; -// TODO: Late config parsing. Right now the config is validated at the very startup in its -// 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, spawner: S) { -} - diff --git a/src/registries.rs b/src/registries.rs index ad82913..1cb9c85 100644 --- a/src/registries.rs +++ b/src/registries.rs @@ -1,21 +1,2 @@ -use std::sync::Arc; - pub mod actuators; pub mod sensors; - -#[derive(Clone)] -/// BFFH registries -/// -/// This struct is only a reference to the underlying registries - cloning it will generate a new -/// reference, not clone the registries -pub struct Registries { - pub sensors: sensors::Sensors, -} - -impl Registries { - pub fn new() -> Self { - Registries { - sensors: sensors::Sensors::new(), - } - } -} diff --git a/src/registries/sensors.rs b/src/registries/sensors.rs index 0fe85d1..180f58a 100644 --- a/src/registries/sensors.rs +++ b/src/registries/sensors.rs @@ -1,28 +1,10 @@ use std::pin::Pin; use futures::task::{Context, Poll}; -use futures::{Future, Stream}; use futures::future::BoxFuture; -use futures_signals::signal::Signal; -use crate::db::user::UserId; +use crate::db::user::User; +use crate::db::machine::MachineState; -use std::sync::Arc; -use smol::lock::RwLock; -use std::collections::HashMap; - -#[derive(Clone)] -pub struct Sensors { - inner: Arc>, +pub trait Sensor { + type State: Sized; + fn run_sensor(&mut self, state: Option) -> BoxFuture<'static, (Self::State, Option, MachineState)>; } - -impl Sensors { - pub fn new() -> Self { - Sensors { - inner: Arc::new(RwLock::new(Inner::new())), - } - } -} - -pub type SensBox = Box + Send + Sync>; -type Inner = HashMap; - -