145 lines
3.2 KiB
C++
145 lines
3.2 KiB
C++
#include "SmartVariable.hpp"
|
|
SmartVariable::SmartVariable()
|
|
{
|
|
}
|
|
|
|
SmartVariable::~SmartVariable()
|
|
{
|
|
if (this->value != nullptr)
|
|
free(this->value);
|
|
}
|
|
|
|
void SmartVariable::begin(size_t size)
|
|
{
|
|
this->value = (char *)calloc(size, sizeof(char));
|
|
this->size = size;
|
|
}
|
|
|
|
void SmartVariable::enableIoT(ESPMegaIoT *iot, const char *topic)
|
|
{
|
|
bool iotEnabled = true;
|
|
auto bindedMqttCallback = std::bind(&SmartVariable::handleMqttCallback, this, std::placeholders::_1, std::placeholders::_2);
|
|
this->iot->registerRelativeMqttCallback(bindedMqttCallback);
|
|
this->subscribeMqtt();
|
|
}
|
|
|
|
void SmartVariable::enableValueRequest(const char *valueRequestTopic)
|
|
{
|
|
this->useValueRequest = true;
|
|
this->valueRequestTopic = valueRequestTopic;
|
|
this->subscribeMqtt();
|
|
}
|
|
|
|
void SmartVariable::setValue(const char *value)
|
|
{
|
|
strncpy(this->value, value, this->size - 1);
|
|
this->value[this->size - 1] = '\0';
|
|
if (this->autoSave)
|
|
this->saveValue();
|
|
if (this->iotEnabled)
|
|
this->publishValue();
|
|
}
|
|
|
|
char *SmartVariable::getValue()
|
|
{
|
|
return this->value;
|
|
}
|
|
|
|
void SmartVariable::enableSetValue(const char *setValueTopic)
|
|
{
|
|
this->setValueEnabled = true;
|
|
this->setValueTopic = setValueTopic;
|
|
this->subscribeMqtt();
|
|
}
|
|
|
|
void SmartVariable::publishValue()
|
|
{
|
|
if (this->iotEnabled)
|
|
this->iot->publish(this->topic, this->value);
|
|
}
|
|
|
|
void SmartVariable::bindFRAM(FRAM fram, uint32_t framAddress)
|
|
{
|
|
this->bindFRAM(fram, framAddress, true);
|
|
}
|
|
|
|
void SmartVariable::bindFRAM(FRAM *fram, uint32_t framAddress, bool loadValue)
|
|
{
|
|
this->framAddress = framAddress;
|
|
this->fram = fram;
|
|
if (loadValue)
|
|
this->loadValue();
|
|
}
|
|
|
|
void SmartVariable::loadValue()
|
|
{
|
|
this->fram.read(this->framAddress, (uint8_t *)this->value, this->size);
|
|
this->setValue(this->value);
|
|
}
|
|
|
|
void SmartVariable::saveValue()
|
|
{
|
|
this->fram.write(this->framAddress, (uint8_t *)this->value, this->size);
|
|
}
|
|
|
|
void SmartVariable::setValueAutoSave(bool autoSave)
|
|
{
|
|
this->autoSave = autoSave;
|
|
}
|
|
|
|
uint16_t SmartVariable::registerCallback(void (*callback)(char *))
|
|
{
|
|
this->valueChangeCallbacks[this->currentHandlerId] = callback;
|
|
return this->currentHandlerId++;
|
|
}
|
|
|
|
void SmartVariable::unregisterCallback(uint16_t handlerId)
|
|
{
|
|
this->valueChangeCallbacks.erase(handlerId);
|
|
}
|
|
|
|
void SmartVariable::handleMqttCallback(char *topic, char *payload)
|
|
{
|
|
if (!strcmp(topic, this->valueRequestTopic))
|
|
{
|
|
this->publishValue();
|
|
}
|
|
else if (!strcmp(topic, this->setValueTopic))
|
|
{
|
|
this->setValue(payload);
|
|
}
|
|
}
|
|
|
|
void SmartVariable::subscribeMqtt()
|
|
{
|
|
if (this->iotEnabled)
|
|
{
|
|
if (this->useValueRequest)
|
|
this->iot->subscribe(this->valueRequestTopic);
|
|
if (this->setValueEnabled)
|
|
this->iot->subscribe(this->setValueTopic);
|
|
}
|
|
}
|
|
|
|
int32_t SmartVariable::getIntValue()
|
|
{
|
|
return atoi(this->value);
|
|
}
|
|
|
|
void SmartVariable::setIntValue(int32_t value)
|
|
{
|
|
itoa(value, this->value, 10);
|
|
this->setValue(this->value);
|
|
}
|
|
|
|
double SmartVariable::getDoubleValue()
|
|
{
|
|
return atof(this->value);
|
|
}
|
|
|
|
void SmartVariable::setDoubleValue(double value)
|
|
{
|
|
dtostrf(value, 0, 2, this->value);
|
|
this->setValue(this->value);
|
|
}
|