2024-01-16 04:22:41 +00:00
|
|
|
#pragma once
|
|
|
|
#include <ESPMegaIoT.hpp>
|
2024-02-12 09:40:46 +00:00
|
|
|
#include <map>
|
2024-02-10 07:26:41 +00:00
|
|
|
/**
|
|
|
|
* @brief A class that create a variable that exists on other devices and can be accessed remotely
|
|
|
|
*
|
|
|
|
* This class is used to create a variable that exists on other devices and can be accessed remotely.
|
|
|
|
* Supports setting and getting values from the variable.
|
|
|
|
* Also support value request.
|
|
|
|
*/
|
2024-01-16 04:22:41 +00:00
|
|
|
class RemoteVariable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RemoteVariable();
|
|
|
|
~RemoteVariable();
|
2024-02-09 10:48:43 +00:00
|
|
|
void begin(size_t size, const char* topic, ESPMegaIoT* iot);
|
|
|
|
void begin(size_t size, const char* topic, ESPMegaIoT* iot, bool useValueRequest, const char* valueRequestTopic);
|
|
|
|
void setValue(const char* value);
|
|
|
|
void enableSetValue(const char* setValueTopic);
|
2024-01-16 04:22:41 +00:00
|
|
|
void subscribe();
|
2024-01-16 05:48:06 +00:00
|
|
|
void requestValue();
|
2024-01-16 04:22:41 +00:00
|
|
|
char* getValue();
|
2024-02-12 19:28:34 +00:00
|
|
|
int getValueAsInt();
|
|
|
|
long getValueAsLong();
|
|
|
|
double getValueAsDouble();
|
|
|
|
void setIntValue(int value);
|
|
|
|
void setLongValue(long value);
|
|
|
|
void setDoubleValue(double value);
|
2024-02-12 09:40:46 +00:00
|
|
|
uint8_t registerCallback(std::function<void(char*)>);
|
|
|
|
void unregisterCallback(uint8_t handler);
|
|
|
|
|
2024-01-16 04:22:41 +00:00
|
|
|
private:
|
2024-01-16 05:48:06 +00:00
|
|
|
void mqtt_callback(char* topic, char* payload);
|
2024-01-16 04:22:41 +00:00
|
|
|
ESPMegaIoT* iot;
|
2024-02-09 10:48:43 +00:00
|
|
|
const char* topic;
|
2024-01-16 04:22:41 +00:00
|
|
|
char* value;
|
|
|
|
size_t size;
|
2024-01-16 05:48:06 +00:00
|
|
|
bool useValueRequest;
|
2024-02-09 10:48:43 +00:00
|
|
|
const char* valueRequestTopic;
|
2024-01-16 05:48:06 +00:00
|
|
|
bool useSetValue;
|
2024-02-09 10:48:43 +00:00
|
|
|
const char* setValueTopic;
|
2024-02-12 09:40:46 +00:00
|
|
|
uint8_t valueChangeCallbackCount;
|
|
|
|
std::map<uint8_t,std::function<void(char*)>> valueChangeCallback;
|
2024-01-16 04:22:41 +00:00
|
|
|
};
|