#include "gridmeter.h"
#include "orno_modbus.h"
#include "esp_event.h"
#include "esp_log.h"

static const char *TAG = "gridmeter_modbus";

esp_err_t gridmeter_init(void)
{
    ESP_LOGI(TAG, "Initializing grid meter (Modbus)");
    return orno_modbus_init();
}

esp_err_t gridmeter_read_current(float *current)
{
    esp_err_t err = orno_modbus_read_current(ORNO_METER_GRID, current);
    if (err == ESP_OK)
    {
        esp_event_post(GRIDMETER_EVENT, GRIDMETER_EVENT_UPDATE, current, sizeof(float), portMAX_DELAY);
    }
    return err;
}
