mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
Merge remote-tracking branch 'origin/ft_webui_log' into dev
This commit is contained in:
@@ -40,6 +40,7 @@ WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &
|
||||
WebStatusService EMSESP::webStatusService = WebStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
WebDevicesService EMSESP::webDevicesService = WebDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
WebAPIService EMSESP::webAPIService = WebAPIService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
WebLogService EMSESP::webLogService = WebLogService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
using DeviceFlags = EMSdevice;
|
||||
using DeviceType = EMSdevice::DeviceType;
|
||||
@@ -1209,11 +1210,12 @@ void EMSESP::start() {
|
||||
|
||||
// main loop calling all services
|
||||
void EMSESP::loop() {
|
||||
esp8266React.loop(); // web
|
||||
esp8266React.loop(); // web services
|
||||
system_.loop(); // does LED and checks system health, and syslog service
|
||||
|
||||
// if we're doing an OTA upload, skip MQTT and EMS
|
||||
if (!system_.upload_status()) {
|
||||
webLogService.loop(); // log in Web UI
|
||||
rxservice_.loop(); // process any incoming Rx telegrams
|
||||
shower_.loop(); // check for shower on/off
|
||||
dallassensor_.loop(); // read dallas sensor temperatures
|
||||
|
||||
10
src/emsesp.h
10
src/emsesp.h
@@ -35,10 +35,11 @@
|
||||
|
||||
#include <ESP8266React.h>
|
||||
|
||||
#include "WebStatusService.h"
|
||||
#include "WebDevicesService.h"
|
||||
#include "WebSettingsService.h"
|
||||
#include "WebAPIService.h"
|
||||
#include "web/WebStatusService.h"
|
||||
#include "web/WebDevicesService.h"
|
||||
#include "web/WebSettingsService.h"
|
||||
#include "web/WebAPIService.h"
|
||||
#include "web/WebLogService.h"
|
||||
|
||||
#include "emsdevice.h"
|
||||
#include "emsfactory.h"
|
||||
@@ -205,6 +206,7 @@ class EMSESP {
|
||||
static WebStatusService webStatusService;
|
||||
static WebDevicesService webDevicesService;
|
||||
static WebAPIService webAPIService;
|
||||
static WebLogService webLogService;
|
||||
|
||||
static uuid::log::Logger logger();
|
||||
|
||||
|
||||
@@ -413,7 +413,6 @@ void System::loop() {
|
||||
send_heartbeat();
|
||||
}
|
||||
|
||||
/*
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#if defined(EMSESP_DEBUG)
|
||||
static uint32_t last_memcheck_ = 0;
|
||||
@@ -423,7 +422,6 @@ void System::loop() {
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
*/
|
||||
|
||||
#endif
|
||||
}
|
||||
@@ -849,6 +847,7 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
|
||||
EMSESP::webSettingsService.read([&](WebSettings & settings) {
|
||||
node = json.createNestedObject("Settings");
|
||||
node["tx_mode"] = settings.tx_mode;
|
||||
node["tx_delay"] = settings.tx_delay;
|
||||
node["ems_bus_id"] = settings.ems_bus_id;
|
||||
node["syslog_enabled"] = settings.syslog_enabled;
|
||||
node["syslog_level"] = settings.syslog_level;
|
||||
|
||||
@@ -930,6 +930,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
|
||||
}
|
||||
|
||||
if (command == "api") {
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
shell.printfln(F("Testing RESTful API..."));
|
||||
Mqtt::ha_enabled(false);
|
||||
run_test("general");
|
||||
@@ -942,6 +943,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
|
||||
|
||||
request.url("/api/boiler/syspress");
|
||||
EMSESP::webAPIService.webAPIService_get(&request);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define EMSESP_APP_VERSION "3.1.1b6"
|
||||
#define EMSESP_APP_VERSION "3.1.1b7"
|
||||
|
||||
@@ -223,11 +223,8 @@ void WebAPIService::send_message_response(AsyncWebServerRequest * request, uint1
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract only the path component from the passed URI
|
||||
* and normalized it.
|
||||
* Ex. //one/two////three///
|
||||
* becomes
|
||||
* /one/two/three
|
||||
* Extract only the path component from the passed URI and normalized it.
|
||||
* Ex. //one/two////three/// becomes /one/two/three
|
||||
*/
|
||||
std::string SUrlParser::path() {
|
||||
std::string s = "/"; // set up the beginning slash
|
||||
@@ -29,7 +29,6 @@
|
||||
#define DEVICE_DATA_SERVICE_PATH "/rest/deviceData"
|
||||
#define WRITE_VALUE_SERVICE_PATH "/rest/writeValue"
|
||||
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class WebDevicesService {
|
||||
171
src/web/WebLogService.cpp
Normal file
171
src/web/WebLogService.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020 Paul Derbyshire
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "emsesp.h"
|
||||
|
||||
using namespace std::placeholders;
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
WebLogService::WebLogService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: _events(EVENT_SOURCE_LOG_PATH)
|
||||
, _setLevel(LOG_SETTINGS_PATH, std::bind(&WebLogService::setLevel, this, _1, _2), 256) { // for POSTS
|
||||
|
||||
_events.setFilter(securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||
server->addHandler(&_events);
|
||||
server->on(EVENT_SOURCE_LOG_PATH, HTTP_GET, std::bind(&WebLogService::forbidden, this, _1));
|
||||
|
||||
// for bring back the whole log
|
||||
server->on(FETCH_LOG_PATH, HTTP_GET, std::bind(&WebLogService::fetchLog, this, _1));
|
||||
|
||||
// get when page is loaded
|
||||
server->on(LOG_SETTINGS_PATH, HTTP_GET, std::bind(&WebLogService::getLevel, this, _1));
|
||||
|
||||
// for setting a level
|
||||
server->addHandler(&_setLevel);
|
||||
|
||||
// start event source service
|
||||
start();
|
||||
}
|
||||
|
||||
void WebLogService::forbidden(AsyncWebServerRequest * request) {
|
||||
request->send(403);
|
||||
}
|
||||
|
||||
void WebLogService::start() {
|
||||
uuid::log::Logger::register_handler(this, uuid::log::Level::INFO); // default is INFO
|
||||
}
|
||||
|
||||
uuid::log::Level WebLogService::log_level() const {
|
||||
return uuid::log::Logger::get_log_level(this);
|
||||
}
|
||||
|
||||
void WebLogService::log_level(uuid::log::Level level) {
|
||||
uuid::log::Logger::register_handler(this, level);
|
||||
}
|
||||
|
||||
size_t WebLogService::maximum_log_messages() const {
|
||||
return maximum_log_messages_;
|
||||
}
|
||||
|
||||
void WebLogService::maximum_log_messages(size_t count) {
|
||||
maximum_log_messages_ = std::max((size_t)1, count);
|
||||
while (log_messages_.size() > maximum_log_messages_) {
|
||||
log_messages_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
WebLogService::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content)
|
||||
: id_(id)
|
||||
, content_(std::move(content)) {
|
||||
}
|
||||
|
||||
void WebLogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
|
||||
if (log_messages_.size() >= maximum_log_messages_) {
|
||||
log_messages_.pop_front();
|
||||
}
|
||||
log_messages_.emplace_back(log_message_id_++, std::move(message));
|
||||
}
|
||||
|
||||
void WebLogService::loop() {
|
||||
if (!_events.count() || log_messages_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// put a small delay in
|
||||
const uint64_t now = uuid::get_uptime_ms();
|
||||
if (now < last_transmit_ || now - last_transmit_ < 100) {
|
||||
return;
|
||||
}
|
||||
|
||||
// see if we've advanced
|
||||
if (log_messages_.back().id_ > log_message_id_tail_) {
|
||||
transmit(log_messages_.back());
|
||||
log_message_id_tail_ = log_messages_.back().id_;
|
||||
last_transmit_ = uuid::get_uptime_ms();
|
||||
}
|
||||
}
|
||||
|
||||
// send to web eventsource
|
||||
void WebLogService::transmit(const QueuedLogMessage & message) {
|
||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(EMSESP_JSON_SIZE_SMALL);
|
||||
JsonObject logEvent = jsonDocument.to<JsonObject>();
|
||||
logEvent["t"] = uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3);
|
||||
logEvent["l"] = message.content_->level;
|
||||
logEvent["n"] = message.content_->name;
|
||||
logEvent["m"] = message.content_->text;
|
||||
|
||||
size_t len = measureJson(jsonDocument);
|
||||
char * buffer = new char[len + 1];
|
||||
if (buffer) {
|
||||
serializeJson(jsonDocument, buffer, len + 1);
|
||||
_events.send(buffer, "message", millis());
|
||||
}
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// send the current log buffer to the API
|
||||
void WebLogService::fetchLog(AsyncWebServerRequest * request) {
|
||||
MsgpackAsyncJsonResponse * response = new MsgpackAsyncJsonResponse(false, EMSESP_JSON_SIZE_XXLARGE_DYN); // 8kb buffer
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
JsonArray log = root.createNestedArray("events");
|
||||
|
||||
for (const auto & msg : log_messages_) {
|
||||
JsonObject logEvent = log.createNestedObject();
|
||||
auto message = std::move(msg);
|
||||
logEvent["t"] = uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3);
|
||||
logEvent["l"] = message.content_->level;
|
||||
logEvent["n"] = message.content_->name;
|
||||
logEvent["m"] = message.content_->text;
|
||||
}
|
||||
|
||||
log_message_id_tail_ = log_messages_.back().id_;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// sets the level after a POST
|
||||
void WebLogService::setLevel(AsyncWebServerRequest * request, JsonVariant & json) {
|
||||
if (not json.is<JsonObject>()) {
|
||||
return;
|
||||
}
|
||||
auto && body = json.as<JsonObject>();
|
||||
uuid::log::Level level = body["level"];
|
||||
log_level(level);
|
||||
|
||||
// send the value back
|
||||
AsyncJsonResponse * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
||||
JsonObject root = response->getRoot();
|
||||
root["level"] = log_level();
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// return the current log level after a GET
|
||||
void WebLogService::getLevel(AsyncWebServerRequest * request) {
|
||||
AsyncJsonResponse * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
||||
JsonObject root = response->getRoot();
|
||||
root["level"] = log_level();
|
||||
root["max_messages"] = maximum_log_messages();
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
80
src/web/WebLogService.h
Normal file
80
src/web/WebLogService.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* EMS-ESP - https://github.com/emsesp/EMS-ESP
|
||||
* Copyright 2020 Paul Derbyshire
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef WebLogService_h
|
||||
#define WebLogService_h
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <AsyncJson.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
|
||||
#include <uuid/log.h>
|
||||
|
||||
#define EVENT_SOURCE_LOG_PATH "/es/log"
|
||||
#define FETCH_LOG_PATH "/rest/fetchLog"
|
||||
#define LOG_SETTINGS_PATH "/rest/logSettings"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class WebLogService : public uuid::log::Handler {
|
||||
public:
|
||||
static constexpr size_t MAX_LOG_MESSAGES = 30;
|
||||
|
||||
WebLogService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
void start();
|
||||
uuid::log::Level log_level() const;
|
||||
void log_level(uuid::log::Level level);
|
||||
size_t maximum_log_messages() const;
|
||||
void maximum_log_messages(size_t count);
|
||||
void loop();
|
||||
|
||||
virtual void operator<<(std::shared_ptr<uuid::log::Message> message);
|
||||
|
||||
private:
|
||||
AsyncEventSource _events;
|
||||
|
||||
class QueuedLogMessage {
|
||||
public:
|
||||
QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content);
|
||||
~QueuedLogMessage() = default;
|
||||
|
||||
unsigned long id_; // Sequential identifier for this log message
|
||||
struct timeval time_; // Time message was received
|
||||
const std::shared_ptr<const uuid::log::Message> content_; // Log message content
|
||||
};
|
||||
|
||||
void forbidden(AsyncWebServerRequest * request);
|
||||
void transmit(const QueuedLogMessage & message);
|
||||
void fetchLog(AsyncWebServerRequest * request);
|
||||
void getLevel(AsyncWebServerRequest * request);
|
||||
|
||||
void setLevel(AsyncWebServerRequest * request, JsonVariant & json);
|
||||
AsyncCallbackJsonWebHandler _setLevel; // for POSTs
|
||||
|
||||
uint64_t last_transmit_ = 0; // Last transmit time
|
||||
size_t maximum_log_messages_ = MAX_LOG_MESSAGES; // Maximum number of log messages to buffer before they are output
|
||||
unsigned long log_message_id_ = 0; // The next identifier to use for queued log messages
|
||||
unsigned long log_message_id_tail_ = 0; // last event shown on the screen after fetch
|
||||
std::list<QueuedLogMessage> log_messages_; // Queued log messages, in the order they were received
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
@@ -22,7 +22,7 @@
|
||||
#include <HttpEndpoint.h>
|
||||
#include <FSPersistence.h>
|
||||
|
||||
#include "default_settings.h"
|
||||
#include "../default_settings.h"
|
||||
|
||||
#define EMSESP_SETTINGS_FILE "/config/emsespSettings.json"
|
||||
#define EMSESP_SETTINGS_SERVICE_PATH "/rest/emsespSettings"
|
||||
Reference in New Issue
Block a user