read heap stats once and put them in both sensors and json attributes
This commit is contained in:
parent
df1b900b5d
commit
138c7b5cd8
19
src/esp.h
19
src/esp.h
@ -12,9 +12,16 @@ namespace HaESP {
|
|||||||
} activeSensors;
|
} activeSensors;
|
||||||
|
|
||||||
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||||
Sensor::mapSensors["heap_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
|
uint32_t hfree;
|
||||||
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).c_str());
|
uint32_t hmax;
|
||||||
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
|
uint8_t hfrag;
|
||||||
|
ESP.getHeapStats(&hfree, &hmax, &hfrag);
|
||||||
|
|
||||||
|
char value[256];
|
||||||
|
sprintf(value, "{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}", hfrag, hfree, hmax);
|
||||||
|
Sensor::mapSensors["heap_fragmentation"]->updateState(value);
|
||||||
|
Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
|
||||||
|
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());
|
||||||
}, &ts);
|
}, &ts);
|
||||||
|
|
||||||
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||||
@ -23,9 +30,13 @@ namespace HaESP {
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Builder<T>& heapStats(Builder<T>& builder) {
|
Builder<T>& heapStats(Builder<T>& builder) {
|
||||||
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" })
|
auto heap = new Sensor{ "Heap fragmentation", "heap_fragmentation" };
|
||||||
|
builder.addDiagnostic(Builder<Sensor>::instance(heap)
|
||||||
.withUnitMeasure("%")
|
.withUnitMeasure("%")
|
||||||
.withPrecision(0)
|
.withPrecision(0)
|
||||||
|
.withValueTemplate("{{ value_json.fragmentation }}")
|
||||||
|
.overrideConfig("json_attributes_topic", heap->stateTopic)
|
||||||
|
.overrideConfig("json_attributes_template", "{{ value_json.heap | tojson }}")
|
||||||
.build());
|
.build());
|
||||||
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
||||||
.withDeviceClass("data_size")
|
.withDeviceClass("data_size")
|
||||||
|
|||||||
5
src/ha.h
5
src/ha.h
@ -205,8 +205,7 @@ namespace Ha {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename V>
|
Builder& overrideConfig(const char* key, float value) {
|
||||||
Builder& overrideConfig(const char* key, V value) {
|
|
||||||
cmp->jsonNumbers.add({key, value});
|
cmp->jsonNumbers.add({key, value});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -293,6 +292,7 @@ namespace Ha {
|
|||||||
|
|
||||||
struct StateConfig {
|
struct StateConfig {
|
||||||
static unordered_map<string, Command*> mapStateTopics;
|
static unordered_map<string, Command*> mapStateTopics;
|
||||||
|
char stateTopic[TOPIC_SIZE];
|
||||||
|
|
||||||
StateConfig(Component* cmp) : cmp(cmp) {}
|
StateConfig(Component* cmp) : cmp(cmp) {}
|
||||||
|
|
||||||
@ -305,7 +305,6 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char stateTopic[TOPIC_SIZE];
|
|
||||||
Component* cmp;
|
Component* cmp;
|
||||||
void buildConfig(JsonDocument& jsonDoc) {
|
void buildConfig(JsonDocument& jsonDoc) {
|
||||||
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user