forked from viamrobotics/micro-rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RDSK-1750 Add sensor support and implement a moisture sensor
- Loading branch information
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::common::analog::AnalogReader; | ||
use crate::common::sensor::GenericReadingsResult; | ||
use crate::common::sensor::Sensor; | ||
use crate::common::sensor::SensorResult; | ||
use crate::common::sensor::SensorT; | ||
use crate::common::sensor::TypedReadingsResult; | ||
use crate::common::status::Status; | ||
use std::cell::RefCell; | ||
use std::collections::BTreeMap; | ||
use std::collections::HashMap; | ||
use std::rc::Rc; | ||
|
||
pub struct MoistureSensor { | ||
analog: Rc<RefCell<dyn AnalogReader<u16, Error = anyhow::Error>>>, | ||
} | ||
|
||
impl MoistureSensor { | ||
pub fn new(analog: Rc<RefCell<dyn AnalogReader<u16, Error = anyhow::Error>>>) -> Self { | ||
MoistureSensor { analog } | ||
} | ||
} | ||
|
||
impl Sensor for MoistureSensor { | ||
fn get_generic_readings(&self) -> anyhow::Result<GenericReadingsResult> { | ||
Ok(self | ||
.get_readings()? | ||
.into_iter() | ||
.map(|v| (v.0, SensorResult::<f64> { value: v.1 }.into())) | ||
.collect()) | ||
} | ||
} | ||
|
||
impl SensorT<f64> for MoistureSensor { | ||
fn get_readings(&self) -> anyhow::Result<TypedReadingsResult<f64>> { | ||
let reading = self.analog.borrow_mut().read()?; | ||
let mut x = HashMap::new(); | ||
x.insert("millivolts".to_string(), reading as f64); | ||
Ok(x) | ||
} | ||
} | ||
|
||
impl Status for MoistureSensor { | ||
fn get_status(&self) -> anyhow::Result<Option<prost_types::Struct>> { | ||
Ok(Some(prost_types::Struct { | ||
fields: BTreeMap::new(), | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#![allow(dead_code)] | ||
|
||
use crate::common::status::Status; | ||
use std::collections::BTreeMap; | ||
use std::collections::HashMap; | ||
|
||
pub type GenericReadingsResult = | ||
::std::collections::HashMap<::prost::alloc::string::String, ::prost_types::Value>; | ||
|
||
pub type TypedReadingsResult<T> = ::std::collections::HashMap<String, T>; | ||
|
||
pub trait Sensor: Status { | ||
fn get_generic_readings(&self) -> anyhow::Result<GenericReadingsResult>; | ||
} | ||
|
||
pub trait SensorT<T>: Sensor { | ||
fn get_readings(&self) -> anyhow::Result<TypedReadingsResult<T>>; | ||
} | ||
|
||
// A local wrapper type we can use to specialize `From` for `prost_types::Value`` | ||
pub struct SensorResult<T> { | ||
pub value: T, | ||
} | ||
|
||
impl From<SensorResult<f64>> for ::prost_types::Value { | ||
fn from(value: SensorResult<f64>) -> ::prost_types::Value { | ||
prost_types::Value { | ||
kind: Some(::prost_types::value::Kind::NumberValue(value.value)), | ||
} | ||
} | ||
} | ||
|
||
pub struct FakeSensor { | ||
fake_reading: f64, | ||
} | ||
|
||
impl FakeSensor { | ||
pub fn new() -> Self { | ||
FakeSensor { | ||
fake_reading: 42.42, | ||
} | ||
} | ||
} | ||
|
||
impl Default for FakeSensor { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Sensor for FakeSensor { | ||
fn get_generic_readings(&self) -> anyhow::Result<GenericReadingsResult> { | ||
Ok(self | ||
.get_readings()? | ||
.into_iter() | ||
.map(|v| (v.0, SensorResult::<f64> { value: v.1 }.into())) | ||
.collect()) | ||
} | ||
} | ||
|
||
impl SensorT<f64> for FakeSensor { | ||
fn get_readings(&self) -> anyhow::Result<TypedReadingsResult<f64>> { | ||
let mut x = HashMap::new(); | ||
x.insert("fake_sensor".to_string(), self.fake_reading); | ||
Ok(x) | ||
} | ||
} | ||
|
||
impl Status for FakeSensor { | ||
fn get_status(&self) -> anyhow::Result<Option<prost_types::Struct>> { | ||
Ok(Some(prost_types::Struct { | ||
fields: BTreeMap::new(), | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters