Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDN] Set override url for CDN hosted files #4881

Merged
merged 16 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/source/Config/Config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,15 @@ N.B. the maximum possible duration depends on the used core library version and
Sleep on connection failure
---------------------------

CDN (Content delivery network)
------------------------------

The **Custom CDN URL** can be set to a server url (https preferred, they are downloaded by the browser, **not** by the ESPEasy unit) that hosts *all* files as listed on :ref:`ExternalHostedStaticFiles` (System variables can be used to customize the URL).

To reduce the build size where possible, some builds fetch the javascript (.js) and .css UI files from an external server, as this is used in the browser only. The default location for this data is the Content Delivery Network (CDN) of ``jsdelivr.net``, where these file are uploaded for specific releases.

For users that want to host these files in their local network, or if the files on jsdeliver.net have expired, an alternative location for these .js and .css file can be configured.

Setting this location will **override** the default CDN url, except for your Custom build that may already *have* the CDN url set, then this setting won't be available!

**Warning**: If an invalid location is set, the UI will be distorted, though should still be usable.
2 changes: 2 additions & 0 deletions docs/source/Reference/ExternalHostedStaticFiles.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _ExternalHostedStaticFiles:

External Hosted Static Files
****************************

Expand Down
71 changes: 62 additions & 9 deletions docs/source/Tools/Tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1043,20 +1043,73 @@ See the ``Custom-sample.h`` file for some examples.
Allow Fetch by Command
----------------------

This checkbox allows provisioning via commands.
These commands are not restricted, so they can also be given via HTTP or MQTT.
This list of checkboxes per file allows provisioning via commands.
These ``Provision*`` commands are not restricted, so they can also be given via HTTP or MQTT.

However, they can only be executed when:

* Allow Fetch by Command is enabled
* the file to download is checked
* the file at Allow Fetch by Command is checked
* the file at Files to Download is *also* checked
* URL (+ optional credentials) is stored

The commands are:

Changed: 2023-11-18: Single-word commands split into 2 words: ``Provision,<subcmd>[,<params>]``

* ``Provision,Config`` Fetch ``config.dat``
* ``Provision,Security`` Fetch ``security.dat``
* ``Provision,Notification`` Fetch ``notification.dat``
* ``Provision,Provision`` Fetch ``provisioning.dat``
* ``Provision,Rules,1`` Fetch ``rules1.txt``
* ``Provision,CustomCdnUrl`` Fetch ``customcdnurl.dat`` (When the Custom CDN Url feature is included in the build.)

* ``Provision,Firmware,<FirmwareBinary.bin>`` Fetch and install ``FirmwareBinary.bin`` on the unit

Once the Firmware download & install is finished the outcome is completed by a generated event (gets the download filename as an argument):

* ``ProvisionFirmware#Success=<FirmwareBinary.bin>`` When download and install where succesfull
* ``ProvisionFirmware#Failed=<FirmwareBinary.bin>`` When something went wrong during download or install

These events can be handled in rules, an provisioning support script could look like this:

.. code-block:: none

On updateSettings Do
provision,provision
provision,config
Endon

On updateCredentials Do
provision,security
Endon

On updateRules Do
provision,rules,1
provision,rules,2
provision,rules,3
Endon

On updateRulesSettings Do
AsyncEvent,updateSettings
AsyncEvent,updateRules
Reboot
Endon

// e.g.
// event,PerformFirmwareUpdate=firmware_max_ESP32_16M8M_LittleFS.bin
On PerformFirmwareUpdate=* Do
pwm,2,100,0,8
provision,firmware,%eventvalue1%
Endon

On provisionfirmware#success=* Do
gpio,2,0
Reboot
Endon

On provisionfirmware#failure Do
gpio,2,0
Reboot
Endon


