1
0
Fork 0

Pass with clippy

gh-action
Bo Jeanes 2022-08-18 09:28:43 +10:00
parent 9e421b91b3
commit c300575dc8
3 changed files with 15 additions and 32 deletions

View File

@ -4,7 +4,7 @@ use serde_json::json;
use std::{collections::HashMap, time::Duration}; use std::{collections::HashMap, time::Duration};
use tokio::{sync::mpsc, sync::oneshot, time::MissedTickBehavior}; use tokio::{sync::mpsc, sync::oneshot, time::MissedTickBehavior};
use tokio_modbus::prelude::*; use tokio_modbus::prelude::*;
use tracing::{debug, error, info, span, warn, Level}; use tracing::{debug, error, info};
use clap::Parser; use clap::Parser;
@ -44,8 +44,8 @@ async fn main() {
let args = Cli::parse(); let args = Cli::parse();
let (registry_tx, mut registry_rx) = mpsc::channel::<RegistryCommand>(32); let (registry_tx, registry_rx) = mpsc::channel::<RegistryCommand>(32);
let (dispatcher_tx, mut dispatcher_rx) = mpsc::channel::<DispatchCommand>(32); let (dispatcher_tx, dispatcher_rx) = mpsc::channel::<DispatchCommand>(32);
// Modbus connection registry // Modbus connection registry
let registry_handle = { let registry_handle = {
@ -86,7 +86,7 @@ async fn mqtt_dispatcher(
info!("Connecting to MQTT broker..."); info!("Connecting to MQTT broker...");
options.set_last_will(LastWill { options.set_last_will(LastWill {
topic: format!("{}/status", prefix).to_string(), topic: format!("{}/status", prefix),
message: serde_json::to_vec(&json!({ message: serde_json::to_vec(&json!({
"status": MainStatus::Stopped, "status": MainStatus::Stopped,
})) }))
@ -100,7 +100,7 @@ async fn mqtt_dispatcher(
client client
.publish( .publish(
format!("{}/status", prefix).to_string(), format!("{}/status", prefix),
QoS::AtMostOnce, QoS::AtMostOnce,
false, false,
serde_json::to_vec(&json!({ serde_json::to_vec(&json!({
@ -344,7 +344,7 @@ async fn handle_connect(
topic: format!("{}/status/{}", topic_prefix, id), topic: format!("{}/status/{}", topic_prefix, id),
payload: serde_json::to_vec(&json!({ payload: serde_json::to_vec(&json!({
"status": ConnectState::Errored, "status": ConnectState::Errored,
"error": format!("Invalid config: {}", err.to_string()), "error": format!("Invalid config: {}", err),
})) }))
.unwrap(), .unwrap(),
}) })
@ -396,7 +396,7 @@ async fn watch_registers(
let swapped_words = r.apply_swaps(&words); let swapped_words = r.apply_swaps(&words);
let value = r.from_words(&swapped_words); let value = r.parse_words(&swapped_words);
debug!( debug!(
name = r.name.as_ref().unwrap_or(&"".to_string()), name = r.name.as_ref().unwrap_or(&"".to_string()),
@ -420,7 +420,7 @@ async fn watch_registers(
dispatcher dispatcher
.send(DispatchCommand::Publish { .send(DispatchCommand::Publish {
topic: format!("{}/{}", registers_prefix, name), topic: format!("{}/{}", registers_prefix, name),
payload: payload, payload,
}) })
.await .await
.unwrap(); .unwrap();

View File

@ -53,7 +53,7 @@ fn default_modbus_parity() -> tokio_serial::Parity {
tokio_serial::Parity::None tokio_serial::Parity::None
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", default)] #[serde(rename_all = "lowercase", default)]
pub struct RegisterNumericAdjustment { pub struct RegisterNumericAdjustment {
pub scale: i8, // powers of 10 (0 = no adjustment, 1 = x10, -1 = /10) pub scale: i8, // powers of 10 (0 = no adjustment, 1 = x10, -1 = /10)
@ -61,15 +61,6 @@ pub struct RegisterNumericAdjustment {
// precision: Option<u8>, // precision: Option<u8>,
} }
impl Default for RegisterNumericAdjustment {
fn default() -> Self {
Self {
scale: 0,
offset: 0,
}
}
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum RegisterNumeric { pub enum RegisterNumeric {
@ -185,16 +176,10 @@ impl RegisterValueType {
} }
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct Swap(pub bool); pub struct Swap(pub bool);
impl Default for Swap {
fn default() -> Self {
Self(false)
}
}
trait IsDefault { trait IsDefault {
fn is_default(&self) -> bool; fn is_default(&self) -> bool;
} }

View File

@ -1,8 +1,6 @@
use rust_decimal::{prelude::FromPrimitive, Decimal}; use rust_decimal::{prelude::FromPrimitive, Decimal};
use serde::Serialize; use serde::Serialize;
use crate::modbus::config::RegisterNumericAdjustment;
use self::config::{Register, RegisterValueType}; use self::config::{Register, RegisterValueType};
pub mod config; pub mod config;
@ -26,7 +24,7 @@ pub type UnitId = tokio_modbus::prelude::SlaveId;
pub type Unit = tokio_modbus::prelude::Slave; pub type Unit = tokio_modbus::prelude::Slave;
impl RegisterValueType { impl RegisterValueType {
pub fn from_words(&self, words: &[u16]) -> serde_json::Value { pub fn parse_words(&self, words: &[u16]) -> serde_json::Value {
use self::config::RegisterValueType as T; use self::config::RegisterValueType as T;
use self::config::{RegisterArray, RegisterNumeric as N, RegisterString}; use self::config::{RegisterArray, RegisterNumeric as N, RegisterString};
use serde_json::json; use serde_json::json;
@ -96,7 +94,7 @@ impl RegisterValueType {
} }
} }
T::String(RegisterString { .. }) => { T::String(RegisterString { .. }) => {
json!(String::from_utf16_lossy(&words)) json!(String::from_utf16_lossy(words))
} }
T::Array(RegisterArray { .. }) => todo!(), T::Array(RegisterArray { .. }) => todo!(),
} }
@ -104,8 +102,8 @@ impl RegisterValueType {
} }
impl Register { impl Register {
pub fn from_words(&self, words: &[u16]) -> serde_json::Value { pub fn parse_words(&self, words: &[u16]) -> serde_json::Value {
self.parse.value_type.from_words(words) self.parse.value_type.parse_words(words)
} }
pub fn apply_swaps(&self, words: &[u16]) -> Vec<u16> { pub fn apply_swaps(&self, words: &[u16]) -> Vec<u16> {
@ -148,5 +146,5 @@ fn test_parse_1() {
}, },
}; };
assert_eq!(reg.from_words(&vec![843, 0]), json!(843)); assert_eq!(reg.parse_words(&vec![843, 0]), json!(843));
} }