ha-mqtt/src/esp.h

47 lines
1.7 KiB
C++

#pragma once
#include "TaskScheduler.h"
#include "ha.h"
using namespace Ha;
namespace HaESP {
Task tHeap(1 * TASK_MINUTE, TASK_FOREVER, []() {
Sensor::mapSensors["heap_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).c_str());
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
}, &ts);
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, [](){
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
}, &ts);
template <class T>
Builder<T>& heapStats(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" })
.withUnitMeasure("%")
.withPrecision(0)
.build());
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
.withDeviceClass("data_size")
.withUnitMeasure("B")
.withPrecision(0)
.build()
);
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap max free block", "heap_max_free_block" })
.withDeviceClass("data_size")
.withUnitMeasure("B")
.withPrecision(0)
.build()
);
tHeap.enable();
return builder;
}
template <class T>
Builder<T>& restartInfo(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{"Restart reason", "restart_reason"})).build());
tRestartInfo.enable();
return builder;
}
}