From 0b9840669e3e886edc0a21d67818cf2fa4446711 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Mon, 17 Oct 2022 18:51:05 +0100 Subject: [PATCH] lol --- src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.rs b/src/main.rs index f5c4647..33f4f51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ enum Error { JSONError(serde_json::Error), MQTTError(mqtt::Error), IoError(std::io::Error), + MyError, } impl From for Error { @@ -140,6 +141,11 @@ impl TryFrom for MQTTData { type Error = Error; fn try_from(s: SolaxResponse) -> Result { + // On my inverter it's always exactly 100 entries, but avoid panics + if s.data.len() < 56 { + return Err(Error::MyError); + } + let status = s.data[10]; let inverter_data = InverterData { @@ -209,6 +215,11 @@ fn mqtt_connect(uri: String, client_id: String) -> Result Result { let body = format!("optType=ReadRealTimeData&pwd={}", password); let response = ureq::post(uri).send_string(&body)?; + + if response.status() < 200 || response.status() > 299 { + return Err(Error::MyError); + } + let solax_response: SolaxResponse = response.into_json()?; solax_response.try_into()