🎛️ Adds system service
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
const features = useFeatureFlags();
|
||||
|
||||
const postSleep = async () => await api.post('/api/sleep');
|
||||
const postSleep = async () => await api.post('/api/system/sleep');
|
||||
|
||||
const confirmSleep = () => {
|
||||
openModal(ConfirmDialog, {
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
let systemInformation: SystemInformation;
|
||||
|
||||
async function getSystemStatus() {
|
||||
const result = await api.get<SystemInformation>('/api/systemStatus');
|
||||
const result = await api.get<SystemInformation>('/api/system/status');
|
||||
if (result.isErr()) {
|
||||
console.error('Error:', result.inner);
|
||||
return;
|
||||
@@ -46,7 +46,7 @@
|
||||
return systemInformation;
|
||||
}
|
||||
|
||||
const postFactoryReset = async () => await api.post('/api/factoryReset');
|
||||
const postFactoryReset = async () => await api.post('/api/system/reset');
|
||||
|
||||
const postSleep = async () => await api.post('api/sleep');
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
const handleSystemData = (data: Analytics) =>
|
||||
(systemInformation = { ...systemInformation, ...data });
|
||||
|
||||
const postRestart = async () => await api.post('/api/restart');
|
||||
const postRestart = async () => await api.post('/api/system/restart');
|
||||
|
||||
function confirmRestart() {
|
||||
openModal(ConfirmDialog, {
|
||||
|
||||
@@ -43,4 +43,8 @@ build_flags =
|
||||
-D FACTORY_SERVO_NUM=12
|
||||
-D FACTORY_SERVO_OSCILLATOR_FREQUENCY=27000000
|
||||
-D FACTORY_SERVO_PWM_FREQUENCY=50
|
||||
-D FACTORY_SERVO_CENTER_ANGLE=90
|
||||
-D FACTORY_SERVO_CENTER_ANGLE=90
|
||||
|
||||
; Deep Sleep Configuration
|
||||
-D WAKEUP_PIN_NUMBER=38 ; pin number to wake up the ESP
|
||||
-D WAKEUP_SIGNAL=0 ; 1 for wakeup on HIGH, 0 for wakeup on LOW
|
||||
@@ -44,9 +44,6 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
|
||||
_cameraService(server, &_taskManager),
|
||||
_cameraSettingsService(server, &ESPFS, &_socket),
|
||||
#endif
|
||||
_restartService(server),
|
||||
_factoryResetService(server, &ESPFS),
|
||||
_systemStatus(server),
|
||||
_fileExplorer(server),
|
||||
_servoController(server, &ESPFS, &_peripherals, &_socket),
|
||||
#if FT_ENABLED(USE_MOTION)
|
||||
@@ -100,6 +97,13 @@ void ESP32SvelteKit::setupServer() {
|
||||
return _apService.endpoint.handleStateUpdate(request, json);
|
||||
});
|
||||
|
||||
// SYSTEM
|
||||
_server->on("/api/system/reset", HTTP_POST, system_service::handleReset);
|
||||
_server->on("/api/system/restart", HTTP_POST, system_service::handleRestart);
|
||||
_server->on("/api/system/sleep", HTTP_POST, system_service::handleSleep);
|
||||
_server->on("/api/system/status", HTTP_GET, system_service::getStatus);
|
||||
_server->on("/api/system/metrics", HTTP_GET, system_service::getMetrics);
|
||||
|
||||
// servo
|
||||
_server->on("/api/servo/config", HTTP_GET,
|
||||
[this](PsychicRequest *request) { return _servoController.endpoint.getState(request); });
|
||||
@@ -169,10 +173,7 @@ void ESP32SvelteKit::setupMDNS() {
|
||||
void ESP32SvelteKit::startServices() {
|
||||
_apService.begin();
|
||||
_socket.begin();
|
||||
_factoryResetService.begin();
|
||||
_featureService.begin();
|
||||
_restartService.begin();
|
||||
_systemStatus.begin();
|
||||
|
||||
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
||||
_uploadFirmwareService.begin();
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include <ESPmDNS.h>
|
||||
#include <LEDService.h>
|
||||
#include <EventSocket.h>
|
||||
#include <FactoryResetService.h>
|
||||
#include <FeaturesService.h>
|
||||
#include <MotionService.h>
|
||||
#include <NTPSettingsService.h>
|
||||
@@ -36,9 +35,6 @@
|
||||
#include <CameraSettingsService.h>
|
||||
#include <NTPStatus.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <RestartService.h>
|
||||
#include <SleepService.h>
|
||||
#include <SystemStatus.h>
|
||||
#include <TaskManager.h>
|
||||
#include <UploadFirmwareService.h>
|
||||
#include <WiFi.h>
|
||||
@@ -82,10 +78,6 @@ class ESP32SvelteKit {
|
||||
StatefulService<NTPSettings> *getNTPSettingsService() { return &_ntpSettingsService; }
|
||||
#endif
|
||||
|
||||
#if FT_ENABLED(USE_SLEEP)
|
||||
SleepService *getSleepService() { return &_sleepService; }
|
||||
#endif
|
||||
|
||||
#if FT_ENABLED(USE_BATTERY)
|
||||
BatteryService *getBatteryService() { return &_batteryService; }
|
||||
#endif
|
||||
@@ -111,8 +103,6 @@ class ESP32SvelteKit {
|
||||
ServoController *getServoController() { return &_servoController; }
|
||||
#endif
|
||||
|
||||
void factoryReset() { _factoryResetService.factoryReset(); }
|
||||
|
||||
void setMDNSAppName(String name) { _appName = name; }
|
||||
|
||||
void recoveryMode() { _apService.recoveryMode(); }
|
||||
@@ -136,18 +126,12 @@ class ESP32SvelteKit {
|
||||
#if FT_ENABLED(USE_DOWNLOAD_FIRMWARE)
|
||||
DownloadFirmwareService _downloadFirmwareService;
|
||||
#endif
|
||||
#if FT_ENABLED(USE_SLEEP)
|
||||
SleepService _sleepService;
|
||||
#endif
|
||||
#if FT_ENABLED(USE_BATTERY)
|
||||
BatteryService _batteryService;
|
||||
#endif
|
||||
#if FT_ENABLED(USE_ANALYTICS)
|
||||
AnalyticsService _analyticsService;
|
||||
#endif
|
||||
RestartService _restartService;
|
||||
FactoryResetService _factoryResetService;
|
||||
SystemStatus _systemStatus;
|
||||
TaskManager _taskManager;
|
||||
FileExplorer _fileExplorer;
|
||||
#if FT_ENABLED(USE_MOTION)
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <FactoryResetService.h>
|
||||
|
||||
FactoryResetService::FactoryResetService(PsychicHttpServer *server, FS *fs) : _server(server), fs(fs) {}
|
||||
|
||||
void FactoryResetService::begin() {
|
||||
_server->on(FACTORY_RESET_SERVICE_PATH, HTTP_POST,
|
||||
[this](PsychicRequest *request) { return handleRequest(request); });
|
||||
|
||||
ESP_LOGV("FactoryResetService", "Registered POST endpoint: %s", FACTORY_RESET_SERVICE_PATH);
|
||||
}
|
||||
|
||||
esp_err_t FactoryResetService::handleRequest(PsychicRequest *request) {
|
||||
request->reply(200);
|
||||
factoryReset();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete function assumes that all files are stored flat, within the config directory.
|
||||
*/
|
||||
void FactoryResetService::factoryReset() {
|
||||
File root = fs->open(FS_CONFIG_DIRECTORY);
|
||||
File file;
|
||||
while (file = root.openNextFile()) {
|
||||
String path = file.path();
|
||||
file.close();
|
||||
fs->remove(path);
|
||||
}
|
||||
RestartService::restartNow();
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
#ifndef FactoryResetService_h
|
||||
#define FactoryResetService_h
|
||||
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
#include <RestartService.h>
|
||||
#include <FS.h>
|
||||
#include <ESPFS.h>
|
||||
|
||||
#define FACTORY_RESET_SERVICE_PATH "/api/factoryReset"
|
||||
|
||||
class FactoryResetService {
|
||||
FS *fs;
|
||||
|
||||
public:
|
||||
FactoryResetService(PsychicHttpServer *server, FS *fs);
|
||||
|
||||
void begin();
|
||||
void factoryReset();
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
esp_err_t handleRequest(PsychicRequest *request);
|
||||
};
|
||||
|
||||
#endif // end FactoryResetService_h
|
||||
@@ -1,29 +0,0 @@
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <RestartService.h>
|
||||
|
||||
RestartService::RestartService(PsychicHttpServer *server) : _server(server) {}
|
||||
|
||||
void RestartService::begin() {
|
||||
_server->on(RESTART_SERVICE_PATH, HTTP_POST, [this](PsychicRequest *request) { return restart(request); });
|
||||
|
||||
ESP_LOGV("RestartService", "Registered POST endpoint: %s", RESTART_SERVICE_PATH);
|
||||
}
|
||||
|
||||
esp_err_t RestartService::restart(PsychicRequest *request) {
|
||||
request->reply(200);
|
||||
restartNow();
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
#ifndef RestartService_h
|
||||
#define RestartService_h
|
||||
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
|
||||
#define RESTART_SERVICE_PATH "/api/restart"
|
||||
|
||||
class RestartService {
|
||||
public:
|
||||
RestartService(PsychicHttpServer *server);
|
||||
|
||||
void begin();
|
||||
|
||||
static void restartNow() {
|
||||
WiFi.disconnect(true);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
esp_err_t restart(PsychicRequest *request);
|
||||
};
|
||||
|
||||
#endif // end RestartService_h
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <SleepService.h>
|
||||
|
||||
// Definition of static member variable
|
||||
void (*SleepService::_callbackSleep)() = nullptr;
|
||||
|
||||
SleepService::SleepService(PsychicHttpServer *server) : _server(server) {}
|
||||
|
||||
void SleepService::begin() {
|
||||
_server->on(SLEEP_SERVICE_PATH, HTTP_POST, [this](PsychicRequest *request) { return sleep(request); });
|
||||
|
||||
ESP_LOGV("SleepService", "Registered POST endpoint: %s", SLEEP_SERVICE_PATH);
|
||||
}
|
||||
|
||||
esp_err_t SleepService::sleep(PsychicRequest *request) {
|
||||
request->reply(200);
|
||||
sleepNow();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void SleepService::sleepNow() {
|
||||
ESP_LOGI("SleepService", "Going into deep sleep now");
|
||||
// Callback for main code sleep preparation
|
||||
if (_callbackSleep != nullptr) {
|
||||
_callbackSleep();
|
||||
}
|
||||
delay(100);
|
||||
|
||||
MDNS.end();
|
||||
delay(100);
|
||||
|
||||
WiFi.disconnect(true);
|
||||
delay(500);
|
||||
|
||||
// Prepare ESP for sleep
|
||||
uint64_t bitmask = (uint64_t)1 << (WAKEUP_PIN_NUMBER);
|
||||
|
||||
// special treatment for ESP32-C3 because of the RISC-V architecture
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32C3
|
||||
esp_deep_sleep_enable_gpio_wakeup(bitmask, (esp_deepsleep_gpio_wake_up_mode_t)WAKEUP_SIGNAL);
|
||||
#else
|
||||
esp_sleep_enable_ext1_wakeup(bitmask, (esp_sleep_ext1_wakeup_mode_t)WAKEUP_SIGNAL);
|
||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
||||
#endif
|
||||
|
||||
// Just to be sure
|
||||
delay(100);
|
||||
|
||||
// Hibernate
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ESPmDNS.h>
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
|
||||
#define SLEEP_SERVICE_PATH "/api/sleep"
|
||||
|
||||
#ifndef WAKEUP_PIN_NUMBER
|
||||
#define WAKEUP_PIN_NUMBER 0
|
||||
#endif
|
||||
|
||||
#ifndef WAKEUP_SIGNAL
|
||||
#define WAKEUP_SIGNAL 0
|
||||
#endif
|
||||
|
||||
class SleepService {
|
||||
public:
|
||||
SleepService(PsychicHttpServer *server);
|
||||
|
||||
void begin();
|
||||
|
||||
static void sleepNow();
|
||||
|
||||
void attachOnSleepCallback(void (*callbackSleep)()) { _callbackSleep = callbackSleep; }
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
esp_err_t sleep(PsychicRequest *request);
|
||||
|
||||
protected:
|
||||
static void (*_callbackSleep)();
|
||||
};
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <SystemStatus.h>
|
||||
|
||||
String verbosePrintResetReason(int reason) {
|
||||
switch (reason) {
|
||||
case 1: return ("Vbat power on reset"); break;
|
||||
case 3: return ("Software reset digital core"); break;
|
||||
case 4: return ("Legacy watch dog reset digital core"); break;
|
||||
case 5: return ("Deep Sleep reset digital core"); break;
|
||||
case 6: return ("Reset by SLC module, reset digital core"); break;
|
||||
case 7: return ("Timer Group0 Watch dog reset digital core"); break;
|
||||
case 8: return ("Timer Group1 Watch dog reset digital core"); break;
|
||||
case 9: return ("RTC Watch dog Reset digital core"); break;
|
||||
case 10: return ("Intrusion tested to reset CPU"); break;
|
||||
case 11: return ("Time Group reset CPU"); break;
|
||||
case 12: return ("Software reset CPU"); break;
|
||||
case 13: return ("RTC Watch dog Reset CPU"); break;
|
||||
case 14: return ("for APP CPU, reseted by PRO CPU"); break;
|
||||
case 15: return ("Reset when the vdd voltage is not stable"); break;
|
||||
case 16: return ("RTC Watch dog reset digital core and rtc module"); break;
|
||||
default: return ("NO_MEAN");
|
||||
}
|
||||
}
|
||||
|
||||
SystemStatus::SystemStatus(PsychicHttpServer *server) : _server(server) {}
|
||||
|
||||
void SystemStatus::begin() {
|
||||
_server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET,
|
||||
[this](PsychicRequest *request) { return SystemStatus::systemStatus(request); });
|
||||
|
||||
ESP_LOGV("SystemStatus", "Registered GET endpoint: %s", SYSTEM_STATUS_SERVICE_PATH);
|
||||
}
|
||||
|
||||
esp_err_t SystemStatus::systemStatus(PsychicRequest *request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonObject root = response.getRoot();
|
||||
|
||||
root["esp_platform"] = ESP_PLATFORM;
|
||||
root["firmware_version"] = APP_VERSION;
|
||||
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
|
||||
root["psram_size"] = ESP.getPsramSize();
|
||||
root["free_psram"] = ESP.getFreePsram();
|
||||
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.getFreeHeap();
|
||||
root["min_free_heap"] = ESP.getMinFreeHeap();
|
||||
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"] = ESPFS.totalBytes();
|
||||
root["fs_used"] = ESPFS.usedBytes();
|
||||
root["core_temp"] = temperatureRead();
|
||||
root["cpu_reset_reason"] = verbosePrintResetReason(rtc_get_reset_reason(0));
|
||||
root["uptime"] = millis() / 1000;
|
||||
|
||||
return response.send();
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
#ifndef SystemStatus_h
|
||||
#define SystemStatus_h
|
||||
|
||||
/**
|
||||
* ESP32 SvelteKit
|
||||
*
|
||||
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
|
||||
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
|
||||
* https://github.com/theelims/ESP32-sveltekit
|
||||
*
|
||||
* Copyright (C) 2018 - 2023 rjwats
|
||||
* Copyright (C) 2023 theelims
|
||||
*
|
||||
* All Rights Reserved. This software may be modified and distributed under
|
||||
* the terms of the LGPL v3 license. See the LICENSE file for details.
|
||||
**/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <ESPFS.h>
|
||||
#include <esp32-hal.h>
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
|
||||
#include "esp32/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
#include "esp32/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-S2"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#include "esp32c3/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-C3"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32S3
|
||||
#include "esp32s3/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-S3"
|
||||
#endif
|
||||
#else
|
||||
#error Target CONFIG_IDF_TARGET is not supported
|
||||
#endif
|
||||
|
||||
#ifndef ARDUINO_VERSION
|
||||
#ifndef STRINGIZE
|
||||
#define STRINGIZE(s) #s
|
||||
#endif
|
||||
#define ARDUINO_VERSION_STR(major, minor, patch) "v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch)
|
||||
#define ARDUINO_VERSION \
|
||||
ARDUINO_VERSION_STR(ESP_ARDUINO_VERSION_MAJOR, ESP_ARDUINO_VERSION_MINOR, ESP_ARDUINO_VERSION_PATCH)
|
||||
#endif
|
||||
|
||||
#define SYSTEM_STATUS_SERVICE_PATH "/api/systemStatus"
|
||||
|
||||
class SystemStatus {
|
||||
public:
|
||||
SystemStatus(PsychicHttpServer *server);
|
||||
|
||||
void begin();
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
esp_err_t systemStatus(PsychicRequest *request);
|
||||
};
|
||||
|
||||
#endif // end SystemStatus_h
|
||||
@@ -125,7 +125,7 @@ esp_err_t UploadFirmwareService::uploadComplete(PsychicRequest *request) {
|
||||
// if no error, send the success response
|
||||
if (!request->_tempObject) {
|
||||
request->reply(200);
|
||||
RestartService::restartNow();
|
||||
system_service::restart();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
#include <RestartService.h>
|
||||
#include <system_service.h>
|
||||
|
||||
#define UPLOAD_FIRMWARE_PATH "/api/uploadFirmware"
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp32-hal.h>
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
|
||||
#include "esp32/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
#include "esp32/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-S2"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#include "esp32c3/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-C3"
|
||||
#endif
|
||||
#elif CONFIG_IDF_TARGET_ESP32S3
|
||||
#include "esp32s3/rom/rtc.h"
|
||||
#ifndef ESP_PLATFORM
|
||||
#define ESP_PLATFORM "ESP32-S3"
|
||||
#endif
|
||||
#else
|
||||
#error Target CONFIG_IDF_TARGET is not supported
|
||||
#endif
|
||||
|
||||
#ifndef ARDUINO_VERSION
|
||||
#ifndef STRINGIFY
|
||||
#define STRINGIFY(s) #s
|
||||
#endif
|
||||
#define ARDUINO_VERSION_STR(major, minor, patch) "v" STRINGIFY(major) "." STRINGIFY(minor) "." STRINGIFY(patch)
|
||||
#define ARDUINO_VERSION \
|
||||
ARDUINO_VERSION_STR(ESP_ARDUINO_VERSION_MAJOR, ESP_ARDUINO_VERSION_MINOR, ESP_ARDUINO_VERSION_PATCH)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* I2C software connection
|
||||
*/
|
||||
#ifndef SDA_PIN
|
||||
#define SDA_PIN SDA
|
||||
#endif
|
||||
#ifndef SCL_PIN
|
||||
#define SCL_PIN SCL
|
||||
#endif
|
||||
#ifndef I2C_FREQUENCY
|
||||
#define I2C_FREQUENCY 100000UL
|
||||
#endif
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "system_service.h"
|
||||
|
||||
namespace system_service {
|
||||
|
||||
static const char *TAG = "SystemService";
|
||||
|
||||
esp_err_t handleReset(PsychicRequest *request) {
|
||||
reset();
|
||||
return request->reply(200);
|
||||
}
|
||||
|
||||
esp_err_t handleRestart(PsychicRequest *request) {
|
||||
restart();
|
||||
return request->reply(200);
|
||||
}
|
||||
|
||||
esp_err_t handleSleep(PsychicRequest *request) {
|
||||
sleep();
|
||||
return request->reply(200);
|
||||
}
|
||||
|
||||
esp_err_t getStatus(PsychicRequest *request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonObject root = response.getRoot();
|
||||
status(root);
|
||||
return response.send();
|
||||
}
|
||||
|
||||
esp_err_t getMetrics(PsychicRequest *request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonObject root = response.getRoot();
|
||||
metrics(root);
|
||||
return response.send();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
ESP_LOGI(TAG, "Resetting device");
|
||||
File root = ESPFS.open(FS_CONFIG_DIRECTORY);
|
||||
File file;
|
||||
while (file = root.openNextFile()) {
|
||||
String path = file.path();
|
||||
file.close();
|
||||
ESPFS.remove(path);
|
||||
}
|
||||
restart();
|
||||
}
|
||||
|
||||
void restart() {
|
||||
xTaskCreate(
|
||||
[](void *pvParameters) {
|
||||
for (;;) {
|
||||
delay(250);
|
||||
MDNS.end();
|
||||
delay(100);
|
||||
WiFi.disconnect(true);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
},
|
||||
"Restart task", 4096, nullptr, 10, nullptr);
|
||||
}
|
||||
|
||||
void sleep() {
|
||||
xTaskCreate(
|
||||
[](void *pvParameters) {
|
||||
for (;;) {
|
||||
delay(250);
|
||||
MDNS.end();
|
||||
delay(100);
|
||||
WiFi.disconnect(true);
|
||||
delay(500);
|
||||
|
||||
uint64_t bitmask = (uint64_t)1 << (WAKEUP_PIN_NUMBER);
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32C3
|
||||
esp_deep_sleep_enable_gpio_wakeup(bitmask, (esp_deepsleep_gpio_wake_up_mode_t)WAKEUP_SIGNAL);
|
||||
#else
|
||||
esp_sleep_enable_ext1_wakeup(bitmask, (esp_sleep_ext1_wakeup_mode_t)WAKEUP_SIGNAL);
|
||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
||||
#endif
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
},
|
||||
"Sleep task", 4096, nullptr, 10, nullptr);
|
||||
ESP_LOGI(TAG, "Setting device to sleep");
|
||||
}
|
||||
|
||||
void status(JsonObject &root) {
|
||||
root["esp_platform"] = ESP_PLATFORM;
|
||||
root["firmware_version"] = APP_VERSION;
|
||||
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
|
||||
root["psram_size"] = ESP.getPsramSize();
|
||||
root["free_psram"] = ESP.getFreePsram();
|
||||
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.getFreeHeap();
|
||||
root["min_free_heap"] = ESP.getMinFreeHeap();
|
||||
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"] = ESPFS.totalBytes();
|
||||
root["fs_used"] = ESPFS.usedBytes();
|
||||
root["core_temp"] = temperatureRead();
|
||||
root["cpu_reset_reason"] = resetReason(rtc_get_reset_reason(0));
|
||||
root["uptime"] = millis() / 1000;
|
||||
}
|
||||
|
||||
void metrics(JsonObject &root) {
|
||||
root["uptime"] = millis() / 1000;
|
||||
root["free_heap"] = ESP.getFreeHeap();
|
||||
root["total_heap"] = ESP.getHeapSize();
|
||||
root["min_free_heap"] = ESP.getMinFreeHeap();
|
||||
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
|
||||
root["fs_used"] = ESPFS.usedBytes();
|
||||
root["fs_total"] = ESPFS.totalBytes();
|
||||
root["core_temp"] = temperatureRead();
|
||||
}
|
||||
|
||||
const char *resetReason(int reason) {
|
||||
switch (reason) {
|
||||
case 1: return "Vbat power on reset";
|
||||
case 3: return "Software reset digital core";
|
||||
case 4: return "Legacy watch dog reset digital core";
|
||||
case 5: return "Deep Sleep reset digital core";
|
||||
case 6: return "Reset by SLC module, reset digital core";
|
||||
case 7: return "Timer Group0 Watch dog reset digital core";
|
||||
case 8: return "Timer Group1 Watch dog reset digital core";
|
||||
case 9: return "RTC Watch dog Reset digital core";
|
||||
case 10: return "Intrusion tested to reset CPU";
|
||||
case 11: return "Time Group reset CPU";
|
||||
case 12: return "Software reset CPU";
|
||||
case 13: return "RTC Watch dog Reset CPU";
|
||||
case 14: return "for APP CPU, reset by PRO CPU";
|
||||
case 15: return "Reset when the vdd voltage is not stable";
|
||||
case 16: return "RTC Watch dog reset digital core and rtc module";
|
||||
default: return "NO_MEAN";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace system_service
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef SYSTEM_SERVICE_H
|
||||
#define SYSTEM_SERVICE_H
|
||||
|
||||
#include <ESPFS.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <PsychicHttp.h>
|
||||
#include <WiFi.h>
|
||||
#include <global.h>
|
||||
|
||||
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);
|
||||
esp_err_t getMetrics(PsychicRequest *request);
|
||||
|
||||
void reset();
|
||||
void restart();
|
||||
void sleep();
|
||||
void status(JsonObject &root);
|
||||
void metrics(JsonObject &root);
|
||||
const char *resetReason(int reason);
|
||||
} // namespace system_service
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user