aboutsummaryrefslogtreecommitdiff
path: root/main/routes/shelly.cpp
blob: 565a9254debd804d4dbc0e722ea0554c15eca027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "esp_mac.h"
#include "esp_log.h"

#include "utils.h"
#include "routes.h"

static const char *TAG = "routes/shelly";

/**
 * Handles requests for the `/shelly` endpoint
 */
esp_err_t shelly_handler(httpd_req_t *req)
{
    ESP_LOGD(TAG, "shelly_get_handler %s", req->uri);

    char* resp = NULL;
    int rc = asprintf(
        &resp,
        "{"
            "\"type\":\"SHSW-%s\","
            "\"mac\":\""MACFMT"\","
            "\"auth\":false,"
            "\"fw\":\"20230913-112003/v1.14.0-gcb84623\","
            "\"discoverable\":true,"
            "\"longid\":1,"
            "\"num_outputs\":1"
        "}",
        CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()));

    if (!rc || !resp)
    {
        ESP_LOGE(TAG, "Failed to allocate response");

        httpd_resp_set_status(req, HTTPD_500);
        httpd_resp_send(req, NULL, 0);

        return ESP_FAIL;
    }

    httpd_resp_set_status(req, HTTPD_200);
    httpd_resp_set_type(req, "application/json");

    httpd_resp_sendstr(req, resp);

    free(resp);

    return ESP_OK;
}