* ``ProvisionConfig`` Fetch ``config.dat``
* ``ProvisionSecurity`` Fetch ``security.dat``
* ``ProvisionNotification`` Fetch ``notification.dat``
* ``ProvisionProvision`` Fetch ``provisioning.dat``
* ``ProvisionRules,1`` Fetch ``rules1.txt``
Binary file modified docs/source/Tools/images/SettingsArchive_download1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/source/Tools/images/SettingsArchive_provisioning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 9 additions & 6 deletions src/src/Commands/InternalCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,17 @@ bool executeInternalCommand(command_case_data & data)
COMMAND_CASE_A("posttohttp", Command_HTTP_PostToHTTP, -1); // HTTP.h
#endif // if FEATURE_POST_TO_HTTP
#if FEATURE_CUSTOM_PROVISIONING
COMMAND_CASE_A( "provisionconfig", Command_Provisioning_Config, 0); // Provisioning.h
COMMAND_CASE_A( "provisionsecurity", Command_Provisioning_Security, 0); // Provisioning.h
COMMAND_CASE_A( "provision", Command_Provisioning_Dispatcher, -1); // Provisioning.h
#ifdef PLUGIN_BUILD_MAX_ESP32 // FIXME DEPRECATED: Fallback for temporary backward compatibility
COMMAND_CASE_A( "provisionconfig", Command_Provisioning_ConfigFallback, 0); // Provisioning.h
COMMAND_CASE_A( "provisionsecurity", Command_Provisioning_SecurityFallback, 0); // Provisioning.h
tonhuisman marked this conversation as resolved.
Show resolved Hide resolved
#if FEATURE_NOTIFIER
COMMAND_CASE_A( "provisionnotification", Command_Provisioning_Notification, 0); // Provisioning.h
COMMAND_CASE_A( "provisionnotification", Command_Provisioning_NotificationFallback, 0); // Provisioning.h
#endif
COMMAND_CASE_A( "provisionprovision", Command_Provisioning_Provision, 0); // Provisioning.h
COMMAND_CASE_A( "provisionrules", Command_Provisioning_Rules, 1); // Provisioning.h
COMMAND_CASE_A( "provisionfirmware", Command_Provisioning_Firmware, 1); // Provisioning.h
COMMAND_CASE_A( "provisionprovision", Command_Provisioning_ProvisionFallback, 0); // Provisioning.h
COMMAND_CASE_A( "provisionrules", Command_Provisioning_RulesFallback, 1); // Provisioning.h
COMMAND_CASE_A( "provisionfirmware", Command_Provisioning_FirmwareFallback, 1); // Provisioning.h
#endif // ifdef PLUGIN_BUILD_MAX_ESP32
#endif
COMMAND_CASE_A( "pulse", Command_GPIO_Pulse, 3); // GPIO.h
#if FEATURE_MQTT
Expand Down
105 changes: 94 additions & 11 deletions src/src/Commands/Provisioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,129 @@
# include "../Helpers/Networking.h"
# include "../Helpers/StringConverter.h"

String Command_Provisioning_Config(struct EventStruct *event, const char *Line)
String Command_Provisioning_Dispatcher(struct EventStruct *event,
const char *Line)
{
const String cmd = parseString(Line, 2);

if (equals(cmd, F("config"))) {
return Command_Provisioning_Config();
} else
if (equals(cmd, F("firmware"))) {
return Command_Provisioning_Firmware(event, Line);
} else
# if FEATURE_NOTIFIER
if (equals(cmd, F("notification"))) {
return Command_Provisioning_Notification();
} else
# endif // if FEATURE_NOTIFIER
if (equals(cmd, F("provision"))) {
return Command_Provisioning_Provision();
} else
if (equals(cmd, F("rules"))) {
return Command_Provisioning_Rules(event);
} else
if (equals(cmd, F("security"))) {
return Command_Provisioning_Security();
}
return return_command_failed_flashstr();
}

String Command_Provisioning_Config()
{
return downloadFileType(FileType::CONFIG_DAT);
}

String Command_Provisioning_Security(struct EventStruct *event, const char *Line)
String Command_Provisioning_Security()
{
return downloadFileType(FileType::SECURITY_DAT);
}

#if FEATURE_NOTIFIER
String Command_Provisioning_Notification(struct EventStruct *event, const char *Line)
# if FEATURE_NOTIFIER
String Command_Provisioning_Notification()
{
return downloadFileType(FileType::NOTIFICATION_DAT);
}
#endif

String Command_Provisioning_Provision(struct EventStruct *event, const char *Line)
# endif // if FEATURE_NOTIFIER

String Command_Provisioning_Provision()
{
return downloadFileType(FileType::PROVISIONING_DAT);
}

String Command_Provisioning_Rules(struct EventStruct *event, const char *Line)
String Command_Provisioning_Rules(struct EventStruct *event)
{
if ((event->Par2 <= 0) || (event->Par2 > 4)) {
return F("Provision,Rules: rules index out of range");
}
return downloadFileType(FileType::RULES_TXT, event->Par2 - 1);
}

String Command_Provisioning_Firmware(struct EventStruct *event, const char *Line)
{
// FIXME TD-er: Must only allow to use set prefix in the provisioning settings
const String url = parseStringToEndKeepCase(Line, 3);
String error;

downloadFirmware(url, error); // Events are sent from download handler
return error;
}

# ifdef PLUGIN_BUILD_MAX_ESP32
void Command_Provisioning_DeprecatedMessage(const String& param) {
addLog(LOG_LEVEL_ERROR, strformat(F("WARNING: 'Provision%s' is deprecated, change to 'Provision,%s'"), param.c_str(), param.c_str()));
}

