From cbb6242f89bc17928a080502811b4e579ff67757 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Fri, 20 Nov 2020 15:43:03 +0100 Subject: [PATCH] =?UTF-8?q?Get=20an=20example=20to=20work=20=E2=80=94=20ba?= =?UTF-8?q?rely.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/README.md | 12 ++++++++++++ src/api.rs | 6 ++---- src/api/auth.rs | 1 + src/api/machine.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/api/machines.rs | 7 ++++++- src/config.rs | 2 +- src/db/machine.rs | 3 ++- src/machine.rs | 8 ++++---- src/main.rs | 1 + 9 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 examples/README.md diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..f3b2795 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,12 @@ +# API-Testsetup + +wirklich nur um das API zu testen. ATM implementiert: machine::read + +1. `cargo run -- --print-default > /tmp/bffh.toml` um eine default config zu generieren +1. in /tmp/bffh.toml den parameter `machines` auf ./examples/machines.toml umbiegen + * Bei mir z.b. `~/Development/FabInfra/Diflouroborane/examples/machines.toml` +1. Ein mosquitto o.รค MQTT Server starten + * Bringt aber leider gerade nicht viel ^^' +1. `cargo run -- -c /tmp/bffh.toml` +1. ??? +1. PROFIT! diff --git a/src/api.rs b/src/api.rs index 9655cc8..54a2a97 100644 --- a/src/api.rs +++ b/src/api.rs @@ -55,10 +55,8 @@ impl connection_capnp::bootstrap::Server for Bootstrap { mut res: Results ) -> Promise<(), capnp::Error> { // TODO actual permission check and stuff - if self.session.user.is_some() { - let c = capnp_rpc::new_client(Machines::new(self.session.clone(), self.db.clone())); - res.get().set_machines(c); - } + let c = capnp_rpc::new_client(Machines::new(self.session.clone(), self.db.clone())); + res.get().set_machines(c); Promise::ok(()) } diff --git a/src/api/auth.rs b/src/api/auth.rs index 7f4989e..9f39651 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -139,6 +139,7 @@ impl auth_capnp::authentication::Server for Auth { }; // The step may either return an error, a success or the need for more data + // TODO: Set the session user. Needs a lookup though <.> match step_res { Ok(Step::Done(b)) => { use auth_capnp::response::Result; diff --git a/src/api/machine.rs b/src/api/machine.rs index c41208a..da6485d 100644 --- a/src/api/machine.rs +++ b/src/api/machine.rs @@ -3,10 +3,12 @@ use std::sync::Arc; use capnp::capability::Promise; use capnp::Error; +use crate::schema::api_capnp::State; use crate::schema::api_capnp::machine::*; use crate::db::machine::MachineIdentifier; use crate::connection::Session; use crate::db::Databases; +use crate::db::machine::Status; #[derive(Clone)] pub struct Machine { @@ -19,6 +21,42 @@ impl Machine { pub fn new(session: Arc, id: MachineIdentifier, db: Databases) -> Self { Machine { session, id, db } } + + pub fn fill(self: Arc, builder: &mut Builder) { + // TODO check permissions + builder.set_read(capnp_rpc::new_client(Read(self.clone()))); + // TODO set all the others + } + + pub fn fill_info(&self, builder: &mut m_info::Builder) { + if let Some(desc) = self.db.machine.get_desc(&self.id) { + builder.set_name(&desc.name); + if let Some(d) = desc.description.as_ref() { + builder.set_description(d); + } + + // TODO: Set `responsible` + // TODO: Error Handling + if let Some(state) = self.db.machine.get_state(&self.id) { + match state.state { + Status::Free => builder.set_state(State::Free), + Status::InUse(_u) => { + builder.set_state(State::InUse); + } + Status::ToCheck(_u) => { + builder.set_state(State::ToCheck); + } + Status::Blocked(_u) => { + builder.set_state(State::Blocked); + } + Status::Disabled => builder.set_state(State::Disabled), + Status::Reserved(_u) => { + builder.set_state(State::Reserved); + } + } + } + } + } } struct Read(Arc); @@ -26,10 +64,12 @@ struct Read(Arc); impl read::Server for Read { fn info(&mut self, _params: read::InfoParams, - _results: read::InfoResults) + mut results: read::InfoResults) -> Promise<(), Error> { - unimplemented!() + let mut b = results.get().init_minfo(); + self.0.fill_info(&mut b); + Promise::ok(()) } } diff --git a/src/api/machines.rs b/src/api/machines.rs index dd325e5..42f4171 100644 --- a/src/api/machines.rs +++ b/src/api/machines.rs @@ -47,11 +47,16 @@ impl machines::Server for Machines { if let Ok(api_id) = reader.get_uuid() { let id = uuid_from_api(api_id); if self.db.machine.exists(id) { + debug!(self.session.log, "Accessing machine {}", id); // TODO check disclose permission - let builder = results.get().init_machine(); + let mut builder = results.get().init_machine(); let m = Machine::new(self.session.clone(), id, self.db.clone()); + + Machine::fill(Arc::new(m), &mut builder); + } else { + debug!(self.session.log, "Client requested nonexisting machine {}", id); } } Promise::ok(()) diff --git a/src/config.rs b/src/config.rs index c56ea26..d1e5b07 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,9 +24,9 @@ pub fn read(path: &Path) -> Result { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Settings { + pub machines: PathBuf, pub listens: Box<[Listen]>, pub shelly: Option, - pub machines: PathBuf, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/db/machine.rs b/src/db/machine.rs index 83b6b99..e508643 100644 --- a/src/db/machine.rs +++ b/src/db/machine.rs @@ -63,7 +63,7 @@ pub fn uuid_from_api(uuid: crate::schema::api_capnp::u_u_i_d::Reader) -> Uuid { Uuid::from_u128(num) } pub fn api_from_uuid(uuid: Uuid, mut wr: crate::schema::api_capnp::u_u_i_d::Builder) { - let num = uuid.to_u128_le(); + let num = uuid.as_u128(); let uuid0 = num as u64; let uuid1 = (num >> 64) as u64; wr.set_uuid0(uuid0); @@ -88,6 +88,7 @@ pub fn init(log: Logger, config: &Settings, env: Arc) -> Resu type MachMap = HashMap; +#[derive(Debug)] pub struct MachineDB { state_db: Internal, def_db: MachMap, diff --git a/src/machine.rs b/src/machine.rs index bb86055..cec9211 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -24,10 +24,10 @@ use crate::db::machine::{MachineIdentifier, Status, MachineState}; /// permissions. pub struct Machine { /// Globally unique machine readable identifier - id: MachineIdentifier, + pub id: MachineIdentifier, /// Descriptor of the machine - desc: MachineDescription, + pub desc: MachineDescription, /// The state of the machine as bffh thinks the machine *should* be in. /// @@ -87,9 +87,9 @@ impl Machine { /// Combining this with the actual state of the system will return a machine pub struct MachineDescription { /// The name of the machine. Doesn't need to be unique but is what humans will be presented. - name: String, + pub name: String, /// An optional description of the Machine. - description: Option, + pub description: Option, /// The permission required #[serde(flatten)] diff --git a/src/main.rs b/src/main.rs index 0bf8573..547750e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,6 +212,7 @@ fn main() -> Result<(), Error> { let mdb = mdb?; let defs = machine::MachineDescription::load_file(&config.machines)?; let machdb = db::machine::MachineDB::new(mdb, defs); + info!(log, "{:?}", machdb); let pdb = pdb?; let mut ac = db::access::AccessControl::new(); ac.add_source_unchecked("Internal".to_string(), Box::new(pdb));