ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.hpp

44 lines
1.5 KiB
C++
Raw Normal View History

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();
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;
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;
const char* valueRequestTopic;
2024-01-16 05:48:06 +00:00
bool useSetValue;
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
};