new buzzer component

This commit is contained in:
2025-07-22 00:09:58 +01:00
parent 84f106eee5
commit bd587a10c0
58 changed files with 3215 additions and 6961 deletions

View File

@@ -1,22 +1,33 @@
// =========================
// auth_api.c
// =========================
#include "auth_api.h"
#include "auth.h"
#include "esp_log.h"
#include "cJSON.h"
#include <string.h>
static const char *TAG = "auth_api";
// =================================
// Dummy user storage (static list)
// =================================
static struct {
char username[128];
} users[10] = { /*{"admin"}, {"user1"}*/ };
} users[10] = { /* {"admin"}, {"user1"} */ };
static int num_users = 2;
// =================================
// Handlers for Auth Methods (RFID)
// =================================
static esp_err_t auth_methods_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
cJSON *json = cJSON_CreateObject();
cJSON_AddBoolToObject(json, "RFID", auth_is_enabled() );
cJSON_AddBoolToObject(json, "RFID", auth_is_enabled());
char *str = cJSON_PrintUnformatted(json);
httpd_resp_sendstr(req, str);
free(str);
@@ -53,6 +64,9 @@ static esp_err_t auth_methods_post_handler(httpd_req_t *req) {
return ESP_OK;
}
// =================================
// User Management Handlers
// =================================
static esp_err_t users_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
@@ -64,7 +78,7 @@ static esp_err_t users_get_handler(httpd_req_t *req) {
cJSON_AddItemToArray(list, u);
}
cJSON_AddItemToObject(root, "users", list);
char *str = cJSON_Print(root);
char *str = cJSON_PrintUnformatted(root);
httpd_resp_sendstr(req, str);
free(str);
cJSON_Delete(root);
@@ -75,7 +89,9 @@ static esp_err_t users_post_handler(httpd_req_t *req) {
char buf[128];
int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
if (len <= 0) return ESP_FAIL;
buf[len] = '\0';
if (num_users < 10) {
strlcpy(users[num_users].username, buf, sizeof(users[num_users].username));
num_users++;
@@ -107,7 +123,58 @@ static esp_err_t users_delete_handler(httpd_req_t *req) {
return ESP_FAIL;
}
// =================================
// Tag Management Handlers
// =================================
static esp_err_t tags_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
cJSON *list = cJSON_CreateArray();
int count = auth_get_tag_count();
for (int i = 0; i < count; i++) {
const char *tag = auth_get_tag_by_index(i);
if (tag) {
cJSON_AddItemToArray(list, cJSON_CreateString(tag));
}
}
cJSON_AddItemToObject(root, "tags", list);
char *str = cJSON_PrintUnformatted(root);
httpd_resp_sendstr(req, str);
free(str);
cJSON_Delete(root);
return ESP_OK;
}
static esp_err_t tags_delete_handler(httpd_req_t *req) {
char query[128];
if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK) {
char tag[AUTH_TAG_MAX_LEN];
if (httpd_query_key_value(query, "tag", tag, sizeof(tag)) == ESP_OK) {
if (auth_remove_tag(tag)) {
httpd_resp_sendstr(req, "Tag removida com sucesso");
return ESP_OK;
}
}
}
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Tag não encontrada ou inválida");
return ESP_FAIL;
}
static esp_err_t tags_register_handler(httpd_req_t *req) {
auth_wait_for_tag_registration();
httpd_resp_sendstr(req, "Modo de registro de tag ativado");
return ESP_OK;
}
// =================================
// Register All REST Endpoints
// =================================
void register_auth_handlers(httpd_handle_t server, void *ctx) {
// Auth methods
httpd_register_uri_handler(server, &(httpd_uri_t){
.uri = "/api/v1/config/auth-methods",
.method = HTTP_GET,
@@ -120,6 +187,8 @@ void register_auth_handlers(httpd_handle_t server, void *ctx) {
.handler = auth_methods_post_handler,
.user_ctx = ctx
});
// Users
httpd_register_uri_handler(server, &(httpd_uri_t){
.uri = "/api/v1/config/users",
.method = HTTP_GET,
@@ -138,4 +207,24 @@ void register_auth_handlers(httpd_handle_t server, void *ctx) {
.handler = users_delete_handler,
.user_ctx = ctx
});
// Tags
httpd_register_uri_handler(server, &(httpd_uri_t){
.uri = "/api/v1/config/tags",
.method = HTTP_GET,
.handler = tags_get_handler,
.user_ctx = ctx
});
httpd_register_uri_handler(server, &(httpd_uri_t){
.uri = "/api/v1/config/tags",
.method = HTTP_DELETE,
.handler = tags_delete_handler,
.user_ctx = ctx
});
httpd_register_uri_handler(server, &(httpd_uri_t){
.uri = "/api/v1/config/tags/register",
.method = HTTP_POST,
.handler = tags_register_handler,
.user_ctx = ctx
});
}

View File

@@ -24,11 +24,11 @@ static esp_err_t dashboard_get_handler(httpd_req_t *req) {
cJSON *charger1 = cJSON_CreateObject();
cJSON_AddNumberToObject(charger1, "id", 1);
cJSON_AddStringToObject(charger1, "status", evse_state_to_str(state));
cJSON_AddNumberToObject(charger1, "current", evse_get_charging_current() / 10);
cJSON_AddNumberToObject(charger1, "current", evse_get_charging_current());
cJSON_AddNumberToObject(charger1, "maxCurrent", evse_get_max_charging_current());
// Calcular a potência com base na corrente (considerando 230V)
int power = (evse_get_charging_current() / 10) * 230;
int power = (evse_get_charging_current()) * 230;
cJSON_AddNumberToObject(charger1, "power", power);
cJSON_AddItemToArray(chargers, charger1);

View File

@@ -5,7 +5,7 @@
#include "network_api.h"
#include "esp_log.h"
#include "cJSON.h"
#include "wifi.h"
#include "network.h"
#include "mqtt.h"
static const char *TAG = "network_api";