|
- #pragma once
-
- #include <string>
-
- #include <cJSON.h>
-
- #include "mqtt.hpp"
-
-
- class Device {
- public:
- Device(std::string id);
-
- void set_json_config(const cJSON*);
-
- void lock() const { xSemaphoreTake(mutex, portMAX_DELAY); }
- void unlock() const { xSemaphoreGive(mutex); }
-
- bool wait(int timeout_ms = -1) const {
- return pdTRUE == xSemaphoreTake(
- sem,
- (timeout_ms < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms))
- );
- }
-
- // Only call while locked
- const std::string& get_effect() const { return effect; }
- bool is_on() const { return power_on; }
- int get_strip_length() const { return strip_length; }
-
- cJSON* make_json_config_locked() const;
-
- private:
- std::string id;
-
- std::string topic_prefix;
-
- bool ready, should_publish;
-
- SemaphoreHandle_t mutex;
- SemaphoreHandle_t sem;
-
- bool power_on;
- std::string effect;
- int strip_length;
-
- esp_mqtt_client_handle_t client;
-
- static void on_mqtt_connect(esp_mqtt_client_handle_t client, void* data) {
- ((Device*)data)->on_mqtt_connect(client);
- }
- static void on_mqtt_message(esp_mqtt_client_handle_t client, esp_mqtt_event_handle_t event, void* data) {
- ((Device*)data)->on_mqtt_message(client, event);
- }
-
- void on_mqtt_connect(esp_mqtt_client_handle_t);
- void on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t);
-
- void publish_state_locked();
-
- std::string subtopic(const std::string &topic);
- };
|