summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2018-05-30 17:01:41 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2018-05-30 17:01:41 +0100
commitf3781dc7a7b72f404702cc1e7d10d24c838a6696 (patch)
tree434e231023f93f67d24c38c75cda8f333ca2a5cb
parente44ecc96fb79a3aea3cbabbcf9797fb7b21530c6 (diff)
downloadcanopied-f3781dc7a7b72f404702cc1e7d10d24c838a6696.tar.bz2
Support reporting changes in derived values
-rw-r--r--definitions/derived.yaml4
-rw-r--r--src/definitions.rs34
-rw-r--r--src/main.rs21
-rw-r--r--src/qvalue.rs14
4 files changed, 62 insertions, 11 deletions
diff --git a/definitions/derived.yaml b/definitions/derived.yaml
index 13b7946..a9b7733 100644
--- a/definitions/derived.yaml
+++ b/definitions/derived.yaml
@@ -13,11 +13,11 @@
- id: main_voltage
display: volt
- expr: bms1.battery_voltage * volt
+ expr: (bms1.battery_voltage / 10) * volt
- id: main_current
display: amp
- expr: bms1.battery_current * amp
+ expr: (bms1.battery_current / 10) * amp
- id: main_power
display: kw
diff --git a/src/definitions.rs b/src/definitions.rs
index ea6319a..84703f9 100644
--- a/src/definitions.rs
+++ b/src/definitions.rs
@@ -363,6 +363,16 @@ impl Units {
pub fn find(&self, unit: &str) -> Option<QValue> {
self.all.get(unit).and_then(|i| Some(self.qvals[*i]))
}
+
+ pub fn render(&self, unit: &str, value: QValue) -> String {
+ if let Some(idx) = self.all.get(unit) {
+ let scal = (value / self.qvals[*idx]).value();
+ format!("{} {}", scal, self.raw[*idx].suffix)
+ } else {
+ // Unable to find the unit, so render rawish?
+ format!("{} ({}?)", value.value(), unit)
+ }
+ }
}
#[derive(Debug, Deserialize)]
@@ -438,4 +448,28 @@ impl Derived {
pub fn count(&self) -> usize {
self.raw.len()
}
+
+ pub fn update(&self, symbols: &mut HashMap<String, QValue>) {
+ for (raw, expr) in self.raw.iter().zip(self.expr.iter()) {
+ if let Some(val) = qexpr::evaluate(expr, symbols) {
+ if let Some(curval) = symbols.get(&raw.id) {
+ // updating
+ if val != *curval {
+ info!(
+ "{} is now {}",
+ raw.id,
+ Units::get().render(&raw.display, val)
+ );
+ }
+ } else {
+ info!(
+ "{} is now {}",
+ raw.id,
+ Units::get().render(&raw.display, val)
+ );
+ }
+ symbols.insert(raw.id.clone(), val);
+ }
+ }
+ }
}
diff --git a/src/main.rs b/src/main.rs
index 8daac6c..f9e91a3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,7 +37,7 @@ use tokio::prelude::*;
use tokio::timer::Interval;
use definitions::CadencedECUs;
-use types::Value;
+use qvalue::*;
fn packet_print(id: u16, bytes: &[u8]) -> String {
let mut ret: String = String::new();
@@ -105,7 +105,9 @@ fn main() {
}))
}));
- let mut current: Mutex<HashMap<String, Value>> = Mutex::new(HashMap::new());
+ let mut currentraw: Mutex<HashMap<String, types::Value>> = Mutex::new(HashMap::new());
+ let mut currentsym: Mutex<HashMap<String, QValue>> = Mutex::new(HashMap::new());
+ units.populate(&mut currentsym.get_mut().unwrap());
tokio::spawn(
stream
@@ -119,22 +121,23 @@ fn main() {
if let Some(ecu) = ecu {
if let Some(decoded) = ecu.decode(&frame.1) {
trace!("Decoded: {:?}", decoded);
- let curmap = current.get_mut().unwrap();
+ let curmap = currentraw.get_mut().unwrap();
+ let cursym = currentsym.get_mut().unwrap();
for entry in decoded {
let name = entry.get_name().to_owned();
- if !name.contains("speed") {
- continue;
- }
let value = entry.get_value().unwrap();
match curmap.entry(name.clone()) {
Entry::Occupied(o) => if value != *o.get() {
- info!("{} changed {} -> {}", name, o.get(), value);
+ trace!("{} changed {} -> {}", name, o.get(), value);
},
Entry::Vacant(_) => {
- info!("{} changed None -> {}", name, value);
+ trace!("{} changed None -> {}", name, value);
}
};
- curmap.insert(name, value);
+ curmap.insert(name.clone(), value);
+ let qv = QValue::from(value);
+ cursym.insert(name, qv);
+ derived.update(cursym);
}
}
} else {
diff --git a/src/qvalue.rs b/src/qvalue.rs
index 85d7d32..095dce1 100644
--- a/src/qvalue.rs
+++ b/src/qvalue.rs
@@ -1,5 +1,7 @@
use std::ops;
+use types::Value;
+
#[derive(Eq, PartialEq, PartialOrd, Copy, Clone)]
pub enum BaseQuantity {
Voltage,
@@ -164,6 +166,18 @@ impl ops::Div for QValue {
}
}
+impl From<Value> for QValue {
+ fn from(v: Value) -> QValue {
+ match v {
+ Value::Boolean(b) => QValue::new(if b { 1.0 } else { 0.0 }),
+ Value::Unsigned(u) => QValue::new(u as f64),
+ Value::Signed(s) => QValue::new(s as f64),
+ Value::Floating(f) => QValue::new(f as f64),
+ Value::RawHex(v) => QValue::new(v as f64),
+ }
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;