@ -21,13 +21,18 @@
# include "Web.h"
# include "Web.h"
# include "Wlan.h"
# include "Wlan.h"
# include "HTMLaccesspoint_DE.h"
# include "HTMLaccesspoint_EN .h"
# include "HTMLmanagement_DE.h"
# include "HTMLmanagement_EN.h"
# if (LANGUAGE == 1)
# include "HTMLaccesspoint_D E.h"
# include "HTMLmanagement_DE.h"
# endif
typedef struct
{
# if (LANGUAGE == 2)
# include "HTMLaccesspoint_EN.h"
# include "HTMLmanagement_EN.h"
# endif
typedef struct {
char nvsKey [ 13 ] ;
char nvsKey [ 13 ] ;
char nvsEntry [ 275 ] ;
char nvsEntry [ 275 ] ;
} nvs_t ;
} nvs_t ;
@ -60,15 +65,13 @@ static void onWebsocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *clien
static String templateProcessor ( const String & templ ) ;
static String templateProcessor ( const String & templ ) ;
static void webserverStart ( void ) ;
static void webserverStart ( void ) ;
void Web_Init ( void )
{
void Web_Init ( void ) {
wServer . on ( " / " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
wServer . on ( " / " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
request - > send_P ( 200 , " text/html " , accesspoint_HTML ) ;
request - > send_P ( 200 , " text/html " , accesspoint_HTML ) ;
} ) ;
} ) ;
wServer . on ( " /init " , HTTP_POST , [ ] ( AsyncWebServerRequest * request ) {
wServer . on ( " /init " , HTTP_POST , [ ] ( AsyncWebServerRequest * request ) {
if ( request - > hasParam ( " ssid " , true ) & & request - > hasParam ( " pwd " , true ) & & request - > hasParam ( " hostname " , true ) )
{
if ( request - > hasParam ( " ssid " , true ) & & request - > hasParam ( " pwd " , true ) & & request - > hasParam ( " hostname " , true ) ) {
Serial . println ( request - > getParam ( " ssid " , true ) - > value ( ) ) ;
Serial . println ( request - > getParam ( " ssid " , true ) - > value ( ) ) ;
Serial . println ( request - > getParam ( " pwd " , true ) - > value ( ) ) ;
Serial . println ( request - > getParam ( " pwd " , true ) - > value ( ) ) ;
Serial . println ( request - > getParam ( " hostname " , true ) - > value ( ) ) ;
Serial . println ( request - > getParam ( " hostname " , true ) - > value ( ) ) ;
@ -80,45 +83,41 @@ void Web_Init(void)
} ) ;
} ) ;
wServer . on ( " /restart " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
wServer . on ( " /restart " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
# if (LANGUAGE == 1)
# if (LANGUAGE == 1)
request - > send ( 200 , " text/html " , " ESPuino wird neu gestartet... " ) ;
request - > send ( 200 , " text/html " , " ESPuino wird neu gestartet... " ) ;
# else
# else
request - > send ( 200 , " text/html " , " ESPuino is being restarted... " ) ;
request - > send ( 200 , " text/html " , " ESPuino is being restarted... " ) ;
# endif
# endif
Serial . flush ( ) ;
Serial . flush ( ) ;
ESP . restart ( ) ;
ESP . restart ( ) ;
} ) ;
} ) ;
wServer . on ( " /shutdown " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
wServer . on ( " /shutdown " , HTTP_GET , [ ] ( AsyncWebServerRequest * request ) {
# if (LANGUAGE == 1)
# if (LANGUAGE == 1)
request - > send ( 200 , " text/html " , " ESPuino wird ausgeschaltet... " ) ;
request - > send ( 200 , " text/html " , " ESPuino wird ausgeschaltet... " ) ;
# else
# else
request - > send ( 200 , " text/html " , " ESPuino is being shutdown... " ) ;
request - > send ( 200 , " text/html " , " ESPuino is being shutdown... " ) ;
# endif
# endif
System_RequestSleep ( ) ;
System_RequestSleep ( ) ;
} ) ;
} ) ;
// allow cors for local debug
// allow cors for local debug
DefaultHeaders : : Instance ( ) . addHeader ( " Access-Control-Allow-Origin " , " * " ) ;
DefaultHeaders : : Instance ( ) . addHeader ( " Access-Control-Allow-Origin " , " * " ) ;
wServer . begin ( ) ;
wServer . begin ( ) ;
Log_Println ( ( char * ) FPSTR ( httpReady ) , LOGLEVEL_NOTICE ) ;
Log_Println ( ( char * ) FPSTR ( httpReady ) , LOGLEVEL_NOTICE ) ;
}
}
void Web_Cyclic ( void )
{
void Web_Cyclic ( void ) {
webserverStart ( ) ;
webserverStart ( ) ;
ws . cleanupClients ( ) ;
ws . cleanupClients ( ) ;
}
}
void notFound ( AsyncWebServerRequest * request )
{
void notFound ( AsyncWebServerRequest * request ) {
request - > send ( 404 , " text/plain " , " Not found " ) ;
request - > send ( 404 , " text/plain " , " Not found " ) ;
}
}
void webserverStart ( void )
{
if ( Wlan_IsConnected ( ) & & ! webserverStarted )
{
void webserverStart ( void ) {
if ( Wlan_IsConnected ( ) & & ! webserverStarted ) {
// attach AsyncWebSocket for Mgmt-Interface
// attach AsyncWebSocket for Mgmt-Interface
ws . onEvent ( onWebsocketEvent ) ;
ws . onEvent ( onWebsocketEvent ) ;
wServer . addHandler ( & ws ) ;
wServer . addHandler ( & ws ) ;
@ -185,133 +184,75 @@ void webserverStart(void)
// Used for substitution of some variables/templates of html-files. Is called by webserver's template-engine
// Used for substitution of some variables/templates of html-files. Is called by webserver's template-engine
String templateProcessor ( const String & templ )
String templateProcessor ( const String & templ )
{
{
if ( templ = = " FTP_USER " )
{
if ( templ = = " FTP_USER " ) {
return gPrefsSettings . getString ( " ftpuser " , " -1 " ) ;
return gPrefsSettings . getString ( " ftpuser " , " -1 " ) ;
}
else if ( templ = = " FTP_PWD " )
{
} else if ( templ = = " FTP_PWD " ) {
return gPrefsSettings . getString ( " ftppassword " , " -1 " ) ;
return gPrefsSettings . getString ( " ftppassword " , " -1 " ) ;
}
else if ( templ = = " FTP_USER_LENGTH " )
{
} else if ( templ = = " FTP_USER_LENGTH " ) {
return String ( ftpUserLength - 1 ) ;
return String ( ftpUserLength - 1 ) ;
}
else if ( templ = = " FTP_PWD_LENGTH " )
{
} else if ( templ = = " FTP_PWD_LENGTH " ) {
return String ( ftpPasswordLength - 1 ) ;
return String ( ftpPasswordLength - 1 ) ;
}
else if ( templ = = " SHOW_FTP_TAB " )
{ // Only show FTP-tab if FTP-support was compiled
# ifdef FTP_ENABLE
return ( String ) FPSTR ( ftpTab ) ;
# else
} else if ( templ = = " SHOW_FTP_TAB " ) { // Only show FTP-tab if FTP-support was compiled
# ifdef FTP_ENABLE
return ( String ) FPSTR ( ftpTab ) ;
# else
return String ( ) ;
return String ( ) ;
# endif
}
else if ( templ = = " INIT_LED_BRIGHTNESS " )
{
# endif
} else if ( templ = = " INIT_LED_BRIGHTNESS " ) {
return String ( gPrefsSettings . getUChar ( " iLedBrightness " , 0 ) ) ;
return String ( gPrefsSettings . getUChar ( " iLedBrightness " , 0 ) ) ;
}
else if ( templ = = " NIGHT_LED_BRIGHTNESS " )
{
} else if ( templ = = " NIGHT_LED_BRIGHTNESS " ) {
return String ( gPrefsSettings . getUChar ( " nLedBrightness " , 0 ) ) ;
return String ( gPrefsSettings . getUChar ( " nLedBrightness " , 0 ) ) ;
}
else if ( templ = = " MAX_INACTIVITY " )
{
} else if ( templ = = " MAX_INACTIVITY " ) {
return String ( gPrefsSettings . getUInt ( " mInactiviyT " , 0 ) ) ;
return String ( gPrefsSettings . getUInt ( " mInactiviyT " , 0 ) ) ;
}
else if ( templ = = " INIT_VOLUME " )
{
} else if ( templ = = " INIT_VOLUME " ) {
return String ( gPrefsSettings . getUInt ( " initVolume " , 0 ) ) ;
return String ( gPrefsSettings . getUInt ( " initVolume " , 0 ) ) ;
}
else if ( templ = = " CURRENT_VOLUME " )
{
} else if ( templ = = " CURRENT_VOLUME " ) {
return String ( AudioPlayer_GetCurrentVolume ( ) ) ;
return String ( AudioPlayer_GetCurrentVolume ( ) ) ;
}
else if ( templ = = " MAX_VOLUME_SPEAKER " )
{
} else if ( templ = = " MAX_VOLUME_SPEAKER " ) {
return String ( gPrefsSettings . getUInt ( " maxVolumeSp " , 0 ) ) ;
return String ( gPrefsSettings . getUInt ( " maxVolumeSp " , 0 ) ) ;
}
else if ( templ = = " MAX_VOLUME_HEADPHONE " )
{
} else if ( templ = = " MAX_VOLUME_HEADPHONE " ) {
return String ( gPrefsSettings . getUInt ( " maxVolumeHp " , 0 ) ) ;
return String ( gPrefsSettings . getUInt ( " maxVolumeHp " , 0 ) ) ;
}
else if ( templ = = " WARNING_LOW_VOLTAGE " )
{
} else if ( templ = = " WARNING_LOW_VOLTAGE " ) {
return String ( gPrefsSettings . getFloat ( " wLowVoltage " , warningLowVoltage ) ) ;
return String ( gPrefsSettings . getFloat ( " wLowVoltage " , warningLowVoltage ) ) ;
}
else if ( templ = = " VOLTAGE_INDICATOR_LOW " )
{
} else if ( templ = = " VOLTAGE_INDICATOR_LOW " ) {
return String ( gPrefsSettings . getFloat ( " vIndicatorLow " , voltageIndicatorLow ) ) ;
return String ( gPrefsSettings . getFloat ( " vIndicatorLow " , voltageIndicatorLow ) ) ;
}
else if ( templ = = " VOLTAGE_INDICATOR_HIGH " )
{
} else if ( templ = = " VOLTAGE_INDICATOR_HIGH " ) {
return String ( gPrefsSettings . getFloat ( " vIndicatorHigh " , voltageIndicatorHigh ) ) ;
return String ( gPrefsSettings . getFloat ( " vIndicatorHigh " , voltageIndicatorHigh ) ) ;
}
else if ( templ = = " VOLTAGE_CHECK_INTERVAL " )
{
} else if ( templ = = " VOLTAGE_CHECK_INTERVAL " ) {
return String ( gPrefsSettings . getUInt ( " vCheckIntv " , voltageCheckInterval ) ) ;
return String ( gPrefsSettings . getUInt ( " vCheckIntv " , voltageCheckInterval ) ) ;
}
else if ( templ = = " MQTT_SERVER " )
{
} else if ( templ = = " MQTT_SERVER " ) {
return gPrefsSettings . getString ( " mqttServer " , " -1 " ) ;
return gPrefsSettings . getString ( " mqttServer " , " -1 " ) ;
}
else if ( templ = = " SHOW_MQTT_TAB " )
{ // Only show MQTT-tab if MQTT-support was compiled
# ifdef MQTT_ENABLE
return ( String ) FPSTR ( mqttTab ) ;
# else
} else if ( templ = = " SHOW_MQTT_TAB " ) { // Only show MQTT-tab if MQTT-support was compiled
# ifdef MQTT_ENABLE
return ( String ) FPSTR ( mqttTab ) ;
# else
return String ( ) ;
return String ( ) ;
# endif
}
else if ( templ = = " MQTT_ENABLE " )
{
if ( Mqtt_IsEnabled ( ) )
{
# endif
} else if ( templ = = " MQTT_ENABLE " ) {
if ( Mqtt_IsEnabled ( ) ) {
return String ( " checked= \" checked \" " ) ;
return String ( " checked= \" checked \" " ) ;
}
else
{
} else {
return String ( ) ;
return String ( ) ;
}
}
}
else if ( templ = = " MQTT_USER " )
{
} else if ( templ = = " MQTT_USER " ) {
return gPrefsSettings . getString ( " mqttUser " , " -1 " ) ;
return gPrefsSettings . getString ( " mqttUser " , " -1 " ) ;
}
else if ( templ = = " MQTT_PWD " )
{
} else if ( templ = = " MQTT_PWD " ) {
return gPrefsSettings . getString ( " mqttPassword " , " -1 " ) ;
return gPrefsSettings . getString ( " mqttPassword " , " -1 " ) ;
}
else if ( templ = = " MQTT_USER_LENGTH " )
{
} else if ( templ = = " MQTT_USER_LENGTH " ) {
return String ( mqttUserLength - 1 ) ;
return String ( mqttUserLength - 1 ) ;
}
else if ( templ = = " MQTT_PWD_LENGTH " )
{
} else if ( templ = = " MQTT_PWD_LENGTH " ) {
return String ( mqttPasswordLength - 1 ) ;
return String ( mqttPasswordLength - 1 ) ;
}
else if ( templ = = " MQTT_SERVER_LENGTH " )
{
} else if ( templ = = " MQTT_SERVER_LENGTH " ) {
return String ( mqttServerLength - 1 ) ;
return String ( mqttServerLength - 1 ) ;
}
else if ( templ = = " MQTT_PORT " )
{
} else if ( templ = = " MQTT_PORT " ) {
return String ( gMqttPort ) ;
return String ( gMqttPort ) ;
}
else if ( templ = = " IPv4 " )
{
} else if ( templ = = " IPv4 " ) {
IPAddress myIP = WiFi . localIP ( ) ;
IPAddress myIP = WiFi . localIP ( ) ;
snprintf ( Log_Buffer , Log_BufferLength , " %d.%d.%d.%d " , myIP [ 0 ] , myIP [ 1 ] , myIP [ 2 ] , myIP [ 3 ] ) ;
snprintf ( Log_Buffer , Log_BufferLength , " %d.%d.%d.%d " , myIP [ 0 ] , myIP [ 1 ] , myIP [ 2 ] , myIP [ 3 ] ) ;
return String ( Log_Buffer ) ;
return String ( Log_Buffer ) ;
}
else if ( templ = = " RFID_TAG_ID " )
{
} else if ( templ = = " RFID_TAG_ID " ) {
return String ( gCurrentRfidTagId ) ;
return String ( gCurrentRfidTagId ) ;
}
else if ( templ = = " HOSTNAME " )
{
} else if ( templ = = " HOSTNAME " ) {
return gPrefsSettings . getString ( " Hostname " , " -1 " ) ;
return gPrefsSettings . getString ( " Hostname " , " -1 " ) ;
}
}
@ -320,25 +261,22 @@ String templateProcessor(const String &templ)
// Takes inputs from webgui, parses JSON and saves values in NVS
// Takes inputs from webgui, parses JSON and saves values in NVS
// If operation was successful (NVS-write is verified) true is returned
// If operation was successful (NVS-write is verified) true is returned
bool processJsonRequest ( char * _serialJson )
{
bool processJsonRequest ( char * _serialJson ) {
StaticJsonDocument < 1000 > doc ;
StaticJsonDocument < 1000 > doc ;
DeserializationError error = deserializeJson ( doc , _serialJson ) ;
DeserializationError error = deserializeJson ( doc , _serialJson ) ;
JsonObject object = doc . as < JsonObject > ( ) ;
JsonObject object = doc . as < JsonObject > ( ) ;
if ( error )
{
# if (LANGUAGE == 1)
if ( error ) {
# if (LANGUAGE == 1)
Serial . print ( F ( " deserializeJson() fehlgeschlagen: " ) ) ;
Serial . print ( F ( " deserializeJson() fehlgeschlagen: " ) ) ;
# else
# else
Serial . print ( F ( " deserializeJson() failed: " ) ) ;
Serial . print ( F ( " deserializeJson() failed: " ) ) ;
# endif
# endif
Serial . println ( error . c_str ( ) ) ;
Serial . println ( error . c_str ( ) ) ;
return false ;
return false ;
}
}
if ( doc . containsKey ( " general " ) )
{
if ( doc . containsKey ( " general " ) ) {
uint8_t iVol = doc [ " general " ] [ " iVol " ] . as < uint8_t > ( ) ;
uint8_t iVol = doc [ " general " ] [ " iVol " ] . as < uint8_t > ( ) ;
uint8_t mVolSpeaker = doc [ " general " ] [ " mVolSpeaker " ] . as < uint8_t > ( ) ;
uint8_t mVolSpeaker = doc [ " general " ] [ " mVolSpeaker " ] . as < uint8_t > ( ) ;
uint8_t mVolHeadphone = doc [ " general " ] [ " mVolHeadphone " ] . as < uint8_t > ( ) ;
uint8_t mVolHeadphone = doc [ " general " ] [ " mVolHeadphone " ] . as < uint8_t > ( ) ;
@ -371,13 +309,10 @@ bool processJsonRequest(char *_serialJson)
gPrefsSettings . getFloat ( " wLowVoltage " , 999.99 ) ! = vWarning | |
gPrefsSettings . getFloat ( " wLowVoltage " , 999.99 ) ! = vWarning | |
gPrefsSettings . getFloat ( " vIndicatorLow " , 999.99 ) ! = vIndLow | |
gPrefsSettings . getFloat ( " vIndicatorLow " , 999.99 ) ! = vIndLow | |
gPrefsSettings . getFloat ( " vIndicatorHigh " , 999.99 ) ! = vIndHi | |
gPrefsSettings . getFloat ( " vIndicatorHigh " , 999.99 ) ! = vIndHi | |
gPrefsSettings . getUInt ( " vCheckIntv " , 17777 ) ! = vInt )
{
gPrefsSettings . getUInt ( " vCheckIntv " , 17777 ) ! = vInt ) {
return false ;
return false ;
}
}
}
else if ( doc . containsKey ( " ftp " ) )
{
} else if ( doc . containsKey ( " ftp " ) ) {
const char * _ftpUser = doc [ " ftp " ] [ " ftpUser " ] ;
const char * _ftpUser = doc [ " ftp " ] [ " ftpUser " ] ;
const char * _ftpPwd = doc [ " ftp " ] [ " ftpPwd " ] ;
const char * _ftpPwd = doc [ " ftp " ] [ " ftpPwd " ] ;
@ -385,13 +320,10 @@ bool processJsonRequest(char *_serialJson)
gPrefsSettings . putString ( " ftppassword " , ( String ) _ftpPwd ) ;
gPrefsSettings . putString ( " ftppassword " , ( String ) _ftpPwd ) ;
if ( ! ( String ( _ftpUser ) . equals ( gPrefsSettings . getString ( " ftpuser " , " -1 " ) ) | |
if ( ! ( String ( _ftpUser ) . equals ( gPrefsSettings . getString ( " ftpuser " , " -1 " ) ) | |
String ( _ftpPwd ) . equals ( gPrefsSettings . getString ( " ftppassword " , " -1 " ) ) ) )
{
String ( _ftpPwd ) . equals ( gPrefsSettings . getString ( " ftppassword " , " -1 " ) ) ) ) {
return false ;
return false ;
}
}
}
else if ( doc . containsKey ( " mqtt " ) )
{
} else if ( doc . containsKey ( " mqtt " ) ) {
uint8_t _mqttEnable = doc [ " mqtt " ] [ " mqttEnable " ] . as < uint8_t > ( ) ;
uint8_t _mqttEnable = doc [ " mqtt " ] [ " mqttEnable " ] . as < uint8_t > ( ) ;
const char * _mqttServer = object [ " mqtt " ] [ " mqttServer " ] ;
const char * _mqttServer = object [ " mqtt " ] [ " mqttServer " ] ;
gPrefsSettings . putUChar ( " enableMQTT " , _mqttEnable ) ;
gPrefsSettings . putUChar ( " enableMQTT " , _mqttEnable ) ;
@ -408,35 +340,26 @@ bool processJsonRequest(char *_serialJson)
gPrefsSettings . putUInt ( " mqttPort " , _mqttPort ) ;
gPrefsSettings . putUInt ( " mqttPort " , _mqttPort ) ;
if ( ( gPrefsSettings . getUChar ( " enableMQTT " , 99 ) ! = _mqttEnable ) | |
if ( ( gPrefsSettings . getUChar ( " enableMQTT " , 99 ) ! = _mqttEnable ) | |
( ! String ( _mqttServer ) . equals ( gPrefsSettings . getString ( " mqttServer " , " -1 " ) ) ) )
{
( ! String ( _mqttServer ) . equals ( gPrefsSettings . getString ( " mqttServer " , " -1 " ) ) ) ) {
return false ;
return false ;
}
}
}
else if ( doc . containsKey ( " rfidMod " ) )
{
} else if ( doc . containsKey ( " rfidMod " ) ) {
const char * _rfidIdModId = object [ " rfidMod " ] [ " rfidIdMod " ] ;
const char * _rfidIdModId = object [ " rfidMod " ] [ " rfidIdMod " ] ;
uint8_t _modId = object [ " rfidMod " ] [ " modId " ] ;
uint8_t _modId = object [ " rfidMod " ] [ " modId " ] ;
char rfidString [ 12 ] ;
char rfidString [ 12 ] ;
if ( _modId < = 0 )
{
if ( _modId < = 0 ) {
gPrefsRfid . remove ( _rfidIdModId ) ;
gPrefsRfid . remove ( _rfidIdModId ) ;
}
else
{
} else {
snprintf ( rfidString , sizeof ( rfidString ) / sizeof ( rfidString [ 0 ] ) , " %s0%s0%s%u%s0 " , stringDelimiter , stringDelimiter , stringDelimiter , _modId , stringDelimiter ) ;
snprintf ( rfidString , sizeof ( rfidString ) / sizeof ( rfidString [ 0 ] ) , " %s0%s0%s%u%s0 " , stringDelimiter , stringDelimiter , stringDelimiter , _modId , stringDelimiter ) ;
gPrefsRfid . putString ( _rfidIdModId , rfidString ) ;
gPrefsRfid . putString ( _rfidIdModId , rfidString ) ;
String s = gPrefsRfid . getString ( _rfidIdModId , " -1 " ) ;
String s = gPrefsRfid . getString ( _rfidIdModId , " -1 " ) ;
if ( s . compareTo ( rfidString ) )
{
if ( s . compareTo ( rfidString ) ) {
return false ;
return false ;
}
}
}
}
Web_DumpNvsToSd ( " rfidTags " , ( const char * ) FPSTR ( backupFile ) ) ; // Store backup-file every time when a new rfid-tag is programmed
}
else if ( doc . containsKey ( " rfidAssign " ) )
{
Web_DumpNvsToSd ( " rfidTags " , ( const char * ) FPSTR ( backupFile ) ) ; // Store backup-file every time when a new rfid-tag is programmed
} else if ( doc . containsKey ( " rfidAssign " ) ) {
const char * _rfidIdAssinId = object [ " rfidAssign " ] [ " rfidIdMusic " ] ;
const char * _rfidIdAssinId = object [ " rfidAssign " ] [ " rfidIdMusic " ] ;
char _fileOrUrlAscii [ MAX_FILEPATH_LENTGH ] ;
char _fileOrUrlAscii [ MAX_FILEPATH_LENTGH ] ;
convertUtf8ToAscii ( object [ " rfidAssign " ] [ " fileOrUrl " ] , _fileOrUrlAscii ) ;
convertUtf8ToAscii ( object [ " rfidAssign " ] [ " fileOrUrl " ] , _fileOrUrlAscii ) ;
@ -448,14 +371,11 @@ bool processJsonRequest(char *_serialJson)
Serial . println ( rfidString ) ;
Serial . println ( rfidString ) ;
String s = gPrefsRfid . getString ( _rfidIdAssinId , " -1 " ) ;
String s = gPrefsRfid . getString ( _rfidIdAssinId , " -1 " ) ;
if ( s . compareTo ( rfidString ) )
{
if ( s . compareTo ( rfidString ) ) {
return false ;
return false ;
}
}
Web_DumpNvsToSd ( " rfidTags " , ( const char * ) FPSTR ( backupFile ) ) ; // Store backup-file every time when a new rfid-tag is programmed
}
else if ( doc . containsKey ( " wifiConfig " ) )
{
Web_DumpNvsToSd ( " rfidTags " , ( const char * ) FPSTR ( backupFile ) ) ; // Store backup-file every time when a new rfid-tag is programmed
} else if ( doc . containsKey ( " wifiConfig " ) ) {
const char * _ssid = object [ " wifiConfig " ] [ " ssid " ] ;
const char * _ssid = object [ " wifiConfig " ] [ " ssid " ] ;
const char * _pwd = object [ " wifiConfig " ] [ " pwd " ] ;
const char * _pwd = object [ " wifiConfig " ] [ " pwd " ] ;
const char * _hostname = object [ " wifiConfig " ] [ " hostname " ] ;
const char * _hostname = object [ " wifiConfig " ] [ " hostname " ] ;
@ -468,25 +388,18 @@ bool processJsonRequest(char *_serialJson)
String sPwd = gPrefsSettings . getString ( " Password " , " -1 " ) ;
String sPwd = gPrefsSettings . getString ( " Password " , " -1 " ) ;
String sHostname = gPrefsSettings . getString ( " Hostname " , " -1 " ) ;
String sHostname = gPrefsSettings . getString ( " Hostname " , " -1 " ) ;
if ( sSsid . compareTo ( _ssid ) | | sPwd . compareTo ( _pwd ) )
{
if ( sSsid . compareTo ( _ssid ) | | sPwd . compareTo ( _pwd ) ) {
return false ;
return false ;
}
}
}
}
else if ( doc . containsKey ( " ping " ) )
{
else if ( doc . containsKey ( " ping " ) ) {
Web_SendWebsocketData ( 0 , 20 ) ;
Web_SendWebsocketData ( 0 , 20 ) ;
return false ;
return false ;
}
else if ( doc . containsKey ( " controls " ) )
{
if ( object [ " controls " ] . containsKey ( " set_volume " ) )
{
} else if ( doc . containsKey ( " controls " ) ) {
if ( object [ " controls " ] . containsKey ( " set_volume " ) ) {
uint8_t new_vol = doc [ " controls " ] [ " set_volume " ] . as < uint8_t > ( ) ;
uint8_t new_vol = doc [ " controls " ] [ " set_volume " ] . as < uint8_t > ( ) ;
AudioPlayer_VolumeToQueueSender ( new_vol , true ) ;
AudioPlayer_VolumeToQueueSender ( new_vol , true ) ;
}
if ( object [ " controls " ] . containsKey ( " action " ) )
{
} if ( object [ " controls " ] . containsKey ( " action " ) ) {
uint8_t cmd = doc [ " controls " ] [ " action " ] . as < uint8_t > ( ) ;
uint8_t cmd = doc [ " controls " ] [ " action " ] . as < uint8_t > ( ) ;
Cmd_Action ( cmd ) ;
Cmd_Action ( cmd ) ;
}
}
@ -496,8 +409,7 @@ bool processJsonRequest(char *_serialJson)
}
}
// Sends JSON-answers via websocket
// Sends JSON-answers via websocket
void Web_SendWebsocketData ( uint32_t client , uint8_t code )
{
void Web_SendWebsocketData ( uint32_t client , uint8_t code ) {
char * jBuf ;
char * jBuf ;
jBuf = ( char * ) x_calloc ( 255 , sizeof ( char ) ) ;
jBuf = ( char * ) x_calloc ( 255 , sizeof ( char ) ) ;
@ -505,84 +417,58 @@ void Web_SendWebsocketData(uint32_t client, uint8_t code)
StaticJsonDocument < CAPACITY > doc ;
StaticJsonDocument < CAPACITY > doc ;
JsonObject object = doc . to < JsonObject > ( ) ;
JsonObject object = doc . to < JsonObject > ( ) ;
if ( code = = 1 )
{
if ( code = = 1 ) {
object [ " status " ] = " ok " ;
object [ " status " ] = " ok " ;
}
else if ( code = = 2 )
{
} else if ( code = = 2 ) {
object [ " status " ] = " error " ;
object [ " status " ] = " error " ;
}
else if ( code = = 10 )
{
} else if ( code = = 10 ) {
object [ " rfidId " ] = gCurrentRfidTagId ;
object [ " rfidId " ] = gCurrentRfidTagId ;
}
else if ( code = = 20 )
{
} else if ( code = = 20 ) {
object [ " pong " ] = " pong " ;
object [ " pong " ] = " pong " ;
}
}
serializeJson ( doc , jBuf , 255 ) ;
serializeJson ( doc , jBuf , 255 ) ;
if ( client = = 0 )
{
if ( client = = 0 ) {
ws . printfAll ( jBuf ) ;
ws . printfAll ( jBuf ) ;
}
else
{
} else {
ws . printf ( client , jBuf ) ;
ws . printf ( client , jBuf ) ;
}
}
free ( jBuf ) ;
free ( jBuf ) ;
}
}
// Processes websocket-requests
// Processes websocket-requests
void onWebsocketEvent ( AsyncWebSocket * server , AsyncWebSocketClient * client , AwsEventType type , void * arg , uint8_t * data , size_t len )
{
if ( type = = WS_EVT_CONNECT )
{
void onWebsocketEvent ( AsyncWebSocket * server , AsyncWebSocketClient * client , AwsEventType type , void * arg , uint8_t * data , size_t len ) {
if ( type = = WS_EVT_CONNECT ) {
//client connected
//client connected
Serial . printf ( " ws[%s][%u] connect \n " , server - > url ( ) , client - > id ( ) ) ;
Serial . printf ( " ws[%s][%u] connect \n " , server - > url ( ) , client - > id ( ) ) ;
//client->printf("Hello Client %u :)", client->id());
//client->printf("Hello Client %u :)", client->id());
client - > ping ( ) ;
client - > ping ( ) ;
}
else if ( type = = WS_EVT_DISCONNECT )
{
} else if ( type = = WS_EVT_DISCONNECT ) {
//client disconnected
//client disconnected
Serial . printf ( " ws[%s][%u] disconnect \n " , server - > url ( ) , client - > id ( ) ) ;
Serial . printf ( " ws[%s][%u] disconnect \n " , server - > url ( ) , client - > id ( ) ) ;
}
else if ( type = = WS_EVT_ERROR )
{
} else if ( type = = WS_EVT_ERROR ) {
//error was received from the other end
//error was received from the other end
Serial . printf ( " ws[%s][%u] error(%u): %s \n " , server - > url ( ) , client - > id ( ) , * ( ( uint16_t * ) arg ) , ( char * ) data ) ;
Serial . printf ( " ws[%s][%u] error(%u): %s \n " , server - > url ( ) , client - > id ( ) , * ( ( uint16_t * ) arg ) , ( char * ) data ) ;
}
else if ( type = = WS_EVT_PONG )
{
} else if ( type = = WS_EVT_PONG ) {
//pong message was received (in response to a ping request maybe)
//pong message was received (in response to a ping request maybe)
Serial . printf ( " ws[%s][%u] pong[%u]: %s \n " , server - > url ( ) , client - > id ( ) , len , ( len ) ? ( char * ) data : " " ) ;
Serial . printf ( " ws[%s][%u] pong[%u]: %s \n " , server - > url ( ) , client - > id ( ) , len , ( len ) ? ( char * ) data : " " ) ;
}
else if ( type = = WS_EVT_DATA )
{
} else if ( type = = WS_EVT_DATA ) {
//data packet
//data packet
AwsFrameInfo * info = ( AwsFrameInfo * ) arg ;
AwsFrameInfo * info = ( AwsFrameInfo * ) arg ;
if ( info - > final & & info - > index = = 0 & & info - > len = = len )
{
if ( info - > final & & info - > index = = 0 & & info - > len = = len ) {
//the whole message is in a single frame and we got all of it's data
//the whole message is in a single frame and we got all of it's data
Serial . printf ( " ws[%s][%u] %s-message[%llu]: " , server - > url ( ) , client - > id ( ) , ( info - > opcode = = WS_TEXT ) ? " text " : " binary " , info - > len ) ;
Serial . printf ( " ws[%s][%u] %s-message[%llu]: " , server - > url ( ) , client - > id ( ) , ( info - > opcode = = WS_TEXT ) ? " text " : " binary " , info - > len ) ;
if ( processJsonRequest ( ( char * ) data ) )
{
if ( processJsonRequest ( ( char * ) data ) ) {
Web_SendWebsocketData ( client - > id ( ) , 1 ) ;
Web_SendWebsocketData ( client - > id ( ) , 1 ) ;
}
}
if ( info - > opcode = = WS_TEXT )
{
if ( info - > opcode = = WS_TEXT ) {
data [ len ] = 0 ;
data [ len ] = 0 ;
Serial . printf ( " %s \n " , ( char * ) data ) ;
Serial . printf ( " %s \n " , ( char * ) data ) ;
}
else
{
for ( size_t i = 0 ; i < info - > len ; i + + )
{
} else {
for ( size_t i = 0 ; i < info - > len ; i + + ) {
Serial . printf ( " %02x " , data [ i ] ) ;
Serial . printf ( " %02x " , data [ i ] ) ;
}
}
Serial . printf ( " \n " ) ;
Serial . printf ( " \n " ) ;
@ -593,40 +479,33 @@ void onWebsocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsE
// Handles file upload request from the explorer
// Handles file upload request from the explorer
// requires a GET parameter path, as directory path to the file
// requires a GET parameter path, as directory path to the file
void explorerHandleFileUpload ( AsyncWebServerRequest * request , String filename , size_t index , uint8_t * data , size_t len , bool final )
{
void explorerHandleFileUpload ( AsyncWebServerRequest * request , String filename , size_t index , uint8_t * data , size_t len , bool final ) {
System_UpdateActivityTimer ( ) ;
System_UpdateActivityTimer ( ) ;
// New File
// New File
if ( ! index )
{
if ( ! index ) {
String utf8FilePath ;
String utf8FilePath ;
static char filePath [ MAX_FILEPATH_LENTGH ] ;
static char filePath [ MAX_FILEPATH_LENTGH ] ;
if ( request - > hasParam ( " path " ) )
{
if ( request - > hasParam ( " path " ) ) {
AsyncWebParameter * param = request - > getParam ( " path " ) ;
AsyncWebParameter * param = request - > getParam ( " path " ) ;
utf8FilePath = param - > value ( ) + " / " + filename ;
utf8FilePath = param - > value ( ) + " / " + filename ;
}
else
{
} else {
utf8FilePath = " / " + filename ;
utf8FilePath = " / " + filename ;
}
}
convertUtf8ToAscii ( utf8FilePath , filePath ) ;
convertUtf8ToAscii ( utf8FilePath , filePath ) ;
snprintf ( Log_Buffer , Log_BufferLength , " %s: %s " , ( char * ) FPSTR ( writingFile ) , utf8FilePath . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " %s: %s " , ( char * ) FPSTR ( writingFile ) , utf8FilePath . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
// Create Ringbuffer for upload
// Create Ringbuffer for upload
if ( explorerFileUploadRingBuffer = = NULL )
{
if ( explorerFileUploadRingBuffer = = NULL ) {
explorerFileUploadRingBuffer = xRingbufferCreate ( 4096 , RINGBUF_TYPE_BYTEBUF ) ;
explorerFileUploadRingBuffer = xRingbufferCreate ( 4096 , RINGBUF_TYPE_BYTEBUF ) ;
}
}
// Create Queue for receiving a signal from the store task as synchronisation
// Create Queue for receiving a signal from the store task as synchronisation
if ( explorerFileUploadStatusQueue = = NULL )
{
if ( explorerFileUploadStatusQueue = = NULL ) {
explorerFileUploadStatusQueue = xQueueCreate ( 1 , sizeof ( uint8_t ) ) ;
explorerFileUploadStatusQueue = xQueueCreate ( 1 , sizeof ( uint8_t ) ) ;
}
}
@ -641,14 +520,12 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s
) ;
) ;
}
}
if ( len )
{
if ( len ) {
// stream the incoming chunk to the ringbuffer
// stream the incoming chunk to the ringbuffer
xRingbufferSend ( explorerFileUploadRingBuffer , data , len , portTICK_PERIOD_MS * 1000 ) ;
xRingbufferSend ( explorerFileUploadRingBuffer , data , len , portTICK_PERIOD_MS * 1000 ) ;
}
}
if ( final )
{
if ( final ) {
// notify storage task that last data was stored on the ring buffer
// notify storage task that last data was stored on the ring buffer
xTaskNotify ( fileStorageTaskHandle , 1u , eNoAction ) ;
xTaskNotify ( fileStorageTaskHandle , 1u , eNoAction ) ;
// watit until the storage task is sending the signal to finish
// watit until the storage task is sending the signal to finish
@ -660,8 +537,7 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s
}
}
}
}
void explorerHandleFileStorageTask ( void * parameter )
{
void explorerHandleFileStorageTask ( void * parameter ) {
File uploadFile ;
File uploadFile ;
size_t item_size ;
size_t item_size ;
@ -673,22 +549,17 @@ void explorerHandleFileStorageTask(void *parameter)
uploadFile = gFSystem . open ( ( char * ) parameter , " w " ) ;
uploadFile = gFSystem . open ( ( char * ) parameter , " w " ) ;
for ( ; ; )
{
for ( ; ; ) {
esp_task_wdt_reset ( ) ;
esp_task_wdt_reset ( ) ;
item = ( uint8_t * ) xRingbufferReceive ( explorerFileUploadRingBuffer , & item_size , portTICK_PERIOD_MS * 100 ) ;
item = ( uint8_t * ) xRingbufferReceive ( explorerFileUploadRingBuffer , & item_size , portTICK_PERIOD_MS * 100 ) ;
if ( item ! = NULL )
{
if ( item ! = NULL ) {
uploadFile . write ( item , item_size ) ;
uploadFile . write ( item , item_size ) ;
vRingbufferReturnItem ( explorerFileUploadRingBuffer , ( void * ) item ) ;
vRingbufferReturnItem ( explorerFileUploadRingBuffer , ( void * ) item ) ;
}
else
{
} else {
// No data in the buffer, check if all data arrived for the file
// No data in the buffer, check if all data arrived for the file
uploadFileNotification = xTaskNotifyWait ( 0 , 0 , & uploadFileNotificationValue , 0 ) ;
uploadFileNotification = xTaskNotifyWait ( 0 , 0 , & uploadFileNotificationValue , 0 ) ;
if ( uploadFileNotification = = pdPASS )
{
if ( uploadFileNotification = = pdPASS ) {
uploadFile . close ( ) ;
uploadFile . close ( ) ;
// done exit loop to terminate
// done exit loop to terminate
break ;
break ;
@ -703,8 +574,7 @@ void explorerHandleFileStorageTask(void *parameter)
// Sends a list of the content of a directory as JSON file
// Sends a list of the content of a directory as JSON file
// requires a GET parameter path for the directory
// requires a GET parameter path for the directory
void explorerHandleListRequest ( AsyncWebServerRequest * request )
{
void explorerHandleListRequest ( AsyncWebServerRequest * request ) {
DynamicJsonDocument jsonBuffer ( 16384 ) ;
DynamicJsonDocument jsonBuffer ( 16384 ) ;
//StaticJsonDocument<4096> jsonBuffer;
//StaticJsonDocument<4096> jsonBuffer;
String serializedJsonString ;
String serializedJsonString ;
@ -712,38 +582,31 @@ void explorerHandleListRequest(AsyncWebServerRequest *request)
char filePath [ MAX_FILEPATH_LENTGH ] ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
JsonArray obj = jsonBuffer . createNestedArray ( ) ;
JsonArray obj = jsonBuffer . createNestedArray ( ) ;
File root ;
File root ;
if ( request - > hasParam ( " path " ) )
{
if ( request - > hasParam ( " path " ) ) {
param = request - > getParam ( " path " ) ;
param = request - > getParam ( " path " ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
root = gFSystem . open ( filePath ) ;
root = gFSystem . open ( filePath ) ;
}
else
{
} else {
root = gFSystem . open ( " / " ) ;
root = gFSystem . open ( " / " ) ;
}
}
if ( ! root )
{
snprintf ( Log_Buffer , Log_BufferLength , ( char * ) FPSTR ( failedToOpenDirectory ) ) ;
if ( ! root ) {
snprintf ( Log_Buffer , Log_BufferLength , ( char * ) FPSTR ( failedToOpenDirectory ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_DEBUG ) ;
Log_Println ( Log_Buffer , LOGLEVEL_DEBUG ) ;
return ;
return ;
}
}
if ( ! root . isDirectory ( ) )
{
snprintf ( Log_Buffer , Log_BufferLength , ( char * ) FPSTR ( notADirectory ) ) ;
if ( ! root . isDirectory ( ) ) {
snprintf ( Log_Buffer , Log_BufferLength , ( char * ) FPSTR ( notADirectory ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_DEBUG ) ;
Log_Println ( Log_Buffer , LOGLEVEL_DEBUG ) ;
return ;
return ;
}
}
File file = root . openNextFile ( ) ;
File file = root . openNextFile ( ) ;
while ( file )
{
while ( file ) {
// ignore hidden folders, e.g. MacOS spotlight files
// ignore hidden folders, e.g. MacOS spotlight files
if ( ! startsWith ( file . name ( ) , ( char * ) " /. " ) )
{
if ( ! startsWith ( file . name ( ) , ( char * ) " /. " ) ) {
JsonObject entry = obj . createNestedObject ( ) ;
JsonObject entry = obj . createNestedObject ( ) ;
convertAsciiToUtf8 ( file . name ( ) , filePath ) ;
convertAsciiToUtf8 ( file . name ( ) , filePath ) ;
std : : string path = filePath ;
std : : string path = filePath ;
@ -761,19 +624,14 @@ void explorerHandleListRequest(AsyncWebServerRequest *request)
request - > send ( 200 , " application/json; charset=utf-8 " , serializedJsonString ) ;
request - > send ( 200 , " application/json; charset=utf-8 " , serializedJsonString ) ;
}
}
bool explorerDeleteDirectory ( File dir )
{
bool explorerDeleteDirectory ( File dir ) {
File file = dir . openNextFile ( ) ;
File file = dir . openNextFile ( ) ;
while ( file )
{
while ( file ) {
if ( file . isDirectory ( ) )
{
if ( file . isDirectory ( ) ) {
explorerDeleteDirectory ( file ) ;
explorerDeleteDirectory ( file ) ;
}
else
{
} else {
gFSystem . remove ( file . name ( ) ) ;
gFSystem . remove ( file . name ( ) ) ;
}
}
@ -787,53 +645,37 @@ bool explorerDeleteDirectory(File dir)
// Handles delete request of a file or directory
// Handles delete request of a file or directory
// requires a GET parameter path to the file or directory
// requires a GET parameter path to the file or directory
void explorerHandleDeleteRequest ( AsyncWebServerRequest * request )
{
void explorerHandleDeleteRequest ( AsyncWebServerRequest * request ) {
File file ;
File file ;
AsyncWebParameter * param ;
AsyncWebParameter * param ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
if ( request - > hasParam ( " path " ) )
{
if ( request - > hasParam ( " path " ) ) {
param = request - > getParam ( " path " ) ;
param = request - > getParam ( " path " ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
if ( gFSystem . exists ( filePath ) )
{
if ( gFSystem . exists ( filePath ) ) {
file = gFSystem . open ( filePath ) ;
file = gFSystem . open ( filePath ) ;
if ( file . isDirectory ( ) )
{
if ( explorerDeleteDirectory ( file ) )
{
if ( file . isDirectory ( ) ) {
if ( explorerDeleteDirectory ( file ) ) {
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: %s deleted " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: %s deleted " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Cannot delete %s " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Cannot delete %s " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
else
{
if ( gFSystem . remove ( filePath ) )
{
} else {
if ( gFSystem . remove ( filePath ) ) {
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: %s deleted " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: %s deleted " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Cannot delete %s " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Cannot delete %s " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
}
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Path %s does not exist " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " DELETE: Path %s does not exist " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
else
{
} else {
Log_Println ( " DELETE: No path variable set " , LOGLEVEL_ERROR ) ;
Log_Println ( " DELETE: No path variable set " , LOGLEVEL_ERROR ) ;
}
}
request - > send ( 200 ) ;
request - > send ( 200 ) ;
@ -842,27 +684,20 @@ void explorerHandleDeleteRequest(AsyncWebServerRequest *request)
// Handles create request of a directory
// Handles create request of a directory
// requires a GET parameter path to the new directory
// requires a GET parameter path to the new directory
void explorerHandleCreateRequest ( AsyncWebServerRequest * request )
{
void explorerHandleCreateRequest ( AsyncWebServerRequest * request ) {
AsyncWebParameter * param ;
AsyncWebParameter * param ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
if ( request - > hasParam ( " path " ) )
{
if ( request - > hasParam ( " path " ) ) {
param = request - > getParam ( " path " ) ;
param = request - > getParam ( " path " ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
if ( gFSystem . mkdir ( filePath ) )
{
if ( gFSystem . mkdir ( filePath ) ) {
snprintf ( Log_Buffer , Log_BufferLength , " CREATE: %s created " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " CREATE: %s created " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " CREATE: Cannot create %s " , param - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " CREATE: Cannot create %s " , param - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
else
{
} else {
Log_Println ( " CREATE: No path variable set " , LOGLEVEL_ERROR ) ;
Log_Println ( " CREATE: No path variable set " , LOGLEVEL_ERROR ) ;
}
}
request - > send ( 200 ) ;
request - > send ( 200 ) ;
@ -871,39 +706,29 @@ void explorerHandleCreateRequest(AsyncWebServerRequest *request)
// Handles rename request of a file or directory
// Handles rename request of a file or directory
// requires a GET parameter srcpath to the old file or directory name
// requires a GET parameter srcpath to the old file or directory name
// requires a GET parameter dstpath to the new file or directory name
// requires a GET parameter dstpath to the new file or directory name
void explorerHandleRenameRequest ( AsyncWebServerRequest * request )
{
void explorerHandleRenameRequest ( AsyncWebServerRequest * request ) {
AsyncWebParameter * srcPath ;
AsyncWebParameter * srcPath ;
AsyncWebParameter * dstPath ;
AsyncWebParameter * dstPath ;
char srcFullFilePath [ MAX_FILEPATH_LENTGH ] ;
char srcFullFilePath [ MAX_FILEPATH_LENTGH ] ;
char dstFullFilePath [ MAX_FILEPATH_LENTGH ] ;
char dstFullFilePath [ MAX_FILEPATH_LENTGH ] ;
if ( request - > hasParam ( " srcpath " ) & & request - > hasParam ( " dstpath " ) )
{
if ( request - > hasParam ( " srcpath " ) & & request - > hasParam ( " dstpath " ) ) {
srcPath = request - > getParam ( " srcpath " ) ;
srcPath = request - > getParam ( " srcpath " ) ;
dstPath = request - > getParam ( " dstpath " ) ;
dstPath = request - > getParam ( " dstpath " ) ;
convertUtf8ToAscii ( srcPath - > value ( ) , srcFullFilePath ) ;
convertUtf8ToAscii ( srcPath - > value ( ) , srcFullFilePath ) ;
convertUtf8ToAscii ( dstPath - > value ( ) , dstFullFilePath ) ;
convertUtf8ToAscii ( dstPath - > value ( ) , dstFullFilePath ) ;
if ( gFSystem . exists ( srcFullFilePath ) )
{
if ( gFSystem . rename ( srcFullFilePath , dstFullFilePath ) )
{
if ( gFSystem . exists ( srcFullFilePath ) ) {
if ( gFSystem . rename ( srcFullFilePath , dstFullFilePath ) ) {
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: %s renamed to %s " , srcPath - > value ( ) . c_str ( ) , dstPath - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: %s renamed to %s " , srcPath - > value ( ) . c_str ( ) , dstPath - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
Log_Println ( Log_Buffer , LOGLEVEL_INFO ) ;
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: Cannot rename %s " , srcPath - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: Cannot rename %s " , srcPath - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: Path %s does not exist " , srcPath - > value ( ) . c_str ( ) ) ;
snprintf ( Log_Buffer , Log_BufferLength , " RENAME: Path %s does not exist " , srcPath - > value ( ) . c_str ( ) ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
}
}
}
else
{
} else {
Log_Println ( " RENAME: No path variable set " , LOGLEVEL_ERROR ) ;
Log_Println ( " RENAME: No path variable set " , LOGLEVEL_ERROR ) ;
}
}
@ -913,14 +738,12 @@ void explorerHandleRenameRequest(AsyncWebServerRequest *request)
// Handles audio play requests
// Handles audio play requests
// requires a GET parameter path to the audio file or directory
// requires a GET parameter path to the audio file or directory
// requires a GET parameter playmode
// requires a GET parameter playmode
void explorerHandleAudioRequest ( AsyncWebServerRequest * request )
{
void explorerHandleAudioRequest ( AsyncWebServerRequest * request ) {
AsyncWebParameter * param ;
AsyncWebParameter * param ;
String playModeString ;
String playModeString ;
uint32_t playMode ;
uint32_t playMode ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
char filePath [ MAX_FILEPATH_LENTGH ] ;
if ( request - > hasParam ( " path " ) & & request - > hasParam ( " playmode " ) )
{
if ( request - > hasParam ( " path " ) & & request - > hasParam ( " playmode " ) ) {
param = request - > getParam ( " path " ) ;
param = request - > getParam ( " path " ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
convertUtf8ToAscii ( param - > value ( ) , filePath ) ;
param = request - > getParam ( " playmode " ) ;
param = request - > getParam ( " playmode " ) ;
@ -928,9 +751,7 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request)
playMode = atoi ( playModeString . c_str ( ) ) ;
playMode = atoi ( playModeString . c_str ( ) ) ;
AudioPlayer_TrackQueueDispatcher ( filePath , 0 , playMode , 0 ) ;
AudioPlayer_TrackQueueDispatcher ( filePath , 0 , playMode , 0 ) ;
}
else
{
} else {
Log_Println ( " AUDIO: No path variable set " , LOGLEVEL_ERROR ) ;
Log_Println ( " AUDIO: No path variable set " , LOGLEVEL_ERROR ) ;
}
}
@ -938,8 +759,7 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request)
}
}
// Handles uploaded backup-file and writes valid entries into NVS
// Handles uploaded backup-file and writes valid entries into NVS
void handleUpload ( AsyncWebServerRequest * request , String filename , size_t index , uint8_t * data , size_t len , bool final )
{
void handleUpload ( AsyncWebServerRequest * request , String filename , size_t index , uint8_t * data , size_t len , bool final ) {
Led_SetPause ( true ) ; // Workaround to prevent exceptions due to Neopixel-signalisation while NVS-write
Led_SetPause ( true ) ; // Workaround to prevent exceptions due to Neopixel-signalisation while NVS-write
char ebuf [ 290 ] ;
char ebuf [ 290 ] ;
uint16_t j = 0 ;
uint16_t j = 0 ;
@ -947,36 +767,27 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
uint8_t count = 0 ;
uint8_t count = 0 ;
nvs_t nvsEntry [ 1 ] ;
nvs_t nvsEntry [ 1 ] ;
for ( size_t i = 0 ; i < len ; i + + )
{
if ( data [ i ] ! = ' \n ' )
{
for ( size_t i = 0 ; i < len ; i + + ) {
if ( data [ i ] ! = ' \n ' ) {
ebuf [ j + + ] = data [ i ] ;
ebuf [ j + + ] = data [ i ] ;
}
else
{
} else {
ebuf [ j ] = ' \0 ' ;
ebuf [ j ] = ' \0 ' ;
j = 0 ;
j = 0 ;
token = strtok ( ebuf , stringOuterDelimiter ) ;
token = strtok ( ebuf , stringOuterDelimiter ) ;
while ( token ! = NULL )
{
if ( ! count )
{
while ( token ! = NULL ) {
if ( ! count ) {
count + + ;
count + + ;
memcpy ( nvsEntry [ 0 ] . nvsKey , token , strlen ( token ) ) ;
memcpy ( nvsEntry [ 0 ] . nvsKey , token , strlen ( token ) ) ;
nvsEntry [ 0 ] . nvsKey [ strlen ( token ) ] = ' \0 ' ;
nvsEntry [ 0 ] . nvsKey [ strlen ( token ) ] = ' \0 ' ;
}
else if ( count = = 1 )
{
} else if ( count = = 1 ) {
count = 0 ;
count = 0 ;
memcpy ( nvsEntry [ 0 ] . nvsEntry , token , strlen ( token ) ) ;
memcpy ( nvsEntry [ 0 ] . nvsEntry , token , strlen ( token ) ) ;
nvsEntry [ 0 ] . nvsEntry [ strlen ( token ) ] = ' \0 ' ;
nvsEntry [ 0 ] . nvsEntry [ strlen ( token ) ] = ' \0 ' ;
}
}
token = strtok ( NULL , stringOuterDelimiter ) ;
token = strtok ( NULL , stringOuterDelimiter ) ;
}
}
if ( isNumber ( nvsEntry [ 0 ] . nvsKey ) & & nvsEntry [ 0 ] . nvsEntry [ 0 ] = = ' # ' )
{
snprintf ( Log_Buffer , Log_BufferLength , " %s: %s => %s " , ( char * ) FPSTR ( writeEntryToNvs ) , nvsEntry [ 0 ] . nvsKey , nvsEntry [ 0 ] . nvsEntry ) ;
if ( isNumber ( nvsEntry [ 0 ] . nvsKey ) & & nvsEntry [ 0 ] . nvsEntry [ 0 ] = = ' # ' ) {
snprintf ( Log_Buffer , Log_BufferLength , " %s: %s => %s " , ( char * ) FPSTR ( writeEntryToNvs ) , nvsEntry [ 0 ] . nvsKey , nvsEntry [ 0 ] . nvsEntry ) ;
Log_Println ( Log_Buffer , LOGLEVEL_NOTICE ) ;
Log_Println ( Log_Buffer , LOGLEVEL_NOTICE ) ;
gPrefsRfid . putString ( nvsEntry [ 0 ] . nvsKey , nvsEntry [ 0 ] . nvsEntry ) ;
gPrefsRfid . putString ( nvsEntry [ 0 ] . nvsKey , nvsEntry [ 0 ] . nvsEntry ) ;
}
}
@ -986,8 +797,7 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
}
}
// Dumps all RFID-entries from NVS into a file on SD-card
// Dumps all RFID-entries from NVS into a file on SD-card
bool Web_DumpNvsToSd ( const char * _namespace , const char * _destFile )
{
bool Web_DumpNvsToSd ( const char * _namespace , const char * _destFile ) {
Led_SetPause ( true ) ; // Workaround to prevent exceptions due to Neopixel-signalisation while NVS-write
Led_SetPause ( true ) ; // Workaround to prevent exceptions due to Neopixel-signalisation while NVS-write
esp_partition_iterator_t pi ; // Iterator for find
esp_partition_iterator_t pi ; // Iterator for find
const esp_partition_t * nvs ; // Pointer to partition struct
const esp_partition_t * nvs ; // Pointer to partition struct
@ -1002,31 +812,25 @@ bool Web_DumpNvsToSd(const char *_namespace, const char *_destFile)
pi = esp_partition_find ( ESP_PARTITION_TYPE_DATA , // Get partition iterator for
pi = esp_partition_find ( ESP_PARTITION_TYPE_DATA , // Get partition iterator for
ESP_PARTITION_SUBTYPE_ANY , // this partition
ESP_PARTITION_SUBTYPE_ANY , // this partition
partname ) ;
partname ) ;
if ( pi )
{
if ( pi ) {
nvs = esp_partition_get ( pi ) ; // Get partition struct
nvs = esp_partition_get ( pi ) ; // Get partition struct
esp_partition_iterator_release ( pi ) ; // Release the iterator
esp_partition_iterator_release ( pi ) ; // Release the iterator
dbgprint ( " Partition %s found, %d bytes " , partname , nvs - > size ) ;
dbgprint ( " Partition %s found, %d bytes " , partname , nvs - > size ) ;
}
else
{
} else {
snprintf ( Log_Buffer , Log_BufferLength , " Partition %s not found! " , partname ) ;
snprintf ( Log_Buffer , Log_BufferLength , " Partition %s not found! " , partname ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
return NULL ;
return NULL ;
}
}
namespace_ID = FindNsID ( nvs , _namespace ) ; // Find ID of our namespace in NVS
namespace_ID = FindNsID ( nvs , _namespace ) ; // Find ID of our namespace in NVS
File backupFile = gFSystem . open ( _destFile , FILE_WRITE ) ;
File backupFile = gFSystem . open ( _destFile , FILE_WRITE ) ;
if ( ! backupFile )
{
if ( ! backupFile ) {
return false ;
return false ;
}
}
while ( offset < nvs - > size )
{
while ( offset < nvs - > size ) {
result = esp_partition_read ( nvs , offset , // Read 1 page in nvs partition
result = esp_partition_read ( nvs , offset , // Read 1 page in nvs partition
& buf ,
& buf ,
sizeof ( nvs_page ) ) ;
sizeof ( nvs_page ) ) ;
if ( result ! = ESP_OK )
{
if ( result ! = ESP_OK ) {
snprintf ( Log_Buffer , Log_BufferLength , " Error reading NVS! " ) ;
snprintf ( Log_Buffer , Log_BufferLength , " Error reading NVS! " ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
Log_Println ( Log_Buffer , LOGLEVEL_ERROR ) ;
return false ;
return false ;
@ -1034,24 +838,18 @@ bool Web_DumpNvsToSd(const char *_namespace, const char *_destFile)
i = 0 ;
i = 0 ;
while ( i < 126 )
{
while ( i < 126 ) {
bm = ( buf . Bitmap [ i / 4 ] > > ( ( i % 4 ) * 2 ) ) & 0x03 ; // Get bitmap for this entry
bm = ( buf . Bitmap [ i / 4 ] > > ( ( i % 4 ) * 2 ) ) & 0x03 ; // Get bitmap for this entry
if ( bm = = 2 )
{
if ( bm = = 2 ) {
if ( ( namespace_ID = = 0xFF ) | | // Show all if ID = 0xFF
if ( ( namespace_ID = = 0xFF ) | | // Show all if ID = 0xFF
( buf . Entry [ i ] . Ns = = namespace_ID ) )
{ // otherwise just my namespace
if ( isNumber ( buf . Entry [ i ] . Key ) )
{
( buf . Entry [ i ] . Ns = = namespace_ID ) ) { // otherwise just my namespace
if ( isNumber ( buf . Entry [ i ] . Key ) ) {
String s = gPrefsRfid . getString ( ( const char * ) buf . Entry [ i ] . Key ) ;
String s = gPrefsRfid . getString ( ( const char * ) buf . Entry [ i ] . Key ) ;
backupFile . printf ( " %s%s%s%s \n " , stringOuterDelimiter , buf . Entry [ i ] . Key , stringOuterDelimiter , s . c_str ( ) ) ;
backupFile . printf ( " %s%s%s%s \n " , stringOuterDelimiter , buf . Entry [ i ] . Key , stringOuterDelimiter , s . c_str ( ) ) ;
}
}
}
}
i + = buf . Entry [ i ] . Span ; // Next entry
i + = buf . Entry [ i ] . Span ; // Next entry
}
else
{
} else {
i + + ;
i + + ;
}
}
}
}