Skip to content

Commit

Permalink
feat(debug): possibility to debug frame timing intervalls
Browse files Browse the repository at this point in the history
  • Loading branch information
tspopp committed Feb 15, 2025
1 parent 7db1d44 commit 62b5c16
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 25 deletions.
3 changes: 3 additions & 0 deletions AquaMQTT/include/buffer/FrameBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class FrameBuffer
uint64_t mCRCFailCount;
uint64_t mUnhandledCount;
uint64_t mHandledCount;

uint8_t mPreviousFrameId;
unsigned long mLastValidFrameTimestamp;
};

#endif // AQUAMQTT_FRAMEBUFFER_H
1 change: 1 addition & 0 deletions AquaMQTT/include/mqtt/MQTTDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ constexpr char STATS_ACTIVE_OVERRIDES_MAIN[] = { "activeOverridesMain" };
constexpr char STATS_ENABLE_FLAG_PV_HEATPUMP[] = { "flagPVModeHeatPump" };
constexpr char STATS_ENABLE_FLAG_PV_HEATELEMENT[] = { "flagPVModeHeatElement" };
constexpr char STATS_AQUAMQTT_PROTOCOL[] = { "protocolVersion" };
constexpr char STATS_TIMING[] = { "timing"};

constexpr char DEBUG[] = { "debug" };

Expand Down
6 changes: 6 additions & 0 deletions AquaMQTT/include/state/DHWState.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define AQUAMQTT_DHWSTATE_H

#include <Arduino.h>
#include <map>

#include "message/MessageConstants.h"

Expand Down Expand Up @@ -57,6 +58,9 @@ class DHWState
message::ProtocolVersion& version,
message::ProtocolChecksum& type);

void saveTiming(uint8_t fromFrameId, uint8_t toFrameId, unsigned long millis);
std::map<uint8_t, std::map<uint8_t, unsigned long>> getTiming() const;

private:
TaskHandle_t mNotify;

Expand All @@ -80,6 +84,8 @@ class DHWState
BufferStatistics mHmiStats;
BufferStatistics mMainStats;
BufferStatistics mListenerStats;

std::map<uint8_t, std::map<uint8_t, unsigned long>> mFrameTiming;
};

} // namespace aquamqtt
Expand Down
22 changes: 17 additions & 5 deletions AquaMQTT/src/buffer/FrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
using namespace aquamqtt;
using namespace aquamqtt::message;

