Feature: SML power meters: reset SML decoder

implement a function which allows to reset the SML decoder. this new
function is used after a datagram ends. for the SML HTTP power meter
this is simple: after all bytes from the request's answer have been
decoded, we reset the decoder. for the SML serial power meter, we
perform the reset after a datagram ended based on timing (no new bytes
have been received for a specific amount of time).
This commit is contained in:
Bernhard Kirchen 2024-06-17 21:02:04 +02:00
parent ea454972b9
commit 6b09ca056e
6 changed files with 20 additions and 2 deletions

View File

@ -20,6 +20,7 @@ protected:
explicit PowerMeterSml(char const* user)
: _user(user) { }
void reset();
void processSmlByte(uint8_t byte);
private:

View File

@ -189,6 +189,12 @@ void checkMagicByte(unsigned char &byte)
}
}
void smlReset(void)
{
len = 4; // expect start sequence
currentState = SML_START;
}
sml_states_t smlState(unsigned char currentByte)
{
unsigned char size;

View File

@ -92,6 +92,7 @@ typedef enum {
SML_COUNT = 255
} sml_units_t;
void smlReset(void);
sml_states_t smlState(unsigned char byte);
bool smlOBISCheck(const unsigned char *obis);
void smlOBISManufacturer(unsigned char *str, int maxSize);

View File

@ -107,5 +107,7 @@ String PowerMeterHttpSml::poll()
processSmlByte(pStream->read());
}
PowerMeterSml::reset();
return "";
}

View File

@ -110,6 +110,8 @@ void PowerMeterSerialSml::pollingLoop()
lastAvailable = 0;
PowerMeterSml::reset();
lock.lock();
}
}

View File

@ -30,6 +30,12 @@ void PowerMeterSml::doMqttPublish() const
#undef PUB
}
void PowerMeterSml::reset()
{
smlReset();
_cache = { std::nullopt };
}
void PowerMeterSml::processSmlByte(uint8_t byte)
{
switch (smlState(byte)) {
@ -52,12 +58,12 @@ void PowerMeterSml::processSmlByte(uint8_t byte)
case SML_FINAL:
gotUpdate();
_values = _cache;
_cache = { std::nullopt };
reset();
MessageOutput.printf("[%s] TotalPower: %5.2f\r\n",
_user.c_str(), getPowerTotal());
break;
case SML_CHECKSUM_ERROR:
_cache = { std::nullopt };
reset();
MessageOutput.printf("[%s] checksum verification failed\r\n",
_user.c_str());
break;