Skip to content

Commit

Permalink
Fixed clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0ps committed Sep 29, 2024
1 parent 23a9cb7 commit 343961b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
26 changes: 13 additions & 13 deletions src/inverter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::BorrowMut;
use std::mem::MaybeUninit;
use bytebuffer_new::Endian::{BigEndian, LittleEndian};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::inverter::LRI::{BatChaStt, BatTmpVal, BatAmp, BatVol, DcMsVol, DcMsAmp, MeteringTotWhOut, MeteringDyWhOut};
use crate::inverter::Lri::{BatChaStt, BatTmpVal, BatAmp, BatVol, DcMsVol, DcMsAmp, MeteringTotWhOut, MeteringDyWhOut};
use std::net::SocketAddr;

#[derive(Clone)]
Expand Down Expand Up @@ -33,7 +33,7 @@ fn gen_serial() -> u32 {
900000000 + rand::random::<u32>() % 100000000
}

pub enum LRI {
pub enum Lri {
BatChaStt = 0x00295A00, // *00* Current battery charge status
DcMsVol = 0x00451F00, // *40* DC voltage input (aka SPOT_UDC1 / SPOT_UDC2)
DcMsAmp = 0x00452100, // *40* DC current input (aka SPOT_IDC1 / SPOT_IDC2)
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Inverter {

fn write_packet(&mut self, buffer : &mut ByteBuffer, long_words : u8, control : u8, control_2 : u16)
{
self.packet_id = self.packet_id + 1;
self.packet_id += 1;
buffer.write_u32(0x65601000);

buffer.write_u8(long_words);
Expand Down Expand Up @@ -140,8 +140,8 @@ impl Inverter {
let enc_char = 0x88; //admin:0xbb
let password_bytes = password.as_bytes();

for i in 0..password_bytes.len() {
buffer.write_u8(password_bytes[i] + enc_char );
for byte in password_bytes {
buffer.write_u8(byte + enc_char );
}
for _i in password_bytes.len()..12 {
buffer.write_u8(enc_char );
Expand All @@ -160,7 +160,7 @@ impl Inverter {
}
}

let mut buf = [MaybeUninit::new(0 as u8); 500];
let mut buf = [MaybeUninit::new(0_u8); 500];
return match socket.recv_from(buf.as_mut()) {
Ok((len, remote_addr)) => {
if remote_addr.as_socket().unwrap().eq(&self.address) {
Expand Down Expand Up @@ -273,8 +273,8 @@ impl Inverter {
}
}

let mut buf = [MaybeUninit::new(0 as u8); 1024];
return match socket.recv_from(buf.as_mut()) {
let mut buf = [MaybeUninit::new(0_u8); 1024];
match socket.recv_from(buf.as_mut()) {
Ok((len, remote_addr)) => {
if remote_addr.as_socket().unwrap().eq(&self.address) {
let mut buffer = ByteBuffer::from_bytes(unsafe { self.assume_init(&buf[0..len]) } );
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Inverter {
if error_code == 0 {
buffer.read_bytes(12);

return Ok(buffer);
Ok(buffer)
}
else if error_code == 21 {
Err(InverterError { message: "Unsupported" })
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Inverter {

pub fn get_battery_charge_status(&mut self, socket: &Socket) -> Result<[u8;3], InverterError>
{
return match self.get_data(socket, &Inverter::BATTERY_CHARGE_STATUS) {
match self.get_data(socket, &Inverter::BATTERY_CHARGE_STATUS) {
Ok(mut buffer) => {

let mut battery_charge:[u8;3] = [0,0,0];
Expand Down Expand Up @@ -398,7 +398,7 @@ impl Inverter {

pub fn get_battery_info(&mut self, socket: &Socket) -> Result<BatteryInfo, InverterError>
{
return match self.get_data(socket, &Inverter::BATTERY_INFO) {
match self.get_data(socket, &Inverter::BATTERY_INFO) {
Ok(mut buffer) => {
let mut battery_info = BatteryInfo { temperature: [0,0,0], voltage: [0,0,0], current: [0,0,0] };

Expand Down Expand Up @@ -518,7 +518,7 @@ impl Inverter {

pub fn get_dc_voltage(&mut self, socket: &Socket) -> Result<DCInfo, InverterError>
{
return match self.get_data(socket, &Inverter::SPOT_DC_VOLTAGE) {
match self.get_data(socket, &Inverter::SPOT_DC_VOLTAGE) {
Ok(mut buffer) => {

let mut dc_info = DCInfo{voltage: [0,0], current: [0,0]};
Expand Down Expand Up @@ -577,7 +577,7 @@ impl Inverter {

pub fn get_energy_production(&mut self, socket: &Socket) -> Result<EnergyProductionInfo, InverterError>
{
return match self.get_data(socket, &Inverter::ENERGY_PRODUCTION) {
match self.get_data(socket, &Inverter::ENERGY_PRODUCTION) {
Ok(mut buffer) => {

let mut ep_info = EnergyProductionInfo{daily_wh: 0, total_wh: 0};
Expand Down
29 changes: 14 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn find_inverters() -> Result<Vec<Inverter>, Error> {
}

let mut inverters = Vec::new();
let mut buf = [MaybeUninit::new(0 as u8); 65];
let mut buf = [MaybeUninit::new(0_u8); 65];
match socket.set_read_timeout(Some(Duration::from_millis(100)))
{
Ok(_x) => {
Expand Down Expand Up @@ -195,16 +195,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let builder = Config::builder()
.add_source(File::with_name("/etc/sma_inverter_exporter.ini"));

let settings;
match builder.build() {
let settings= match builder.build() {
Err(error) => {
println!("Config error: {}", error);
exit(1);
}
Ok(config) => {
settings = config;
config
}
}
};

let inverters = match find_inverters() {
Ok(found_inverters) => {
Expand All @@ -218,7 +217,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

socket = initialize_socket(false);

for mut i in inverters.to_vec() {
for mut i in inverters.iter().cloned() {
let pass_key = format!("{}{}", &i.address.ip().to_string(), ".password");
let password = settings.get_string(pass_key.as_str()).unwrap_or("0000".to_string());
match i.login(&socket, password.as_str()) {
Expand All @@ -232,7 +231,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
}

counter = counter+1;
counter += 1;
if counter >= 60
{
counter = 0;
Expand All @@ -259,12 +258,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
match i.get_battery_info(&socket) {
Ok(data) => {
let _lock = LOCK.lock().unwrap();
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["A"]).set(data.temperature[0] as f64 / 10 as f64);
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["B"]).set(data.temperature[1] as f64 / 10 as f64);
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["C"]).set(data.temperature[2] as f64 / 10 as f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["A"]).set(data.voltage[0] as f64 * 10 as f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["B"]).set(data.voltage[1] as f64 * 10 as f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["C"]).set(data.voltage[2] as f64 * 10 as f64);
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["A"]).set(data.temperature[0] as f64 / 10_f64);
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["B"]).set(data.temperature[1] as f64 / 10_f64);
gauges.get(BAT_TEMPERATURE).unwrap().with_label_values(&["C"]).set(data.temperature[2] as f64 / 10_f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["A"]).set(data.voltage[0] as f64 * 10_f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["B"]).set(data.voltage[1] as f64 * 10_f64);
gauges.get(BAT_VOLTAGE).unwrap().with_label_values(&["C"]).set(data.voltage[2] as f64 * 10_f64);
gauges.get(BAT_CURRENT).unwrap().with_label_values(&["A"]).set(data.current[0] as f64);
gauges.get(BAT_CURRENT).unwrap().with_label_values(&["B"]).set(data.current[1] as f64);
gauges.get(BAT_CURRENT).unwrap().with_label_values(&["C"]).set(data.current[2] as f64);
Expand All @@ -280,8 +279,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(data) => {
gauges.get(DC_CURRENT).unwrap().with_label_values(&["1"]).set(data.current[0] as f64);
gauges.get(DC_CURRENT).unwrap().with_label_values(&["2"]).set(data.current[1] as f64);
gauges.get(DC_VOLTAGE).unwrap().with_label_values(&["1"]).set(data.voltage[0] as f64 * 10 as f64);
gauges.get(DC_VOLTAGE).unwrap().with_label_values(&["2"]).set(data.voltage[1] as f64 * 10 as f64);
gauges.get(DC_VOLTAGE).unwrap().with_label_values(&["1"]).set(data.voltage[0] as f64 * 10_f64);
gauges.get(DC_VOLTAGE).unwrap().with_label_values(&["2"]).set(data.voltage[1] as f64 * 10_f64);
}
Err(inverter_error) => {
if inverter_error.message.ne("Unsupported")
Expand Down

0 comments on commit 343961b

Please sign in to comment.