mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-11 02:09:57 +03:00
add eventsource for log
This commit is contained in:
@@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
; default_envs = esp32_4M
|
; default_envs = esp32_4M
|
||||||
default_envs = lolin_s3
|
; default_envs = lolin_s3
|
||||||
; default_envs = esp32_16M
|
; default_envs = esp32_16M
|
||||||
; default_envs = standalone
|
; default_envs = standalone
|
||||||
; default_envs = https
|
default_envs = https
|
||||||
|
|
||||||
extra_configs =
|
extra_configs =
|
||||||
factory_settings.ini
|
factory_settings.ini
|
||||||
@@ -206,13 +206,14 @@ build_flags =
|
|||||||
-D NO_GLOBAL_ARDUINOOTA
|
-D NO_GLOBAL_ARDUINOOTA
|
||||||
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
||||||
-D ARDUINOJSON_USE_DOUBLE=0
|
-D ARDUINOJSON_USE_DOUBLE=0
|
||||||
; -D ARDUINOTRACE_ENABLE=1
|
-D ARDUINOTRACE_ENABLE=1
|
||||||
-D ARDUINOTRACE_ENABLE=0
|
; -D ARDUINOTRACE_ENABLE=0
|
||||||
-D TASMOTA_SDK
|
-D TASMOTA_SDK
|
||||||
; -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_WARN
|
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_WARN
|
||||||
-D EMSESP_TEST
|
-D EMSESP_TEST
|
||||||
-D EMSESP_DEBUG
|
-D EMSESP_DEBUG
|
||||||
-D CONFIG_ETH_ENABLED
|
; -D CONFIG_ETH_ENABLED
|
||||||
|
; -D ENABLE_ASYNC
|
||||||
; -D BOARD_HAS_PSRAM
|
; -D BOARD_HAS_PSRAM
|
||||||
'-DEMSESP_DEFAULT_BOARD_PROFILE="Test"'
|
'-DEMSESP_DEFAULT_BOARD_PROFILE="Test"'
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
#define EMSESP_APP_VERSION "3.6.5-https.1"
|
#define EMSESP_APP_VERSION "3.6.5-https.2"
|
||||||
|
|||||||
@@ -23,27 +23,19 @@ using namespace std::placeholders;
|
|||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
WebLogService::WebLogService(PsychicHttpServer * server, SecurityManager * securityManager)
|
WebLogService::WebLogService(PsychicHttpServer * server, SecurityManager * securityManager)
|
||||||
// TODO fix event source
|
: _server(server)
|
||||||
: // : _events(EVENT_SOURCE_LOG_PATH)
|
|
||||||
_server(server)
|
|
||||||
, _securityManager(securityManager) {
|
, _securityManager(securityManager) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void WebLogService::registerURI() {
|
void WebLogService::registerURI() {
|
||||||
// TODO fix event source
|
// eventsource
|
||||||
/*
|
_server->on(EVENT_SOURCE_LOG_PATH, &_events);
|
||||||
_events_.onOpen([](PsychicEventSourceClient * client) {
|
|
||||||
Serial.printf("[eventsource] connection #%u connected from %s\n", client->socket(), client->remoteIP().toString());
|
|
||||||
client->send("Hello user!", NULL, millis(), 1000);
|
|
||||||
});
|
|
||||||
_events_.onClose([](PsychicEventSourceClient * client) {
|
|
||||||
Serial.printf("[eventsource] connection #%u closed from %s\n", client->socket(), client->remoteIP().toString());
|
|
||||||
});
|
|
||||||
_server->on(EVENT_SOURCE_LOG_PATH, &events_);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// post
|
// TODO this doesn't work with PsychicEventSource
|
||||||
|
// _server->on(EVENT_SOURCE_LOG_PATH, &_events)->setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||||
|
// _events.setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||||
|
|
||||||
|
// POST
|
||||||
_server->on(LOG_SETTINGS_PATH, HTTP_POST, [this](PsychicRequest * request, JsonVariant & json) {
|
_server->on(LOG_SETTINGS_PATH, HTTP_POST, [this](PsychicRequest * request, JsonVariant & json) {
|
||||||
auto && body = json.as<JsonObject>();
|
auto && body = json.as<JsonObject>();
|
||||||
|
|
||||||
@@ -59,9 +51,7 @@ void WebLogService::registerURI() {
|
|||||||
return request->reply(200); // OK
|
return request->reply(200); // OK
|
||||||
});
|
});
|
||||||
|
|
||||||
_events.setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
// GET settings
|
||||||
|
|
||||||
// get settings
|
|
||||||
_server->on(LOG_SETTINGS_PATH, HTTP_GET, [this](PsychicRequest * request) {
|
_server->on(LOG_SETTINGS_PATH, HTTP_GET, [this](PsychicRequest * request) {
|
||||||
PsychicJsonResponse response = PsychicJsonResponse(request, false, EMSESP_JSON_SIZE_SMALL);
|
PsychicJsonResponse response = PsychicJsonResponse(request, false, EMSESP_JSON_SIZE_SMALL);
|
||||||
JsonObject root = response.getRoot();
|
JsonObject root = response.getRoot();
|
||||||
@@ -72,6 +62,7 @@ void WebLogService::registerURI() {
|
|||||||
return response.send();
|
return response.send();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// /rest/fetchLog
|
||||||
// for bring back the whole log - is a command, hence a POST
|
// for bring back the whole log - is a command, hence a POST
|
||||||
// send the complete log buffer to the API, not filtering on log level
|
// send the complete log buffer to the API, not filtering on log level
|
||||||
// done by resetting the pointer
|
// done by resetting the pointer
|
||||||
@@ -79,9 +70,6 @@ void WebLogService::registerURI() {
|
|||||||
log_message_id_tail_ = 0;
|
log_message_id_tail_ = 0;
|
||||||
return request->reply(200);
|
return request->reply(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO this can be removed when ported over
|
|
||||||
// server->addHandler(&events_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the log service with INFO level
|
// start the log service with INFO level
|
||||||
@@ -232,9 +220,9 @@ char * WebLogService::messagetime(char * out, const uint64_t t, const size_t buf
|
|||||||
// send to web eventsource
|
// send to web eventsource
|
||||||
void WebLogService::transmit(const QueuedLogMessage & message) {
|
void WebLogService::transmit(const QueuedLogMessage & message) {
|
||||||
DynamicJsonDocument jsonDocument(EMSESP_JSON_SIZE_LARGE);
|
DynamicJsonDocument jsonDocument(EMSESP_JSON_SIZE_LARGE);
|
||||||
if (jsonDocument.capacity() == 0) {
|
// if (jsonDocument.capacity() == 0) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
JsonObject logEvent = jsonDocument.to<JsonObject>();
|
JsonObject logEvent = jsonDocument.to<JsonObject>();
|
||||||
char time_string[25];
|
char time_string[25];
|
||||||
|
|
||||||
@@ -244,47 +232,13 @@ void WebLogService::transmit(const QueuedLogMessage & message) {
|
|||||||
logEvent["n"] = message.content_->name;
|
logEvent["n"] = message.content_->name;
|
||||||
logEvent["m"] = message.content_->text;
|
logEvent["m"] = message.content_->text;
|
||||||
|
|
||||||
size_t len = measureJson(jsonDocument);
|
size_t len = measureJson(jsonDocument) + 1;
|
||||||
char * buffer = new char[len + 1];
|
char * buffer = (char *)malloc(len);
|
||||||
if (buffer) {
|
if (buffer != NULL) {
|
||||||
serializeJson(jsonDocument, buffer, len + 1);
|
serializeJson(jsonDocument, buffer, len);
|
||||||
_events.send(buffer, "message", message.id_);
|
_events.send(buffer, "message", message.id_);
|
||||||
}
|
}
|
||||||
delete[] buffer;
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the complete log buffer to the API, not filtering on log level
|
} // namespace emsesp
|
||||||
// done by resetting the pointer
|
|
||||||
// esp_err_t WebLogService::fetchLog(PsychicRequest * request) {
|
|
||||||
// log_message_id_tail_ = 0;
|
|
||||||
// request->send(200);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return the current value settings after a GET
|
|
||||||
// esp_err_t WebLogService::getValues(PsychicRequest * request) {
|
|
||||||
// auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
|
||||||
// JsonObject root = response->getRoot();
|
|
||||||
// root["level"] = log_level();
|
|
||||||
// root["max_messages"] = maximum_log_messages();
|
|
||||||
// root["compact"] = compact();
|
|
||||||
// response->setLength();
|
|
||||||
// request->send(response);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sets the values like level after a POST
|
|
||||||
// esp_err_t WebLogService::setValues(PsychicRequest * request, JsonVariant & json) {
|
|
||||||
// auto && body = json.as<JsonObject>();
|
|
||||||
|
|
||||||
// uuid::log::Level level = body["level"];
|
|
||||||
// log_level(level);
|
|
||||||
|
|
||||||
// uint8_t max_messages = body["max_messages"];
|
|
||||||
// maximum_log_messages(max_messages);
|
|
||||||
|
|
||||||
// bool comp = body["compact"];
|
|
||||||
// compact(comp);
|
|
||||||
|
|
||||||
// return request->reply(200); // OK
|
|
||||||
// }
|
|
||||||
|
|
||||||
} // namespace emsesp
|
|
||||||
|
|||||||
@@ -50,8 +50,7 @@ class WebLogService : public uuid::log::Handler {
|
|||||||
private:
|
private:
|
||||||
SecurityManager * _securityManager;
|
SecurityManager * _securityManager;
|
||||||
PsychicHttpServer * _server;
|
PsychicHttpServer * _server;
|
||||||
|
PsychicEventSource _events;
|
||||||
PsychicEventSource _events;
|
|
||||||
|
|
||||||
class QueuedLogMessage {
|
class QueuedLogMessage {
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user