new post server
This commit is contained in:
@@ -53,8 +53,38 @@ static struct {
|
||||
char topic[128];
|
||||
} mqtt_config = {false, "", 1883, "", "", ""};
|
||||
|
||||
// Estrutura para armazenar as configurações OCPP em memória
|
||||
static struct {
|
||||
char url[256];
|
||||
char chargeBoxId[128];
|
||||
char certificate[256];
|
||||
char privateKey[256];
|
||||
} ocpp_config = {"", "", "", ""};
|
||||
|
||||
/* Set HTTP response content type according to file extension */
|
||||
// Estrutura para armazenar as configurações de energia
|
||||
static struct {
|
||||
int currentLimit;
|
||||
int powerLimit;
|
||||
int energyLimit;
|
||||
int chargingTimeLimit;
|
||||
int temperatureLimit;
|
||||
} settings_config = {32, 0, 0, 0, 60};
|
||||
|
||||
|
||||
// Estruturas para armazenar as configurações de autenticação e usuários
|
||||
static struct {
|
||||
bool RFID;
|
||||
bool App;
|
||||
bool Password;
|
||||
} auth_methods = {false, false, true};
|
||||
|
||||
static struct {
|
||||
char username[128];
|
||||
} users[10] = {{"admin"}, {"user1"}};
|
||||
static int num_users = 2; // Contador de usuários cadastrados
|
||||
|
||||
|
||||
// Set HTTP response content type according to file extension
|
||||
static esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filepath)
|
||||
{
|
||||
const char *type = "text/plain";
|
||||
@@ -218,18 +248,6 @@ static esp_err_t config_load_balancing_get_handler(httpd_req_t *req)
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Estrutura para armazenar as configurações OCPP em memória
|
||||
static struct {
|
||||
char url[256];
|
||||
char chargeBoxId[128];
|
||||
char certificate[256];
|
||||
char privateKey[256];
|
||||
} ocpp_config = {"", "", "", ""};
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/ocpp (Status do OCPP)
|
||||
static esp_err_t ocpp_status_get_handler(httpd_req_t *req)
|
||||
{
|
||||
@@ -312,15 +330,6 @@ static esp_err_t config_ocpp_post_handler(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Estrutura para armazenar as configurações
|
||||
static struct {
|
||||
int currentLimit;
|
||||
int powerLimit;
|
||||
int energyLimit;
|
||||
int chargingTimeLimit;
|
||||
int temperatureLimit;
|
||||
} settings_config = {32, 0, 0, 0, 60};
|
||||
|
||||
// Manipulador para o endpoint POST /api/v1/config/settings
|
||||
static esp_err_t config_settings_post_handler(httpd_req_t *req)
|
||||
{
|
||||
@@ -455,10 +464,10 @@ static esp_err_t config_wifi_get_handler(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
/* Manipulador para o endpoint POST /api/v1/config/wifi */
|
||||
static esp_err_t config_wifi_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[256]; // Buffer para armazenar a requisição
|
||||
char buf[512]; // Buffer para armazenar a requisição
|
||||
int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
if (len <= 0) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid request body");
|
||||
@@ -491,7 +500,7 @@ static esp_err_t config_wifi_post_handler(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
/* Manipulador para o endpoint GET /api/v1/config/mqtt */
|
||||
static esp_err_t config_mqtt_get_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
@@ -516,7 +525,7 @@ static esp_err_t config_mqtt_get_handler(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
/* Manipulador para o endpoint POST /api/v1/config/mqtt */
|
||||
static esp_err_t config_mqtt_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[512]; // Buffer para armazenar a requisição
|
||||
@@ -561,6 +570,139 @@ static esp_err_t config_mqtt_post_handler(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/config/auth-methods
|
||||
static esp_err_t config_auth_methods_get_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
// Criar objeto JSON com as configurações de métodos de autenticação
|
||||
cJSON *config = cJSON_CreateObject();
|
||||
cJSON_AddBoolToObject(config, "RFID", auth_methods.RFID);
|
||||
cJSON_AddBoolToObject(config, "App", auth_methods.App);
|
||||
cJSON_AddBoolToObject(config, "Password", auth_methods.Password);
|
||||
|
||||
// Convertendo para string e enviando a resposta
|
||||
const char *config_str = cJSON_Print(config);
|
||||
httpd_resp_sendstr(req, config_str);
|
||||
|
||||
// Liberando a memória
|
||||
free((void *)config_str);
|
||||
cJSON_Delete(config);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Manipulador para o endpoint POST /api/v1/config/auth-methods
|
||||
static esp_err_t config_auth_methods_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[512]; // Buffer para armazenar a requisição
|
||||
int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
if (len <= 0) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid request body");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[len] = '\0'; // Garantir que a string esteja terminada
|
||||
|
||||
// Parse JSON recebido
|
||||
cJSON *json = cJSON_Parse(buf);
|
||||
if (json == NULL) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Atualizando as configurações de autenticação
|
||||
cJSON *RFID = cJSON_GetObjectItem(json, "RFID");
|
||||
if (RFID) auth_methods.RFID = RFID->valueint;
|
||||
|
||||
cJSON *App = cJSON_GetObjectItem(json, "App");
|
||||
if (App) auth_methods.App = App->valueint;
|
||||
|
||||
cJSON *Password = cJSON_GetObjectItem(json, "Password");
|
||||
if (Password) auth_methods.Password = Password->valueint;
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
// Responder com uma mensagem de sucesso
|
||||
httpd_resp_sendstr(req, "Configurações de Autenticação atualizadas com sucesso");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/config/users
|
||||
static esp_err_t config_users_get_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
// Criar objeto JSON com a lista de usuários
|
||||
cJSON *config = cJSON_CreateObject();
|
||||
cJSON *users_list = cJSON_CreateArray();
|
||||
|
||||
for (int i = 0; i < num_users; i++) {
|
||||
cJSON *user = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(user, "username", users[i].username);
|
||||
cJSON_AddItemToArray(users_list, user);
|
||||
}
|
||||
|
||||
cJSON_AddItemToObject(config, "users", users_list);
|
||||
|
||||
// Convertendo para string e enviando a resposta
|
||||
const char *config_str = cJSON_Print(config);
|
||||
httpd_resp_sendstr(req, config_str);
|
||||
|
||||
// Liberando a memória
|
||||
free((void *)config_str);
|
||||
cJSON_Delete(config);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
// Manipulador para o endpoint POST /api/v1/config/users
|
||||
static esp_err_t config_users_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[128]; // Buffer para armazenar o nome do novo usuário
|
||||
int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
if (len <= 0) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid request body");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[len] = '\0'; // Garantir que a string esteja terminada
|
||||
|
||||
// Adicionar o novo usuário
|
||||
if (num_users < 10) {
|
||||
strlcpy(users[num_users].username, buf, sizeof(users[num_users].username));
|
||||
num_users++;
|
||||
httpd_resp_sendstr(req, "Usuário adicionado com sucesso");
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Máximo de usuários atingido");
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
// Manipulador para o endpoint DELETE /api/v1/config/users/{username}
|
||||
static esp_err_t config_users_delete_handler(httpd_req_t *req)
|
||||
{
|
||||
char username[128]; // Nome do usuário a ser removido
|
||||
if (httpd_req_get_url_query_str(req, username, sizeof(username)) == ESP_OK) {
|
||||
// Verificar e remover o usuário
|
||||
for (int i = 0; i < num_users; i++) {
|
||||
if (strcmp(users[i].username, username) == 0) {
|
||||
// Remover o usuário
|
||||
for (int j = i; j < num_users - 1; j++) {
|
||||
users[j] = users[j + 1];
|
||||
}
|
||||
num_users--;
|
||||
httpd_resp_sendstr(req, "Usuário removido com sucesso");
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Usuário não encontrado");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -573,14 +715,10 @@ esp_err_t rest_init(const char *base_path)
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.max_uri_handlers = 20; // Ajuste conforme necessário
|
||||
config.max_uri_handlers = 30; // Ajuste conforme necessário
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
|
||||
ESP_LOGI(REST_TAG, "Starting HTTP Server");
|
||||
REST_CHECK(httpd_start(&server, &config) == ESP_OK, "Start server failed", err_start);
|
||||
|
||||
|
||||
/* URI handler for getting load balancing config */
|
||||
// Registrar manipuladores de URI para as configurações
|
||||
httpd_uri_t config_load_balancing_get_uri = {
|
||||
.uri = "/api/v1/config/load-balancing",
|
||||
.method = HTTP_GET,
|
||||
@@ -589,8 +727,7 @@ esp_err_t rest_init(const char *base_path)
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_load_balancing_get_uri);
|
||||
|
||||
|
||||
// URI handler for fetching OCPP status
|
||||
// URI handler for fetching OCPP status
|
||||
httpd_uri_t ocpp_status_get_uri = {
|
||||
.uri = "/api/v1/ocpp",
|
||||
.method = HTTP_GET,
|
||||
@@ -626,7 +763,6 @@ esp_err_t rest_init(const char *base_path)
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_settings_post_uri);
|
||||
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/config/settings
|
||||
httpd_uri_t config_settings_get_uri = {
|
||||
.uri = "/api/v1/config/settings",
|
||||
@@ -636,18 +772,16 @@ esp_err_t rest_init(const char *base_path)
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_settings_get_uri);
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/dashboard
|
||||
httpd_uri_t dashboard_get_uri = {
|
||||
.uri = "/api/v1/dashboard",
|
||||
.method = HTTP_GET,
|
||||
.handler = dashboard_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &dashboard_get_uri);
|
||||
|
||||
// Manipulador para o endpoint GET /api/v1/dashboard
|
||||
httpd_uri_t dashboard_get_uri = {
|
||||
.uri = "/api/v1/dashboard",
|
||||
.method = HTTP_GET,
|
||||
.handler = dashboard_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &dashboard_get_uri);
|
||||
|
||||
|
||||
/* Register URI Handlers */
|
||||
// Register URI Handlers for electrical configuration
|
||||
httpd_uri_t electrical_config_get_uri = {
|
||||
.uri = "/api/v1/config/electrical",
|
||||
.method = HTTP_GET,
|
||||
@@ -664,52 +798,96 @@ esp_err_t rest_init(const char *base_path)
|
||||
};
|
||||
httpd_register_uri_handler(server, &electrical_config_post_uri);
|
||||
|
||||
// URI handler for getting Wi-Fi config
|
||||
httpd_uri_t config_wifi_get_uri = {
|
||||
.uri = "/api/v1/config/wifi",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_wifi_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_wifi_get_uri);
|
||||
// URI handler for getting Wi-Fi config
|
||||
httpd_uri_t config_wifi_get_uri = {
|
||||
.uri = "/api/v1/config/wifi",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_wifi_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_wifi_get_uri);
|
||||
|
||||
// URI handler for posting Wi-Fi config
|
||||
httpd_uri_t config_wifi_post_uri = {
|
||||
.uri = "/api/v1/config/wifi",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_wifi_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_wifi_post_uri);
|
||||
// URI handler for posting Wi-Fi config
|
||||
httpd_uri_t config_wifi_post_uri = {
|
||||
.uri = "/api/v1/config/wifi",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_wifi_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_wifi_post_uri);
|
||||
|
||||
// URI handler for getting MQTT config
|
||||
httpd_uri_t config_mqtt_get_uri = {
|
||||
.uri = "/api/v1/config/mqtt",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_mqtt_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_mqtt_get_uri);
|
||||
// URI handler for getting MQTT config
|
||||
httpd_uri_t config_mqtt_get_uri = {
|
||||
.uri = "/api/v1/config/mqtt",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_mqtt_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_mqtt_get_uri);
|
||||
|
||||
// URI handler for posting MQTT config
|
||||
httpd_uri_t config_mqtt_post_uri = {
|
||||
.uri = "/api/v1/config/mqtt",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_mqtt_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_mqtt_post_uri);
|
||||
// URI handler for posting MQTT config
|
||||
httpd_uri_t config_mqtt_post_uri = {
|
||||
.uri = "/api/v1/config/mqtt",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_mqtt_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_mqtt_post_uri);
|
||||
|
||||
// Registrar manipuladores para as configurações de autenticação
|
||||
httpd_uri_t config_auth_methods_get_uri = {
|
||||
.uri = "/api/v1/config/auth-methods",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_auth_methods_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_auth_methods_get_uri);
|
||||
|
||||
/* URI handler for getting web server files */
|
||||
httpd_uri_t config_auth_methods_post_uri = {
|
||||
.uri = "/api/v1/config/auth-methods",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_auth_methods_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_auth_methods_post_uri);
|
||||
|
||||
// Registrar manipuladores para as configurações de usuários
|
||||
httpd_uri_t config_users_get_uri = {
|
||||
.uri = "/api/v1/config/users",
|
||||
.method = HTTP_GET,
|
||||
.handler = config_users_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_users_get_uri);
|
||||
|
||||
httpd_uri_t config_users_post_uri = {
|
||||
.uri = "/api/v1/config/users",
|
||||
.method = HTTP_POST,
|
||||
.handler = config_users_post_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_users_post_uri);
|
||||
|
||||
httpd_uri_t config_users_delete_uri = {
|
||||
.uri = "/api/v1/config/users",
|
||||
.method = HTTP_DELETE,
|
||||
.handler = config_users_delete_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &config_users_delete_uri);
|
||||
|
||||
// URI handler for getting web server files
|
||||
httpd_uri_t common_get_uri = {
|
||||
.uri = "/*",
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = rest_common_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &common_get_uri);
|
||||
|
||||
|
||||
// Finalmente, inicie o servidor
|
||||
ESP_LOGI(REST_TAG, "Starting HTTP Server");
|
||||
REST_CHECK(httpd_start(&server, &config) == ESP_OK, "Start server failed", err_start);
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
@@ -717,4 +895,4 @@ err_start:
|
||||
free(rest_context);
|
||||
err:
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user