new meter
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#include "evse_hardware.h"
|
||||
#include "evse_config.h"
|
||||
#include "evse_api.h"
|
||||
#include "auth.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -13,12 +12,14 @@
|
||||
#include "esp_log.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "auth_events.h"
|
||||
#include "loadbalancer_events.h"
|
||||
#include "esp_event.h"
|
||||
|
||||
static const char *TAG = "EVSE_Manager";
|
||||
|
||||
static TickType_t auth_expiration = 0;
|
||||
|
||||
static SemaphoreHandle_t evse_mutex;
|
||||
static QueueHandle_t auth_event_queue = NULL;
|
||||
static bool auth_enabled = false;
|
||||
|
||||
#define EVSE_MANAGER_TICK_PERIOD_MS 1000 // 1 segundo
|
||||
|
||||
@@ -30,27 +31,55 @@ static void evse_manager_task(void *arg) {
|
||||
}
|
||||
}
|
||||
|
||||
static void evse_auth_event_task(void *arg) {
|
||||
auth_event_t evt;
|
||||
// ===== Tratador de eventos de autenticação =====
|
||||
static void on_auth_event(void* arg, esp_event_base_t base, int32_t id, void* data) {
|
||||
if (base != AUTH_EVENTS || data == NULL) return;
|
||||
|
||||
while (true) {
|
||||
if (xQueueReceive(auth_event_queue, &evt, portMAX_DELAY)) {
|
||||
ESP_LOGI(TAG, "Evento de autenticação recebido: %s (%s)",
|
||||
evt.tag, evt.authorized ? "AUTORIZADO" : "NEGADO");
|
||||
switch (id) {
|
||||
case AUTH_EVENT_TAG_PROCESSED: {
|
||||
auth_tag_event_data_t *evt = (auth_tag_event_data_t*)data;
|
||||
ESP_LOGI("EVSE", "Tag: %s | Autorizada: %s", evt->tag, evt->authorized ? "SIM" : "NÃO");
|
||||
evse_state_set_authorized(evt->authorized);
|
||||
break;
|
||||
}
|
||||
|
||||
if (evt.authorized) {
|
||||
evse_authorize();
|
||||
auth_expiration = xTaskGetTickCount() + pdMS_TO_TICKS(2 * 60 * 1000); // 2 minutos
|
||||
case AUTH_EVENT_ENABLED_CHANGED:
|
||||
case AUTH_EVENT_INIT: {
|
||||
auth_enabled_event_data_t *evt = (auth_enabled_event_data_t*)data;
|
||||
auth_enabled = evt->enabled;
|
||||
|
||||
ESP_LOGI("EVSE", "Auth %s (%s)",
|
||||
id == AUTH_EVENT_ENABLED_CHANGED ? "ficou" : "init",
|
||||
evt->enabled ? "ATIVO" : "INATIVO");
|
||||
|
||||
if (!auth_enabled) {
|
||||
evse_state_set_authorized(true);
|
||||
ESP_LOGI("EVSE", "Autenticação desativada → autorização forçada.");
|
||||
} else {
|
||||
evse_manager_set_authorized(false);
|
||||
ESP_LOGW(TAG, "Tag inválida, carregamento negado.");
|
||||
evse_state_set_authorized(false);
|
||||
ESP_LOGI("EVSE", "Autenticação ativada → aguardando autorização por tag.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Tratador de eventos de loadbalancer =====
|
||||
static void on_loadbalancer_event(void* handler_arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data) {
|
||||
if (event_id == LOADBALANCER_EVENT_INIT || event_id == LOADBALANCER_EVENT_STATE_CHANGED) {
|
||||
const loadbalancer_state_event_t* evt = (const loadbalancer_state_event_t*) event_data;
|
||||
ESP_LOGI(TAG, "Loadbalancer %s (ts: %lld)",
|
||||
evt->enabled ? "ENABLED" : "DISABLED", evt->timestamp_us);
|
||||
// Ações adicionais podem ser adicionadas aqui conforme necessário
|
||||
} else if (event_id == LOADBALANCER_EVENT_CHARGING_LIMIT_CHANGED) {
|
||||
const loadbalancer_charging_limit_event_t* evt = (const loadbalancer_charging_limit_event_t*) event_data;
|
||||
ESP_LOGD(TAG, "Novo limite de corrente: %.1f A (ts: %lld)", evt->limit, evt->timestamp_us);
|
||||
evse_set_runtime_charging_current((uint16_t)(evt->limit));
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Inicialização dos módulos do EVSE =====
|
||||
// ===== Inicialização =====
|
||||
void evse_manager_init(void) {
|
||||
evse_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
@@ -59,17 +88,14 @@ void evse_manager_init(void) {
|
||||
evse_hardware_init();
|
||||
evse_state_init();
|
||||
|
||||
ESP_LOGI(TAG, "EVSE Manager inicializado.");
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(AUTH_EVENTS, ESP_EVENT_ANY_ID, &on_auth_event, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(LOADBALANCER_EVENTS, ESP_EVENT_ANY_ID, &on_loadbalancer_event, NULL));
|
||||
|
||||
ESP_LOGI(TAG, "EVSE Manager inicializado.");
|
||||
xTaskCreate(evse_manager_task, "evse_manager_task", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
// ===== Inicia processamento de eventos de autenticação =====
|
||||
void evse_manager_start(QueueHandle_t queue) {
|
||||
auth_event_queue = queue;
|
||||
xTaskCreate(evse_auth_event_task, "evse_auth_evt", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
// ===== Main Tick =====
|
||||
void evse_manager_tick(void) {
|
||||
xSemaphoreTake(evse_mutex, portMAX_DELAY);
|
||||
|
||||
@@ -78,27 +104,25 @@ void evse_manager_tick(void) {
|
||||
evse_state_tick();
|
||||
evse_temperature_check();
|
||||
|
||||
// Verifica expiração de autorização somente se auth está habilitado
|
||||
if (auth_is_enabled()) {
|
||||
if (evse_state_get_authorized() && auth_expiration > 0 &&
|
||||
xTaskGetTickCount() >= auth_expiration) {
|
||||
ESP_LOGI(TAG, "Autorização expirada após 2 minutos.");
|
||||
if (auth_enabled) {
|
||||
// If the car is disconnected, revoke authorization
|
||||
if (evse_state_get_authorized() && evse_get_state() == EVSE_STATE_A) {
|
||||
ESP_LOGI(TAG, "Vehicle disconnected → revoking authorization.");
|
||||
evse_state_set_authorized(false);
|
||||
auth_expiration = 0;
|
||||
}
|
||||
} else {
|
||||
// Se autenticação não é necessária, sempre considera autorizado
|
||||
// If authentication is disabled, ensure authorization is always granted
|
||||
if (!evse_state_get_authorized()) {
|
||||
evse_state_set_authorized(true);
|
||||
ESP_LOGI(TAG, "Autenticação desativada: autorização forçada.");
|
||||
ESP_LOGI(TAG, "Authentication disabled → forced authorization.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
xSemaphoreGive(evse_mutex);
|
||||
}
|
||||
|
||||
// ===== Controles e status =====
|
||||
|
||||
// ===== API pública =====
|
||||
bool evse_manager_is_available(void) {
|
||||
return evse_config_is_available();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user