From cda947a0e9ff327e5be11d1a6387c5245000a3c3 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 20 Jan 2021 11:55:15 +0000 Subject: [PATCH] No more priority pls --- examples/bffh.dhall | 5 ++++- src/api/machine.rs | 28 ++++++++++++++++++++++------ src/connection.rs | 4 ++-- src/db/machine.rs | 28 ++++++---------------------- src/initiator.rs | 3 +-- src/machine.rs | 5 ++--- src/main.rs | 2 ++ src/modules/shelly.rs | 2 +- 8 files changed, 40 insertions(+), 37 deletions(-) diff --git a/examples/bffh.dhall b/examples/bffh.dhall index 1c483b0..4ff8bb3 100644 --- a/examples/bffh.dhall +++ b/examples/bffh.dhall @@ -6,7 +6,10 @@ , initiators = { Initiator = { module = "Dummy", params = {=} } } -, listens = [{ address = "localhost", port = Some 59661 }] +, listens = + [ { address = "127.0.0.1", port = Some 59661 } + , { address = "::1", port = Some 59661 } + ] , machines = { Testmachine = { description = Some "A test machine" diff --git a/src/api/machine.rs b/src/api/machine.rs index 90b2ed5..983abe3 100644 --- a/src/api/machine.rs +++ b/src/api/machine.rs @@ -8,7 +8,7 @@ use crate::schema::api_capnp::State; use crate::schema::api_capnp::machine::*; use crate::connection::Session; use crate::db::Databases; -use crate::db::machine::Status; +use crate::db::machine::{Status, MachineState}; use crate::machine::Machine as NwMachine; #[derive(Clone)] @@ -45,16 +45,16 @@ impl Machine { Status::Disabled => { builder.set_state(State::Disabled); } - Status::Blocked(_,_) => { + Status::Blocked(_) => { builder.set_state(State::Blocked); } - Status::InUse(_,_) => { + Status::InUse(_) => { builder.set_state(State::InUse); } - Status::ToCheck(_,_) => { + Status::ToCheck(_) => { builder.set_state(State::ToCheck); } - Status::Reserved(_,_) => { + Status::Reserved(_) => { builder.set_state(State::Reserved); } } @@ -80,7 +80,23 @@ struct Write(Arc); impl write::Server for Write { fn use_(&mut self, _params: write::UseParams, - _results: write::UseResults) + results: write::UseResults) + -> Promise<(), Error> + { + let uid = self.0.session.user.as_ref().map(|u| u.id.clone()); + let new_state = MachineState::used(uid.clone()); + if let Ok(tok) = self.0.machine.request_state_change(self.0.session.user.as_ref(), new_state) { + info!(self.0.session.log, "yay"); + } else { + info!(self.0.session.log, "nay"); + } + + Promise::ok(()) + } + + fn reserve(&mut self, + _params: write::ReserveParams, + _results: write::ReserveResults) -> Promise<(), Error> { unimplemented!() diff --git a/src/connection.rs b/src/connection.rs index 7022c7d..2915c5e 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -26,8 +26,8 @@ use crate::network::Network; pub struct Session { // Session-spezific log pub log: Logger, - user: Option, - accessdb: Arc, + pub user: Option, + pub accessdb: Arc, } impl Session { diff --git a/src/db/machine.rs b/src/db/machine.rs index a243408..e28d66f 100644 --- a/src/db/machine.rs +++ b/src/db/machine.rs @@ -43,15 +43,15 @@ pub enum Status { /// Not currently used by anybody Free, /// Used by somebody - InUse(UserId, Priority), + InUse(Option), /// Was used by somebody and now needs to be checked for cleanliness - ToCheck(UserId, Priority), + ToCheck(UserId), /// Not used by anybody but also can not be used. E.g. down for maintenance - Blocked(UserId, Priority), + Blocked(UserId), /// Disabled for some other reason Disabled, /// Reserved - Reserved(UserId, Priority), + Reserved(UserId), } pub fn uuid_from_api(uuid: crate::schema::api_capnp::u_u_i_d::Reader) -> Uuid { @@ -83,24 +83,8 @@ impl MachineState { Self { state: Status::Free } } - pub fn used(uid: UserId, priority: Priority) -> Self { - Self { state: Status::InUse(uid, priority) } - } - - /// Check if the given priority is higher than one's own. - /// - /// If `self` does not have a priority then this function always returns `true` - pub fn is_higher_priority(&self, priority: u64) -> bool { - match self.state { - Status::Disabled | Status::Free => { true }, - Status::Blocked(_, self_prio) | - Status::InUse(_, self_prio) | - Status::ToCheck(_, self_prio) | - Status::Reserved(_, self_prio) => - { - priority > self_prio - } - } + pub fn used(uid: Option) -> Self { + Self { state: Status::InUse(uid) } } } diff --git a/src/initiator.rs b/src/initiator.rs index 94765f8..1e6264d 100644 --- a/src/initiator.rs +++ b/src/initiator.rs @@ -157,9 +157,8 @@ impl Sensor for Dummy { 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)); + return (Some(user), MachineState::used(Some(id))); } }; diff --git a/src/machine.rs b/src/machine.rs index f96943e..a65d208 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -154,9 +154,8 @@ impl Inner { return self.do_state_change(new_state); } } else { - if self.state.lock_ref().is_higher_priority(who.unwrap().data.priority) { - return self.do_state_change(new_state); - } + // TODO: Correctly check permissions here + return self.do_state_change(new_state); } return Err(Error::Denied); diff --git a/src/main.rs b/src/main.rs index bf0f984..15f4991 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,8 +195,10 @@ fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { // when bffh should exit let r = server::serve_api_connections(log.clone(), config, db, network); + // One of them would be enough really, but *shrug* signal.try_send(()); std::mem::drop(signal); + return r; }); diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index c4c6cdc..798a1a5 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -50,7 +50,7 @@ impl Actuator for Shelly { info!(self.log, "Machine Status changed: {:?}", state); let topic = format!("shellies/{}/relay/0/command", self.name); let pl = match state.state { - Status::InUse(_, _) => "on", + Status::InUse(_) => "on", _ => "off", }; let msg = mqtt::Message::new(topic, pl, 0);