#pragma once #include #include #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; } int get_brightness() const { return brightness; } cJSON* make_json_config_locked() const; private: std::string id; std::string topic_prefix; bool ready; SemaphoreHandle_t mutex; SemaphoreHandle_t sem; bool power_on; std::string effect; int strip_length; int brightness; esp_mqtt_client_handle_t client; static void on_mqtt_connect(esp_mqtt_client_handle_t client, void* data) { ((Device*)data)->on_mqtt_connect(); } static void on_mqtt_message(esp_mqtt_client_handle_t client, esp_mqtt_event_handle_t event, void* data) { ((Device*)data)->on_mqtt_message(event); } void on_mqtt_connect(); void on_mqtt_message(esp_mqtt_event_handle_t); void publish_state_locked(); void advertise_locked(); void advertise(); };