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()