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

@@ -4,6 +4,7 @@
#include "evse_limits.h"
#include "esp_log.h"
#include "nvs.h"
#include "esp_timer.h"
static const char *TAG = "evse_config";
@@ -60,7 +61,7 @@ void evse_check_defaults(void) {
}
// Runtime charging current initialized from persisted default
charging_current_runtime = charging_current;
charging_current_runtime = max_charging_current;
ESP_LOGD(TAG, "Runtime charging current initialized to: %d", charging_current_runtime);
// Auth required
@@ -127,6 +128,7 @@ esp_err_t evse_set_max_charging_current(uint8_t value) {
if (value < MIN_CHARGING_CURRENT_LIMIT || value > MAX_CHARGING_CURRENT_LIMIT)
return ESP_ERR_INVALID_ARG;
max_charging_current = value;
evse_set_runtime_charging_current(value);
nvs_set_u8(nvs, "max_chrg_curr", value);
return nvs_commit(nvs);
}
@@ -161,19 +163,35 @@ esp_err_t evse_set_default_charging_current(uint16_t value) {
// Runtime current (not saved)
// ========================
void evse_set_runtime_charging_current(uint16_t value) {
if (value > (max_charging_current)) {
value= max_charging_current;
}
if (value < (MIN_CHARGING_CURRENT_LIMIT) ) {
value= MIN_CHARGING_CURRENT_LIMIT;
ESP_LOGI(TAG, "Runtime charging current updated: %d", charging_current_runtime);
if (value > max_charging_current) {
value = max_charging_current;
} else if (value < MIN_CHARGING_CURRENT_LIMIT) {
value = MIN_CHARGING_CURRENT_LIMIT;
}
charging_current_runtime = value;
ESP_LOGI(TAG, "Runtime charging current updated: %d", charging_current_runtime);
// --- PUBLICA ALTERAÇÃO DE CONFIG DO EVSE ---
evse_config_event_data_t evt = {
.charging = evse_state_is_charging(evse_get_state()),
.hw_max_current = (float)evse_get_max_charging_current(),
.runtime_current = (float)charging_current_runtime,
.timestamp_us = esp_timer_get_time()
};
esp_event_post(EVSE_EVENTS,
EVSE_EVENT_CONFIG_UPDATED,
&evt,
sizeof(evt),
portMAX_DELAY);
}
uint16_t evse_get_runtime_charging_current(void) {
return charging_current_runtime;
}

View File

@@ -40,7 +40,7 @@ static void on_auth_event(void* arg, esp_event_base_t base, int32_t id, void* da
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");
ESP_LOGI("EVSE", "Tag: %s | Autorized: %s", evt->tag, evt->authorized ? "AUTHORIZED" : "DENIED");
evse_state_set_authorized(evt->authorized);
break;
}
@@ -74,11 +74,11 @@ static void on_loadbalancer_event(void* handler_arg, esp_event_base_t event_base
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));
}
} else if (event_id == LOADBALANCER_EVENT_MASTER_CURRENT_LIMIT) {
const loadbalancer_master_limit_event_t* evt = (const loadbalancer_master_limit_event_t*) event_data;
ESP_LOGI(TAG, "Novo limite de corrente (master): %u A (ts: %lld)", evt->max_current, evt->timestamp_us);
evse_set_runtime_charging_current(evt->max_current);
}
}
// ===== Inicialização =====

View File

@@ -24,8 +24,8 @@ static const char *TAG = "evse_state";
static evse_state_event_t map_state_to_event(evse_state_t s) {
switch (s) {
case EVSE_STATE_A: return EVSE_STATE_EVENT_IDLE;
case EVSE_STATE_B1: return EVSE_STATE_EVENT_WAITING;
case EVSE_STATE_B2:
case EVSE_STATE_B1:
case EVSE_STATE_B2: return EVSE_STATE_EVENT_WAITING;
case EVSE_STATE_C1:
case EVSE_STATE_C2: return EVSE_STATE_EVENT_CHARGING;
case EVSE_STATE_E:

View File

@@ -4,6 +4,8 @@
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "evse_events.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@ ESP_EVENT_DECLARE_BASE(EVSE_EVENTS);
typedef enum {
EVSE_EVENT_INIT,
EVSE_EVENT_STATE_CHANGED,
// Outros eventos possíveis futuramente
EVSE_EVENT_CONFIG_UPDATED,
} evse_event_id_t;
typedef enum {
@@ -23,5 +23,11 @@ typedef struct {
evse_state_event_t state;
} evse_state_event_data_t;
typedef struct {
bool charging; // Estado de carregamento
float hw_max_current; // Corrente máxima suportada pelo hardware
float runtime_current; // Corrente de carregamento em uso
int64_t timestamp_us; // Momento da atualização
} evse_config_event_data_t;
#endif // EVSE_EVENTS_H