Browse Source

sns: more lenient eneTotal parser

mcspr-patch-1
Maxim Prokhorov 3 years ago
parent
commit
54f51ad2cd
1 changed files with 26 additions and 6 deletions
  1. +26
    -6
      code/espurna/sensor.cpp

+ 26
- 6
code/espurna/sensor.cpp View File

@ -428,14 +428,34 @@ void _sensorRtcmemSaveEnergy(unsigned char index, const sensor::Energy& source)
sensor::Energy _sensorParseEnergy(const String& value) { sensor::Energy _sensorParseEnergy(const String& value) {
sensor::Energy result; 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; return result;
} }


Loading…
Cancel
Save