2024-03-23 03:25:16 +00:00
|
|
|
#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)
|
|
|
|
{
|
2024-03-23 04:54:53 +00:00
|
|
|
this->iot = iot;
|
|
|
|
this->iotEnabled = true;
|
|
|
|
this->topic = topic;
|
|
|
|
ESP_LOGV("SmartVariable", "Binding MQTT Callback");
|
2024-03-23 03:25:16 +00:00
|
|
|
auto bindedMqttCallback = std::bind(&SmartVariable::handleMqttCallback, this, std::placeholders::_1, std::placeholders::_2);
|
2024-03-23 04:54:53 +00:00
|
|
|
this->iot->registerMqttCallback(bindedMqttCallback);
|
|
|
|
ESP_LOGV("SmartVariable", "Binding MQTT Subscription");
|
|
|
|
auto bindedMqttSubscription = std::bind(&SmartVariable::subscribeMqtt, this);
|
|
|
|
this->iot->registerSubscribeCallback(bindedMqttSubscription);
|
|
|
|
ESP_LOGI("SmartVariable", "Calling MQTT Subscribe");
|
2024-03-23 03:25:16 +00:00
|
|
|
this->subscribeMqtt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SmartVariable::enableValueRequest(const char *valueRequestTopic)
|
|
|
|
{
|
2024-03-23 04:54:53 +00:00
|
|
|
ESP_LOGD("SmartVariable", "Enabling Value Request");
|
2024-03-23 03:25:16 +00:00
|
|
|
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()
|
|
|
|
{
|
2024-03-23 04:54:53 +00:00
|
|
|
if (this->iotEnabled) {
|
|
|
|
if (this->value == nullptr) {
|
|
|
|
ESP_LOGE("SmartVariable", "Value is NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this->topic == nullptr) {
|
|
|
|
ESP_LOGE("SmartVariable", "Topic is NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGV("SmartVariable", "Publishing Value: %s to %s", this->value, this->topic);
|
2024-03-23 03:25:16 +00:00
|
|
|
this->iot->publish(this->topic, this->value);
|
2024-03-23 04:54:53 +00:00
|
|
|
}
|
2024-03-23 03:25:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 03:29:04 +00:00
|
|
|
void SmartVariable::bindFRAM(FRAM *fram, uint32_t framAddress)
|
2024-03-23 03:25:16 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2024-03-23 03:29:04 +00:00
|
|
|
this->fram->read(this->framAddress, (uint8_t *)this->value, this->size);
|
2024-03-23 03:25:16 +00:00
|
|
|
this->setValue(this->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SmartVariable::saveValue()
|
|
|
|
{
|
2024-03-23 03:29:04 +00:00
|
|
|
this->fram->write(this->framAddress, (uint8_t *)this->value, this->size);
|
2024-03-23 03:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SmartVariable::setValueAutoSave(bool autoSave)
|
|
|
|
{
|
|
|
|
this->autoSave = autoSave;
|
|
|
|
}
|
|
|
|
|
2024-03-23 05:38:13 +00:00
|
|
|
uint16_t SmartVariable::registerCallback(std::function<void(char *)> callback)
|
2024-03-23 03:25:16 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2024-03-23 04:54:53 +00:00
|
|
|
|
2024-03-23 03:25:16 +00:00
|
|
|
if (this->iotEnabled)
|
|
|
|
{
|
2024-03-23 04:54:53 +00:00
|
|
|
ESP_LOGV("SmartVariable", "IoT Enabled, running MQTT Subscribe");
|
|
|
|
ESP_LOGV("SmartVariable", "Value Request: %d, Set Value: %d", this->useValueRequest, this->setValueEnabled);
|
|
|
|
if (this->useValueRequest) {
|
|
|
|
if (this->valueRequestTopic == nullptr) {
|
|
|
|
ESP_LOGE("SmartVariable", "Value Request Topic is NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGV("SmartVariable", "Subscribing to %s", this->valueRequestTopic);
|
2024-03-23 03:25:16 +00:00
|
|
|
this->iot->subscribe(this->valueRequestTopic);
|
2024-03-23 04:54:53 +00:00
|
|
|
}
|
|
|
|
if (this->setValueEnabled) {
|
|
|
|
if (this->setValueTopic == nullptr) {
|
|
|
|
ESP_LOGE("SmartVariable", "Set Value Topic is NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGV("SmartVariable", "Subscribing to %s", this->setValueTopic);
|
2024-03-23 03:25:16 +00:00
|
|
|
this->iot->subscribe(this->setValueTopic);
|
2024-03-23 04:54:53 +00:00
|
|
|
}
|
|
|
|
ESP_LOGV("SmartVariable", "Publishing Value");
|
|
|
|
this->publishValue();
|
2024-03-23 03:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|