nlabapi/scope/
power.rs

1/***************************************************************************************************
2 *
3 *  nLabs, LLC
4 *  https://getnlab.com
5 *  Copyright(c) 2020. All Rights Reserved
6 *
7 *  This file is part of the nLab API
8 *
9 **************************************************************************************************/
10
11use std::io;
12#[cfg(feature = "python_support")]
13use pyo3::{pyclass, pymethods};
14use super::Nlab;
15
16/// Information about the power supply status of nLab
17#[derive(Debug, Copy, Clone)]
18#[cfg_attr(feature = "python_support", pyclass(get_all))]
19pub struct PowerStatus {
20    pub state: PowerState,
21    pub usage: f64,
22}
23
24impl Default for PowerStatus {
25    fn default() -> Self {
26        PowerStatus {
27            state: PowerState::Unknown,
28            usage: 0.0,
29        }
30    }
31}
32
33#[cfg_attr(feature = "python_support", pymethods)]
34impl PowerStatus {
35    fn __repr__(&self) -> String {
36        format!("{self:?}")
37    }
38}
39
40/// Possible states of the nLab power supply
41#[derive(Debug, PartialEq, Copy, Clone)]
42#[cfg_attr(feature = "python_support", pyclass(eq, eq_int))]
43pub enum PowerState {
44    PowerOff,
45    PowerOn,
46    Shorted,
47    Overcurrent,
48    Startup,
49    Unknown,
50}
51
52impl From<u8> for PowerState {
53    fn from(orig: u8) -> Self {
54        match orig {
55            0 => PowerState::PowerOff,
56            1 => PowerState::PowerOn,
57            2 => PowerState::Shorted,
58            3 => PowerState::Overcurrent,
59            4 => PowerState::Startup,
60            _ => PowerState::Unknown,
61        }
62    }
63}
64
65impl Nlab {
66    pub fn power_status(&self) -> Result<PowerStatus, io::Error> {
67        if !self.is_connected() {
68            return Err(io::Error::new(
69                io::ErrorKind::ConnectionAborted,
70                "nLab connection aborted",
71            ));
72        }
73        Ok(*self.power_status.read().unwrap())
74    }
75}