From b13396469444c016f7997b1d495d07e0c9e16b3f Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 23 Apr 2024 21:41:18 +0200 Subject: [PATCH 01/20] Updates ui and fixes breaking changes --- interface/package.json | 2 +- interface/src/lib/DaisyUiHelper.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/package.json b/interface/package.json index 8cf9e180..7d52c390 100644 --- a/interface/package.json +++ b/interface/package.json @@ -39,7 +39,7 @@ "dependencies": { "chart.js": "^4.4.0", "compare-versions": "^6.0.0", - "daisyui": "^3.5.1", + "daisyui": "^4.10.2", "jwt-decode": "^4.0.0", "postcss-remove-classes": "^2.0.0", "svelte-dnd-list": "^0.1.8", diff --git a/interface/src/lib/DaisyUiHelper.ts b/interface/src/lib/DaisyUiHelper.ts index 273c8553..0c7028a2 100644 --- a/interface/src/lib/DaisyUiHelper.ts +++ b/interface/src/lib/DaisyUiHelper.ts @@ -1,4 +1,4 @@ export function daisyColor(name: string, opacity: number = 100) { const color = getComputedStyle(document.documentElement).getPropertyValue(name); - return 'hsla(' + color + '/ ' + Math.min(Math.max(Math.round(opacity), 0), 100) + '%)'; + return `oklch(${color} / ${Math.min(Math.max(Math.round(opacity), 0), 100)}%)`; } From 6209235afef601021b652728f2430275cd652f45 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Thu, 25 Apr 2024 23:27:49 +0200 Subject: [PATCH 02/20] Upgrade ArduinoJson to v7 --- CHANGELOG.md | 4 ++- LICENSE | 2 +- docs/statefulservice.md | 2 +- interface/LICENSE | 2 +- lib/framework/APSettingsService.cpp | 2 +- lib/framework/APSettingsService.h | 2 +- lib/framework/APStatus.cpp | 4 +-- lib/framework/APStatus.h | 3 +- lib/framework/AnalyticsService.h | 4 +-- lib/framework/ArduinoJsonJWT.cpp | 2 +- lib/framework/ArduinoJsonJWT.h | 2 +- lib/framework/AuthenticationService.cpp | 12 +++---- lib/framework/AuthenticationService.h | 4 +-- lib/framework/BatteryService.cpp | 4 +-- lib/framework/BatteryService.h | 2 +- lib/framework/DownloadFirmwareService.cpp | 4 +-- lib/framework/DownloadFirmwareService.h | 2 +- lib/framework/ESP32SvelteKit.cpp | 2 ++ lib/framework/ESP32SvelteKit.h | 9 ++++- lib/framework/ESPFS.h | 2 +- lib/framework/EventEndpoint.h | 17 ++++----- lib/framework/EventSocket.cpp | 29 ++------------- lib/framework/EventSocket.h | 25 +++++++------ lib/framework/FSPersistence.h | 23 ++++++------ lib/framework/FactoryResetService.cpp | 2 +- lib/framework/FactoryResetService.h | 2 +- lib/framework/Features.h | 2 +- lib/framework/FeaturesService.cpp | 4 +-- lib/framework/FeaturesService.h | 3 +- lib/framework/HttpEndpoint.h | 15 ++++---- lib/framework/IPUtils.h | 2 +- lib/framework/JsonUtils.h | 2 +- lib/framework/MqttEndpoint.h | 21 +++++------ lib/framework/MqttSettingsService.cpp | 2 +- lib/framework/MqttSettingsService.h | 2 +- lib/framework/MqttStatus.cpp | 4 +-- lib/framework/MqttStatus.h | 3 +- lib/framework/NTPSettingsService.cpp | 2 +- lib/framework/NTPSettingsService.h | 3 +- lib/framework/NTPStatus.cpp | 4 +-- lib/framework/NTPStatus.h | 3 +- lib/framework/NotificationService.cpp | 25 +++++++++++++ lib/framework/NotificationService.h | 43 +++++++++++++++++++++++ lib/framework/RestartService.cpp | 2 +- lib/framework/RestartService.h | 2 +- lib/framework/SecurityManager.h | 4 +-- lib/framework/SecuritySettingsService.cpp | 10 +++--- lib/framework/SecuritySettingsService.h | 7 ++-- lib/framework/SettingValue.cpp | 2 +- lib/framework/SettingValue.h | 2 +- lib/framework/SleepService.cpp | 2 +- lib/framework/SleepService.h | 2 +- lib/framework/StatefulService.cpp | 2 +- lib/framework/StatefulService.h | 6 +--- lib/framework/SystemStatus.cpp | 4 +-- lib/framework/SystemStatus.h | 3 +- lib/framework/UploadFirmwareService.cpp | 4 +-- lib/framework/UploadFirmwareService.h | 2 +- lib/framework/WebSocketClient.bak | 9 +++-- lib/framework/WebSocketServer.h | 27 ++++++-------- lib/framework/WiFiScanner.cpp | 8 ++--- lib/framework/WiFiScanner.h | 4 +-- lib/framework/WiFiSettingsService.cpp | 4 +-- lib/framework/WiFiSettingsService.h | 8 ++--- lib/framework/WiFiStatus.cpp | 4 +-- lib/framework/WiFiStatus.h | 3 +- platformio.ini | 2 +- scripts/build_interface.py | 2 +- src/LightMqttSettingsService.cpp | 2 +- src/LightMqttSettingsService.h | 2 +- src/LightStateService.cpp | 7 ++-- src/LightStateService.h | 3 +- src/main.cpp | 2 +- 73 files changed, 236 insertions(+), 213 deletions(-) create mode 100644 lib/framework/NotificationService.cpp create mode 100644 lib/framework/NotificationService.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 425b42f1..1c9c871b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ All notable changes to this project will be documented in this file. ### Changed - Moved MQTT types to models.ts as well. [#49](https://github.com/theelims/ESP32-sveltekit/pull/49) -- Fixed spelling error in models.tsnpm audit +- Fixed spelling error in models.ts +- Changed ArduinoJson from v6 to v7 +- Split NotificationService out of EventSocket into own class ### Fixed diff --git a/LICENSE b/LICENSE index 61238e99..9a14391d 100644 --- a/LICENSE +++ b/LICENSE @@ -5,7 +5,7 @@ under the MIT License. MIT License -Copyright (c) 2023 theelims +Copyright (C) 2023 - 2024 theelims Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/docs/statefulservice.md b/docs/statefulservice.md index 5da1f4a2..dd969d04 100644 --- a/docs/statefulservice.md +++ b/docs/statefulservice.md @@ -344,7 +344,7 @@ The boolean parameter provided will always be `true`. It is possibly to send push notifications to all clients by using the Event Socket. These will be displayed as toasts an the client side. Either directly call ```cpp -esp32sveltekit.getSocket()->pushNotification("Pushed a message!", PUSHINFO); +esp32sveltekit.getNotificationService()->pushNotification("Pushed a message!", PUSHINFO); ``` or keep a local pointer to the `EventSocket` instance. It is possible to send `PUSHINFO`, `PUSHWARNING`, `PUSHERROR` and `PUSHSUCCESS` events to all clients. diff --git a/interface/LICENSE b/interface/LICENSE index 8a47f001..9155cf7c 100644 --- a/interface/LICENSE +++ b/interface/LICENSE @@ -5,7 +5,7 @@ under the MIT License. MIT License -Copyright (c) 2023 theelims +Copyright (C) 2023 - 2024 theelims Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/lib/framework/APSettingsService.cpp b/lib/framework/APSettingsService.cpp index 9f6b0e55..0478dd71 100644 --- a/lib/framework/APSettingsService.cpp +++ b/lib/framework/APSettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/APSettingsService.h b/lib/framework/APSettingsService.h index 0e342d35..3f91c678 100644 --- a/lib/framework/APSettingsService.h +++ b/lib/framework/APSettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/APStatus.cpp b/lib/framework/APStatus.cpp index d6d65641..cb8b70a2 100644 --- a/lib/framework/APStatus.cpp +++ b/lib/framework/APStatus.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -33,7 +33,7 @@ void APStatus::begin() esp_err_t APStatus::apStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_AP_STATUS_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); root["status"] = _apSettingsService->getAPNetworkStatus(); diff --git a/lib/framework/APStatus.h b/lib/framework/APStatus.h index 55b2357f..25b62f72 100644 --- a/lib/framework/APStatus.h +++ b/lib/framework/APStatus.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -23,7 +23,6 @@ #include #include -#define MAX_AP_STATUS_SIZE 1024 #define AP_STATUS_SERVICE_PATH "/rest/apStatus" class APStatus diff --git a/lib/framework/AnalyticsService.h b/lib/framework/AnalyticsService.h index 3aaa15e3..1c1b24dc 100644 --- a/lib/framework/AnalyticsService.h +++ b/lib/framework/AnalyticsService.h @@ -7,7 +7,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -49,7 +49,7 @@ class AnalyticsService void _loop() { TickType_t xLastWakeTime = xTaskGetTickCount(); - StaticJsonDocument doc; + JsonDocument doc; char message[MAX_ESP_ANALYTICS_SIZE]; while (1) { diff --git a/lib/framework/ArduinoJsonJWT.cpp b/lib/framework/ArduinoJsonJWT.cpp index 1489ecb9..0a2691d4 100644 --- a/lib/framework/ArduinoJsonJWT.cpp +++ b/lib/framework/ArduinoJsonJWT.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/ArduinoJsonJWT.h b/lib/framework/ArduinoJsonJWT.h index 38abcf20..b6ad8a1b 100644 --- a/lib/framework/ArduinoJsonJWT.h +++ b/lib/framework/ArduinoJsonJWT.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/AuthenticationService.cpp b/lib/framework/AuthenticationService.cpp index c7f808d5..0aa1c8f6 100644 --- a/lib/framework/AuthenticationService.cpp +++ b/lib/framework/AuthenticationService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -27,13 +27,13 @@ void AuthenticationService::begin() _server->on(SIGN_IN_PATH, HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) { if (json.is()) { - String username = json["username"]; - String password = json["password"]; + String username = json["username"]; + String password = json["password"]; Authentication authentication = _securityManager->authenticate(username, password); if (authentication.authenticated) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, 256); - JsonObject root = response.getRoot(); - root["access_token"] = _securityManager->generateJWT(authentication.user); + PsychicJsonResponse response = PsychicJsonResponse(request, false); + JsonObject root = response.getRoot(); + root["access_token"] = _securityManager->generateJWT(authentication.user); return response.send(); } } diff --git a/lib/framework/AuthenticationService.h b/lib/framework/AuthenticationService.h index 1cc02ba5..9308e377 100644 --- a/lib/framework/AuthenticationService.h +++ b/lib/framework/AuthenticationService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,8 +22,6 @@ #define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization" #define SIGN_IN_PATH "/rest/signIn" -#define MAX_AUTHENTICATION_SIZE 256 - #if FT_ENABLED(FT_SECURITY) class AuthenticationService diff --git a/lib/framework/BatteryService.cpp b/lib/framework/BatteryService.cpp index 808bdc51..1567a26f 100644 --- a/lib/framework/BatteryService.cpp +++ b/lib/framework/BatteryService.cpp @@ -5,7 +5,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -24,7 +24,7 @@ void BatteryService::begin() void BatteryService::batteryEvent() { - StaticJsonDocument<32> doc; + JsonDocument doc; char message[32]; doc["soc"] = _lastSOC; doc["charging"] = _isCharging; diff --git a/lib/framework/BatteryService.h b/lib/framework/BatteryService.h index 2496a2a1..e785f539 100644 --- a/lib/framework/BatteryService.h +++ b/lib/framework/BatteryService.h @@ -7,7 +7,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/DownloadFirmwareService.cpp b/lib/framework/DownloadFirmwareService.cpp index 07a09ede..c3255a64 100644 --- a/lib/framework/DownloadFirmwareService.cpp +++ b/lib/framework/DownloadFirmwareService.cpp @@ -5,7 +5,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -17,7 +17,7 @@ extern const uint8_t rootca_crt_bundle_start[] asm("_binary_src_certs_x509_crt_b static EventSocket *_socket = nullptr; static int previousProgress = 0; -StaticJsonDocument<128> doc; +JsonDocument doc; void update_started() { diff --git a/lib/framework/DownloadFirmwareService.h b/lib/framework/DownloadFirmwareService.h index e9b6869c..96fcb35f 100644 --- a/lib/framework/DownloadFirmwareService.h +++ b/lib/framework/DownloadFirmwareService.h @@ -8,7 +8,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/ESP32SvelteKit.cpp b/lib/framework/ESP32SvelteKit.cpp index 1648455c..ec8e68dd 100644 --- a/lib/framework/ESP32SvelteKit.cpp +++ b/lib/framework/ESP32SvelteKit.cpp @@ -24,6 +24,7 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd _apSettingsService(server, &ESPFS, &_securitySettingsService), _apStatus(server, &_securitySettingsService, &_apSettingsService), _socket(server, &_securitySettingsService, AuthenticationPredicates::IS_AUTHENTICATED), + _notificationService(&_socket), #if FT_ENABLED(FT_NTP) _ntpSettingsService(server, &ESPFS, &_securitySettingsService), _ntpStatus(server, &_securitySettingsService), @@ -137,6 +138,7 @@ void ESP32SvelteKit::begin() // Start the services _apStatus.begin(); _socket.begin(); + _notificationService.begin(); _apSettingsService.begin(); _factoryResetService.begin(); _featureService.begin(); diff --git a/lib/framework/ESP32SvelteKit.h b/lib/framework/ESP32SvelteKit.h index 6f41932f..6bd02c79 100644 --- a/lib/framework/ESP32SvelteKit.h +++ b/lib/framework/ESP32SvelteKit.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,11 @@ class ESP32SvelteKit return &_apSettingsService; } + NotificationService *getNotificationService() + { + return &_notificationService; + } + #if FT_ENABLED(FT_NTP) StatefulService *getNTPSettingsService() { @@ -171,6 +177,7 @@ class ESP32SvelteKit APSettingsService _apSettingsService; APStatus _apStatus; EventSocket _socket; + NotificationService _notificationService; #if FT_ENABLED(FT_NTP) NTPSettingsService _ntpSettingsService; NTPStatus _ntpStatus; diff --git a/lib/framework/ESPFS.h b/lib/framework/ESPFS.h index 3ec3a6cd..5cb0dd48 100644 --- a/lib/framework/ESPFS.h +++ b/lib/framework/ESPFS.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/EventEndpoint.h b/lib/framework/EventEndpoint.h index c5041675..2d07910b 100644 --- a/lib/framework/EventEndpoint.h +++ b/lib/framework/EventEndpoint.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -27,13 +27,11 @@ class EventEndpoint EventEndpoint(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService *statefulService, - EventSocket *socket, const char *event, - size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader), - _stateUpdater(stateUpdater), - _statefulService(statefulService), - _socket(socket), - _bufferSize(bufferSize), - _event(event) + EventSocket *socket, const char *event) : _stateReader(stateReader), + _stateUpdater(stateUpdater), + _statefulService(statefulService), + _socket(socket), + _event(event) { _statefulService->addUpdateHandler([&](const String &originId) { syncState(originId); }, @@ -53,7 +51,6 @@ class EventEndpoint StatefulService *_statefulService; EventSocket *_socket; const char *_event; - size_t _bufferSize; void updateState(JsonObject &root, int originId) { @@ -62,7 +59,7 @@ class EventEndpoint void syncState(const String &originId, bool sync = false) { - DynamicJsonDocument jsonDocument{_bufferSize}; + JsonDocument jsonDocument; JsonObject root = jsonDocument.to(); String output; _statefulService->read(root, _stateReader); diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 17c49400..e3c6316a 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -6,8 +6,7 @@ EventSocket::EventSocket(PsychicHttpServer *server, SecurityManager *securityManager, AuthenticationPredicate authenticationPredicate) : _server(server), _securityManager(securityManager), - _authenticationPredicate(authenticationPredicate), - _bufferSize(1024) + _authenticationPredicate(authenticationPredicate) { } @@ -64,7 +63,7 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame ESP_LOGV("EventSocket", "ws[%s][%u] request: %s", request->client()->remoteIP().toString().c_str(), request->client()->socket(), (char *)frame->payload); - DynamicJsonDocument doc = DynamicJsonDocument(_bufferSize); + JsonDocument doc; DeserializationError error = deserializeJson(doc, (char *)frame->payload, frame->len); if (!error && doc.is()) @@ -159,30 +158,6 @@ void EventSocket::emit(const char *event, const char *payload, const char *origi xSemaphoreGive(clientSubscriptionsMutex); } -void EventSocket::pushNotification(String message, pushEvent event) -{ - String eventType; - switch (event) - { - case (PUSHERROR): - eventType = "errorToast"; - break; - case (PUSHWARNING): - eventType = "warningToast"; - break; - case (PUSHINFO): - eventType = "infoToast"; - break; - case (PUSHSUCCESS): - eventType = "successToast"; - break; - default: - ESP_LOGW("EventSocket", "Client tried invalid push notification: %s", event); - return; - } - emit(eventType.c_str(), message.c_str()); -} - void EventSocket::handleEventCallbacks(String event, JsonObject &jsonObject, int originId) { for (auto &callback : event_callbacks[event]) diff --git a/lib/framework/EventSocket.h b/lib/framework/EventSocket.h index 84b52c2b..5fc9e1e5 100644 --- a/lib/framework/EventSocket.h +++ b/lib/framework/EventSocket.h @@ -1,6 +1,20 @@ #ifndef Socket_h #define Socket_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 - 2024 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 #include #include @@ -13,14 +27,6 @@ typedef std::function EventCallback; typedef std::function SubscribeCallback; -enum pushEvent -{ - PUSHERROR, - PUSHWARNING, - PUSHINFO, - PUSHSUCCESS -}; - class EventSocket { public: @@ -41,8 +47,6 @@ class EventSocket void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false); // if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId - void pushNotification(String message, pushEvent event); - private: PsychicHttpServer *_server; PsychicWebSocketHandler _socket; @@ -58,7 +62,6 @@ class EventSocket bool isEventValid(String event); - size_t _bufferSize; void onWSOpen(PsychicWebSocketClient *client); void onWSClose(PsychicWebSocketClient *client); esp_err_t onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame); diff --git a/lib/framework/FSPersistence.h b/lib/framework/FSPersistence.h index a1de7c32..729ce081 100644 --- a/lib/framework/FSPersistence.h +++ b/lib/framework/FSPersistence.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -26,14 +26,12 @@ class FSPersistence JsonStateUpdater stateUpdater, StatefulService *statefulService, FS *fs, - const char *filePath, - size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader), - _stateUpdater(stateUpdater), - _statefulService(statefulService), - _fs(fs), - _filePath(filePath), - _bufferSize(bufferSize), - _updateHandlerId(0) + const char *filePath) : _stateReader(stateReader), + _stateUpdater(stateUpdater), + _statefulService(statefulService), + _fs(fs), + _filePath(filePath), + _updateHandlerId(0) { enableUpdateHandler(); } @@ -44,7 +42,7 @@ class FSPersistence if (settingsFile) { - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; DeserializationError error = deserializeJson(jsonDocument, settingsFile); if (error == DeserializationError::Ok && jsonDocument.is()) { @@ -66,7 +64,7 @@ class FSPersistence bool writeToFS() { // create and populate a new json object - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; JsonObject jsonObject = jsonDocument.to(); _statefulService->read(jsonObject, _stateReader); @@ -112,7 +110,6 @@ class FSPersistence StatefulService *_statefulService; FS *_fs; const char *_filePath; - size_t _bufferSize; update_handler_id_t _updateHandlerId; // We assume we have a _filePath with format "/directory1/directory2/filename" @@ -136,7 +133,7 @@ class FSPersistence // is supplied, this virtual function allows that to be changed. virtual void applyDefaults() { - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; JsonObject jsonObject = jsonDocument.as(); _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater); } diff --git a/lib/framework/FactoryResetService.cpp b/lib/framework/FactoryResetService.cpp index e7bb6448..a16a00e6 100644 --- a/lib/framework/FactoryResetService.cpp +++ b/lib/framework/FactoryResetService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/FactoryResetService.h b/lib/framework/FactoryResetService.h index 9299cb69..71873c25 100644 --- a/lib/framework/FactoryResetService.h +++ b/lib/framework/FactoryResetService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/Features.h b/lib/framework/Features.h index 60957cf4..74d60a14 100644 --- a/lib/framework/Features.h +++ b/lib/framework/Features.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/FeaturesService.cpp b/lib/framework/FeaturesService.cpp index 2c05ae90..a4fcbc66 100644 --- a/lib/framework/FeaturesService.cpp +++ b/lib/framework/FeaturesService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,7 +22,7 @@ void FeaturesService::begin() { _server->on(FEATURES_SERVICE_PATH, HTTP_GET, [&](PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_FEATURES_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); #if FT_ENABLED(FT_SECURITY) diff --git a/lib/framework/FeaturesService.h b/lib/framework/FeaturesService.h index 7f524bd6..f132c900 100644 --- a/lib/framework/FeaturesService.h +++ b/lib/framework/FeaturesService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,7 +22,6 @@ #include #include -#define MAX_FEATURES_SIZE 256 #define FEATURES_SERVICE_PATH "/rest/features" typedef struct diff --git a/lib/framework/HttpEndpoint.h b/lib/framework/HttpEndpoint.h index 0397a48a..4232c3e4 100644 --- a/lib/framework/HttpEndpoint.h +++ b/lib/framework/HttpEndpoint.h @@ -20,7 +20,6 @@ class HttpEndpoint JsonStateReader _stateReader; JsonStateUpdater _stateUpdater; StatefulService *_statefulService; - size_t _bufferSize; SecurityManager *_securityManager; AuthenticationPredicate _authenticationPredicate; PsychicHttpServer *_server; @@ -33,9 +32,13 @@ class HttpEndpoint PsychicHttpServer *server, const char *servicePath, SecurityManager *securityManager, - AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService), _server(server), _servicePath(servicePath), _securityManager(securityManager), _authenticationPredicate(authenticationPredicate), _bufferSize(bufferSize) + AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) : _stateReader(stateReader), + _stateUpdater(stateUpdater), + _statefulService(statefulService), + _server(server), + _servicePath(servicePath), + _securityManager(securityManager), + _authenticationPredicate(authenticationPredicate) { } @@ -61,7 +64,7 @@ class HttpEndpoint _securityManager->wrapRequest( [this](PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject jsonObject = response.getRoot(); _statefulService->read(jsonObject, _stateReader); return response.send(); @@ -93,7 +96,7 @@ class HttpEndpoint _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); } - PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize); + PsychicJsonResponse response = PsychicJsonResponse(request, false); jsonObject = response.getRoot(); _statefulService->read(jsonObject, _stateReader); diff --git a/lib/framework/IPUtils.h b/lib/framework/IPUtils.h index c2e548b3..ba69f9dc 100644 --- a/lib/framework/IPUtils.h +++ b/lib/framework/IPUtils.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/JsonUtils.h b/lib/framework/JsonUtils.h index 067adae9..e3f94656 100644 --- a/lib/framework/JsonUtils.h +++ b/lib/framework/JsonUtils.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/MqttEndpoint.h b/lib/framework/MqttEndpoint.h index b97087e7..c8bf64c2 100644 --- a/lib/framework/MqttEndpoint.h +++ b/lib/framework/MqttEndpoint.h @@ -30,15 +30,13 @@ class MqttEndpoint PsychicMqttClient *mqttClient, const String &pubTopic = "", const String &subTopic = "", - bool retain = false, - size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader), - _stateUpdater(stateUpdater), - _statefulService(statefulService), - _mqttClient(mqttClient), - _pubTopic(pubTopic), - _subTopic(subTopic), - _retain(retain), - _bufferSize(bufferSize) + bool retain = false) : _stateReader(stateReader), + _stateUpdater(stateUpdater), + _statefulService(statefulService), + _mqttClient(mqttClient), + _pubTopic(pubTopic), + _subTopic(subTopic), + _retain(retain) { _statefulService->addUpdateHandler([&](const String &originId) @@ -95,7 +93,7 @@ class MqttEndpoint if (_pubTopic.length() > 0 && _mqttClient->connected()) { // serialize to json doc - DynamicJsonDocument json(_bufferSize); + JsonDocument json; JsonObject jsonObject = json.to(); _statefulService->read(jsonObject, _stateReader); @@ -116,7 +114,6 @@ class MqttEndpoint protected: StatefulService *_statefulService; PsychicMqttClient *_mqttClient; - int _bufferSize; JsonStateUpdater _stateUpdater; JsonStateReader _stateReader; String _subTopic; @@ -136,7 +133,7 @@ class MqttEndpoint } // deserialize from string - DynamicJsonDocument json(_bufferSize); + JsonDocument json; DeserializationError error = deserializeJson(json, payload); if (!error && json.is()) { diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 881e2777..97216221 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/MqttSettingsService.h b/lib/framework/MqttSettingsService.h index 8624b874..d45f3199 100644 --- a/lib/framework/MqttSettingsService.h +++ b/lib/framework/MqttSettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/MqttStatus.cpp b/lib/framework/MqttStatus.cpp index 8a56feb8..85235d6f 100644 --- a/lib/framework/MqttStatus.cpp +++ b/lib/framework/MqttStatus.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -34,7 +34,7 @@ void MqttStatus::begin() esp_err_t MqttStatus::mqttStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_MQTT_STATUS_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); root["enabled"] = _mqttSettingsService->isEnabled(); diff --git a/lib/framework/MqttStatus.h b/lib/framework/MqttStatus.h index 4ec80fef..364b100c 100644 --- a/lib/framework/MqttStatus.h +++ b/lib/framework/MqttStatus.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,7 +22,6 @@ #include #include -#define MAX_MQTT_STATUS_SIZE 1024 #define MQTT_STATUS_SERVICE_PATH "/rest/mqttStatus" class MqttStatus diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index 0e0ffe5d..c5b37efb 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/NTPSettingsService.h b/lib/framework/NTPSettingsService.h index 1b511501..0ec7fa29 100644 --- a/lib/framework/NTPSettingsService.h +++ b/lib/framework/NTPSettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -41,7 +41,6 @@ #define NTP_SETTINGS_FILE "/config/ntpSettings.json" #define NTP_SETTINGS_SERVICE_PATH "/rest/ntpSettings" -#define MAX_TIME_SIZE 256 #define TIME_PATH "/rest/time" class NTPSettings diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index 27a9a5c9..90d3b060 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -53,7 +53,7 @@ String toLocalTimeString(tm *time) esp_err_t NTPStatus::ntpStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_NTP_STATUS_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); // grab the current instant in unix seconds diff --git a/lib/framework/NTPStatus.h b/lib/framework/NTPStatus.h index f9baeef3..2ae9d2fd 100644 --- a/lib/framework/NTPStatus.h +++ b/lib/framework/NTPStatus.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -23,7 +23,6 @@ #include #include -#define MAX_NTP_STATUS_SIZE 1024 #define NTP_STATUS_SERVICE_PATH "/rest/ntpStatus" class NTPStatus diff --git a/lib/framework/NotificationService.cpp b/lib/framework/NotificationService.cpp new file mode 100644 index 00000000..65385285 --- /dev/null +++ b/lib/framework/NotificationService.cpp @@ -0,0 +1,25 @@ +#include + +// array translating pushType into strings +const char *pushTypeStrings[] = {"error", "warning", "info", "success"}; + +NotificationService::NotificationService(EventSocket *eventSocket) : _eventSocket(eventSocket) +{ +} + +void NotificationService::begin() +{ + _eventSocket->registerEvent(NOTIFICATION_EVENT); +} + +void NotificationService::pushNotification(String message, pushType event) +{ + JsonDocument doc; + doc["type"] = pushTypeStrings[event]; + doc["message"] = message; + // convert into JsonVariant + + String output; + serializeJson(doc, output); + _eventSocket->emit(NOTIFICATION_EVENT, output.c_str()); +} diff --git a/lib/framework/NotificationService.h b/lib/framework/NotificationService.h new file mode 100644 index 00000000..aee238c2 --- /dev/null +++ b/lib/framework/NotificationService.h @@ -0,0 +1,43 @@ +#ifndef NotificationService_h +#define NotificationService_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 - 2024 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 + +#define NOTIFICATION_EVENT "notification" + +enum pushType +{ + PUSHERROR, + PUSHWARNING, + PUSHINFO, + PUSHSUCCESS +}; + +class NotificationService +{ +public: + NotificationService(EventSocket *eventSocket); + + void begin(); + + void pushNotification(String message, pushType event); + +private: + EventSocket *_eventSocket; +}; + +#endif // NotificationService_h \ No newline at end of file diff --git a/lib/framework/RestartService.cpp b/lib/framework/RestartService.cpp index 6a918148..6e7a5cdc 100644 --- a/lib/framework/RestartService.cpp +++ b/lib/framework/RestartService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/RestartService.h b/lib/framework/RestartService.h index 56f16840..a3b7b893 100644 --- a/lib/framework/RestartService.h +++ b/lib/framework/RestartService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/SecurityManager.h b/lib/framework/SecurityManager.h index 4224c0e1..5aef1f46 100644 --- a/lib/framework/SecurityManager.h +++ b/lib/framework/SecurityManager.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -26,8 +26,6 @@ #define AUTHORIZATION_HEADER_PREFIX "Bearer " #define AUTHORIZATION_HEADER_PREFIX_LEN 7 -#define MAX_JWT_SIZE 128 - class User { public: diff --git a/lib/framework/SecuritySettingsService.cpp b/lib/framework/SecuritySettingsService.cpp index 4acdbc79..37eb09b5 100644 --- a/lib/framework/SecuritySettingsService.cpp +++ b/lib/framework/SecuritySettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -69,7 +69,7 @@ void SecuritySettingsService::configureJWTHandler() Authentication SecuritySettingsService::authenticateJWT(String &jwt) { - DynamicJsonDocument payloadDocument(MAX_JWT_SIZE); + JsonDocument payloadDocument; _jwtHandler.parseJWT(jwt, payloadDocument); if (payloadDocument.is()) { @@ -106,7 +106,7 @@ inline void populateJWTPayload(JsonObject &payload, User *user) boolean SecuritySettingsService::validatePayload(JsonObject &parsedPayload, User *user) { - DynamicJsonDocument jsonDocument(MAX_JWT_SIZE); + JsonDocument jsonDocument; JsonObject payload = jsonDocument.to(); populateJWTPayload(payload, user); return payload == parsedPayload; @@ -114,7 +114,7 @@ boolean SecuritySettingsService::validatePayload(JsonObject &parsedPayload, User String SecuritySettingsService::generateJWT(User *user) { - DynamicJsonDocument jsonDocument(MAX_JWT_SIZE); + JsonDocument jsonDocument; JsonObject payload = jsonDocument.to(); populateJWTPayload(payload, user); return _jwtHandler.buildJWT(payload); @@ -179,7 +179,7 @@ esp_err_t SecuritySettingsService::generateToken(PsychicRequest *request) { if (_user.username == usernameParam) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, GENERATE_TOKEN_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); root["token"] = generateJWT(&_user); return response.send(); diff --git a/lib/framework/SecuritySettingsService.h b/lib/framework/SecuritySettingsService.h index ce6de701..4b62013d 100644 --- a/lib/framework/SecuritySettingsService.h +++ b/lib/framework/SecuritySettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -44,7 +44,6 @@ #define SECURITY_SETTINGS_FILE "/config/securitySettings.json" #define SECURITY_SETTINGS_PATH "/rest/securitySettings" -#define GENERATE_TOKEN_SIZE 512 #define GENERATE_TOKEN_PATH "/rest/generateToken" #if FT_ENABLED(FT_SECURITY) @@ -61,10 +60,10 @@ class SecuritySettings root["jwt_secret"] = settings.jwtSecret; // users - JsonArray users = root.createNestedArray("users"); + JsonArray users = root["users"].add(); for (User user : settings.users) { - JsonObject userRoot = users.createNestedObject(); + JsonObject userRoot = users.add(); userRoot["username"] = user.username; userRoot["password"] = user.password; userRoot["admin"] = user.admin; diff --git a/lib/framework/SettingValue.cpp b/lib/framework/SettingValue.cpp index 9ebba8e2..8eff5802 100644 --- a/lib/framework/SettingValue.cpp +++ b/lib/framework/SettingValue.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/SettingValue.h b/lib/framework/SettingValue.h index d0aee406..46e79ab9 100644 --- a/lib/framework/SettingValue.h +++ b/lib/framework/SettingValue.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/SleepService.cpp b/lib/framework/SleepService.cpp index 633d675f..15e12b3d 100644 --- a/lib/framework/SleepService.cpp +++ b/lib/framework/SleepService.cpp @@ -5,7 +5,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/SleepService.h b/lib/framework/SleepService.h index 2524e2da..ffa09618 100644 --- a/lib/framework/SleepService.h +++ b/lib/framework/SleepService.h @@ -7,7 +7,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/StatefulService.cpp b/lib/framework/StatefulService.cpp index e3599074..297f74d8 100644 --- a/lib/framework/StatefulService.cpp +++ b/lib/framework/StatefulService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/StatefulService.h b/lib/framework/StatefulService.h index 530ecef6..32893c17 100644 --- a/lib/framework/StatefulService.h +++ b/lib/framework/StatefulService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -23,10 +23,6 @@ #include #include -#ifndef DEFAULT_BUFFER_SIZE -#define DEFAULT_BUFFER_SIZE 1024 -#endif - enum class StateUpdateResult { CHANGED = 0, // The update changed the state and propagation should take place if required diff --git a/lib/framework/SystemStatus.cpp b/lib/framework/SystemStatus.cpp index f41848ed..7e0700f2 100644 --- a/lib/framework/SystemStatus.cpp +++ b/lib/framework/SystemStatus.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -111,7 +111,7 @@ void SystemStatus::begin() esp_err_t SystemStatus::systemStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_ESP_STATUS_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); root["esp_platform"] = ESP_PLATFORM; diff --git a/lib/framework/SystemStatus.h b/lib/framework/SystemStatus.h index 139d6de2..87367c4a 100644 --- a/lib/framework/SystemStatus.h +++ b/lib/framework/SystemStatus.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,7 +22,6 @@ #include #include -#define MAX_ESP_STATUS_SIZE 1024 #define SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus" class SystemStatus diff --git a/lib/framework/UploadFirmwareService.cpp b/lib/framework/UploadFirmwareService.cpp index 1466c5c1..bf9f7a51 100644 --- a/lib/framework/UploadFirmwareService.cpp +++ b/lib/framework/UploadFirmwareService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -152,7 +152,7 @@ esp_err_t UploadFirmwareService::uploadComplete(PsychicRequest *request) { if (strlen(md5) == 32) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, 256); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); root["md5"] = md5; return response.send(); diff --git a/lib/framework/UploadFirmwareService.h b/lib/framework/UploadFirmwareService.h index 1e9d4344..0ea5771e 100644 --- a/lib/framework/UploadFirmwareService.h +++ b/lib/framework/UploadFirmwareService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/lib/framework/WebSocketClient.bak b/lib/framework/WebSocketClient.bak index 1d5b9d30..4a88dc4b 100644 --- a/lib/framework/WebSocketClient.bak +++ b/lib/framework/WebSocketClient.bak @@ -6,7 +6,7 @@ * with responsive Sveltekit front-end built with TailwindCSS and DaisyUI. * https://github.com/theelims/ESP32-sveltekit * - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -63,8 +63,7 @@ public: WebSocketClient(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService *statefulService, - const char *webSocketUri, - size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader), + const char *webSocketUri) : _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService), _bufferSize(bufferSize) @@ -194,7 +193,7 @@ protected: payload[data->data_len] = '\0'; ESP_LOGV(wsTAG, "Payload=%s", payload); - // DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + // JsonDocument jsonDocument; // DeserializationError error = deserializeJson(jsonDocument, payload); // ESP_LOGE(wsTAG, "deserializeJson() status: %s", error.c_str()); @@ -232,7 +231,7 @@ protected: void transmitData(const String &originId) { - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; JsonObject payload = jsonDocument.to(); _statefulService->read(payload, _stateReader); String json_string; diff --git a/lib/framework/WebSocketServer.h b/lib/framework/WebSocketServer.h index 1a325a5b..99f9f536 100644 --- a/lib/framework/WebSocketServer.h +++ b/lib/framework/WebSocketServer.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -19,8 +19,6 @@ #include #include -#define WEB_SOCKET_CLIENT_ID_MSG_SIZE 128 - #define WEB_SOCKET_ORIGIN "wsserver" #define WEB_SOCKET_ORIGIN_CLIENT_ID_PREFIX "wsserver:" @@ -34,15 +32,13 @@ class WebSocketServer PsychicHttpServer *server, const char *webSocketPath, SecurityManager *securityManager, - AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, - size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader), - _stateUpdater(stateUpdater), - _statefulService(statefulService), - _server(server), - _bufferSize(bufferSize), - _webSocketPath(webSocketPath), - _authenticationPredicate(authenticationPredicate), - _securityManager(securityManager) + AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) : _stateReader(stateReader), + _stateUpdater(stateUpdater), + _statefulService(statefulService), + _server(server), + _webSocketPath(webSocketPath), + _authenticationPredicate(authenticationPredicate), + _securityManager(securityManager) { _statefulService->addUpdateHandler( [&](const String &originId) @@ -90,7 +86,7 @@ class WebSocketServer { ESP_LOGV("WebSocketServer", "ws[%s][%u] request: %s", request->client()->remoteIP().toString().c_str(), request->client()->socket(), (char *)frame->payload); - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; DeserializationError error = deserializeJson(jsonDocument, (char *)frame->payload, frame->len); if (!error && jsonDocument.is()) @@ -117,11 +113,10 @@ class WebSocketServer PsychicHttpServer *_server; PsychicWebSocketHandler _webSocket; String _webSocketPath; - size_t _bufferSize; void transmitId(PsychicWebSocketClient *client) { - DynamicJsonDocument jsonDocument = DynamicJsonDocument(WEB_SOCKET_CLIENT_ID_MSG_SIZE); + JsonDocument jsonDocument; JsonObject root = jsonDocument.to(); root["type"] = "id"; root["id"] = clientId(client); @@ -141,7 +136,7 @@ class WebSocketServer */ void transmitData(PsychicWebSocketClient *client, const String &originId) { - DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize); + JsonDocument jsonDocument; JsonObject root = jsonDocument.to(); String buffer; diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index 2f1f8384..6d5612e4 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -52,12 +52,12 @@ esp_err_t WiFiScanner::listNetworks(PsychicRequest *request) int numNetworks = WiFi.scanComplete(); if (numNetworks > -1) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_WIFI_SCANNER_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); - JsonArray networks = root.createNestedArray("networks"); + JsonArray networks = root["networks"].add(); for (int i = 0; i < numNetworks; i++) { - JsonObject network = networks.createNestedObject(); + JsonObject network = networks.add(); network["rssi"] = WiFi.RSSI(i); network["ssid"] = WiFi.SSID(i); network["bssid"] = WiFi.BSSIDstr(i); diff --git a/lib/framework/WiFiScanner.h b/lib/framework/WiFiScanner.h index e963b67a..a846bf6c 100644 --- a/lib/framework/WiFiScanner.h +++ b/lib/framework/WiFiScanner.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -24,8 +24,6 @@ #define SCAN_NETWORKS_SERVICE_PATH "/rest/scanNetworks" #define LIST_NETWORKS_SERVICE_PATH "/rest/listNetworks" -#define MAX_WIFI_SCANNER_SIZE 1024 - class WiFiScanner { public: diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp index 4c510fce..3e1c0fb9 100644 --- a/lib/framework/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -20,7 +20,7 @@ WiFiSettingsService::WiFiSettingsService(PsychicHttpServer *server, EventSocket *socket) : _server(server), _securityManager(securityManager), _httpEndpoint(WiFiSettings::read, WiFiSettings::update, this, server, WIFI_SETTINGS_SERVICE_PATH, securityManager, - AuthenticationPredicates::IS_ADMIN, WIFI_SETTINGS_BUFFER_SIZE), + AuthenticationPredicates::IS_ADMIN), _fsPersistence(WiFiSettings::read, WiFiSettings::update, this, fs, WIFI_SETTINGS_FILE), _lastConnectionAttempt(0), _socket(socket) { diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 986ef667..477130a6 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -49,8 +49,6 @@ #define WIFI_RECONNECTION_DELAY 1000 * 30 #define RSSI_EVENT_DELAY 200 -#define WIFI_SETTINGS_BUFFER_SIZE 2048 - #define EVENT_RSSI "rssi" // Struct defining the wifi settings @@ -81,13 +79,13 @@ class WiFiSettings root["priority_RSSI"] = settings.priorityBySignalStrength; // create JSON array from root - JsonArray wifiNetworks = root.createNestedArray("wifi_networks"); + JsonArray wifiNetworks = root["wifi_networks"].add(); // iterate over the wifiSettings for (auto &wifi : settings.wifiSettings) { // create JSON object for each wifi network - JsonObject wifiNetwork = wifiNetworks.createNestedObject(); + JsonObject wifiNetwork = wifiNetworks.add(); // add the ssid and password to the JSON object wifiNetwork["ssid"] = wifi.ssid; diff --git a/lib/framework/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp index 539499dc..41348626 100644 --- a/lib/framework/WiFiStatus.cpp +++ b/lib/framework/WiFiStatus.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -63,7 +63,7 @@ void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) esp_err_t WiFiStatus::wifiStatus(PsychicRequest *request) { - PsychicJsonResponse response = PsychicJsonResponse(request, false, MAX_WIFI_STATUS_SIZE); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); wl_status_t status = WiFi.status(); root["status"] = (uint8_t)status; diff --git a/lib/framework/WiFiStatus.h b/lib/framework/WiFiStatus.h index 3b761c6d..e7c03ba5 100644 --- a/lib/framework/WiFiStatus.h +++ b/lib/framework/WiFiStatus.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -22,7 +22,6 @@ #include #include -#define MAX_WIFI_STATUS_SIZE 1024 #define WIFI_STATUS_SERVICE_PATH "/rest/wifiStatus" class WiFiStatus diff --git a/platformio.ini b/platformio.ini index 6226862f..2b2ca9d1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,7 +61,7 @@ extra_scripts = pre:scripts/generate_cert_bundle.py scripts/rename_fw.py lib_deps = - ArduinoJson@>=6.0.0,<7.0.0 + ArduinoJson@>=7.0.0 https://github.com/theelims/PsychicMqttClient.git#0.1.1 [env:esp32-c3-devkitm-1] diff --git a/scripts/build_interface.py b/scripts/build_interface.py index af5ce3d4..2229ee94 100644 --- a/scripts/build_interface.py +++ b/scripts/build_interface.py @@ -5,7 +5,7 @@ # https://github.com/theelims/ESP32-sveltekit # # Copyright (C) 2018 - 2023 rjwats -# Copyright (C) 2023 theelims +# Copyright (C) 2023 - 2024 theelims # Copyright (C) 2023 Maxtrium B.V. [ code available under dual license ] # Copyright (C) 2024 runeharlyk # diff --git a/src/LightMqttSettingsService.cpp b/src/LightMqttSettingsService.cpp index 8ff2d04a..82bb2c52 100644 --- a/src/LightMqttSettingsService.cpp +++ b/src/LightMqttSettingsService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/src/LightMqttSettingsService.h b/src/LightMqttSettingsService.h index ec539093..61ff4c63 100644 --- a/src/LightMqttSettingsService.h +++ b/src/LightMqttSettingsService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. diff --git a/src/LightStateService.cpp b/src/LightStateService.cpp index 629888d0..b9f1a9eb 100644 --- a/src/LightStateService.cpp +++ b/src/LightStateService.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -29,8 +29,7 @@ LightStateService::LightStateService(PsychicHttpServer *server, LightState::update, this, socket, - LIGHT_SETTINGS_EVENT, - LIGHT_SETTINGS_MAX_BUFFER_SIZE), + LIGHT_SETTINGS_EVENT), _mqttEndpoint(LightState::homeAssistRead, LightState::homeAssistUpdate, this, @@ -85,7 +84,7 @@ void LightStateService::registerConfig() String subTopic; String pubTopic; - DynamicJsonDocument doc(256); + JsonDocument doc; _lightMqttSettingsService->read([&](LightMqttSettings &settings) { configTopic = settings.mqttPath + "/config"; diff --git a/src/LightStateService.h b/src/LightStateService.h index 15365816..8e08b22e 100644 --- a/src/LightStateService.h +++ b/src/LightStateService.h @@ -9,7 +9,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. @@ -30,7 +30,6 @@ #define LIGHT_SETTINGS_ENDPOINT_PATH "/rest/lightState" #define LIGHT_SETTINGS_SOCKET_PATH "/ws/lightState" #define LIGHT_SETTINGS_EVENT "led" -#define LIGHT_SETTINGS_MAX_BUFFER_SIZE 256 class LightState { diff --git a/src/main.cpp b/src/main.cpp index f35fa5e7..694885ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ * https://github.com/theelims/ESP32-sveltekit * * Copyright (C) 2018 - 2023 rjwats - * Copyright (C) 2023 theelims + * Copyright (C) 2023 - 2024 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. From 6946b7de3e3ba54cd67e48ffd73fe88e8e289447 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Fri, 26 Apr 2024 22:10:30 +0200 Subject: [PATCH 03/20] removed obsolete event registers --- lib/framework/EventSocket.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index e3c6316a..0ddf97cf 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -18,11 +18,6 @@ void EventSocket::begin() _socket.onFrame(std::bind(&EventSocket::onFrame, this, std::placeholders::_1, std::placeholders::_2)); _server->on(EVENT_SERVICE_PATH, &_socket); - registerEvent("errorToast"); - registerEvent("warningToast"); - registerEvent("infoToast"); - registerEvent("successToast"); - ESP_LOGV("EventSocket", "Registered event socket endpoint: %s", EVENT_SERVICE_PATH); } From 25734a6142d5281cc0addd602d76f31284313d08 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Fri, 26 Apr 2024 22:19:17 +0200 Subject: [PATCH 04/20] update package-lock --- interface/package-lock.json | 122 ++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 26 deletions(-) diff --git a/interface/package-lock.json b/interface/package-lock.json index e0acb2ad..34f9b976 100644 --- a/interface/package-lock.json +++ b/interface/package-lock.json @@ -10,7 +10,7 @@ "dependencies": { "chart.js": "^4.4.0", "compare-versions": "^6.0.0", - "daisyui": "^3.5.1", + "daisyui": "^4.10.2", "jwt-decode": "^4.0.0", "postcss-remove-classes": "^2.0.0", "svelte-dnd-list": "^0.1.8", @@ -54,6 +54,7 @@ "version": "5.2.0", "resolved": "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz", "integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==", + "dev": true, "engines": { "node": ">=10" }, @@ -746,6 +747,7 @@ "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", + "dev": true, "dependencies": { "@nodelib/fs.stat": "2.0.5", "run-parallel": "^1.1.9" @@ -758,6 +760,7 @@ "version": "2.0.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", + "dev": true, "engines": { "node": ">= 8" } @@ -766,6 +769,7 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", + "dev": true, "dependencies": { "@nodelib/fs.scandir": "2.1.5", "fastq": "^1.6.0" @@ -1153,12 +1157,14 @@ "node_modules/any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", - "integrity": "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==" + "integrity": "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==", + "dev": true }, "node_modules/anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, "dependencies": { "normalize-path": "^3.0.0", "picomatch": "^2.0.4" @@ -1170,7 +1176,8 @@ "node_modules/arg": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz", - "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==" + "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==", + "dev": true }, "node_modules/argparse": { "version": "2.0.1", @@ -1243,12 +1250,14 @@ "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true, "engines": { "node": ">=8" } @@ -1257,6 +1266,7 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "dependencies": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1266,6 +1276,7 @@ "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, "dependencies": { "fill-range": "^7.0.1" }, @@ -1382,6 +1393,7 @@ "version": "3.6.0", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "dev": true, "dependencies": { "anymatch": "~3.1.2", "braces": "~3.0.2", @@ -1405,6 +1417,7 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, "dependencies": { "is-glob": "^4.0.1" }, @@ -1442,15 +1455,11 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, - "node_modules/colord": { - "version": "2.9.3", - "resolved": "https://registry.npmjs.org/colord/-/colord-2.9.3.tgz", - "integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==" - }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "dev": true, "engines": { "node": ">= 6" } @@ -1463,7 +1472,8 @@ "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", + "dev": true }, "node_modules/confbox": { "version": "0.1.7", @@ -1526,16 +1536,23 @@ "node": ">=4" } }, + "node_modules/culori": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/culori/-/culori-3.3.0.tgz", + "integrity": "sha512-pHJg+jbuFsCjz9iclQBqyL3B2HLCBF71BwVNujUYEvCeQMvV97R59MNK3R2+jgJ3a1fcZgI9B3vYgz8lzr/BFQ==", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + } + }, "node_modules/daisyui": { - "version": "3.9.2", - "resolved": "https://registry.npmjs.org/daisyui/-/daisyui-3.9.2.tgz", - "integrity": "sha512-yJZ1QjHUaL+r9BkquTdzNHb7KIgAJVFh0zbOXql2Wu0r7zx5qZNLxclhjN0WLoIpY+o2h/8lqXg7ijj8oTigOw==", + "version": "4.10.2", + "resolved": "https://registry.npmjs.org/daisyui/-/daisyui-4.10.2.tgz", + "integrity": "sha512-eCWS1W/JPyxW9IvlgW5m0R6rp9ZhRsBTW37rvEUthckkjsV04u8XipV519OoccSA46ixhSJa3q7XLI1WUFtRCA==", "dependencies": { - "colord": "^2.9", "css-selector-tokenizer": "^0.8", - "postcss": "^8", - "postcss-js": "^4", - "tailwindcss": "^3.1" + "culori": "^3", + "picocolors": "^1", + "postcss-js": "^4" }, "engines": { "node": ">=16.9.0" @@ -1603,7 +1620,8 @@ "node_modules/didyoumean": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.2.tgz", - "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==" + "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", + "dev": true }, "node_modules/dir-glob": { "version": "3.0.1", @@ -1620,7 +1638,8 @@ "node_modules/dlv": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/dlv/-/dlv-1.1.3.tgz", - "integrity": "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==" + "integrity": "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==", + "dev": true }, "node_modules/doctrine": { "version": "3.0.0", @@ -1938,6 +1957,7 @@ "version": "3.3.1", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.1.tgz", "integrity": "sha512-kNFPyjhh5cKjrUltxs+wFx+ZkbRaxxmZ+X0ZU31SOsxCEtP9VPgtq2teZw1DebupL5GmDaNQ6yKMMVcM41iqDg==", + "dev": true, "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", @@ -1953,6 +1973,7 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, "dependencies": { "is-glob": "^4.0.1" }, @@ -1981,6 +2002,7 @@ "version": "1.15.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz", "integrity": "sha512-wBrocU2LCXXa+lWBt8RoIRD89Fi8OdABODa/kEnyeyjS5aZO5/GNvI5sEINADqP/h8M29UHTHUb53sUu5Ihqdw==", + "dev": true, "dependencies": { "reusify": "^1.0.4" } @@ -2001,6 +2023,7 @@ "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, "dependencies": { "to-regex-range": "^5.0.1" }, @@ -2060,12 +2083,14 @@ "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", + "dev": true }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, "hasInstallScript": true, "optional": true, "os": [ @@ -2111,6 +2136,7 @@ "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", + "dev": true, "dependencies": { "is-glob": "^4.0.3" }, @@ -2181,6 +2207,7 @@ "version": "1.0.4", "resolved": "https://registry.npmjs.org/has/-/has-1.0.4.tgz", "integrity": "sha512-qdSAmqLF6209RFj4VVItywPMbm3vWylknmB3nvNiUIs72xAimcM8nVYxYr7ncvZq5qzk9MKIZR8ijqD/1QuYjQ==", + "dev": true, "engines": { "node": ">= 0.4.0" } @@ -2251,6 +2278,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "dev": true, "dependencies": { "once": "^1.3.0", "wrappy": "1" @@ -2259,12 +2287,14 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, "dependencies": { "binary-extensions": "^2.0.0" }, @@ -2276,6 +2306,7 @@ "version": "2.13.0", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.0.tgz", "integrity": "sha512-Z7dk6Qo8pOCp3l4tsX2C5ZVas4V+UxwQodwZhLopL91TX8UyyHEXafPcyoeeWuLrwzHcr3igO78wNLwHJHsMCQ==", + "dev": true, "dependencies": { "has": "^1.0.3" }, @@ -2287,6 +2318,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -2295,6 +2327,7 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, "dependencies": { "is-extglob": "^2.1.1" }, @@ -2306,6 +2339,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, "engines": { "node": ">=0.12.0" } @@ -2349,6 +2383,7 @@ "version": "1.20.0", "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.20.0.tgz", "integrity": "sha512-3TV69ZbrvV6U5DfQimop50jE9Dl6J8O1ja1dvBbMba/sZ3YBEQqJ2VZRoQPVnhlzjNtU1vaXRZVrVjU4qtm8yA==", + "dev": true, "bin": { "jiti": "bin/jiti.js" } @@ -2438,6 +2473,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz", "integrity": "sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==", + "dev": true, "engines": { "node": ">=10" } @@ -2445,7 +2481,8 @@ "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", - "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==" + "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==", + "dev": true }, "node_modules/local-pkg": { "version": "0.5.0", @@ -2532,6 +2569,7 @@ "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "dev": true, "engines": { "node": ">= 8" } @@ -2540,6 +2578,7 @@ "version": "4.0.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "dev": true, "dependencies": { "braces": "^3.0.2", "picomatch": "^2.3.1" @@ -2573,6 +2612,7 @@ "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, "dependencies": { "brace-expansion": "^1.1.7" }, @@ -2647,6 +2687,7 @@ "version": "2.7.0", "resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz", "integrity": "sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==", + "dev": true, "dependencies": { "any-promise": "^1.0.0", "object-assign": "^4.0.1", @@ -2686,6 +2727,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -2730,6 +2772,7 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -2738,6 +2781,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", + "dev": true, "engines": { "node": ">= 6" } @@ -2746,6 +2790,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, "dependencies": { "wrappy": "1" } @@ -2837,6 +2882,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -2853,7 +2899,8 @@ "node_modules/path-parse": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", - "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "dev": true }, "node_modules/path-type": { "version": "4.0.0", @@ -2889,6 +2936,7 @@ "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, "engines": { "node": ">=8.6" }, @@ -2900,6 +2948,7 @@ "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha512-udgsAY+fTnvv7kI7aaxbqwWNb0AHiB0qBO89PZKPkoTmGOgdbrHDKD+0B2X4uTfJ/FT1R09r9gTsjUjNJotuog==", + "dev": true, "engines": { "node": ">=0.10.0" } @@ -2908,6 +2957,7 @@ "version": "4.0.6", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.6.tgz", "integrity": "sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==", + "dev": true, "engines": { "node": ">= 6" } @@ -2954,6 +3004,7 @@ "version": "15.1.0", "resolved": "https://registry.npmjs.org/postcss-import/-/postcss-import-15.1.0.tgz", "integrity": "sha512-hpr+J05B2FVYUAXHeK1YyI267J/dDDhMU6B6civm8hSY1jYJnBXxzKDKDswzJmtLHryrjhnDjqqp/49t8FALew==", + "dev": true, "dependencies": { "postcss-value-parser": "^4.0.0", "read-cache": "^1.0.0", @@ -3017,6 +3068,7 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/postcss-nested/-/postcss-nested-6.0.1.tgz", "integrity": "sha512-mEp4xPMi5bSWiMbsgoPfcP74lsWLHkQbZc3sY+jWYd65CUwXrUaTp0fmNpa01ZcETKlIgUdFN/MpS2xZtqL9dQ==", + "dev": true, "dependencies": { "postcss-selector-parser": "^6.0.11" }, @@ -3100,7 +3152,8 @@ "node_modules/postcss-value-parser": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", - "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==" + "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", + "dev": true }, "node_modules/prelude-ls": { "version": "1.2.1", @@ -3221,6 +3274,7 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", + "dev": true, "funding": [ { "type": "github", @@ -3240,6 +3294,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", "integrity": "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==", + "dev": true, "dependencies": { "pify": "^2.3.0" } @@ -3248,6 +3303,7 @@ "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, "dependencies": { "picomatch": "^2.2.1" }, @@ -3259,6 +3315,7 @@ "version": "1.22.6", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.6.tgz", "integrity": "sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==", + "dev": true, "dependencies": { "is-core-module": "^2.13.0", "path-parse": "^1.0.7", @@ -3284,6 +3341,7 @@ "version": "1.0.4", "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", + "dev": true, "engines": { "iojs": ">=1.0.0", "node": ">=0.10.0" @@ -3324,6 +3382,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", + "dev": true, "funding": [ { "type": "github", @@ -3530,6 +3589,7 @@ "version": "3.34.0", "resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.34.0.tgz", "integrity": "sha512-70/LQEZ07TEcxiU2dz51FKaE6hCTWC6vr7FOk3Gr0U60C3shtAN+H+BFr9XlYe5xqf3RA8nrc+VIwzCfnxuXJw==", + "dev": true, "dependencies": { "@jridgewell/gen-mapping": "^0.3.2", "commander": "^4.0.0", @@ -3551,6 +3611,7 @@ "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, "dependencies": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -3582,6 +3643,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "dev": true, "engines": { "node": ">= 0.4" }, @@ -3773,6 +3835,7 @@ "version": "3.3.3", "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.3.3.tgz", "integrity": "sha512-A0KgSkef7eE4Mf+nKJ83i75TMyq8HqY3qmFIJSWy8bNt0v1lG7jUcpGpoTFxAwYcWOphcTBLPPJg+bDfhDf52w==", + "dev": true, "dependencies": { "@alloc/quick-lru": "^5.2.0", "arg": "^5.0.2", @@ -3809,6 +3872,7 @@ "version": "4.0.1", "resolved": "https://registry.npmjs.org/postcss-load-config/-/postcss-load-config-4.0.1.tgz", "integrity": "sha512-vEJIc8RdiBRu3oRAI0ymerOn+7rPuMvRXslTvZUKZonDHFIczxztIyJ1urxM1x9JXEikvpWWTUUqal5j/8QgvA==", + "dev": true, "dependencies": { "lilconfig": "^2.0.5", "yaml": "^2.1.1" @@ -3837,6 +3901,7 @@ "version": "2.3.2", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.2.tgz", "integrity": "sha512-N/lyzTPaJasoDmfV7YTrYCI0G/3ivm/9wdG0aHuheKowWQwGTsK0Eoiw6utmzAnI6pkJa0DUVygvp3spqqEKXg==", + "dev": true, "engines": { "node": ">= 14" } @@ -3851,6 +3916,7 @@ "version": "3.3.1", "resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.1.tgz", "integrity": "sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==", + "dev": true, "dependencies": { "any-promise": "^1.0.0" } @@ -3859,6 +3925,7 @@ "version": "1.6.0", "resolved": "https://registry.npmjs.org/thenify-all/-/thenify-all-1.6.0.tgz", "integrity": "sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==", + "dev": true, "dependencies": { "thenify": ">= 3.1.0 < 4" }, @@ -3880,6 +3947,7 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, "dependencies": { "is-number": "^7.0.0" }, @@ -3911,7 +3979,8 @@ "node_modules/ts-interface-checker": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz", - "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==" + "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==", + "dev": true }, "node_modules/tslib": { "version": "2.6.2", @@ -4177,7 +4246,8 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true }, "node_modules/yallist": { "version": "4.0.0", From f55f0fdac86257940d66a142ae0f500a9480c4e8 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sat, 27 Apr 2024 00:35:44 +0200 Subject: [PATCH 05/20] JsonObject based emit --- lib/framework/AnalyticsService.h | 9 ++--- lib/framework/EventSocket.cpp | 58 ++++++++++++++++++++++++++++++++ lib/framework/EventSocket.h | 2 ++ platformio.ini | 4 +-- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/lib/framework/AnalyticsService.h b/lib/framework/AnalyticsService.h index 1c1b24dc..ee58aa67 100644 --- a/lib/framework/AnalyticsService.h +++ b/lib/framework/AnalyticsService.h @@ -50,10 +50,9 @@ class AnalyticsService { TickType_t xLastWakeTime = xTaskGetTickCount(); JsonDocument doc; - char message[MAX_ESP_ANALYTICS_SIZE]; + // String message; while (1) { - doc.clear(); doc["uptime"] = millis() / 1000; doc["free_heap"] = ESP.getFreeHeap(); doc["total_heap"] = ESP.getHeapSize(); @@ -63,8 +62,10 @@ class AnalyticsService doc["fs_total"] = ESPFS.totalBytes(); doc["core_temp"] = temperatureRead(); - serializeJson(doc, message); - _socket->emit(EVENT_ANALYTICS, message); + JsonObject object = doc.as(); + _socket->emitEvent(EVENT_ANALYTICS, object); + // serializeJson(object, message); + // ESP_LOGV("AnalyticsService", "Emitting analytics: %s", message.c_str()); vTaskDelayUntil(&xLastWakeTime, ANALYTICS_INTERVAL / portTICK_PERIOD_MS); } diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 0ddf97cf..3fe66264 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -153,6 +153,64 @@ void EventSocket::emit(const char *event, const char *payload, const char *origi xSemaphoreGive(clientSubscriptionsMutex); } +void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *originId, bool onlyToSameOrigin) +{ + // Only process valid events + if (!isEventValid(String(event))) + { + ESP_LOGW("EventSocket", "Method tried to emit unregistered event: %s", event); + return; + } + + int originSubscriptionId = originId[0] ? atoi(originId) : -1; + xSemaphoreTake(clientSubscriptionsMutex, portMAX_DELAY); + auto &subscriptions = client_subscriptions[event]; + if (subscriptions.empty()) + { + xSemaphoreGive(clientSubscriptionsMutex); + return; + } + + JsonDocument doc; + doc["event"] = event; + doc["payload"] = jsonObject; + + size_t len = measureJson(doc) + 1; + char *output = new char[len]; + serializeJson(doc, output, len); + + // if onlyToSameOrigin == true, send the message back to the origin + if (onlyToSameOrigin && originSubscriptionId > 0) + { + auto *client = _socket.getClient(originSubscriptionId); + if (client) + { + ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); + client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); + } + } + else + { // else send the message to all other clients + + for (int subscription : client_subscriptions[event]) + { + if (subscription == originSubscriptionId) + continue; + auto *client = _socket.getClient(subscription); + if (!client) + { + subscriptions.remove(subscription); + continue; + } + ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); + client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); + } + } + + delete[] output; + xSemaphoreGive(clientSubscriptionsMutex); +} + void EventSocket::handleEventCallbacks(String event, JsonObject &jsonObject, int originId) { for (auto &callback : event_callbacks[event]) diff --git a/lib/framework/EventSocket.h b/lib/framework/EventSocket.h index 5fc9e1e5..60fbc6fd 100644 --- a/lib/framework/EventSocket.h +++ b/lib/framework/EventSocket.h @@ -47,6 +47,8 @@ class EventSocket void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false); // if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId + void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false); + private: PsychicHttpServer *_server; PsychicWebSocketHandler _socket; diff --git a/platformio.ini b/platformio.ini index 2b2ca9d1..db075e41 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,9 +24,9 @@ build_flags = ${features.build_flags} -D BUILD_TARGET=\"$PIOENV\" -D APP_NAME=\"ESP32-Sveltekit\" ; Must only contain characters from [a-zA-Z0-9-_] as this is converted into a filename - -D APP_VERSION=\"0.4.0\" ; semver compatible version string + -D APP_VERSION=\"0.5.0\" ; semver compatible version string ; Uncomment to receive log messages from the ESP Arduino Core - -D CORE_DEBUG_LEVEL=4 + -D CORE_DEBUG_LEVEL=5 ; Move all networking stuff to the protocol core 0 and leave business logic on application core 1 -D ESP32SVELTEKIT_RUNNING_CORE=0 ; Uncomment EMBED_WWW to embed the WWW data in the firmware binary From ea6d9d628091d30a4d1a580d7bfd1bfab348baa6 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:32:17 +0200 Subject: [PATCH 06/20] emitEvent() API refactor --- CHANGELOG.md | 7 ++- docs/statefulservice.md | 32 +++++++++-- interface/src/routes/+layout.svelte | 32 ++++++----- lib/framework/AnalyticsService.h | 7 +-- lib/framework/BatteryService.cpp | 5 +- lib/framework/DownloadFirmwareService.cpp | 28 +++++----- lib/framework/EventEndpoint.h | 9 ++-- lib/framework/EventSocket.cpp | 65 +---------------------- lib/framework/EventSocket.h | 10 +--- lib/framework/NotificationService.cpp | 7 +-- lib/framework/WiFiSettingsService.cpp | 8 +-- lib/framework/WiFiSettingsService.h | 2 +- 12 files changed, 86 insertions(+), 126 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c9c871b..1f8510cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,16 +2,21 @@ All notable changes to this project will be documented in this file. -## [WIP] - Current Main +## [0.5.0] - WIP + +Introduces a ### Added ### Changed - Moved MQTT types to models.ts as well. [#49](https://github.com/theelims/ESP32-sveltekit/pull/49) +- Updated daisyUI to 4.10.2 [#48](https://github.com/theelims/ESP32-sveltekit/pull/48) - Fixed spelling error in models.ts - Changed ArduinoJson from v6 to v7 - Split NotificationService out of EventSocket into own class +- Changed API of EventSocket.h. Now uses `void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false);`. +- Changed event socket message format to MessagePack ### Fixed diff --git a/docs/statefulservice.md b/docs/statefulservice.md index dd969d04..91b89669 100644 --- a/docs/statefulservice.md +++ b/docs/statefulservice.md @@ -303,14 +303,36 @@ The demo project allows the user to modify the MQTT topics via the UI so they ca Beside RESTful HTTP Endpoints the Event Socket System provides a convenient communication path between the client and the ESP32. It uses a single WebSocket connection to synchronize state and to push realtime data to the client. The client needs to subscribe to the topics he is interested. Only clients who have an active subscription will receive data. Every authenticated client may make use of this system as the security settings are set to `AuthenticationPredicates::IS_AUTHENTICATED`. +### Message Format + +The event messages exchanged between the ESP32 and its clients consists of an "event" head and the "data" payload. For the LightState example a message looks like this in JSON representation: + +```JSON +{ + "event": "led", + "data": { + "led_on": true + } +} +``` + +To save on bandwidth the event message is encoded as binary [MessagePack](https://msgpack.org/) instead of a JSON. + +To subscribe the client has to send the following message (as MessagePack): + +```JSON +{ + "event": "subscribe", + "data": "analytics" +} +``` + ### Emit an Event -The Event Socket provides an overloaded `emit()` function to push data to all subscribed clients. This is used by various esp32sveltekit classes to push real time data to the client. First an event must be registered with the Event Socket by calling `_socket.registerEvent("CustomEvent");`. Only then clients may subscribe to this custom event and you're entitled to emit event data: +The Event Socket provides an `emitEvent()` function to push data to all subscribed clients. This is used by various esp32sveltekit classes to push real time data to the client. First an event must be registered with the Event Socket by calling `_socket.registerEvent("CustomEvent");`. Only then clients may subscribe to this custom event and you're entitled to emit event data: ```cpp -void emit(String event, String payload); -void emit(const char *event, const char *payload); -void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false); +void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false); ``` The latter function allowing a selection of the recipient. If `onlyToSameOrigin = false` the payload is distributed to all subscribed clients, except the `originId`. If `onlyToSameOrigin = true` only the client with `originId` will receive the payload. This is used by the [EventEndpoint](#event-socket-endpoint) to sync the initial state when a new client subscribes. @@ -331,7 +353,7 @@ _socket.onEvent("CostumEvent",[&](JsonObject &root, int originId) Similarly a callback or lambda function may be registered to get notified when a client subscribes to an event: ```cpp -_socket.onEvent("CostumEvent",[&](const String &originId, bool sync) +_socket.onSubscribe("CostumEvent",[&](const String &originId) { Serial.println("New Client subscribed: " + originId); }); diff --git a/interface/src/routes/+layout.svelte b/interface/src/routes/+layout.svelte index cd578c50..ff8388b8 100644 --- a/interface/src/routes/+layout.svelte +++ b/interface/src/routes/+layout.svelte @@ -38,10 +38,7 @@ socket.on('close', handleClose); socket.on('error', handleError); socket.on('rssi', handleNetworkStatus); - socket.on('infoToast', handleInfoToast); - socket.on('successToast', handleSuccessToast); - socket.on('warningToast', handleWarningToast); - socket.on('errorToast', handleErrorToast); + socket.on('notification', handleNotification); if ($page.data.features.analytics) socket.on('analytics', handleAnalytics); if ($page.data.features.battery) socket.on('battery', handleBattery); if ($page.data.features.download_firmware) socket.on('otastatus', handleOAT); @@ -52,10 +49,7 @@ socket.off('open', handleOpen); socket.off('close', handleClose); socket.off('rssi', handleNetworkStatus); - socket.off('infoToast', handleInfoToast); - socket.off('successToast', handleSuccessToast); - socket.off('warningToast', handleWarningToast); - socket.off('errorToast', handleErrorToast); + socket.off('notification', handleNotification); socket.off('battery', handleBattery); socket.off('otastatus', handleOAT); }; @@ -88,10 +82,24 @@ const handleError = (data: any) => console.error(data); - const handleInfoToast = (data: string) => notifications.info(data, 5000); - const handleWarningToast = (data: string) => notifications.warning(data, 5000); - const handleErrorToast = (data: string) => notifications.error(data, 5000); - const handleSuccessToast = (data: string) => notifications.success(data, 5000); + const handleNotification = (data: any) => { + switch (data.type) { + case 'info': + notifications.info(data.message, 5000); + break; + case 'warning': + notifications.warning(data.message, 5000); + break; + case 'error': + notifications.error(data.message, 5000); + break; + case 'success': + notifications.success(data.message, 5000); + break; + default: + break; + } + }; const handleAnalytics = (data: Analytics) => analytics.addData(data); diff --git a/lib/framework/AnalyticsService.h b/lib/framework/AnalyticsService.h index ee58aa67..f699d3ce 100644 --- a/lib/framework/AnalyticsService.h +++ b/lib/framework/AnalyticsService.h @@ -50,7 +50,6 @@ class AnalyticsService { TickType_t xLastWakeTime = xTaskGetTickCount(); JsonDocument doc; - // String message; while (1) { doc["uptime"] = millis() / 1000; @@ -62,10 +61,8 @@ class AnalyticsService doc["fs_total"] = ESPFS.totalBytes(); doc["core_temp"] = temperatureRead(); - JsonObject object = doc.as(); - _socket->emitEvent(EVENT_ANALYTICS, object); - // serializeJson(object, message); - // ESP_LOGV("AnalyticsService", "Emitting analytics: %s", message.c_str()); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_ANALYTICS, jsonObject); vTaskDelayUntil(&xLastWakeTime, ANALYTICS_INTERVAL / portTICK_PERIOD_MS); } diff --git a/lib/framework/BatteryService.cpp b/lib/framework/BatteryService.cpp index 1567a26f..001f0a1e 100644 --- a/lib/framework/BatteryService.cpp +++ b/lib/framework/BatteryService.cpp @@ -25,9 +25,8 @@ void BatteryService::begin() void BatteryService::batteryEvent() { JsonDocument doc; - char message[32]; doc["soc"] = _lastSOC; doc["charging"] = _isCharging; - serializeJson(doc, message); - _socket->emit(EVENT_BATTERY, message); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_BATTERY, jsonObject); } diff --git a/lib/framework/DownloadFirmwareService.cpp b/lib/framework/DownloadFirmwareService.cpp index c3255a64..acdf98c0 100644 --- a/lib/framework/DownloadFirmwareService.cpp +++ b/lib/framework/DownloadFirmwareService.cpp @@ -23,19 +23,19 @@ void update_started() { String output; doc["status"] = "preparing"; - serializeJson(doc, output); - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); } void update_progress(int currentBytes, int totalBytes) { - String output; doc["status"] = "progress"; int progress = ((currentBytes * 100) / totalBytes); if (progress > previousProgress) { doc["progress"] = progress; - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); ESP_LOGV("Download OTA", "HTTP update process at %d of %d bytes... (%d %%)", currentBytes, totalBytes, progress); } previousProgress = progress; @@ -43,10 +43,9 @@ void update_progress(int currentBytes, int totalBytes) void update_finished() { - String output; doc["status"] = "finished"; - serializeJson(doc, output); - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); // delay to allow the event to be sent out vTaskDelay(100 / portTICK_PERIOD_MS); @@ -68,6 +67,7 @@ void updateTask(void *param) // httpUpdate.onEnd(update_finished); t_httpUpdate_return ret = httpUpdate.update(client, url.c_str()); + JsonObject jsonObject; switch (ret) { @@ -75,8 +75,8 @@ void updateTask(void *param) doc["status"] = "error"; doc["error"] = httpUpdate.getLastErrorString().c_str(); - serializeJson(doc, output); - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); ESP_LOGE("Download OTA", "HTTP Update failed with error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); #ifdef SERIAL_INFO @@ -87,8 +87,8 @@ void updateTask(void *param) doc["status"] = "error"; doc["error"] = "Update failed, has same firmware version"; - serializeJson(doc, output); - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); ESP_LOGE("Download OTA", "HTTP Update failed, has same firmware version"); #ifdef SERIAL_INFO @@ -143,10 +143,8 @@ esp_err_t DownloadFirmwareService::downloadUpdate(PsychicRequest *request, JsonV doc["progress"] = 0; doc["error"] = ""; - String output; - serializeJson(doc, output); - - _socket->emit(EVENT_DOWNLOAD_OTA, output.c_str()); + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_DOWNLOAD_OTA, jsonObject); if (xTaskCreatePinnedToCore( &updateTask, // Function that should be called diff --git a/lib/framework/EventEndpoint.h b/lib/framework/EventEndpoint.h index 2d07910b..86ff45cf 100644 --- a/lib/framework/EventEndpoint.h +++ b/lib/framework/EventEndpoint.h @@ -42,7 +42,8 @@ class EventEndpoint { _socket->registerEvent(_event); _socket->onEvent(_event, std::bind(&EventEndpoint::updateState, this, std::placeholders::_1, std::placeholders::_2)); - _socket->onSubscribe(_event, std::bind(&EventEndpoint::syncState, this, std::placeholders::_1, std::placeholders::_2)); + _socket->onSubscribe(_event, [&](const String &originId) + { syncState(originId, true); }); } private: @@ -61,11 +62,9 @@ class EventEndpoint { JsonDocument jsonDocument; JsonObject root = jsonDocument.to(); - String output; _statefulService->read(root, _stateReader); - serializeJson(root, output); - ESP_LOGV("EventEndpoint", "Syncing state: %s", output.c_str()); - _socket->emit(_event, output.c_str(), originId.c_str(), sync); + JsonObject jsonObject = jsonDocument.as(); + _socket->emitEvent(_event, jsonObject, originId.c_str(), sync); } }; diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 3fe66264..0cdee5a3 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -92,67 +92,6 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame return ESP_OK; } -void EventSocket::emit(String event, String payload) -{ - emit(event.c_str(), payload.c_str(), ""); -} - -void EventSocket::emit(const char *event, const char *payload) -{ - emit(event, payload, ""); -} - -void EventSocket::emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin) -{ - // Only process valid events - if (!isEventValid(String(event))) - { - ESP_LOGW("EventSocket", "Method tried to emit unregistered event: %s", event); - return; - } - - int originSubscriptionId = originId[0] ? atoi(originId) : -1; - xSemaphoreTake(clientSubscriptionsMutex, portMAX_DELAY); - auto &subscriptions = client_subscriptions[event]; - if (subscriptions.empty()) - { - xSemaphoreGive(clientSubscriptionsMutex); - return; - } - String msg = "[\"" + String(event) + "\"," + String(payload) + "]"; - - // if onlyToSameOrigin == true, send the message back to the origin - if (onlyToSameOrigin && originSubscriptionId > 0) - { - auto *client = _socket.getClient(originSubscriptionId); - if (client) - { - ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event, client->remoteIP().toString().c_str(), - msg.c_str()); - client->sendMessage(msg.c_str()); - } - } - else - { // else send the message to all other clients - - for (int subscription : client_subscriptions[event]) - { - if (subscription == originSubscriptionId) - continue; - auto *client = _socket.getClient(subscription); - if (!client) - { - subscriptions.remove(subscription); - continue; - } - ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event, client->remoteIP().toString().c_str(), - msg.c_str()); - client->sendMessage(msg.c_str()); - } - } - xSemaphoreGive(clientSubscriptionsMutex); -} - void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *originId, bool onlyToSameOrigin) { // Only process valid events @@ -173,7 +112,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or JsonDocument doc; doc["event"] = event; - doc["payload"] = jsonObject; + doc["data"] = jsonObject; size_t len = measureJson(doc) + 1; char *output = new char[len]; @@ -223,7 +162,7 @@ void EventSocket::handleSubscribeCallbacks(String event, const String &originId) { for (auto &callback : subscribe_callbacks[event]) { - callback(originId, true); + callback(originId); } } diff --git a/lib/framework/EventSocket.h b/lib/framework/EventSocket.h index 60fbc6fd..d10f66d2 100644 --- a/lib/framework/EventSocket.h +++ b/lib/framework/EventSocket.h @@ -25,7 +25,7 @@ #define EVENT_SERVICE_PATH "/ws/events" typedef std::function EventCallback; -typedef std::function SubscribeCallback; +typedef std::function SubscribeCallback; class EventSocket { @@ -40,14 +40,8 @@ class EventSocket void onSubscribe(String event, SubscribeCallback callback); - void emit(String event, String payload); - - void emit(const char *event, const char *payload); - - void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false); - // if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId - void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false); + // if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId private: PsychicHttpServer *_server; diff --git a/lib/framework/NotificationService.cpp b/lib/framework/NotificationService.cpp index 65385285..2940cc8c 100644 --- a/lib/framework/NotificationService.cpp +++ b/lib/framework/NotificationService.cpp @@ -17,9 +17,6 @@ void NotificationService::pushNotification(String message, pushType event) JsonDocument doc; doc["type"] = pushTypeStrings[event]; doc["message"] = message; - // convert into JsonVariant - - String output; - serializeJson(doc, output); - _eventSocket->emit(NOTIFICATION_EVENT, output.c_str()); + JsonObject jsonObject = doc.as(); + _eventSocket->emitEvent(NOTIFICATION_EVENT, jsonObject); } diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp index 3e1c0fb9..dd0c3828 100644 --- a/lib/framework/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -214,9 +214,11 @@ void WiFiSettingsService::configureNetwork(wifi_settings_t &network) void WiFiSettingsService::updateRSSI() { - char buffer[16]; - snprintf(buffer, sizeof(buffer), WiFi.isConnected() ? "%d" : "disconnected", WiFi.RSSI()); - _socket->emit(EVENT_RSSI, buffer); + JsonDocument doc; + doc["rssi"] = WiFi.RSSI(); + doc["ssid"] = WiFi.isConnected() ? WiFi.SSID() : "disconnected"; + JsonObject jsonObject = doc.as(); + _socket->emitEvent(EVENT_RSSI, jsonObject); } void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 477130a6..8c684d56 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -47,7 +47,7 @@ #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" #define WIFI_RECONNECTION_DELAY 1000 * 30 -#define RSSI_EVENT_DELAY 200 +#define RSSI_EVENT_DELAY 500 #define EVENT_RSSI "rssi" From 802bcb505a5f7db691ca931a364f4020d5c7eaf2 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:47:53 +0200 Subject: [PATCH 07/20] MessagePack instead of JSON --- lib/framework/EventSocket.cpp | 18 ++++++++++++++++++ platformio.ini | 2 ++ 2 files changed, 20 insertions(+) diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 0cdee5a3..6c69222c 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -114,9 +114,19 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or doc["event"] = event; doc["data"] = jsonObject; +#ifdef EVENT_JSON size_t len = measureJson(doc) + 1; +#else + size_t len = measureMsgPack(doc) + 1; +#endif + char *output = new char[len]; + +#ifdef EVENT_JSON serializeJson(doc, output, len); +#else + serializeMsgPack(doc, output, len); +#endif // if onlyToSameOrigin == true, send the message back to the origin if (onlyToSameOrigin && originSubscriptionId > 0) @@ -125,7 +135,11 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or if (client) { ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); +#ifdef EVENT_JSON client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); +#else + client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); +#endif } } else @@ -142,7 +156,11 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or continue; } ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); +#ifdef EVENT_JSON client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); +#else + client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); +#endif } } diff --git a/platformio.ini b/platformio.ini index db075e41..8e0be657 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,6 +38,8 @@ build_flags = ;-D SERVE_CONFIG_FILES ; Uncomment to enable informations from ESP32-Sveltekit in Serial Monitor -D SERIAL_INFO + ; Uncomment to use JSON instead of MessagePack for event messages. Default is MessagePack. + -D EVENT_JSON lib_compat_mode = strict From c372ae92a580f29f56735a3cc6bbb200714bb0a6 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:55:12 +0200 Subject: [PATCH 08/20] Renamed EVENT_USE_JSON --- lib/framework/EventSocket.cpp | 8 ++++---- platformio.ini | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 6c69222c..20aff917 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -114,7 +114,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or doc["event"] = event; doc["data"] = jsonObject; -#ifdef EVENT_JSON +#ifdef EVENT_USE_JSON size_t len = measureJson(doc) + 1; #else size_t len = measureMsgPack(doc) + 1; @@ -122,7 +122,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or char *output = new char[len]; -#ifdef EVENT_JSON +#ifdef EVENT_USE_JSON serializeJson(doc, output, len); #else serializeMsgPack(doc, output, len); @@ -135,7 +135,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or if (client) { ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); -#ifdef EVENT_JSON +#ifdef EVENT_USE_JSON client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); #else client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); @@ -156,7 +156,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or continue; } ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); -#ifdef EVENT_JSON +#ifdef EVENT_USE_JSON client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); #else client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); diff --git a/platformio.ini b/platformio.ini index 8e0be657..3be3cebf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,7 +39,7 @@ build_flags = ; Uncomment to enable informations from ESP32-Sveltekit in Serial Monitor -D SERIAL_INFO ; Uncomment to use JSON instead of MessagePack for event messages. Default is MessagePack. - -D EVENT_JSON + -D EVENT_USE_JSON lib_compat_mode = strict From 3d59fab6ab4d5c68d5062bee2e6c46e0f8d19e39 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:00:09 +0200 Subject: [PATCH 09/20] Comments platformio.ini --- platformio.ini | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 3be3cebf..9a0b208e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,19 +25,30 @@ build_flags = -D BUILD_TARGET=\"$PIOENV\" -D APP_NAME=\"ESP32-Sveltekit\" ; Must only contain characters from [a-zA-Z0-9-_] as this is converted into a filename -D APP_VERSION=\"0.5.0\" ; semver compatible version string - ; Uncomment to receive log messages from the ESP Arduino Core - -D CORE_DEBUG_LEVEL=5 + ; Move all networking stuff to the protocol core 0 and leave business logic on application core 1 -D ESP32SVELTEKIT_RUNNING_CORE=0 + ; Uncomment EMBED_WWW to embed the WWW data in the firmware binary -D EMBED_WWW + ; Uncomment to configure Cross-Origin Resource Sharing ; -D ENABLE_CORS ; -D CORS_ORIGIN=\"*\" - ; Serve config files from flash - ;-D SERVE_CONFIG_FILES + ; Uncomment to enable informations from ESP32-Sveltekit in Serial Monitor -D SERIAL_INFO + + ; D E B U G B U I L D F L A G S + ; =============================== + ; These build flags are only for debugging purposes and should not be used in production + + ; Uncomment to show log messages from the ESP Arduino Core and ESP32-SvelteKit + -D CORE_DEBUG_LEVEL=5 + + ; Serve config files from flash and access at /config/filename.json + ;-D SERVE_CONFIG_FILES + ; Uncomment to use JSON instead of MessagePack for event messages. Default is MessagePack. -D EVENT_USE_JSON From d0d4d560a990c7ffdd18d5d477daf908a973fe80 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sun, 28 Apr 2024 14:16:06 +0200 Subject: [PATCH 10/20] Fixes buffer offset --- lib/framework/EventSocket.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 20aff917..5fc521cf 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -115,12 +115,12 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or doc["data"] = jsonObject; #ifdef EVENT_USE_JSON - size_t len = measureJson(doc) + 1; + size_t len = measureJson(doc); #else - size_t len = measureMsgPack(doc) + 1; + size_t len = measureMsgPack(doc); #endif - char *output = new char[len]; + char *output = new char[len + 1]; #ifdef EVENT_USE_JSON serializeJson(doc, output, len); From 78ee2a8b0c0b6aa04c109a14de4212347f8d5687 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sun, 28 Apr 2024 14:25:11 +0200 Subject: [PATCH 11/20] Adds msgpack-lite --- interface/package.json | 2 ++ interface/src/lib/stores/socket.ts | 19 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/interface/package.json b/interface/package.json index 7d52c390..836da6e3 100644 --- a/interface/package.json +++ b/interface/package.json @@ -18,6 +18,7 @@ "@sveltejs/kit": "^1.22.4", "@typescript-eslint/eslint-plugin": "^6.2.1", "@typescript-eslint/parser": "^6.2.1", + "@types/msgpack-lite": "^0.1.11", "autoprefixer": "^10.4.14", "eslint": "^8.46.0", "eslint-config-prettier": "^9.0.0", @@ -41,6 +42,7 @@ "compare-versions": "^6.0.0", "daisyui": "^4.10.2", "jwt-decode": "^4.0.0", + "msgpack-lite": "^0.1.26", "postcss-remove-classes": "^2.0.0", "svelte-dnd-list": "^0.1.8", "svelte-modals": "^1.3.0" diff --git a/interface/src/lib/stores/socket.ts b/interface/src/lib/stores/socket.ts index e230c25b..a27b2772 100644 --- a/interface/src/lib/stores/socket.ts +++ b/interface/src/lib/stores/socket.ts @@ -1,4 +1,5 @@ import { writable } from 'svelte/store'; +import msgpack from 'msgpack-lite'; function createWebSocket() { let listeners = new Map void>>(); @@ -26,6 +27,7 @@ function createWebSocket() { function connect() { ws = new WebSocket(socketUrl); + ws.binaryType = 'arraybuffer'; ws.onopen = (ev) => { set(true); clearTimeout(reconnectTimeoutId); @@ -37,22 +39,19 @@ function createWebSocket() { }; ws.onmessage = (message) => { resetUnresponsiveCheck(); - let data = message.data; + let payload = message.data; - if (data instanceof ArrayBuffer) { - listeners.get('binary')?.forEach((listener) => listener(data)); - return; - } - listeners.get('message')?.forEach((listener) => listener(data)); + const binary = payload instanceof ArrayBuffer; + listeners.get(binary ? 'binary' : 'message')?.forEach((listener) => listener(payload)); try { - data = JSON.parse(message.data); + payload = binary ? msgpack.decode(new Uint8Array(payload)) : JSON.parse(payload); } catch (error) { listeners.get('error')?.forEach((listener) => listener(error)); return; } - listeners.get('json')?.forEach((listener) => listener(data)); - const [event, payload] = data; - if (event) listeners.get(event)?.forEach((listener) => listener(payload)); + listeners.get('json')?.forEach((listener) => listener(payload)); + const { event, data } = payload; + if (event) listeners.get(event)?.forEach((listener) => listener(data)); }; ws.onerror = (ev) => disconnect('error', ev); ws.onclose = (ev) => disconnect('close', ev); From c400a6e45a99125b8dd180e29cad7a7c4bc38d7f Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sun, 28 Apr 2024 18:18:11 +0200 Subject: [PATCH 12/20] Adds menu persistence between browser navigation --- interface/src/routes/menu.svelte | 93 +++++++++++++------------------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/interface/src/routes/menu.svelte b/interface/src/routes/menu.svelte index 169ce948..7c1c3bd1 100644 --- a/interface/src/routes/menu.svelte +++ b/interface/src/routes/menu.svelte @@ -18,7 +18,6 @@ import NTP from '~icons/tabler/clock-check'; import Metrics from '~icons/tabler/report-analytics'; import { page } from '$app/stores'; - import { onMount } from 'svelte'; import { user } from '$lib/stores/user'; import { createEventDispatcher } from 'svelte'; @@ -28,7 +27,7 @@ type menuItem = { title: string; - icon: object; + icon: ConstructorOfATypedSvelteComponent; href?: string; feature: boolean; active?: boolean; @@ -37,7 +36,7 @@ type subMenuItem = { title: string; - icon: object; + icon: ConstructorOfATypedSvelteComponent; href: string; feature: boolean; active: boolean; @@ -49,7 +48,7 @@ icon: Control, href: '/demo', feature: true, - active: false + }, { title: 'Connections', @@ -61,14 +60,14 @@ icon: MQTT, href: '/connections/mqtt', feature: $page.data.features.mqtt, - active: false + }, { title: 'NTP', icon: NTP, href: '/connections/ntp', feature: $page.data.features.ntp, - active: false + } ] }, @@ -82,14 +81,14 @@ icon: Router, href: '/wifi/sta', feature: true, - active: false + }, { title: 'Access Point', icon: AP, href: '/wifi/ap', feature: true, - active: false + } ] }, @@ -98,7 +97,7 @@ icon: Users, href: '/user', feature: $page.data.features.security && $user.admin, - active: false + }, { title: 'System', @@ -110,14 +109,14 @@ icon: Health, href: '/system/status', feature: true, - active: false + }, { title: 'System Metrics', icon: Metrics, href: '/system/metrics', feature: $page.data.features.analytics, - active: false + }, { title: 'Firmware Update', @@ -128,43 +127,25 @@ $page.data.features.upload_firmware || $page.data.features.download_firmware) && (!$page.data.features.security || $user.admin), - active: false } ] } - ]; + ] as menuItem[]; const dispatch = createEventDispatcher(); - function setActiveMenuItem(menuItems: menuItem[], targetTitle: string) { - for (let i = 0; i < menuItems.length; i++) { - const menuItem = menuItems[i]; - - // Clear any previous set active flags - menuItem.active = false; - - // Check if the current menu item's title matches the target title - if (menuItem.title === targetTitle) { - menuItem.active = true; // Set the active property to true - dispatch('menuClicked'); - } - - // Check if the current menu item has a submenu - if (menuItem.submenu && menuItem.submenu.length > 0) { - // Recursively call the function for each submenu item - setActiveMenuItem(menuItem.submenu, targetTitle); - } - } - if (targetTitle == '') { - dispatch('menuClicked'); - } - menuItems = menuItems; + function setActiveMenuItem(targetTitle: string) { + menuItems.forEach(item => { + item.active = item.title === targetTitle; + item.submenu?.forEach(subItem => { + subItem.active = subItem.title === targetTitle; + }); + }); + menuItems = menuItems + dispatch('menuClicked'); } - onMount(() => { - setActiveMenuItem(menuItems, $page.data.title); - menuItems = menuItems; - }); + $: setActiveMenuItem($page.data.title);
@@ -172,7 +153,7 @@ setActiveMenuItem(menuItems, '')} + on:click={() => setActiveMenuItem('')} > Logo