String Command_Provisioning_ConfigFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Config"));
return Command_Provisioning_Config();
}

String Command_Provisioning_SecurityFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Security"));
return Command_Provisioning_Security();
}

# if FEATURE_NOTIFIER
String Command_Provisioning_NotificationFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Notification"));
return Command_Provisioning_Notification();
}

String Command_Provisioning_ProvisionFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Provision"));
return Command_Provisioning_Provision();
}

String Command_Provisioning_RulesFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Rules,<n>"));

if ((event->Par1 <= 0) || (event->Par1 > 4)) {
return F("ProvisionRules: rules index out of range");
}
return downloadFileType(FileType::RULES_TXT, event->Par1 - 1);
}

String Command_Provisioning_Firmware(struct EventStruct *event, const char *Line)
String Command_Provisioning_FirmwareFallback(struct EventStruct *event, const char *Line)
{
Command_Provisioning_DeprecatedMessage(F("Firmware,<Firmware.bin>"));

// FIXME TD-er: Must only allow to use set prefix in the provisioning settings
const String url = parseStringToEndKeepCase(Line, 2);
String error;
if (downloadFirmware(url, error)) {
// TODO TD-er: send events
}

downloadFirmware(url, error);
return error;
}

# endif // if FEATURE_NOTIFIER

# endif // ifdef PLUGIN_BUILD_MAX_ESP32

#endif // if FEATURE_CUSTOM_PROVISIONING
41 changes: 29 additions & 12 deletions src/src/Commands/Provisioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,39 @@

class String;

String Command_Provisioning_Config(struct EventStruct *event,
const char *Line);
String Command_Provisioning_Security(struct EventStruct *event,
const char *Line);
#if FEATURE_NOTIFIER
String Command_Provisioning_Notification(struct EventStruct *event,
const char *Line);
#endif
String Command_Provisioning_Provision(struct EventStruct *event,
const char *Line);
String Command_Provisioning_Rules(struct EventStruct *event,
const char *Line);
String Command_Provisioning_Dispatcher(struct EventStruct *event,
const char *Line);
String Command_Provisioning_Config();
String Command_Provisioning_Security();
# if FEATURE_NOTIFIER
String Command_Provisioning_Notification();
# endif // if FEATURE_NOTIFIER
String Command_Provisioning_Provision();

String Command_Provisioning_Rules(struct EventStruct *event);

String Command_Provisioning_Firmware(struct EventStruct *event,
const char *Line);

// FIXME DEPRECATED!
# ifdef PLUGIN_BUILD_MAX_ESP32
String Command_Provisioning_ConfigFallback(struct EventStruct *event,
const char *Line);
String Command_Provisioning_SecurityFallback(struct EventStruct *event,
const char *Line);
# if FEATURE_NOTIFIER
String Command_Provisioning_NotificationFallback(struct EventStruct *event,
const char *Line);
# endif // if FEATURE_NOTIFIER
String Command_Provisioning_ProvisionFallback(struct EventStruct *event,
const char *Line);
String Command_Provisioning_RulesFallback(struct EventStruct *event,
const char *Line);

String Command_Provisioning_FirmwareFallback(struct EventStruct *event,
const char *Line);
# endif // ifdef PLUGIN_BUILD_MAX_ESP32

#endif // if FEATURE_CUSTOM_PROVISIONING

#endif // ifndef COMMANDS_PROVISIONING_H
4 changes: 2 additions & 2 deletions src/src/CustomBuild/CompiletimeDefines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ const __FlashStringHelper * get_CDN_url_prefix() {
#else
// Some fallback tag
// FIXME TD-er: Not sure which is better, serving the latest (which will have caching issues) or a tag which will become outdated
return F("https://cdn.jsdelivr.net/gh/letscontrolit/ESPEasy@mega-20220809/static/");
//return F("https://cdn.jsdelivr.net/gh/letscontrolit/ESPEasy/static/");
return F("https://cdn.jsdelivr.net/gh/letscontrolit/ESPEasy@mega/static/");
//return F("https://cdn.jsdelivr.net/gh/letscontrolit/ESPEasy@mega-20231013/static/");
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions src/src/CustomBuild/ESPEasyDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@
#ifndef DEFAULT_SYSLOG_FACILITY
#define DEFAULT_SYSLOG_FACILITY 0 // kern
#endif
#ifndef DEFAULT_SYSLOG_PORT
#define DEFAULT_SYSLOG_PORT 0
TD-er marked this conversation as resolved.
Show resolved Hide resolved
#endif

#ifndef DEFAULT_SYNC_UDP_PORT
#define DEFAULT_SYNC_UDP_PORT 8266 // Used for ESPEasy p2p. (IANA registered port: 8266)
Expand Down
Loading