type adaptive remote variable

This commit is contained in:
Siwat Sirichai 2024-02-13 02:28:34 +07:00
parent 57dfa30cf0
commit 1b9163d468
2 changed files with 82 additions and 1 deletions

View File

@ -135,10 +135,85 @@ void RemoteVariable::setValue(const char* value) {
this->iot->publish(this->setValueTopic, value);
}
/**
* @brief Register a callback for value change
* This method will be called when the value of the variable changes on the remote device
* @param callback The callback function
* @return uint8_t The handler for the callback
*/
uint8_t RemoteVariable::registerCallback(std::function<void(char*)> callback) {
this->valueChangeCallback[this->valueChangeCallbackCount] = callback;
return valueChangeCallbackCount++;
}
/**
* @brief Unregister a callback
* This method is used to unregister a callback
* @param handler The handler of the callback
*/
void RemoteVariable::unregisterCallback(uint8_t handler) {
this->valueChangeCallback.erase(handler);
}
}
/**
* @brief Get the value of the variable as an integer
* This method is used to get the value of the variable as an integer
* @return int The value of the variable as an integer
* @note If the value is not a valid integer, it will return 0
*/
int RemoteVariable::getValueAsInt() {
return atoi(this->value);
}
/**
* @brief Get the value of the variable as a long
* This method is used to get the value of the variable as a long
* @return long The value of the variable as a long
* @note If the value is not a valid long, it will return 0
*/
long RemoteVariable::getValueAsLong() {
return atol(this->value);
}
/**
* @brief Get the value of the variable as a double
* This method is used to get the value of the variable as a double
* @return double The value of the variable as a double
* @note If the value is not a valid double, it will return 0
*/
double RemoteVariable::getValueAsDouble() {
return atof(this->value);
}
/**
* @brief Set the value of the variable as an integer
* This method is used to set the value of the variable as an integer
* @param value The value to set
*/
void RemoteVariable::setIntValue(int value) {
char buffer[this->size];
itoa(value, buffer, DEC);
this->setValue(buffer);
}
/**
* @brief Set the value of the variable as a long
* This method is used to set the value of the variable as a long
* @param value The value to set
*/
void RemoteVariable::setLongValue(long value) {
char buffer[this->size];
ltoa(value, buffer, DEC);
this->setValue(buffer);
}
/**
* @brief Set the value of the variable as a double
* This method is used to set the value of the variable as a double
* @param value The value to set
*/
void RemoteVariable::setDoubleValue(double value) {
char buffer[this->size];
snprintf(buffer, this->size, "%f", value);
this->setValue(buffer);
}

View File

@ -20,6 +20,12 @@ class RemoteVariable
void subscribe();
void requestValue();
char* getValue();
int getValueAsInt();
long getValueAsLong();
double getValueAsDouble();
void setIntValue(int value);
void setLongValue(long value);
void setDoubleValue(double value);
uint8_t registerCallback(std::function<void(char*)>);
void unregisterCallback(uint8_t handler);