static const char *TAG = "device"; #include #include "device.hpp" #include "presets.hpp" #include "utils.hpp" Device::Device(std::string _id) : id(_id), state_topic("light/" + id + "/state"), cmd_topic("light/" + id + "/cmd"), data_topic("light/" + id + "/data/+"), ready(false), should_publish(false), mutex(xSemaphoreCreateMutex()), sem(xSemaphoreCreateBinary()), power_on(false), strip_length(0), client(start_mqtt_client(on_mqtt_connect, on_mqtt_message, this)) {} void Device::set_json_config(const cJSON *json) { xSemaphoreTake(mutex, portMAX_DELAY); const cJSON *state = cJSON_GetObjectItem(json, "state"); if (state && cJSON_IsString(state)) { power_on = strcmp(state->valuestring, "ON") == 0; } const cJSON *fx = cJSON_GetObjectItem(json, "effect"); if (fx && cJSON_IsString(fx)) { effect = fx->valuestring; } const cJSON *length = cJSON_GetObjectItem(json, "length"); if (length && cJSON_IsNumber(length)) { strip_length = length->valuedouble; } publish_state_locked(); xSemaphoreGive(mutex); } void Device::on_mqtt_connect(esp_mqtt_client_handle_t client) { xSemaphoreTake(mutex, portMAX_DELAY); ESP_LOGI(TAG, "Connected to MQTT"); if (esp_mqtt_client_subscribe(client, cmd_topic.c_str(), 0) < 0) { ESP_LOGE(TAG, "Failed to subscribe to %s", cmd_topic.c_str()); } if (esp_mqtt_client_subscribe(client, data_topic.c_str(), 0) < 0) { ESP_LOGE(TAG, "Failed to subscribe to %s", data_topic.c_str()); } // TODO: Re-announce when presets change cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "unique_id", id.c_str()); cJSON *device = cJSON_AddObjectToObject(json, "device"); cJSON_AddStringToObject(device, "name", id.c_str()); cJSON *identifiers = cJSON_AddArrayToObject(device, "identifiers"); cJSON_AddItemToArray(identifiers, cJSON_CreateString(id.c_str())); cJSON_AddStringToObject(json, "state_topic", state_topic.c_str()); cJSON_AddStringToObject(json, "command_topic", cmd_topic.c_str()); cJSON_AddStringToObject(json, "schema", "json"); cJSON_AddBoolToObject(json, "effect", true); cJSON *effects = cJSON_AddArrayToObject(json, "effect_list"); for (auto iter = Presets::map_begin(); iter != Presets::map_end(); ++iter) cJSON_AddItemToArray(effects, cJSON_CreateString(iter->first.c_str())); char *config = cJSON_PrintUnformatted(json); if (0 > esp_mqtt_client_publish( client, ("homeassistant/light/" + id + "/config").c_str(), config, 0, 0, 0 )) { ESP_LOGE(TAG, "Failed to publish discovery message"); } cJSON_free(config); cJSON_Delete(json); ready = true; if (should_publish) publish_state_locked(); xSemaphoreGive(mutex); } void Device::on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t event) { std::string topic(event->topic, event->topic_len); ESP_LOGI(TAG, "Received command via MQTT"); if (topic == cmd_topic) { cJSON *json = cJSON_ParseWithLength(event->data, event->data_len); if (json) { set_json_config(json); cJSON_Delete(json); } else { ESP_LOGE(TAG, "Invalid JSON data"); } } else /* data topic */ { size_t slash = topic.rfind('/'); if (slash != std::string::npos) { write_file( ("/spiffs/" + topic.substr(slash + 1) + ".json").c_str(), event->data, event->data_len ); } } xSemaphoreGive(sem); } cJSON* Device::make_json_config_locked() const { cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "state", power_on ? "ON" : "OFF"); cJSON_AddStringToObject(json, "effect", effect.c_str()); cJSON_AddNumberToObject(json, "length", strip_length); return json; } void Device::publish_state_locked() { if (!(should_publish = !ready)) { cJSON *json = make_json_config_locked(); char *config = cJSON_PrintUnformatted(json); ESP_ERROR_CHECK(esp_mqtt_client_publish( client, state_topic.c_str(), config, 0, 0, 0 )); cJSON_free(config); cJSON_Delete(json); } }