|
- static const char *TAG = "device";
- #include <esp_log.h>
-
- #include "device.hpp"
- #include "presets.hpp"
- #include "utils.hpp"
-
-
- Device::Device(std::string _id) :
- id(_id),
- topic_prefix("light/" + id),
- 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");
-
- std::string topic = topic_prefix + "/#";
- if (esp_mqtt_client_subscribe(client, topic.c_str(), 0) < 0) {
- ESP_LOGE(TAG, "Failed to subscribe to %s", 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", (topic_prefix + "/state").c_str());
- cJSON_AddStringToObject(json, "command_topic", (topic_prefix + "/cmd").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);
- }
-
- std::string Device::subtopic(const std::string &topic) {
- auto slash_pos = topic.find('/', topic_prefix.length());
- if (slash_pos == std::string::npos) return "";
- auto another_slash_pos = topic.find('/', slash_pos + 1);
- if (slash_pos == std::string::npos) {
- // No further levels
- return topic.substr(slash_pos + 1);
- }
- // First subtopic
- return topic.substr(slash_pos + 1, another_slash_pos - (slash_pos + 1));
- }
-
- void Device::on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t event) {
- std::string topic(event->topic, event->topic_len);
- std::string command = subtopic(topic);
-
- ESP_LOGI(TAG, "Received command via MQTT: %s", command.c_str());
-
- if (command == "state") {
- // This is from us, just ignore it
- return;
-
- } else if (command == "cmd") {
- 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");
- }
- // Fall through & kick semaphore
-
- } else if (command == "reboot") {
- ESP_LOGI(TAG, "Rebooting immediately");
- esp_restart();
- return;
-
- } else if (command == "data") {
- 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
- );
- }
- // Fall through & kick semaphore
-
- } else {
- ESP_LOGE(TAG, "Unhandled command: %s (%d bytes)", command.c_str(), event->data_len);
- return;
- }
-
- 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, (topic_prefix + "/state").c_str(), config, 0, 0, 0
- ));
- cJSON_free(config);
- cJSON_Delete(json);
- }
- }
|