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::machine::MachineState;
|
||||||
use crate::db::user::{User, UserId, UserData};
|
use crate::db::user::{User, UserId, UserData};
|
||||||
|
|
||||||
|
use crate::registries::sensors::Sensor;
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
|
||||||
pub struct Initiator<'a> {
|
pub struct Initiator<S: Sensor> {
|
||||||
signal: MutableSignalCloned<Option<Machine>>,
|
signal: MutableSignalCloned<Option<Machine>>,
|
||||||
machine: Option<Machine>,
|
machine: Option<Machine>,
|
||||||
future: Option<BoxFuture<'a, (Option<User>, MachineState)>>,
|
future: Option<BoxFuture<'static, (S::State, Option<User>, MachineState)>>,
|
||||||
token: Option<ReturnToken>,
|
token: Option<ReturnToken>,
|
||||||
step: bool,
|
//state: Option<S::State>,
|
||||||
|
sensor: Box<S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn producer(step: bool) -> (Option<User>, MachineState) {
|
impl<S: Sensor> Initiator<S> {
|
||||||
Timer::after(std::time::Duration::from_secs(1)).await;
|
pub fn new(sensor: Box<S>, signal: MutableSignalCloned<Option<Machine>>) -> Self {
|
||||||
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 {
|
|
||||||
Self {
|
Self {
|
||||||
signal: signal,
|
signal: signal,
|
||||||
machine: None,
|
machine: None,
|
||||||
future: None,
|
future: None,
|
||||||
token: None,
|
token: None,
|
||||||
step: false,
|
//state: None,
|
||||||
|
sensor: sensor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Future for Initiator<'a> {
|
impl<S: Sensor> Future for Initiator<S> {
|
||||||
type Output = ();
|
type Output = ();
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::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
|
// If there is a future, poll it
|
||||||
match this.future.as_mut().map(|future| Future::poll(Pin::new(future), cx)) {
|
match this.future.as_mut().map(|future| Future::poll(Pin::new(future), cx)) {
|
||||||
None => {
|
None => {
|
||||||
this.future = Some(Box::pin(producer(this.step)));
|
this.future = Some(this.sensor.run_sensor(None));
|
||||||
this.step = !this.step;
|
|
||||||
},
|
},
|
||||||
Some(Poll::Ready((user, state))) => {
|
Some(Poll::Ready((fut_state, user, state))) => {
|
||||||
this.future.take();
|
this.future.take();
|
||||||
|
//this.state.replace(fut_state);
|
||||||
this.machine.as_mut().map(|machine| machine.request_state_change(user.as_ref(), state));
|
this.machine.as_mut().map(|machine| machine.request_state_change(user.as_ref(), state));
|
||||||
}
|
}
|
||||||
Some(Poll::Pending) => return Poll::Pending,
|
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!()
|
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 slog::Logger;
|
||||||
|
|
||||||
use registries::Registries;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use clap::{crate_version, crate_description, crate_name};
|
use clap::{crate_version, crate_description, crate_name};
|
||||||
|
|
||||||
|
@ -14,11 +14,3 @@ use futures::task::Spawn;
|
|||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::Result;
|
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::config::Settings;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::db::machine::Status;
|
use crate::db::machine::Status;
|
||||||
use crate::registries::Registries;
|
|
||||||
|
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
@ -15,9 +14,3 @@ use futures_signals::signal::Signal;
|
|||||||
|
|
||||||
use paho_mqtt as mqtt;
|
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 actuators;
|
||||||
pub mod sensors;
|
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 std::pin::Pin;
|
||||||
use futures::task::{Context, Poll};
|
use futures::task::{Context, Poll};
|
||||||
use futures::{Future, Stream};
|
|
||||||
use futures::future::BoxFuture;
|
use futures::future::BoxFuture;
|
||||||
use futures_signals::signal::Signal;
|
use crate::db::user::User;
|
||||||
use crate::db::user::UserId;
|
use crate::db::machine::MachineState;
|
||||||
|
|
||||||
use std::sync::Arc;
|
pub trait Sensor {
|
||||||
use smol::lock::RwLock;
|
type State: Sized;
|
||||||
use std::collections::HashMap;
|
fn run_sensor(&mut self, state: Option<Self::State>) -> BoxFuture<'static, (Self::State, Option<User>, MachineState)>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Sensors {
|
|
||||||
inner: Arc<RwLock<Inner>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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