feat: Add API endpoints for system reboot and retrieving system information.
This commit is contained in:
66
Provider/main/api/system/info.cpp
Normal file
66
Provider/main/api/system/info.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_app_format.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
|
||||
static esp_err_t api_system_info_handler(httpd_req_t *req) {
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
esp_chip_info_t chip_info;
|
||||
esp_chip_info(&chip_info);
|
||||
|
||||
const char *model = "ESP32-S3";
|
||||
if (chip_info.model == CHIP_ESP32) {
|
||||
model = "ESP32";
|
||||
} else if (chip_info.model == CHIP_ESP32S2) {
|
||||
model = "ESP32-S2";
|
||||
} else if (chip_info.model == CHIP_ESP32S3) {
|
||||
model = "ESP32-S3";
|
||||
} else if (chip_info.model == CHIP_ESP32C3) {
|
||||
model = "ESP32-C3";
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(root, "chip", model);
|
||||
|
||||
uint32_t free_heap = esp_get_free_heap_size();
|
||||
cJSON_AddNumberToObject(root, "free_heap", free_heap);
|
||||
|
||||
uint64_t uptime_sec = esp_timer_get_time() / 1000000;
|
||||
cJSON_AddNumberToObject(root, "uptime", uptime_sec);
|
||||
|
||||
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||
cJSON_AddStringToObject(root, "firmware", app_desc->version);
|
||||
|
||||
// Relying on internal variables from main.cpp due to unity build
|
||||
extern bool ethernetInitialized;
|
||||
extern bool wifiInitialized;
|
||||
|
||||
const char *conn_type = "offline";
|
||||
if (ethernetInitialized) {
|
||||
conn_type = "ethernet";
|
||||
} else if (wifiInitialized) {
|
||||
conn_type = "wifi";
|
||||
}
|
||||
cJSON_AddStringToObject(root, "connection", conn_type);
|
||||
|
||||
const char *sys_info = cJSON_Print(root);
|
||||
httpd_resp_sendstr(req, sys_info);
|
||||
|
||||
free((void *)sys_info);
|
||||
cJSON_Delete(root);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static const httpd_uri_t api_system_info_uri = {.uri = "/api/system/info",
|
||||
.method = HTTP_GET,
|
||||
.handler =
|
||||
api_system_info_handler,
|
||||
.user_ctx = NULL};
|
||||
Reference in New Issue
Block a user