2020-02-16 16:02:03 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
2020-02-17 14:56:43 +01:00
|
|
|
use slog::Logger;
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
2020-09-08 09:56:40 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use smol::lock::RwLock;
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
use crate::error::Result;
|
2020-09-15 14:31:10 +02:00
|
|
|
use crate::config::Settings;
|
2020-02-17 14:56:43 +01:00
|
|
|
|
|
|
|
use capnp::Error;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-15 14:48:59 +02:00
|
|
|
use lmdb::{Transaction, RwTransaction};
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
/// Status of a Machine
|
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum Status {
|
|
|
|
/// Not currently used by anybody
|
|
|
|
Free,
|
|
|
|
/// Used by somebody
|
|
|
|
Occupied,
|
|
|
|
/// Not used by anybody but also can not be used. E.g. down for maintenance
|
|
|
|
Blocked,
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
pub struct MachinesProvider {
|
2020-02-17 14:56:43 +01:00
|
|
|
log: Logger,
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: MachineDB,
|
2020-02-17 14:56:43 +01:00
|
|
|
}
|
2020-02-17 03:44:02 +01:00
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
impl MachinesProvider {
|
|
|
|
pub fn new(log: Logger, mdb: MachineDB) -> Self {
|
|
|
|
Self { log, mdb }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Machines {
|
2020-09-08 09:56:40 +02:00
|
|
|
inner: Arc<RwLock<MachinesProvider>>,
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
2020-02-17 14:56:43 +01:00
|
|
|
impl Machines {
|
2020-09-08 09:56:40 +02:00
|
|
|
pub fn new(inner: Arc<RwLock<MachinesProvider>>) -> Self {
|
2020-05-04 13:22:14 +02:00
|
|
|
Self { inner }
|
2020-02-17 14:56:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
#[derive(Clone)]
|
2020-02-17 21:07:50 +01:00
|
|
|
pub struct GiveBack {
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: Arc<RwLock<MachinesProvider>>,
|
2020-02-17 21:07:50 +01:00
|
|
|
uuid: Uuid,
|
|
|
|
}
|
|
|
|
impl GiveBack {
|
2020-02-18 16:55:19 +01:00
|
|
|
pub fn new(mdb: Arc<RwLock<MachinesProvider>>, uuid: Uuid) -> Self {
|
|
|
|
Self { mdb, uuid }
|
2020-02-17 21:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 09:56:40 +02:00
|
|
|
fn uuid_from_api(uuid: crate::api::api_capnp::u_u_i_d::Reader) -> Uuid {
|
2020-02-17 14:56:43 +01:00
|
|
|
let uuid0 = uuid.get_uuid0() as u128;
|
|
|
|
let uuid1 = uuid.get_uuid1() as u128;
|
|
|
|
let num: u128 = (uuid1 << 64) + uuid0;
|
|
|
|
Uuid::from_u128(num)
|
|
|
|
}
|
2020-09-08 09:56:40 +02:00
|
|
|
fn api_from_uuid(uuid: Uuid, mut wr: crate::api::api_capnp::u_u_i_d::Builder) {
|
2020-02-17 14:56:43 +01:00
|
|
|
let num = uuid.to_u128_le();
|
|
|
|
let uuid0 = num as u64;
|
|
|
|
let uuid1 = (num >> 64) as u64;
|
|
|
|
wr.set_uuid0(uuid0);
|
|
|
|
wr.set_uuid1(uuid1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MachineManager {
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: Arc<RwLock<MachinesProvider>>,
|
2020-02-17 14:56:43 +01:00
|
|
|
uuid: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MachineManager {
|
2020-02-19 14:50:23 +01:00
|
|
|
pub fn new(uuid: Uuid, mdb: Arc<RwLock<MachinesProvider>>) -> Self {
|
2020-02-17 14:56:43 +01:00
|
|
|
Self { mdb, uuid }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
2020-09-15 15:29:55 +02:00
|
|
|
/// Internal machine representation
|
|
|
|
///
|
|
|
|
/// A machine connects an event from a sensor to an actor activating/deactivating a real-world
|
|
|
|
/// machine, checking that the user who wants the machine (de)activated has the required
|
|
|
|
/// permissions.
|
2020-02-16 16:02:03 +01:00
|
|
|
pub struct Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
pub name: String,
|
|
|
|
pub perm: String,
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
pub fn new(name: String, location: String, perm: String) -> Machine {
|
2020-02-16 16:02:03 +01:00
|
|
|
Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
name: name,
|
2020-02-16 16:02:03 +01:00
|
|
|
location: location,
|
|
|
|
status: Status::Free,
|
2020-02-17 14:56:43 +01:00
|
|
|
perm: perm,
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:56:43 +01:00
|
|
|
pub fn set_blocked(&mut self, blocked: bool) {
|
|
|
|
if blocked {
|
|
|
|
self.status = Status::Blocked;
|
|
|
|
} else {
|
|
|
|
self.status = Status::Free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-15 14:41:50 +02:00
|
|
|
struct MachineDB {
|
|
|
|
db: lmdb::Database,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MachineDB {
|
|
|
|
pub fn new(db: lmdb::Database) -> Self {
|
|
|
|
Self { db }
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:48:59 +02:00
|
|
|
pub fn get_machine<T: Transaction>(&self, txn: &T, uuid: Uuid)
|
2020-09-15 14:41:50 +02:00
|
|
|
-> Result<Option<Machine>>
|
|
|
|
{
|
2020-09-15 14:48:59 +02:00
|
|
|
match txn.get(self.db, &uuid.as_bytes()) {
|
2020-09-15 14:41:50 +02:00
|
|
|
Ok(bytes) => {
|
|
|
|
Ok(Some(flexbuffers::from_slice(bytes)?))
|
|
|
|
},
|
|
|
|
Err(lmdb::Error::NotFound) => { Ok(None) },
|
|
|
|
Err(e) => { Err(e.into()) },
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:48:59 +02:00
|
|
|
|
|
|
|
pub fn put_machine( &self, txn: &mut RwTransaction, uuid: Uuid, machine: Machine)
|
|
|
|
-> Result<()>
|
|
|
|
{
|
|
|
|
let bytes = flexbuffers::to_vec(machine)?;
|
|
|
|
txn.put(self.db, &uuid.as_bytes(), &bytes, lmdb::WriteFlags::empty())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-15 14:41:50 +02:00
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-15 14:31:10 +02:00
|
|
|
pub async fn init(log: Logger, config: &Settings) -> Result<MachinesProvider> {
|
|
|
|
unimplemented!()
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|