{$page.data.appName}

@@ -180,21 +161,23 @@
@@ -232,6 +214,7 @@ {$user.username} +
{ From 99da2529026972727ddb091f1ee59f69b01be789 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sun, 28 Apr 2024 19:34:17 +0200 Subject: [PATCH 13/20] Moves the static ip config to own property --- interface/src/routes/wifi/sta/Wifi.svelte | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/interface/src/routes/wifi/sta/Wifi.svelte b/interface/src/routes/wifi/sta/Wifi.svelte index 34864975..57911a8e 100644 --- a/interface/src/routes/wifi/sta/Wifi.svelte +++ b/interface/src/routes/wifi/sta/Wifi.svelte @@ -34,6 +34,8 @@ import InfoDialog from '$lib/components/InfoDialog.svelte'; import type { KnownNetworkItem, WifiSettings, WifiStatus } from '$lib/types/models'; + let static_ip_config = false; + let networkEditable: KnownNetworkItem = { ssid: '', password: '', @@ -152,7 +154,7 @@ formErrors.ssid = false; } - if (networkEditable.static_ip_config) { + if (static_ip_config) { // RegEx for IPv4 const regexExp = /\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/; @@ -609,13 +611,13 @@ > Static IP Config?
- {#if networkEditable.static_ip_config} + {#if static_ip_config}
Date: Sun, 28 Apr 2024 22:34:13 +0200 Subject: [PATCH 14/20] Various messagepack fixes --- CHANGELOG.md | 12 ++- docs/stores.md | 1 + docs/structure.md | 2 - interface/package-lock.json | 74 +++++++++++++++++++ .../src/lib/components/RSSIIndicator.svelte | 53 +++++++------ interface/src/lib/stores/socket.ts | 5 ++ interface/src/lib/stores/telemetry.ts | 25 ++++--- interface/src/lib/types/models.ts | 18 ++++- interface/src/routes/+layout.svelte | 11 ++- interface/src/routes/statusbar.svelte | 7 +- .../src/routes/wifi/ap/Accesspoint.svelte | 2 +- interface/src/routes/wifi/sta/Wifi.svelte | 8 +- lib/framework/EventSocket.cpp | 10 ++- lib/framework/Features.h | 3 + lib/framework/FeaturesService.cpp | 7 ++ 15 files changed, 189 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f8510cc..1561f257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,16 +4,23 @@ All notable changes to this project will be documented in this file. ## [0.5.0] - WIP -Introduces a +Changes the Event Socket System to use a clearer message structure and MessagePack. Brings breaking changes to the `EventSocket.h` API. + +Updated daisyUI to v4. This has changes in the colors and switches to OKLCH. Also button groups and input groups have been depreciated in favor of join. This might require changes to custom parts of the code. + +Updates ArduinoJSON from v6 to v7. If you make use of ArduinoJSON changes might be required. ### Added +- Debug buildflag to switch between MessagePack and JSON for event messages. +- Show SSID of the current WiFi Station as tooltip of the RSSI icon. + ### Changed - Moved MQTT types to models.ts as well. [#49](https://github.com/theelims/ESP32-sveltekit/pull/49) - Updated daisyUI to 4.10.2 [#48](https://github.com/theelims/ESP32-sveltekit/pull/48) - Fixed spelling error in models.ts -- Changed ArduinoJson from v6 to v7 +- Changed ArduinoJson from v6 to v7 increasing the free heap by ~40kb - Split NotificationService out of EventSocket into own class - Changed API of EventSocket.h. Now uses `void emitEvent(String event, JsonObject &jsonObject, const char *originId = "", bool onlyToSameOrigin = false);`. - Changed event socket message format to MessagePack @@ -21,6 +28,7 @@ Introduces a ### Fixed - Fixes to WiFi.svelte and models.ts to fix type errors and visibility rights. +- Fixes bug in highlighting the menu when navigating with the browser (back/forward) ### Removed diff --git a/docs/stores.md b/docs/stores.md index 07b7077d..09bd1a53 100644 --- a/docs/stores.md +++ b/docs/stores.md @@ -57,6 +57,7 @@ It exposes the following properties you can subscribe to: | Property | Type | Description | | ---------------------------------- | --------- | ------------------------------------------- | | `$telemetry.rssi.rssi` | `Number` | The RSSI signal strength of the WiFi in dBm | +| `$telemetry.rssi.ssid` | `String` | Name of the connected WiFi station | | `$telemetry.rssi.connected` | `Boolean` | Connection status of the WiFi | | `$telemetry.battery.soc` | `Number` | Battery state of charge | | `$telemetry.battery.charging` | `Boolean` | Is battery connected to charger | diff --git a/docs/structure.md b/docs/structure.md index a6a10c84..8c810446 100644 --- a/docs/structure.md +++ b/docs/structure.md @@ -49,7 +49,6 @@ The menu consists of an array of menu items. These are defined as follows: icon: Control, href: '/demo', feature: $page.data.features.project, - active: false }, ``` @@ -68,7 +67,6 @@ export const load = (async ({ fetch }) => { - `icon` must be an icon component giving the menu items icon. - `href` is the link to the route the menu item refers to. - `feature` takes a bool and should be set to `true`. It is used by the [feature selector](#features) to hide a menu entry of it is not present on the back end. -- `active` takes a bool as well and should be set to `false` by default. It is automatically set to `true` to highlight a menu entry as active. ## Advanced Customizations diff --git a/interface/package-lock.json b/interface/package-lock.json index 34f9b976..e60017f5 100644 --- a/interface/package-lock.json +++ b/interface/package-lock.json @@ -12,6 +12,7 @@ "compare-versions": "^6.0.0", "daisyui": "^4.10.2", "jwt-decode": "^4.0.0", + "msgpack-lite": "^0.1.26", "postcss-remove-classes": "^2.0.0", "svelte-dnd-list": "^0.1.8", "svelte-modals": "^1.3.0" @@ -21,6 +22,7 @@ "@sveltejs/adapter-auto": "^2.1.0", "@sveltejs/adapter-static": "^2.0.3", "@sveltejs/kit": "^1.22.4", + "@types/msgpack-lite": "^0.1.11", "@typescript-eslint/eslint-plugin": "^6.2.1", "@typescript-eslint/parser": "^6.2.1", "autoprefixer": "^10.4.14", @@ -893,6 +895,24 @@ "integrity": "sha512-RbSSoHliUbnXj3ny0CNFOoxrIDV6SUGyStHsvDqosw6CkdPV8TtWGlfecuK4ToyMEAql6pzNxgCFKanovUzlgQ==", "dev": true }, + "node_modules/@types/msgpack-lite": { + "version": "0.1.11", + "resolved": "https://registry.npmjs.org/@types/msgpack-lite/-/msgpack-lite-0.1.11.tgz", + "integrity": "sha512-cdCZS/gw+jIN22I4SUZUFf1ZZfVv5JM1//Br/MuZcI373sxiy3eSSoiyLu0oz+BPatTbGGGBO5jrcvd0siCdTQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/node": { + "version": "20.12.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", + "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, "node_modules/@types/pug": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.7.tgz", @@ -1924,6 +1944,11 @@ "node": ">=0.10.0" } }, + "node_modules/event-lite": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/event-lite/-/event-lite-0.1.3.tgz", + "integrity": "sha512-8qz9nOz5VeD2z96elrEKD2U433+L3DWdUdDkOINLGOJvx1GsMBbMn0aCeu28y8/e85A6mCigBiFlYMnTBEGlSw==" + }, "node_modules/execa": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz", @@ -2230,6 +2255,25 @@ "node": ">=16.17.0" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -2290,6 +2334,11 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "dev": true }, + "node_modules/int64-buffer": { + "version": "0.1.10", + "resolved": "https://registry.npmjs.org/int64-buffer/-/int64-buffer-0.1.10.tgz", + "integrity": "sha512-v7cSY1J8ydZ0GyjUHqF+1bshJ6cnEVLo9EnjB8p+4HDRPZc9N5jjmvUV7NvEsqQOKyH0pmIBFWXVQbiS0+OBbA==" + }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", @@ -2373,6 +2422,11 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -2683,6 +2737,20 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "node_modules/msgpack-lite": { + "version": "0.1.26", + "resolved": "https://registry.npmjs.org/msgpack-lite/-/msgpack-lite-0.1.26.tgz", + "integrity": "sha512-SZ2IxeqZ1oRFGo0xFGbvBJWMp3yLIY9rlIJyxy8CGrwZn1f0ZK4r6jV/AM1r0FZMDUkWkglOk/eeKIL9g77Nxw==", + "dependencies": { + "event-lite": "^0.1.1", + "ieee754": "^1.1.8", + "int64-buffer": "^0.1.9", + "isarray": "^1.0.0" + }, + "bin": { + "msgpack": "bin/msgpack" + } + }, "node_modules/mz": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz", @@ -4043,6 +4111,12 @@ "node": ">=14.0" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/unplugin": { "version": "1.10.1", "resolved": "https://registry.npmjs.org/unplugin/-/unplugin-1.10.1.tgz", diff --git a/interface/src/lib/components/RSSIIndicator.svelte b/interface/src/lib/components/RSSIIndicator.svelte index 6810bb4f..7feef375 100644 --- a/interface/src/lib/components/RSSIIndicator.svelte +++ b/interface/src/lib/components/RSSIIndicator.svelte @@ -6,30 +6,37 @@ export let showDBm = false; export let rssi_dbm = 0; + export let ssid = ''; + + $: if (ssid === '') { + ssid = 'Unknown'; + }
- {#if showDBm} - - {rssi_dbm} dBm - - {/if} - {#if rssi_dbm >= -55} - - {:else if rssi_dbm >= -75} -
- - -
- {:else if rssi_dbm >= -85} -
- - -
- {:else} -
- - -
- {/if} +
+ {#if showDBm} + + {rssi_dbm} dBm + + {/if} + {#if rssi_dbm >= -55} + + {:else if rssi_dbm >= -75} +
+ + +
+ {:else if rssi_dbm >= -85} +
+ + +
+ {:else} +
+ + +
+ {/if} +
diff --git a/interface/src/lib/stores/socket.ts b/interface/src/lib/stores/socket.ts index a27b2772..80cb5502 100644 --- a/interface/src/lib/stores/socket.ts +++ b/interface/src/lib/stores/socket.ts @@ -1,4 +1,5 @@ import { writable } from 'svelte/store'; +import { page } from '$app/stores'; import msgpack from 'msgpack-lite'; function createWebSocket() { @@ -78,7 +79,11 @@ function createWebSocket() { function send(msg: unknown) { if (!ws || ws.readyState !== WebSocket.OPEN) return; + // if ($page.data.features.event_use_json) { ws.send(JSON.stringify(msg)); + // } else { + // ws.send(msgpack.encode(msg)); + // } } function sendEvent(event: string, data: unknown) { diff --git a/interface/src/lib/stores/telemetry.ts b/interface/src/lib/stores/telemetry.ts index c6eead61..31ee6d59 100644 --- a/interface/src/lib/stores/telemetry.ts +++ b/interface/src/lib/stores/telemetry.ts @@ -1,8 +1,12 @@ import { writable } from 'svelte/store'; +import type { RSSI } from '../types/models'; +import type { Battery } from '../types/models'; +import type { DownloadOTA } from '../types/models'; let telemetry_data = { rssi: { rssi: 0, + ssid: '', disconnected: true }, battery: { @@ -21,28 +25,29 @@ function createTelemetry() { return { subscribe, - setRSSI: (data: string) => { - if (!isNaN(Number(data))) { + setRSSI: (data: RSSI) => { + if (!isNaN(Number(data.rssi))) { update((telemetry_data) => ({ ...telemetry_data, - rssi: { rssi: Number(data), disconnected: false } + rssi: { rssi: Number(data.rssi), ssid: data.ssid, disconnected: false } })); } else { - update((telemetry_data) => ({ ...telemetry_data, rssi: { rssi: 0, disconnected: true } })); + update((telemetry_data) => ({ + ...telemetry_data, + rssi: { rssi: 0, ssid: data.ssid, disconnected: true } + })); } }, - setBattery: (data: string) => { - const content = JSON.parse(data); + setBattery: (data: Battery) => { update((telemetry_data) => ({ ...telemetry_data, - battery: { soc: content.soc, charging: content.charging } + battery: { soc: data.soc, charging: data.charging } })); }, - setDownloadOTA: (data: string) => { - const content = JSON.parse(data); + setDownloadOTA: (data: DownloadOTA) => { update((telemetry_data) => ({ ...telemetry_data, - download_ota: { status: content.status, progress: content.progress, error: content.error } + download_ota: { status: data.status, progress: data.progress, error: data.error } })); } }; diff --git a/interface/src/lib/types/models.ts b/interface/src/lib/types/models.ts index a6b75708..a1f7d516 100644 --- a/interface/src/lib/types/models.ts +++ b/interface/src/lib/types/models.ts @@ -94,6 +94,22 @@ export type Analytics = { uptime: number; }; +export type RSSI = { + rssi: number; + ssid: string; +}; + +export type Battery = { + soc: number; + charging: boolean; +}; + +export type DownloadOTA = { + status: string; + progress: number; + error: string; +}; + export type StaticSystemInformation = { esp_platform: string; firmware_version: string; @@ -127,4 +143,4 @@ export type MQTTSettings = { client_id: string; keep_alive: number; clean_session: boolean; -}; \ No newline at end of file +}; diff --git a/interface/src/routes/+layout.svelte b/interface/src/routes/+layout.svelte index ff8388b8..57df51ea 100644 --- a/interface/src/routes/+layout.svelte +++ b/interface/src/routes/+layout.svelte @@ -16,6 +16,9 @@ import Statusbar from './statusbar.svelte'; import Login from './login.svelte'; import type { Analytics } from '$lib/types/models'; + import type { RSSI } from '$lib/types/models'; + import type { Battery } from '$lib/types/models'; + import type { DownloadOTA } from '$lib/types/models'; export let data: LayoutData; @@ -77,7 +80,7 @@ const handleClose = () => { notifications.error('Connection to device lost', 5000); - telemetry.setRSSI('lost'); + telemetry.setRSSI({ rssi: 0, ssid: '' }); }; const handleError = (data: any) => console.error(data); @@ -103,11 +106,11 @@ const handleAnalytics = (data: Analytics) => analytics.addData(data); - const handleNetworkStatus = (data: string) => telemetry.setRSSI(data); + const handleNetworkStatus = (data: RSSI) => telemetry.setRSSI(data); - const handleBattery = (data: string) => telemetry.setBattery(data); + const handleBattery = (data: Battery) => telemetry.setBattery(data); - const handleOAT = (data: string) => telemetry.setDownloadOTA(data); + const handleOAT = (data: DownloadOTA) => telemetry.setDownloadOTA(data); let menuOpen = false; diff --git a/interface/src/routes/statusbar.svelte b/interface/src/routes/statusbar.svelte index d790507d..006c7ba2 100644 --- a/interface/src/routes/statusbar.svelte +++ b/interface/src/routes/statusbar.svelte @@ -52,7 +52,12 @@ {#if $telemetry.rssi.disconnected} {:else} - + {/if}
diff --git a/interface/src/routes/wifi/ap/Accesspoint.svelte b/interface/src/routes/wifi/ap/Accesspoint.svelte index 6b110897..93ca2839 100644 --- a/interface/src/routes/wifi/ap/Accesspoint.svelte +++ b/interface/src/routes/wifi/ap/Accesspoint.svelte @@ -258,7 +258,7 @@ transition:slide|local={{ duration: 300, easing: cubicOut }} >
-
+
Saved Networks
{#await getWifiSettings()} @@ -740,7 +740,7 @@ {/if}
-
+
diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 5fc521cf..54d754b7 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -59,7 +59,11 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame request->client()->socket(), (char *)frame->payload); JsonDocument doc; +#ifdef EVENT_USE_JSON DeserializationError error = deserializeJson(doc, (char *)frame->payload, frame->len); +#else + DeserializationError error = deserializeMsgPack(doc, (char *)frame->payload, frame->len); +#endif if (!error && doc.is()) { @@ -88,6 +92,7 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame } return ESP_OK; } + ESP_LOGW("EventSocket", "Error[%d] parsing JSON: %s", error, (char *)frame->payload); } return ESP_OK; } @@ -123,11 +128,14 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or char *output = new char[len + 1]; #ifdef EVENT_USE_JSON - serializeJson(doc, output, len); + serializeJson(doc, output, len + 1); #else serializeMsgPack(doc, output, len); #endif + // null terminate the string + output[len] = '\0'; + // if onlyToSameOrigin == true, send the message back to the origin if (onlyToSameOrigin && originSubscriptionId > 0) { diff --git a/lib/framework/Features.h b/lib/framework/Features.h index 74d60a14..b6367c3b 100644 --- a/lib/framework/Features.h +++ b/lib/framework/Features.h @@ -57,4 +57,7 @@ #define FT_ANALYTICS 1 #endif +// Use JSON for events. Default, use MessagePack for events +#ifndef EVENT_USE_JSON +#define EVENT_USE_JSON 0 #endif diff --git a/lib/framework/FeaturesService.cpp b/lib/framework/FeaturesService.cpp index a4fcbc66..18853fab 100644 --- a/lib/framework/FeaturesService.cpp +++ b/lib/framework/FeaturesService.cpp @@ -65,6 +65,13 @@ void FeaturesService::begin() #else root["analytics"] = false; #endif + +#if FT_ENABLED(EVENT_USE_JSON) + root["event_use_json"] = true; +#else + root["event_use_json"] = false; +#endif + root["firmware_version"] = APP_VERSION; root["firmware_name"] = APP_NAME; root["firmware_built_target"] = BUILD_TARGET; From cd3d730393ddece9e6e231aaa9ba3fdc8bd793c6 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:18:40 +0200 Subject: [PATCH 15/20] Fixes nester array error with ArduinoJson v7 --- CHANGELOG.md | 4 ++-- lib/framework/Features.h | 2 ++ lib/framework/SecuritySettingsService.h | 2 +- lib/framework/WiFiScanner.cpp | 2 +- lib/framework/WiFiSettingsService.h | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1561f257..c70c5e95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ All notable changes to this project will be documented in this file. Changes the Event Socket System to use a clearer message structure and MessagePack. Brings breaking changes to the `EventSocket.h` API. -Updated daisyUI to v4. This has changes in the colors and switches to OKLCH. Also button groups and input groups have been depreciated in favor of join. This might require changes to custom parts of the code. +Updated daisyUI to v4. This has changes in the colors and switches to OKLCH. Also button groups and input groups have been depreciated in favor of join. This might require changes to custom parts of the code. Please double check all websites if the still have the desired looks. -Updates ArduinoJSON from v6 to v7. If you make use of ArduinoJSON changes might be required. +Updates ArduinoJSON from v6 to v7 to increase the available free heap. If you make use of ArduinoJSON, changes might be required. ### Added diff --git a/lib/framework/Features.h b/lib/framework/Features.h index b6367c3b..ce4c5188 100644 --- a/lib/framework/Features.h +++ b/lib/framework/Features.h @@ -61,3 +61,5 @@ #ifndef EVENT_USE_JSON #define EVENT_USE_JSON 0 #endif + +#endif diff --git a/lib/framework/SecuritySettingsService.h b/lib/framework/SecuritySettingsService.h index 4b62013d..d68953e6 100644 --- a/lib/framework/SecuritySettingsService.h +++ b/lib/framework/SecuritySettingsService.h @@ -60,7 +60,7 @@ class SecuritySettings root["jwt_secret"] = settings.jwtSecret; // users - JsonArray users = root["users"].add(); + JsonArray users = root["users"].to(); for (User user : settings.users) { JsonObject userRoot = users.add(); diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index 6d5612e4..39be4a1f 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -54,7 +54,7 @@ esp_err_t WiFiScanner::listNetworks(PsychicRequest *request) { PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); - JsonArray networks = root["networks"].add(); + JsonArray networks = root["networks"].to(); for (int i = 0; i < numNetworks; i++) { JsonObject network = networks.add(); diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 8c684d56..1916fcbb 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -79,7 +79,7 @@ class WiFiSettings root["priority_RSSI"] = settings.priorityBySignalStrength; // create JSON array from root - JsonArray wifiNetworks = root["wifi_networks"].add(); + JsonArray wifiNetworks = root["wifi_networks"].to(); // iterate over the wifiSettings for (auto &wifi : settings.wifiSettings) From 16ffaf97473f11fbbec21ef6fc550e2a003d9551 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:35:25 +0200 Subject: [PATCH 16/20] socket.ts messagepack --- interface/src/lib/stores/socket.ts | 15 ++++++++------- interface/src/routes/+layout.svelte | 5 ++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/interface/src/lib/stores/socket.ts b/interface/src/lib/stores/socket.ts index 80cb5502..fbb7168d 100644 --- a/interface/src/lib/stores/socket.ts +++ b/interface/src/lib/stores/socket.ts @@ -1,5 +1,4 @@ import { writable } from 'svelte/store'; -import { page } from '$app/stores'; import msgpack from 'msgpack-lite'; function createWebSocket() { @@ -11,9 +10,11 @@ function createWebSocket() { let reconnectTimeoutId: number; let ws: WebSocket; let socketUrl: string | URL; + let event_use_json = false; - function init(url: string | URL) { + function init(url: string | URL, use_json: boolean = false) { socketUrl = url; + event_use_json = use_json; connect(); } @@ -79,11 +80,11 @@ function createWebSocket() { function send(msg: unknown) { if (!ws || ws.readyState !== WebSocket.OPEN) return; - // if ($page.data.features.event_use_json) { - ws.send(JSON.stringify(msg)); - // } else { - // ws.send(msgpack.encode(msg)); - // } + if (event_use_json) { + ws.send(JSON.stringify(msg)); + } else { + ws.send(msgpack.encode(msg)); + } } function sendEvent(event: string, data: unknown) { diff --git a/interface/src/routes/+layout.svelte b/interface/src/routes/+layout.svelte index 57df51ea..b11606e7 100644 --- a/interface/src/routes/+layout.svelte +++ b/interface/src/routes/+layout.svelte @@ -27,7 +27,10 @@ await validateUser($user); } const ws_token = $page.data.features.security ? '?access_token=' + $user.bearer_token : ''; - socket.init(`ws://${window.location.host}/ws/events${ws_token}`); + socket.init( + `ws://${window.location.host}/ws/events${ws_token}`, + $page.data.features.event_use_json + ); addEventListeners(); }); From 96bc2368e88342b52c7dffc264ce974c7470285b Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Mon, 29 Apr 2024 22:39:13 +0200 Subject: [PATCH 17/20] Honorable mention --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c70c5e95..e8b9167d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,10 @@ Updates ArduinoJSON from v6 to v7 to increase the available free heap. If you ma - Removed duplicate in ESP32SvelteKit.cpp [#47](https://github.com/theelims/ESP32-sveltekit/pull/47) and WiFi.svelte [#50](https://github.com/theelims/ESP32-sveltekit/pull/50) +### Acknowledgment + +Many thanks to @runeharlyk who contributed significantly to the new event socket system and fixed many smaller issues with the front-end. + ## [0.4.0] - 2024-04-21 This upgrade might require one minor change as `MqttPubSub.h` and its class had been renamed to `MqttEndpoint.h` and `MqttEndoint` respectively. However, it is strongly advised, that you change all existing WebSocketServer endpoints to the new event socket system. From b9e946125f6f4312b12099848b8328e37a4fa880 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Mon, 6 May 2024 21:40:49 +0200 Subject: [PATCH 18/20] Receive MessagePack --- docs/buildprocess.md | 4 ++-- docs/statefulservice.md | 2 +- lib/framework/EventSocket.cpp | 17 +++++++++++------ platformio.ini | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/docs/buildprocess.md b/docs/buildprocess.md index a3ed5357..cee2397f 100644 --- a/docs/buildprocess.md +++ b/docs/buildprocess.md @@ -56,12 +56,12 @@ In addition custom features might be added or removed at runtime. See [Custom Fe ## Factory Settings -The framework has built-in factory settings which act as default values for the various configurable services where settings are not saved on the file system. These settings can be overridden using the build flags defined in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). +The framework has built-in factory settings which act as default values for the various configurable services where settings are not saved on the file system. These settings can be overridden using the build flags defined in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). All strings entered here must be escaped, especially special characters. Customize the settings as you see fit, for example you might configure your home WiFi network as the factory default: ```ini - -D FACTORY_WIFI_SSID=\"My Awesome WiFi Network\" + -D FACTORY_WIFI_SSID=\"My\ Awesome\ WiFi\ Network\" -D FACTORY_WIFI_PASSWORD=\"secret\" -D FACTORY_WIFI_HOSTNAME=\"awesome_light_controller\" ``` diff --git a/docs/statefulservice.md b/docs/statefulservice.md index 91b89669..600dc2dc 100644 --- a/docs/statefulservice.md +++ b/docs/statefulservice.md @@ -399,7 +399,7 @@ In case of a websocket connection the JWT token is supplied as a search paramete ## Placeholder substitution -Various settings support placeholder substitution, indicated by comments in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). This can be particularly useful where settings need to be unique, such as the Access Point SSID or MQTT client id. The following placeholders are supported: +Various settings support placeholder substitution, indicated by comments in [factory_settings.ini](https://github.com/theelims/ESP32-sveltekit/blob/main/factory_settings.ini). This can be particularly useful where settings need to be unique, such as the Access Point SSID or MQTT client id. Strings must be properly escaped in the ini-file. The following placeholders are supported: | Placeholder | Substituted value | | ------------ | --------------------------------------------------------------------- | diff --git a/lib/framework/EventSocket.cpp b/lib/framework/EventSocket.cpp index 54d754b7..e6be158d 100644 --- a/lib/framework/EventSocket.cpp +++ b/lib/framework/EventSocket.cpp @@ -53,15 +53,20 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame ESP_LOGV("EventSocket", "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(), request->client()->socket(), frame->type); + JsonDocument doc; +#if FT_ENABLED(EVENT_USE_JSON) if (frame->type == HTTPD_WS_TYPE_TEXT) { ESP_LOGV("EventSocket", "ws[%s][%u] request: %s", request->client()->remoteIP().toString().c_str(), request->client()->socket(), (char *)frame->payload); - JsonDocument doc; -#ifdef EVENT_USE_JSON DeserializationError error = deserializeJson(doc, (char *)frame->payload, frame->len); #else + if (frame->type == HTTPD_WS_TYPE_BINARY) + { + ESP_LOGV("EventSocket", "ws[%s][%u] request: %s", request->client()->remoteIP().toString().c_str(), + request->client()->socket(), (char *)frame->payload); + DeserializationError error = deserializeMsgPack(doc, (char *)frame->payload, frame->len); #endif @@ -119,7 +124,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or doc["event"] = event; doc["data"] = jsonObject; -#ifdef EVENT_USE_JSON +#if FT_ENABLED(EVENT_USE_JSON) size_t len = measureJson(doc); #else size_t len = measureMsgPack(doc); @@ -127,7 +132,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or char *output = new char[len + 1]; -#ifdef EVENT_USE_JSON +#if FT_ENABLED(EVENT_USE_JSON) serializeJson(doc, output, len + 1); #else serializeMsgPack(doc, output, len); @@ -143,7 +148,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or if (client) { ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); -#ifdef EVENT_USE_JSON +#if FT_ENABLED(EVENT_USE_JSON) client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); #else client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); @@ -164,7 +169,7 @@ void EventSocket::emitEvent(String event, JsonObject &jsonObject, const char *or continue; } ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message[%d]: %s", event, client->remoteIP().toString().c_str(), len, output); -#ifdef EVENT_USE_JSON +#if FT_ENABLED(EVENT_USE_JSON) client->sendMessage(HTTPD_WS_TYPE_TEXT, output, len); #else client->sendMessage(HTTPD_WS_TYPE_BINARY, output, len); diff --git a/platformio.ini b/platformio.ini index 9a0b208e..81886251 100644 --- a/platformio.ini +++ b/platformio.ini @@ -44,13 +44,13 @@ build_flags = ; These build flags are only for debugging purposes and should not be used in production ; Uncomment to show log messages from the ESP Arduino Core and ESP32-SvelteKit - -D CORE_DEBUG_LEVEL=5 + -D CORE_DEBUG_LEVEL=4 ; Serve config files from flash and access at /config/filename.json ;-D SERVE_CONFIG_FILES ; Uncomment to use JSON instead of MessagePack for event messages. Default is MessagePack. - -D EVENT_USE_JSON + ; -D EVENT_USE_JSON=1 lib_compat_mode = strict From 6558ec1ccd580dab3fc2d70e69b017181f28467f Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Mon, 6 May 2024 22:58:26 +0200 Subject: [PATCH 19/20] Use BSSID for wifi sta connection --- CHANGELOG.md | 1 + lib/framework/WiFiSettingsService.cpp | 17 +++++++++++------ lib/framework/WiFiSettingsService.h | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8b9167d..05590327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Updates ArduinoJSON from v6 to v7 to increase the available free heap. If you ma - Fixes to WiFi.svelte and models.ts to fix type errors and visibility rights. - Fixes bug in highlighting the menu when navigating with the browser (back/forward) +- Made WiFi connection routine more robust by using BSSID. Ensures that the STA truly connects to the strongest hotspot, even if several hotspots are in reach. ### Removed diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp index dd0c3828..80626c3f 100644 --- a/lib/framework/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -140,7 +140,7 @@ void WiFiSettingsService::connectToWiFi() int32_t chan_scan; WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan); - ESP_LOGV("WiFiSettingsService", "SSID: %s, RSSI: %d dbm", ssid_scan.c_str(), rssi_scan); + ESP_LOGV("WiFiSettingsService", "SSID: %s, BSSID: " MACSTR ", RSSI: %d dbm, Channel: %d", ssid_scan.c_str(), MAC2STR(BSSID_scan), rssi_scan, chan_scan); for (auto &network : _state.wifiSettings) { @@ -149,12 +149,17 @@ void WiFiSettingsService::connectToWiFi() if (rssi_scan > bestNetworkDb) { // best network bestNetworkDb = rssi_scan; - bestNetwork = &network; + ESP_LOGV("WiFiSettingsService", "--> New best network SSID: %s, BSSID: " MACSTR "", ssid_scan.c_str(), MAC2STR(BSSID_scan)); network.available = true; + network.channel = chan_scan; + memcpy(network.bssid, BSSID_scan, 6); + bestNetwork = &network; } - else if (rssi_scan >= FACTORY_WIFI_RSSI_THRESHOLD) + else if (rssi_scan >= FACTORY_WIFI_RSSI_THRESHOLD && network.available == false) { // available network network.available = true; + network.channel = chan_scan; + memcpy(network.bssid, BSSID_scan, 6); } } break; @@ -176,9 +181,8 @@ void WiFiSettingsService::connectToWiFi() } else if (_state.priorityBySignalStrength == true && bestNetwork) { - ESP_LOGI("WiFiSettingsService", "Connecting to strongest network: %s", bestNetwork->ssid.c_str()); + ESP_LOGI("WiFiSettingsService", "Connecting to strongest network: %s, BSSID: " MACSTR " ", bestNetwork->ssid.c_str(), MAC2STR(bestNetwork->bssid)); configureNetwork(*bestNetwork); - WiFi.begin(bestNetwork->ssid.c_str(), bestNetwork->password.c_str()); } else // no suitable network to connect { @@ -205,7 +209,8 @@ void WiFiSettingsService::configureNetwork(wifi_settings_t &network) WiFi.setHostname(_state.hostname.c_str()); // attempt to connect to the network - WiFi.begin(network.ssid.c_str(), network.password.c_str()); + WiFi.begin(network.ssid.c_str(), network.password.c_str(), network.channel, network.bssid); + // WiFi.begin(network.ssid.c_str(), network.password.c_str()); #if CONFIG_IDF_TARGET_ESP32C3 WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 1916fcbb..f636857a 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -55,6 +55,8 @@ typedef struct { String ssid; + uint8_t bssid[6]; + int32_t channel; String password; bool staticIPConfig; IPAddress localIP; From d1f248baf85486274a81215ae7208f890e2bead2 Mon Sep 17 00:00:00 2001 From: elims <46279009+theelims@users.noreply.github.com> Date: Mon, 6 May 2024 23:00:33 +0200 Subject: [PATCH 20/20] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05590327..e2589c5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. -## [0.5.0] - WIP +## [0.5.0] - 2024-05-06 Changes the Event Socket System to use a clearer message structure and MessagePack. Brings breaking changes to the `EventSocket.h` API.