From c1c34aa7034f1ac0db34d067b304fa0fa8cc7ead Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 2 Dec 2020 17:15:25 +0100 Subject: [PATCH] Working statekeeping --- src/initiator.rs | 33 +++++++++++++++++++-------------- src/registries/sensors.rs | 3 +-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/initiator.rs b/src/initiator.rs index e63558a..7544529 100644 --- a/src/initiator.rs +++ b/src/initiator.rs @@ -20,9 +20,8 @@ use crate::error::Result; pub struct Initiator { signal: MutableSignalCloned>, machine: Option, - future: Option, MachineState)>>, + future: Option, MachineState)>>, token: Option, - //state: Option, sensor: Box, } @@ -33,7 +32,6 @@ impl Initiator { machine: None, future: None, token: None, - //state: None, sensor: sensor, } } @@ -58,11 +56,10 @@ impl Future for Initiator { // If there is a future, poll it match this.future.as_mut().map(|future| Future::poll(Pin::new(future), cx)) { None => { - this.future = Some(this.sensor.run_sensor(None)); + this.future = Some(this.sensor.run_sensor()); }, - Some(Poll::Ready((fut_state, user, state))) => { + Some(Poll::Ready((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, @@ -75,19 +72,27 @@ pub fn load() -> Result> { unimplemented!() } -pub struct Dummy; +pub struct Dummy { + step: bool +} + +impl Dummy { + pub fn new() -> Self { + Self { step: false } + } +} impl Sensor for Dummy { - type State = bool; - - fn run_sensor(&mut self, state: Option) - -> BoxFuture<'static, (Self::State, Option, MachineState)> + fn run_sensor(&mut self) + -> BoxFuture<'static, (Option, MachineState)> { - let step = state.map(|b| !b).unwrap_or(false); + let step = self.step; + self.step != self.step; + let f = async move { Timer::after(std::time::Duration::from_secs(1)).await; if step { - return (step, None, MachineState::free()); + return (None, MachineState::free()); } else { let user = User::new( UserId::new("test".to_string(), None, None), @@ -95,7 +100,7 @@ impl Sensor for Dummy { ); let p = user.data.priority; let id = user.id.clone(); - return (step, Some(user), MachineState::used(id, p)); + return (Some(user), MachineState::used(id, p)); } }; diff --git a/src/registries/sensors.rs b/src/registries/sensors.rs index 180f58a..718f2f9 100644 --- a/src/registries/sensors.rs +++ b/src/registries/sensors.rs @@ -5,6 +5,5 @@ use crate::db::user::User; use crate::db::machine::MachineState; pub trait Sensor { - type State: Sized; - fn run_sensor(&mut self, state: Option) -> BoxFuture<'static, (Self::State, Option, MachineState)>; + fn run_sensor(&mut self) -> BoxFuture<'static, (Option, MachineState)>; }