1
0
Fork 0
refactor
Bo Jeanes 2022-09-09 16:38:24 +10:00
parent c8139038f7
commit 6c01508457
1 changed files with 6 additions and 8 deletions

View File

@ -49,9 +49,7 @@ impl Monitor {
.await .await
.unwrap(); .unwrap();
let value = self let value = self.register.parse_words(&words);
.register
.parse_words(&self.register.apply_swaps(&words));
self.mqtt self.mqtt
.publish("state", serde_json::to_vec(&value).unwrap()) .publish("state", serde_json::to_vec(&value).unwrap())
@ -504,10 +502,10 @@ impl Register {
} }
pub fn parse_words(&self, words: &[u16]) -> serde_json::Value { pub fn parse_words(&self, words: &[u16]) -> serde_json::Value {
self.parse.value_type.parse_words(words) self.parse.value_type.parse_words(&self.apply_swaps(words))
} }
pub fn apply_swaps(&self, words: &[u16]) -> Vec<u16> { fn apply_swaps(&self, words: &[u16]) -> Vec<u16> {
let words: Vec<u16> = if self.parse.swap_bytes.0 { let words: Vec<u16> = if self.parse.swap_bytes.0 {
words.iter().map(|v| v.swap_bytes()).collect() words.iter().map(|v| v.swap_bytes()).collect()
} else { } else {
@ -532,14 +530,14 @@ use crate::mqtt::{self, Payload};
fn test_parse_1() { fn test_parse_1() {
use serde_json::json; use serde_json::json;
Register { let reg = Register {
name: None, name: None,
interval: Default::default(), interval: Default::default(),
parse: RegisterParse { parse: RegisterParse {
swap_bytes: Swap(false), swap_bytes: Swap(false),
swap_words: Swap(false), swap_words: Swap(true),
value_type: RegisterValueType::Numeric { value_type: RegisterValueType::Numeric {
of: RegisterNumeric::I32, of: RegisterNumeric::U32,
adjust: RegisterNumericAdjustment { adjust: RegisterNumericAdjustment {
scale: 0, scale: 0,
offset: 0, offset: 0,