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

View File

@ -187,6 +187,18 @@ fn is_sep_char(c: char) -> bool {
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)]
#[repr(transparent)]
/// An owned permission string

View File

@ -37,15 +37,20 @@ use internal::Internal;
pub type MachineIdentifier = Uuid;
/// Status of a Machine
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[repr(u8)]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum Status {
/// Not currently used by anybody
Free,
/// 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
Blocked,
Blocked(UserIdentifier),
/// Disabled for some other reason
Disabled,
/// Reserved
Reserved(UserIdentifier),
}
#[derive(Clone)]
@ -88,7 +93,7 @@ pub struct Machine {
/// The human-readable name of the machine. Does not need to be unique
name: String,
/// The required permission to use this machine.
/// The required permissions to use this machine.
perm: access::PermIdentifier,
/// 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
// (could for example happen if the machine changes current user but stays activated) no
// 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.
@ -130,7 +135,7 @@ impl Machine {
) -> Result<bool>
{
if pp.check(who, &self.perm)? {
self.state.set(Status::Occupied);
self.state.set(Status::InUse(who.id.clone()));
return Ok(true);
} else {
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
pub trait MachineDB {
fn get_machine(&self, machID: &MachineIdentifier) -> Result<Option<Machine>>;

View File

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

View File

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