evse_link feature

This commit is contained in:
2025-08-05 16:55:11 +01:00
parent bd587a10c0
commit 0d0dc5b129
35 changed files with 4353 additions and 2257 deletions

View File

@@ -6,6 +6,7 @@ set(srcs
"src/network_api.c"
"src/meters_settings_api.c"
"src/loadbalancing_settings_api.c"
"src/evse_link_config_api.c"
"src/dashboard_api.c"
"src/static_file_api.c"
)
@@ -14,7 +15,7 @@ idf_component_register(
SRCS ${srcs}
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS "src"
PRIV_REQUIRES nvs_flash esp_http_server esp_netif vfs spiffs json evse meter_manager loadbalancer
PRIV_REQUIRES nvs_flash esp_http_server esp_netif vfs spiffs json evse meter_manager loadbalancer evse_link
)
# SPIFFS image (opcional)

View File

@@ -0,0 +1,31 @@
// =========================
// evse_link_config_api.h
// =========================
#ifndef EVSE_LINK_CONFIG_API_H
#define EVSE_LINK_CONFIG_API_H
#include "esp_err.h"
#include "esp_http_server.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Registra os manipuladores HTTP para configuração do EVSE-Link.
*
* Isso adiciona endpoints GET e POST em /api/v1/config/link
* para inspecionar e atualizar as configurações de link
* (habilitado, modo, self ID).
*
* @param server Handle do servidor HTTP
* @param ctx Contexto do usuário passado aos handlers
*/
void register_link_config_handlers(httpd_handle_t server, void *ctx);
#ifdef __cplusplus
}
#endif
#endif // EVSE_LINK_CONFIG_API_H

View File

@@ -0,0 +1,108 @@
#include "evse_link.h"
#include "evse_link_config_api.h" // new header for these handlers
#include "esp_log.h"
#include "cJSON.h"
static const char *TAG = "link_config_api";
// GET /api/v1/config/link
static esp_err_t link_config_get_handler(httpd_req_t *req) {
bool enabled = evse_link_is_enabled();
uint8_t mode = evse_link_get_mode(); // 0=MASTER,1=SLAVE
uint8_t self_id = evse_link_get_self_id();
ESP_LOGI(TAG, "GET link config: enabled=%d mode=%u id=%u",
enabled, mode, self_id);
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
cJSON_AddBoolToObject (root, "linkEnabled", enabled);
cJSON_AddStringToObject(root, "linkMode",
mode == EVSE_LINK_MODE_MASTER ? "MASTER" : "SLAVE");
cJSON_AddNumberToObject(root, "linkSelfId", self_id);
char *s = cJSON_Print(root);
httpd_resp_sendstr(req, s);
ESP_LOGI(TAG, " payload: %s", s);
free(s);
cJSON_Delete(root);
return ESP_OK;
}
// POST /api/v1/config/link
static esp_err_t link_config_post_handler(httpd_req_t *req) {
char buf[256];
int len = httpd_req_recv(req, buf, sizeof(buf)-1);
if (len <= 0) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body");
return ESP_FAIL;
}
buf[len] = '\0';
ESP_LOGI(TAG, "POST link config: %s", buf);
cJSON *json = cJSON_Parse(buf);
if (!json) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
return ESP_FAIL;
}
// linkEnabled
cJSON *j_en = cJSON_GetObjectItem(json, "linkEnabled");
if (j_en && cJSON_IsBool(j_en)) {
evse_link_set_enabled(cJSON_IsTrue(j_en));
ESP_LOGI(TAG, " set enabled = %d", cJSON_IsTrue(j_en));
}
// linkMode
cJSON *j_md = cJSON_GetObjectItem(json, "linkMode");
if (j_md && cJSON_IsString(j_md)) {
const char *m = j_md->valuestring;
if (strcmp(m, "MASTER") == 0) {
evse_link_set_mode(EVSE_LINK_MODE_MASTER);
} else if (strcmp(m, "SLAVE") == 0) {
evse_link_set_mode(EVSE_LINK_MODE_SLAVE);
} else {
cJSON_Delete(json);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
"Invalid linkMode (must be MASTER or SLAVE)");
return ESP_FAIL;
}
ESP_LOGI(TAG, " set mode = %s", m);
}
// linkSelfId
cJSON *j_id = cJSON_GetObjectItem(json, "linkSelfId");
if (j_id && cJSON_IsNumber(j_id)) {
int id = j_id->valueint;
if (id < 0 || id > 254) {
cJSON_Delete(json);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
"Invalid linkSelfId (0254)");
return ESP_FAIL;
}
evse_link_set_self_id((uint8_t)id);
ESP_LOGI(TAG, " set self_id = %d", id);
}
cJSON_Delete(json);
httpd_resp_sendstr(req, "Link settings updated");
return ESP_OK;
}
void register_link_config_handlers(httpd_handle_t server, void *ctx) {
httpd_uri_t get = {
.uri = "/api/v1/config/link",
.method = HTTP_GET,
.handler = link_config_get_handler,
.user_ctx = ctx
};
httpd_register_uri_handler(server, &get);
httpd_uri_t post = {
.uri = "/api/v1/config/link",
.method = HTTP_POST,
.handler = link_config_post_handler,
.user_ctx = ctx
};
httpd_register_uri_handler(server, &post);
}

View File

@@ -2,6 +2,7 @@
#include "evse_settings_api.h"
#include "meters_settings_api.h"
#include "loadbalancing_settings_api.h"
#include "evse_link_config_api.h"
#include "network_api.h"
#include "ocpp_api.h"
#include "auth_api.h"
@@ -45,8 +46,10 @@ esp_err_t rest_server_init(const char *base_path) {
register_dashboard_handlers(server, ctx); // Apenas chamando a função sem comparação
register_meters_settings_handlers(server, ctx); // Apenas chamando a função sem comparação
register_loadbalancing_settings_handlers(server, ctx); // Apenas chamando a função sem comparação
register_link_config_handlers(server,ctx);
register_static_file_handlers(server, ctx); // Apenas chamando a função sem comparação
ESP_LOGI(TAG, "All REST API endpoint groups registered successfully");
return ESP_OK;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -13,8 +13,8 @@
}
</style>
<title>Vite + React</title>
<script type="module" crossorigin src="/assets/index-zZ02wEhQ.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DvcKJk-E.css">
<script type="module" crossorigin src="/assets/index-ClgQvp_F.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-AuNGQ-2m.css">
</head>
<body>
<div id="root"></div>