Skip to content

Commit

Permalink
Send wake_start and wake_end WAS websocket messages with wake_volume
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiankielhofner committed Sep 9, 2023
1 parent 2f6c047 commit 922de25
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
3 changes: 3 additions & 0 deletions main/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "slvgl.h"
#include "timer.h"
#include "ui.h"
#include "was.h"

#include "endpoint/hass.h"
#include "endpoint/openhab.h"
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down
83 changes: 83 additions & 0 deletions main/was.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion main/was.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ char was_url[2048];

void deinit_was(void);
esp_err_t init_was(void);
void request_config(void);
void request_config(void);
void send_wake_start(float wake_volume);
void send_wake_end(void);

0 comments on commit 922de25

Please sign in to comment.