From fd7b3951ff33d2ada2b90d8017ce30dd18899dd4 Mon Sep 17 00:00:00 2001 From: Niklas Jensen Date: Sat, 3 Jan 2026 19:44:05 +0100 Subject: [PATCH] System status esp + svelte - protobuf complete --- .../routes/system/status/SystemStatus.svelte | 18 +++++--- esp32/include/global.h | 16 +++---- esp32/include/system_service.h | 2 +- esp32/src/main.cpp | 20 +++++--- esp32/src/system_service.cpp | 46 +++++++------------ platform_shared/message.options | 12 ++--- platform_shared/message.proto | 11 +++++ 7 files changed, 68 insertions(+), 57 deletions(-) diff --git a/app/src/routes/system/status/SystemStatus.svelte b/app/src/routes/system/status/SystemStatus.svelte index 7764fc7..b422c3b 100644 --- a/app/src/routes/system/status/SystemStatus.svelte +++ b/app/src/routes/system/status/SystemStatus.svelte @@ -32,19 +32,23 @@ import StatusItem from '$lib/components/StatusItem.svelte' import ActionButton from './ActionButton.svelte' import { AnalyticsData, type SystemInformation } from '$lib/platform_shared/message' + import Error from '../../+error.svelte' + import { notifications } from '$lib/components/toasts/notifications' const features = useFeatureFlags() let systemInformation: SystemInformation | null = $state(null) async function getSystemStatus() { - const result = await api.get('/api/system/status') - if (result.isErr()) { - console.error('Error:', result.inner) - return - } - systemInformation = result.inner - return systemInformation + socket + .request({ systemInformationRequest: {} }) + .then(response => { + if (response.systemInformationResponse) { + systemInformation = response.systemInformationResponse + return systemInformation; + } else { throw new TypeError("System Information not found in reponse") } + }) + return } const postFactoryReset = async () => await api.post('/api/system/reset') diff --git a/esp32/include/global.h b/esp32/include/global.h index 1521963..e6b1846 100644 --- a/esp32/include/global.h +++ b/esp32/include/global.h @@ -4,23 +4,23 @@ #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 #include "esp32/rom/rtc.h" -#ifndef ESP_PLATFORM -#define ESP_PLATFORM "ESP32" +#ifndef ESP_PLATFORM_NAME +#define ESP_PLATFORM_NAME "ESP32" #endif #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32/rom/rtc.h" -#ifndef ESP_PLATFORM -#define ESP_PLATFORM "ESP32-S2" +#ifndef ESP_PLATFORM_NAME +#define ESP_PLATFORM_NAME "ESP32-S2" #endif #elif CONFIG_IDF_TARGET_ESP32C3 #include "esp32c3/rom/rtc.h" -#ifndef ESP_PLATFORM -#define ESP_PLATFORM "ESP32-C3" +#ifndef ESP_PLATFORM_NAME +#define ESP_PLATFORM_NAME "ESP32-C3" #endif #elif CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/rom/rtc.h" -#ifndef ESP_PLATFORM -#define ESP_PLATFORM "ESP32-S3" +#ifndef ESP_PLATFORM_NAME +#define ESP_PLATFORM_NAME "ESP32-S3" #endif #else #error Target CONFIG_IDF_TARGET is not supported diff --git a/esp32/include/system_service.h b/esp32/include/system_service.h index 50df30b..ac5385d 100644 --- a/esp32/include/system_service.h +++ b/esp32/include/system_service.h @@ -16,13 +16,13 @@ namespace system_service { esp_err_t handleReset(PsychicRequest *request); esp_err_t handleRestart(PsychicRequest *request); esp_err_t handleSleep(PsychicRequest *request); -esp_err_t getStatus(PsychicRequest *request); void reset(); void restart(); void sleep(); void status(JsonObject &root); void getAnalytics(socket_message_AnalyticsData &analytics); +void getStaticSystemInformation(socket_message_StaticSystemInformation &info); const char *resetReason(esp_reset_reason_t reason); } // namespace system_service \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index d88fb14..8cd15d0 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -46,9 +46,6 @@ void setupServer() { server.config.max_uri_handlers = 50 + WWW_ASSETS_COUNT; server.maxUploadSize = 1000000; // 1 MB; server.listen(80); - server.on("/api/features", feature_service::getFeatures); - server.on("/api/system/status", HTTP_GET, - [&](PsychicRequest *request) { return system_service::getStatus(request); }); server.on("/api/system/reset", HTTP_POST, [&](PsychicRequest *request, JsonVariant &json) { return system_service::handleReset(request); }); server.on("/api/system/restart", HTTP_POST, @@ -162,22 +159,33 @@ void setupEventSocket() { using CorrelationHandler = std::function; static std::map correlationHandlers = { - {socket_message_CorrelationRequest_features_data_request_tag, + {socket_message_CorrelationRequest_features_data_request_tag, // Features data [](const auto &req, auto &res) { res.which_response = socket_message_CorrelationResponse_features_data_response_tag; feature_service::features_request(req.request.features_data_request, res.response.features_data_response); }}, - {socket_message_CorrelationRequest_i2c_scan_data_request_tag, + + {socket_message_CorrelationRequest_i2c_scan_data_request_tag, // i2c data [](const auto &req, auto &res) { res.which_response = socket_message_CorrelationResponse_i2c_scan_data_tag; peripherals.scanI2C(); peripherals.getI2CScanProto(res.response.i2c_scan_data); }}, - {socket_message_CorrelationRequest_imu_calibrate_execute_tag, + + {socket_message_CorrelationRequest_imu_calibrate_execute_tag, // Calibration request [](const auto &req, auto &res) { res.which_response = socket_message_CorrelationResponse_imu_calibrate_data_tag; res.response.imu_calibrate_data.success = peripherals.calibrateIMU(); }}, + + {socket_message_CorrelationRequest_system_information_request_tag, // All system information data + [](const auto &req, auto &res) { + res.which_response = socket_message_CorrelationResponse_system_information_response_tag; + res.response.system_information_response.has_analytics_data = true; + res.response.system_information_response.has_static_system_information = true; + system_service::getAnalytics(res.response.system_information_response.analytics_data); + system_service::getStaticSystemInformation(res.response.system_information_response.static_system_information); + }}, }; socket.on([&](const socket_message_CorrelationRequest &data, int clientId) { diff --git a/esp32/src/system_service.cpp b/esp32/src/system_service.cpp index b011c4f..233d124 100644 --- a/esp32/src/system_service.cpp +++ b/esp32/src/system_service.cpp @@ -19,12 +19,7 @@ esp_err_t handleSleep(PsychicRequest *request) { return request->reply(200); } -esp_err_t getStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false); - JsonObject root = response.getRoot(); - status(root); - return response.send(); -} + void reset() { ESP_LOGI(TAG, "Resetting device"); @@ -78,32 +73,25 @@ void sleep() { ESP_LOGI(TAG, "Setting device to sleep"); } -void status(JsonObject &root) { + + +void getStaticSystemInformation(socket_message_StaticSystemInformation &info) { size_t fs_total = 0, fs_used = 0; esp_littlefs_info("spiffs", &fs_total, &fs_used); - root["esp_platform"] = ESP_PLATFORM; - root["firmware_version"] = APP_VERSION; - root["max_alloc_heap"] = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); - root["psram_size"] = heap_caps_get_total_size(MALLOC_CAP_SPIRAM); - root["free_psram"] = heap_caps_get_free_size(MALLOC_CAP_SPIRAM); - root["cpu_freq_mhz"] = ESP.getCpuFreqMHz(); - root["cpu_type"] = ESP.getChipModel(); - root["cpu_rev"] = ESP.getChipRevision(); - root["cpu_cores"] = ESP.getChipCores(); - root["free_heap"] = esp_get_free_heap_size(); - root["min_free_heap"] = esp_get_minimum_free_heap_size(); - root["sketch_size"] = ESP.getSketchSize(); - root["free_sketch_space"] = ESP.getFreeSketchSpace(); - root["sdk_version"] = ESP.getSdkVersion(); - root["arduino_version"] = ARDUINO_VERSION; - root["flash_chip_size"] = ESP.getFlashChipSize(); - root["flash_chip_speed"] = ESP.getFlashChipSpeed(); - root["fs_total"] = fs_total; - root["fs_used"] = fs_used; - root["core_temp"] = temperatureRead(); - root["cpu_reset_reason"] = resetReason(esp_reset_reason()); - root["uptime"] = esp_timer_get_time() / 1000000; + info.esp_platform = (char*) ESP_PLATFORM_NAME; + info.firmware_version = APP_VERSION; + info.cpu_freq_mhz = ESP.getCpuFreqMHz(); + info.cpu_type = (char*) ESP.getChipModel(); + info.cpu_rev = ESP.getChipRevision(); + info.cpu_cores = ESP.getChipCores(); + info.sketch_size = ESP.getSketchSize(); + info.free_sketch_space = ESP.getFreeSketchSpace(); + info.sdk_version = (char*) ESP.getSdkVersion(); + info.arduino_version = ARDUINO_VERSION; + info.flash_chip_size = ESP.getFlashChipSize(); + info.flash_chip_speed = ESP.getFlashChipSpeed(); + info.cpu_reset_reason = (char*) resetReason(esp_reset_reason()); } void getAnalytics(socket_message_AnalyticsData &analytics) { diff --git a/platform_shared/message.options b/platform_shared/message.options index a51ac56..e98365c 100644 --- a/platform_shared/message.options +++ b/platform_shared/message.options @@ -13,12 +13,12 @@ socket_message.KnownNetworkItem.gateway_ip max_size:16 socket_message.KnownNetworkItem.dns_ip_1 max_size:16 socket_message.KnownNetworkItem.dns_ip_2 max_size:16 -socket_message.StaticSystemInformation.esp_platform max_size:32 -socket_message.StaticSystemInformation.firmware_version max_size:16 -socket_message.StaticSystemInformation.cpu_type max_size:32 -socket_message.StaticSystemInformation.sdk_version max_size:32 -socket_message.StaticSystemInformation.arduino_version max_size:16 -socket_message.StaticSystemInformation.cpu_reset_reason max_size:32 +socket_message.StaticSystemInformation.esp_platform type:FT_POINTER +socket_message.StaticSystemInformation.firmware_version type:FT_POINTER +socket_message.StaticSystemInformation.cpu_type type:FT_POINTER +socket_message.StaticSystemInformation.sdk_version type:FT_POINTER +socket_message.StaticSystemInformation.arduino_version type:FT_POINTER +socket_message.StaticSystemInformation.cpu_reset_reason type:FT_POINTER socket_message.AnglesData.angles max_count:12 diff --git a/platform_shared/message.proto b/platform_shared/message.proto index a8a62c0..dd03e1c 100644 --- a/platform_shared/message.proto +++ b/platform_shared/message.proto @@ -19,6 +19,7 @@ message IMUData { float pressure = 7; } +// FEATURE DATA message FeaturesDataResponse { string variant = 10; string firmware_built_target = 20; @@ -38,12 +39,21 @@ message FeaturesDataResponse { message FeaturesDataRequest { } +// SYSTEM INFORMATION + +message SystemInformationRequest { } +message SystemInformationResponse { + AnalyticsData analytics_data = 1; + StaticSystemInformation static_system_information = 2; +} + message CorrelationRequest { uint32 correlation_id = 1; oneof request { FeaturesDataRequest features_data_request = 10; I2CScanDataRequest i2c_scan_data_request = 20; IMUCalibrateExecute imu_calibrate_execute = 30; + SystemInformationRequest system_information_request = 40; } } @@ -54,6 +64,7 @@ message CorrelationResponse { FeaturesDataResponse features_data_response = 10; I2CScanData i2c_scan_data = 20; IMUCalibrateData imu_calibrate_data = 30; + SystemInformationResponse system_information_response = 40; } }