FrameBuffer::FrameBuffer(bool handle194, bool handle193, bool handle67, bool handle74, bool handle217)
: mHandle194(handle194)
FrameBuffer::FrameBuffer(const bool handle194, const bool handle193,
const bool handle67,
const bool handle74,
const bool handle217)
: mLockedProtocol(PROTOCOL_UNKNOWN)
, mLockedChecksum(CHECKSUM_TYPE_UNKNOWN)
, mTransferBuffer{ 0 }
, mHandle194(handle194)
, mHandle193(handle193)
, mHandle67(handle67)
, mHandle74(handle74)
Expand All @@ -17,9 +23,7 @@ FrameBuffer::FrameBuffer(bool handle194, bool handle193, bool handle67, bool han
, mCRCFailCount(0)
, mUnhandledCount(0)
, mHandledCount(0)
, mTransferBuffer{ 0 }
, mLockedProtocol(PROTOCOL_UNKNOWN)
, mLockedChecksum(CHECKSUM_TYPE_UNKNOWN)
, mPreviousFrameId(0)
{
}

Expand Down Expand Up @@ -172,6 +176,13 @@ int FrameBuffer::handleFrame()
// completed and valid frame, move complete frame and ownership to frame handler
if (crcResult)
{
// determine protocol frequencies without signal analyzer
if (mPreviousFrameId != 0) {
DHWState::getInstance().saveTiming(mPreviousFrameId, frameId, millis() - mLastValidFrameTimestamp);
}
mPreviousFrameId = frameId;
mLastValidFrameTimestamp = millis();

// lock the protocol version, since we found a valid message and checksum is good
if (mLockedProtocol != protocolVersion)
{
Expand All @@ -192,6 +203,7 @@ int FrameBuffer::handleFrame()
{
mUnhandledCount++;
}

return frameId;
}
mCRCFailCount++;
Expand Down
58 changes: 41 additions & 17 deletions AquaMQTT/src/state/DHWState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

using namespace aquamqtt::message;

namespace aquamqtt
{

namespace aquamqtt {
DHWState& DHWState::getInstance()
{
static DHWState instance;
Expand All @@ -22,15 +20,15 @@ DHWState::DHWState()
, mHasErrorMessage(false)
, mHasExtraMessage(false)
, mMessageHmi{}
, mMessageMain{}
, mMessageEnergy{}
, mMessageError{}
, mMessageExtra{}
, mHmiStats{ 0, 0, 0, 0, 0 }
, mMainStats{ 0, 0, 0, 0, 0 }
, mListenerStats{ 0, 0, 0, 0, 0 }
, mProtocolVersion(PROTOCOL_UNKNOWN)
, mChecksumType(CHECKSUM_TYPE_UNKNOWN)
, mMessageMain{}
, mMessageEnergy{}
, mMessageError{}
, mMessageExtra{}
, mHmiStats{ 0, 0, 0, 0, 0 }
, mMainStats{ 0, 0, 0, 0, 0 }
, mListenerStats{ 0, 0, 0, 0, 0 }
, mProtocolVersion(PROTOCOL_UNKNOWN)
, mChecksumType(CHECKSUM_TYPE_UNKNOWN)
{
}

Expand Down Expand Up @@ -155,11 +153,7 @@ BufferStatistics DHWState::getFrameBufferStatistics(uint8_t source)
return statistics;
}

size_t DHWState::copyFrame(
uint8_t frameId,
uint8_t* buffer,
ProtocolVersion& version,
ProtocolChecksum& type)
size_t DHWState::copyFrame(uint8_t frameId, uint8_t* buffer, ProtocolVersion& version, ProtocolChecksum& type)
{
size_t length = 0;

Expand Down Expand Up @@ -205,6 +199,7 @@ size_t DHWState::copyFrame(
return length;
}


ProtocolVersion DHWState::getVersion()
{
message::ProtocolVersion version = PROTOCOL_UNKNOWN;
Expand Down Expand Up @@ -244,4 +239,33 @@ void DHWState::setChecksumType(ProtocolChecksum checksum)
xSemaphoreGive(mMutex);
}


void DHWState::saveTiming(const uint8_t fromFrameId, const uint8_t toFrameId, const unsigned long millis)
{
if (!xSemaphoreTake(mMutex, portMAX_DELAY))
{
return;
}

mFrameTiming[fromFrameId][toFrameId] = millis;

xSemaphoreGive(mMutex);
}

std::map<uint8_t, std::map<uint8_t, unsigned long>> DHWState::getTiming() const
{
std::map<uint8_t, std::map<uint8_t, unsigned long>> timing;
if (!xSemaphoreTake(mMutex, portMAX_DELAY))
{
return timing;
}

timing = mFrameTiming;

xSemaphoreGive(mMutex);

return timing;
}


} // namespace aquamqtt
26 changes: 23 additions & 3 deletions AquaMQTT/src/task/MQTTTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ void MQTTTask::updateStats()
publishul(STATS_SUBTOPIC, STATS_MSG_CRC_NOK, listenerStats.msgCRCFail);
publishul(STATS_SUBTOPIC, STATS_DROPPED_BYTES, listenerStats.droppedBytes);
}

// TODO: add a flag if we keep this in main branch
auto timingsMap = DHWState::getInstance().getTiming();
for (const auto& entry : timingsMap)
{
uint8_t fromId = entry.first;
for (auto innerEntry : entry.second)
{
uint8_t toid = innerEntry.first;;
unsigned long timingMillis = innerEntry.second;
sprintf(reinterpret_cast<char*>(mTopicBuffer),
"%s%s%s%s/%u-%u",
config::mqttPrefix,
BASE_TOPIC,
STATS_SUBTOPIC, STATS_TIMING,
fromId, toid);
ultoa(timingMillis, reinterpret_cast<char*>(mPayloadBuffer), 10);
mMQTTClient.publish(reinterpret_cast<char*>(mTopicBuffer), reinterpret_cast<char*>(mPayloadBuffer), false, 0);
}
}
}
else
{
Expand Down Expand Up @@ -911,10 +931,10 @@ void MQTTTask::updateHMIStatus(bool fullUpdate, message::ProtocolVersion& versio
}
}
// some heatpump protocols don't have TIME_SECONDS
else if (message->hasAttr(HMI_ATTR_U8::TIME_MINUTES)
&& message->hasAttr(HMI_ATTR_U8::TIME_HOURS))
else if (message->hasAttr(HMI_ATTR_U8::TIME_MINUTES) && message->hasAttr(HMI_ATTR_U8::TIME_HOURS))
{
if (fullUpdate || message->hasChanged(HMI_ATTR_U8::TIME_MINUTES) || message->hasChanged(HMI_ATTR_U8::TIME_HOURS))
if (fullUpdate || message->hasChanged(HMI_ATTR_U8::TIME_MINUTES)
|| message->hasChanged(HMI_ATTR_U8::TIME_HOURS))
{
sprintf(reinterpret_cast<char*>(mPayloadBuffer),
"%02d:%02d",
Expand Down

0 comments on commit 62b5c16

Please sign in to comment.