Slowly getting there

This commit is contained in:
Gregor Reitzenstein 2020-11-10 14:56:28 +01:00
parent 743de370ab
commit 8442a3d29d
5 changed files with 54 additions and 17 deletions

View File

@ -1,5 +1,4 @@
use smol::net::TcpStream; use std::sync::Arc;
use futures_util::FutureExt;
use slog::Logger; use slog::Logger;
@ -13,11 +12,22 @@ use capnp_rpc::twoparty::VatNetwork;
use capnp_rpc::rpc_twoparty_capnp::Side; use capnp_rpc::rpc_twoparty_capnp::Side;
use capnp::capability::FromServer; use capnp::capability::FromServer;
pub async fn handle_connection(log: Logger, socket: TcpStream) -> Result<()> { use crate::db::machine::Machines;
unimplemented!() use crate::db::user::User;
use uuid::Uuid;
pub struct MachinesAPI {
log: Logger,
user: User,
machines: Arc<Machines>,
} }
pub struct MachinesAPI; impl MachinesAPI {
pub fn new(log: Logger, user: User, machines: Arc<Machines>) -> Self {
Self { log, user, machines }
}
}
impl api_capnp::machines::Server for MachinesAPI { impl api_capnp::machines::Server for MachinesAPI {
fn list_machines(&mut self, fn list_machines(&mut self,
@ -25,8 +35,9 @@ impl api_capnp::machines::Server for MachinesAPI {
mut results: api_capnp::machines::ListMachinesResults) mut results: api_capnp::machines::ListMachinesResults)
-> Promise<(), Error> -> Promise<(), Error>
{ {
let mut l = results.get(); let l = results.get();
l.init_machines(0); let keys: Vec<api_capnp::machine::Reader> = self.machines.iter().map(|x| x.into()).collect();
l.set_machines(keys);
Promise::ok(()) Promise::ok(())
} }

View File

@ -187,6 +187,18 @@ fn is_sep_char(c: char) -> bool {
c == '.' c == '.'
} }
/// A set of privileges to a thing
pub struct PrivilegesBuf {
/// Which permission is required to know about the existance of this thing
disclose: PermissionBuf,
/// Which permission is required to read this thing
read: PermissionBuf,
/// Which permission is required to write parts of this thing
write: PermissionBuf,
/// Which permission is required to manage all parts of this thing
manage: PermissionBuf
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)] #[repr(transparent)]
/// An owned permission string /// An owned permission string

View File

@ -37,15 +37,20 @@ use internal::Internal;
pub type MachineIdentifier = Uuid; pub type MachineIdentifier = Uuid;
/// Status of a Machine /// Status of a Machine
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[repr(u8)]
pub enum Status { pub enum Status {
/// Not currently used by anybody /// Not currently used by anybody
Free, Free,
/// Used by somebody /// Used by somebody
Occupied, InUse(UserIdentifier),
/// Was used by somebody and now needs to be checked for cleanliness
ToCheck(UserIdentifier),
/// Not used by anybody but also can not be used. E.g. down for maintenance /// Not used by anybody but also can not be used. E.g. down for maintenance
Blocked, Blocked(UserIdentifier),
/// Disabled for some other reason
Disabled,
/// Reserved
Reserved(UserIdentifier),
} }
#[derive(Clone)] #[derive(Clone)]
@ -88,7 +93,7 @@ pub struct Machine {
/// The human-readable name of the machine. Does not need to be unique /// The human-readable name of the machine. Does not need to be unique
name: String, name: String,
/// The required permission to use this machine. /// The required permissions to use this machine.
perm: access::PermIdentifier, perm: access::PermIdentifier,
/// The state of the machine as bffh thinks the machine *should* be in. /// The state of the machine as bffh thinks the machine *should* be in.
@ -117,7 +122,7 @@ impl Machine {
// dedupe ensures that if state is changed but only changes to the value it had beforehand // dedupe ensures that if state is changed but only changes to the value it had beforehand
// (could for example happen if the machine changes current user but stays activated) no // (could for example happen if the machine changes current user but stays activated) no
// update is sent. // update is sent.
Box::pin(self.state.signal().dedupe()) Box::pin(self.state.signal_cloned().dedupe_cloned())
} }
/// Requests to use a machine. Returns `true` if successful. /// Requests to use a machine. Returns `true` if successful.
@ -130,7 +135,7 @@ impl Machine {
) -> Result<bool> ) -> Result<bool>
{ {
if pp.check(who, &self.perm)? { if pp.check(who, &self.perm)? {
self.state.set(Status::Occupied); self.state.set(Status::InUse(who.id.clone()));
return Ok(true); return Ok(true);
} else { } else {
return Ok(false); return Ok(false);
@ -142,6 +147,15 @@ impl Machine {
} }
} }
#[derive(Debug)]
pub struct Machines {
inner: HashMap<Uuid, Machine>,
}
impl Machines {
}
// TODO split up for non-writable Definition Databases // TODO split up for non-writable Definition Databases
pub trait MachineDB { pub trait MachineDB {
fn get_machine(&self, machID: &MachineIdentifier) -> Result<Option<Machine>>; fn get_machine(&self, machID: &MachineIdentifier) -> Result<Option<Machine>>;

View File

@ -4,7 +4,7 @@ use crate::db::access::RoleIdentifier;
use std::collections::HashMap; use std::collections::HashMap;
/// A Person, from the Authorization perspective /// A Person, from the Authorization perspective
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct User { pub struct User {
/// The identification of this user. /// The identification of this user.
pub id: UserIdentifier, pub id: UserIdentifier,

View File

@ -102,8 +102,8 @@ impl Stream for Shelly {
info!(unpin.log, "Machine Status changed: {:?}", status); info!(unpin.log, "Machine Status changed: {:?}", status);
let topic = format!("shellies/{}/relay/0/command", unpin.name); let topic = format!("shellies/{}/relay/0/command", unpin.name);
let pl = match status { let pl = match status {
Status::Free | Status::Blocked => "off", Status::InUse(_) => "on",
Status::Occupied => "on", _ => "off",
}; };
let msg = mqtt::Message::new(topic, pl, 0); let msg = mqtt::Message::new(topic, pl, 0);
let f = unpin.client.publish(msg).map(|_| ()); let f = unpin.client.publish(msg).map(|_| ());