mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2024-11-22 14:57:56 +01:00
Make compile
This commit is contained in:
parent
5a4b03a16c
commit
c5d733d888
@ -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<S: Sensor> {
|
||||
signal: MutableSignalCloned<Option<Machine>>,
|
||||
machine: Option<Machine>,
|
||||
future: Option<BoxFuture<'a, (Option<User>, MachineState)>>,
|
||||
future: Option<BoxFuture<'static, (S::State, Option<User>, MachineState)>>,
|
||||
token: Option<ReturnToken>,
|
||||
step: bool,
|
||||
//state: Option<S::State>,
|
||||
sensor: Box<S>,
|
||||
}
|
||||
|
||||
async fn producer(step: bool) -> (Option<User>, 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<Option<Machine>>) -> Self {
|
||||
impl<S: Sensor> Initiator<S> {
|
||||
pub fn new(sensor: Box<S>, signal: MutableSignalCloned<Option<Machine>>) -> Self {
|
||||
Self {
|
||||
signal: signal,
|
||||
machine: None,
|
||||
future: None,
|
||||
token: None,
|
||||
step: false,
|
||||
//state: None,
|
||||
sensor: sensor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Future for Initiator<'a> {
|
||||
impl<S: Sensor> Future for Initiator<S> {
|
||||
type Output = ();
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
@ -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<Initiator<'a>> {
|
||||
pub fn load<S: Sensor>() -> Result<Initiator<S>> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub struct Dummy;
|
||||
|
||||
impl Sensor for Dummy {
|
||||
type State = bool;
|
||||
|
||||
fn run_sensor(&mut self, state: Option<bool>)
|
||||
-> BoxFuture<'static, (Self::State, Option<User>, 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,8 +47,6 @@ use error::Error;
|
||||
|
||||
use slog::Logger;
|
||||
|
||||
use registries::Registries;
|
||||
|
||||
fn main() {
|
||||
use clap::{crate_version, crate_description, crate_name};
|
||||
|
||||
|
@ -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<S: Spawn + Clone + Send>(log: Logger, config: Settings, spawner: S, registries: Registries) -> Result<()> {
|
||||
shelly::run(log.clone(), config.clone(), registries.clone(), spawner.clone()).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -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<S: Spawn>(log: Logger, config: Settings, registries: Registries, spawner: S) {
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<RwLock<Inner>>,
|
||||
pub trait Sensor {
|
||||
type State: Sized;
|
||||
fn run_sensor(&mut self, state: Option<Self::State>) -> BoxFuture<'static, (Self::State, Option<User>, MachineState)>;
|
||||
}
|
||||
|
||||
impl Sensors {
|
||||
pub fn new() -> Self {
|
||||
Sensors {
|
||||
inner: Arc::new(RwLock::new(Inner::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type SensBox = Box<dyn Signal<Item=UserId> + Send + Sync>;
|
||||
type Inner = HashMap<String, SensBox>;
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user