From 54f51ad2cd99b7efee728c627e0baab8cbe7c545 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 24 Dec 2020 16:19:24 +0300 Subject: [PATCH] sns: more lenient eneTotal parser --- code/espurna/sensor.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index b1ec9642..e4525a74 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -428,14 +428,34 @@ void _sensorRtcmemSaveEnergy(unsigned char index, const sensor::Energy& source) sensor::Energy _sensorParseEnergy(const String& value) { sensor::Energy result; + if (!value.length()) { + return result; + } + + const char* p { value.c_str() }; + + char* endp { nullptr }; + auto kwh = strtoul(p, &endp, 10); + if (!endp || (endp == p)) { + return result; + } + result.kwh = kwh; - const bool separator = value.indexOf('+') > 0; - if (value.length() && (separator > 0)) { - const String before = value.substring(0, separator); - const String after = value.substring(separator + 1); - result.kwh = strtoul(before.c_str(), nullptr, 10); - result.ws = strtoul(after.c_str(), nullptr, 10); + const char* plus { strchr(p, '+') }; + if (!plus) { + return; + } + + p = plus + 1; + if (*p == '\0') { + return result; + } + + auto ws = strtoul(p, &endp, 10); + if (!endp || (endp == p)) { + return result; } + result.ws = ws; return result; }