From 922de25e61ba4bb35c24352592c11c9dfb5531d4 Mon Sep 17 00:00:00 2001 From: Kristian Kielhofner Date: Sat, 9 Sep 2023 07:29:47 -0500 Subject: [PATCH] Send wake_start and wake_end WAS websocket messages with wake_volume --- main/audio.c | 3 ++ main/was.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main/was.h | 4 ++- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/main/audio.c b/main/audio.c index a25f2fd6d..881784bbc 100644 --- a/main/audio.c +++ b/main/audio.c @@ -31,6 +31,7 @@ #include "slvgl.h" #include "timer.h" #include "ui.h" +#include "was.h" #include "endpoint/hass.h" #include "endpoint/openhab.h" @@ -275,6 +276,7 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data) ESP_LOGI(TAG, "AUDIO_REC_WAKEUP_END"); msg = MSG_STOP; xQueueSend(q_rec, &msg, 0); + send_wake_end(); if (lvgl_port_lock(lvgl_lock_timeout)) { lv_label_set_text_static(lbl_ln3, "Thinking..."); lvgl_port_unlock(); @@ -291,6 +293,7 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data) } else { float wake_volume = *wake_volume_ptr; ESP_LOGI(TAG, "wake volume: %f", wake_volume); + send_wake_start(wake_volume); } reset_timer(hdl_display_timer, DISPLAY_TIMEOUT_US, true); diff --git a/main/was.c b/main/was.c index 79963c6ca..5ebc1f9da 100644 --- a/main/was.c +++ b/main/was.c @@ -282,3 +282,86 @@ static void send_hello(void) cleanup: cJSON_Delete(cjson); } + +void send_wake_start(float wake_volume) +{ + char *json; + const char *hostname; + esp_err_t ret; + + if (!esp_websocket_client_is_connected(hdl_wc)) { + esp_websocket_client_destroy(hdl_wc); + init_was(); + } + + ret = esp_netif_get_hostname(hdl_netif, &hostname); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to get hostname"); + return; + } + + cJSON *cjson = cJSON_CreateObject(); + cJSON *wake_start = cJSON_CreateObject(); + + if (cJSON_AddStringToObject(wake_start, "hostname", hostname) == NULL) { + goto cleanup; + } + + if (!cJSON_AddNumberToObject(wake_start, "wake_volume", wake_volume)) { + goto cleanup; + } + if (!cJSON_AddItemToObject(cjson, "wake_start", wake_start)) { + goto cleanup; + } + + json = cJSON_Print(cjson); + + ret = esp_websocket_client_send_text(hdl_wc, json, strlen(json), 2000 / portTICK_PERIOD_MS); + cJSON_free(json); + if (ret < 0) { + ESP_LOGE(TAG, "failed to send WAS wake_start message"); + } + +cleanup: + cJSON_Delete(cjson); +} + +void send_wake_end(void) +{ + char *json; + const char *hostname; + esp_err_t ret; + + if (!esp_websocket_client_is_connected(hdl_wc)) { + esp_websocket_client_destroy(hdl_wc); + init_was(); + } + + ret = esp_netif_get_hostname(hdl_netif, &hostname); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to get hostname"); + return; + } + + cJSON *cjson = cJSON_CreateObject(); + cJSON *wake_end = cJSON_CreateObject(); + + if (cJSON_AddStringToObject(wake_end, "hostname", hostname) == NULL) { + goto cleanup; + } + + if (!cJSON_AddItemToObject(cjson, "wake_end", wake_end)) { + goto cleanup; + } + + json = cJSON_Print(cjson); + + ret = esp_websocket_client_send_text(hdl_wc, json, strlen(json), 2000 / portTICK_PERIOD_MS); + cJSON_free(json); + if (ret < 0) { + ESP_LOGE(TAG, "failed to send WAS wake_end message"); + } + +cleanup: + cJSON_Delete(cjson); +} \ No newline at end of file diff --git a/main/was.h b/main/was.h index ad551644d..139eee44f 100644 --- a/main/was.h +++ b/main/was.h @@ -2,4 +2,6 @@ char was_url[2048]; void deinit_was(void); esp_err_t init_was(void); -void request_config(void); \ No newline at end of file +void request_config(void); +void send_wake_start(float wake_volume); +void send_wake_end(void); \ No newline at end of file