smart-oil-heating-control-s.../main/metrics.c
2024-12-07 22:46:38 +01:00

272 lines
8.8 KiB
C

#include "metrics.h"
#include "outputs.h"
#include "inputs.h"
static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "smart-oil-heater-control-system-metrics";
char caHtmlResponse[HTML_RESPONSE_SIZE];
SemaphoreHandle_t xMutexAccessMetricResponse = NULL;
static sMetric aMetrics[METRIC_MAX_COUNT];
static uint16_t u16MetricCounter = 0U;
void taskMetrics(void *pvParameters);
void initMetrics(void)
{
connect_wifi();
setup_server();
BaseType_t taskCreated = xTaskCreate(
taskMetrics, // Function to implement the task
"taskMetrics", // Task name
2048, // Stack size (in words, not bytes)
NULL, // Parameters to the task function (none in this case)
5, // Task priority (higher number = higher priority)
NULL // Task handle (optional)
);
if (taskCreated == pdPASS)
{
ESP_LOGI(TAG, "Task created successfully!");
}
else
{
ESP_LOGE(TAG, "Failed to create task");
}
}
void taskMetrics(void *pvParameters)
{
while (1)
{
ESP_LOGI(TAG, "Running task Metrics...");
vTaskDelay(1000U / portTICK_PERIOD_MS);
u16MetricCounter = 0U;
/*Wifi RSSI*/
wifi_ap_record_t ap;
esp_wifi_sta_get_ap_info(&ap);
// printf("WiFi RSSI: %d\n", ap.rssi);
strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi");
aMetrics[u16MetricCounter].fMetricValue = ap.rssi;
u16MetricCounter++;
/*Burner State*/
if (getBurnerState() == ENABLED)
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "burner_enabled");
aMetrics[u16MetricCounter].fMetricValue = 1.0f;
u16MetricCounter++;
}
else
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "burner_enabled");
aMetrics[u16MetricCounter].fMetricValue = 0.0f;
u16MetricCounter++;
}
/*Circulation Pump State*/
if (getCirculationPumpState() == ENABLED)
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "circulation_pump_enabled");
aMetrics[u16MetricCounter].fMetricValue = 1.0f;
u16MetricCounter++;
}
else
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "circulation_pump_enabled");
aMetrics[u16MetricCounter].fMetricValue = 0.0f;
u16MetricCounter++;
}
/*Burner Error State*/
if (getBurnerError() == FAULT)
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "burner_fault_pending");
aMetrics[u16MetricCounter].fMetricValue = 1.0f;
u16MetricCounter++;
}
else
{
strcpy(aMetrics[u16MetricCounter].caMetricName, "burner_fault_pending");
aMetrics[u16MetricCounter].fMetricValue = 0.0f;
u16MetricCounter++;
}
/*Chamber Temperature*/
strcpy(aMetrics[u16MetricCounter].caMetricName, "chamber_temperature");
aMetrics[u16MetricCounter].fMetricValue = getChamberTemperature();
u16MetricCounter++;
/*Outdoor Temperature*/
strcpy(aMetrics[u16MetricCounter].caMetricName, "outdoor_temperature");
aMetrics[u16MetricCounter].fMetricValue = getOutdoorTemperature();
u16MetricCounter++;
/*Chamber Temperature*/
strcpy(aMetrics[u16MetricCounter].caMetricName, "inlet_flow_temperature");
aMetrics[u16MetricCounter].fMetricValue = getInletFlowTemperature();
u16MetricCounter++;
/*Chamber Temperature*/
strcpy(aMetrics[u16MetricCounter].caMetricName, "return_flow_temperature");
aMetrics[u16MetricCounter].fMetricValue = getReturnFlowTemperature();
u16MetricCounter++;
vSetMetrics(aMetrics, u16MetricCounter);
}
}
void vSetMetrics(sMetric *paMetrics, uint16_t u16Size)
{
if (xSemaphoreTake(xMutexAccessMetricResponse, (TickType_t)100) == pdTRUE)
{
memset(caHtmlResponse, 0, strlen(caHtmlResponse));
for (uint16_t u16Index = 0U; u16Index < u16Size; u16Index++)
{
char caValueBuffer[64];
sprintf(caValueBuffer, " %f", paMetrics[u16Index].fMetricValue);
// printf("%s\n", caValueBuffer);
strcat(caHtmlResponse, paMetrics[u16Index].caMetricName);
strcat(caHtmlResponse, caValueBuffer);
strcat(caHtmlResponse, "\n");
}
xSemaphoreGive(xMutexAccessMetricResponse);
}
else
{
ESP_LOGI(TAG, "[SET] Unable to obtain mutex for metric response");
}
}
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
esp_wifi_connect();
ESP_LOGI(TAG, "Retry to connect to the AP");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip));
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void connect_wifi(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
esp_netif_dhcpc_stop(my_sta);
esp_netif_ip_info_t ip_info;
ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR);
ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR);
ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK);
esp_netif_set_ip_info(my_sta, &ip_info);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_SSID,
.password = CONFIG_WIFI_PASSWORD,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "Connected to ap SSID:%s", CONFIG_SSID);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_SSID);
}
else
{
ESP_LOGE(TAG, "Unexpected event");
}
vEventGroupDelete(s_wifi_event_group);
}
esp_err_t get_metrics_handler(httpd_req_t *req)
{
if (xSemaphoreTake(xMutexAccessMetricResponse, (TickType_t)100) == pdTRUE)
{
esp_err_t err = httpd_resp_send(req, caHtmlResponse, HTTPD_RESP_USE_STRLEN);
xSemaphoreGive(xMutexAccessMetricResponse);
return err;
}
else
{
ESP_LOGI(TAG, "[GET] Unable to obtain mutex for metric response");
return httpd_resp_send(req, 0, 0);
}
}
httpd_uri_t uri_get = {
.uri = "/metrics",
.method = HTTP_GET,
.handler = get_metrics_handler,
.user_ctx = NULL};
httpd_handle_t setup_server(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = 9100;
httpd_handle_t server = NULL;
xMutexAccessMetricResponse = xSemaphoreCreateBinary();
if (xMutexAccessMetricResponse == NULL)
{
ESP_LOGE(TAG, "Unable to create mutex for metric response");
vTaskDelay(pdMS_TO_TICKS(300 * 60)); // wait 5min before restart
esp_restart();
}
xSemaphoreGive(xMutexAccessMetricResponse);
if (httpd_start(&server, &config) == ESP_OK)
{
httpd_register_uri_handler(server, &uri_get);
}
return server;
}