No more priority pls

This commit is contained in:
Gregor Reitzenstein 2021-01-20 11:55:15 +00:00
parent 53564b6bca
commit cda947a0e9
8 changed files with 40 additions and 37 deletions

View File

@ -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"

View File

@ -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<Machine>);
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!()

View File

@ -26,8 +26,8 @@ use crate::network::Network;
pub struct Session {
// Session-spezific log
pub log: Logger,
user: Option<User>,
accessdb: Arc<AccessControl>,
pub user: Option<User>,
pub accessdb: Arc<AccessControl>,
}
impl Session {

View File

@ -43,15 +43,15 @@ pub enum Status {
/// Not currently used by anybody
Free,
/// Used by somebody
InUse(UserId, Priority),
InUse(Option<UserId>),
/// 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<UserId>) -> Self {
Self { state: Status::InUse(uid) }
}
}

View File

@ -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)));
}
};

View File

@ -154,10 +154,9 @@ impl Inner {
return self.do_state_change(new_state);
}
} else {
if self.state.lock_ref().is_higher_priority(who.unwrap().data.priority) {
// TODO: Correctly check permissions here
return self.do_state_change(new_state);
}
}
return Err(Error::Denied);
}

View File

@ -195,8 +195,10 @@ fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> 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;
});

View File

@ -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);