Skip to content

Commit

Permalink
Rename DeviceInfo to DeviceMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
PoorRican committed Jan 27, 2023
1 parent bce418f commit 8caeb1f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
15 changes: 7 additions & 8 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::container::{Container, Containerized};

/// Basic interface for GPIO device metadata
pub trait Device<T> {
fn get_info(&self) -> &DeviceInfo<T>;
fn get_metadata(&self) -> &DeviceMetadata<T>;
fn name(&self) -> String;
fn id(&self) -> i32;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ pub trait Sensor<T>: Device<T> {

impl<T> std::fmt::Debug for dyn Sensor<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Sensor {{ name: {}, id: {}, info: {}", self.name(), self.id(), self.get_info())
write!(f, "Sensor {{ name: {}, id: {}, info: {}", self.name(), self.id(), self.get_metadata())
}
}

Expand Down Expand Up @@ -86,8 +86,7 @@ pub trait Calibrated {
/// let info = crate::DeviceInfo::new(name, version_id, sensor_id, kind, min_value, max_value, resolution, min_delay);
/// ```
#[derive(Debug, Clone, PartialEq)]
// TODO: rename to `DeviceMetadata`
pub struct DeviceInfo<T> {
pub struct DeviceMetadata<T> {
// TODO: what changes should be made? Dedicated struct for number space? Should `min_delay` be moved to `Device`?
pub name: String,
pub version_id: i32,
Expand All @@ -101,7 +100,7 @@ pub struct DeviceInfo<T> {
min_delay: Duration,
}

impl<T> DeviceInfo<T> {
impl<T> DeviceMetadata<T> {
/// Creates a new instance of `DeviceInfo`
///
/// # Arguments
Expand All @@ -120,15 +119,15 @@ impl<T> DeviceInfo<T> {
/// A new instance with given specified parameters
pub fn new(name: String, version_id: i32, sensor_id: i32, kind: io::IOKind,
min_value: T, max_value: T, resolution: T, min_delay: Duration) -> Self {
DeviceInfo {
DeviceMetadata {
name, version_id, sensor_id, kind,
min_value, max_value, resolution,
min_delay,
}
}
}

impl<T> std::fmt::Display for DeviceInfo<T> {
impl<T> std::fmt::Display for DeviceMetadata<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Device Info {{ Kind: {}, Min. Delay: {} }}", self.kind, self.min_delay.to_string())
}
Expand All @@ -139,7 +138,7 @@ impl<T> std::fmt::Display for DeviceInfo<T> {
/// Objects are stored as `Box<dyn Sensor<T>>`
impl<T, K> Containerized<Box<dyn Sensor<T>>, K> for dyn Sensor<T>
where T: std::fmt::Debug,
K: std::hash::Hash
K: std::hash::Hash + Eq
{
fn container() -> Container<Box<dyn Sensor<T>>, K> {
Container::<Box<dyn Sensor<T>>, K>::new()
Expand Down
4 changes: 2 additions & 2 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::device;
use crate::container::{Container, Containerized};

/// Defines sensor type. Used to classify data along with `IOData`.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IOKind {
Light,
Pressure,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<T> IOEvent<T> {
///
/// ```
pub fn create( device: &impl device::Device<T>, timestamp: DateTime<Utc>, value: T ) -> Self {
let info = &device.get_info();
let info = device.get_metadata();
let version_id = info.version_id;
let sensor_id = info.sensor_id;
let data = IOData {
Expand Down
18 changes: 9 additions & 9 deletions src/sensors/ph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{device, io, units::Ph};

#[derive(Debug, Clone)]
pub struct MockPhSensor {
info: device::DeviceInfo<Ph>
metadata: device::DeviceMetadata<Ph>
}

/** Represents a mock pH sensor.
Expand All @@ -26,27 +26,27 @@ impl MockPhSensor {
let max_value = Ph(14.0);
let resolution = Ph(0.1);

let info: device::DeviceInfo<Ph> = device::DeviceInfo::new(name, version_id, sensor_id,
kind, min_value, max_value, resolution,
min_delay);
let metadata: device::DeviceMetadata<Ph> = device::DeviceMetadata::new(name, version_id, sensor_id,
kind, min_value, max_value, resolution,
min_delay);

MockPhSensor {
info
metadata
}
}
}


// Implement traits
impl device::Device<Ph> for MockPhSensor {
fn get_info(&self) -> &device::DeviceInfo<Ph> {
&self.info
fn get_metadata(&self) -> &device::DeviceMetadata<Ph> {
&self.metadata
}
fn name(&self) -> String {
self.info.name.clone()
self.metadata.name.clone()
}
fn id(&self) -> i32 {
self.info.sensor_id
self.metadata.sensor_id
}
}

Expand Down

0 comments on commit 8caeb1f

Please sign in to comment.