@ -39,72 +39,59 @@ const char* const pinArgNames[] = {"pinCs", "pinCe", "pinIrq", "pinLed0", "pinLe
template < class HMSYSTEM >
class Web {
public :
Web ( void ) {
Web ( void ) : mWeb ( 80 ) , mEvts ( " /events " ) {
mProtected = true ;
mLogoutTimeout = 0 ;
memset ( mSerialBuf , 0 , WEB_SERIAL_BUF_SIZE ) ;
mSerialBufFill = 0 ;
mWebSerialTicker = 0 ;
mWebSerialInterval = 1000 ; // [ms]
mSerialAddTime = true ;
mSerialClientConnnected = false ;
}
void setup ( IApp * app , HMSYSTEM * sys , settings_t * config ) {
mApp = app ;
mSys = sys ;
mConfig = config ;
mWeb = new AsyncWebServer ( 80 ) ;
mEvts = new AsyncEventSource ( " /events " ) ;
DPRINTLN ( DBG_VERBOSE , F ( " app::setup-begin " ) ) ;
mWeb - > begin ( ) ;
DPRINTLN ( DBG_VERBOSE , F ( " app::setup-on " ) ) ;
mWeb - > on ( " / " , HTTP_GET , std : : bind ( & Web : : onIndex , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /login " , HTTP_ANY , std : : bind ( & Web : : onLogin , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /logout " , HTTP_GET , std : : bind ( & Web : : onLogout , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /style.css " , HTTP_GET , std : : bind ( & Web : : onCss , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /api.js " , HTTP_GET , std : : bind ( & Web : : onApiJs , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /favicon.ico " , HTTP_GET , std : : bind ( & Web : : onFavicon , this , std : : placeholders : : _1 ) ) ;
mWeb - > onNotFound ( std : : bind ( & Web : : showNotFound , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /reboot " , HTTP_ANY , std : : bind ( & Web : : onReboot , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /system " , HTTP_ANY , std : : bind ( & Web : : onSystem , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /erase " , HTTP_ANY , std : : bind ( & Web : : showErase , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /factory " , HTTP_ANY , std : : bind ( & Web : : showFactoryRst , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /setup " , HTTP_GET , std : : bind ( & Web : : onSetup , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /save " , HTTP_ANY , std : : bind ( & Web : : showSave , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /live " , HTTP_ANY , std : : bind ( & Web : : onLive , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /api1 " , HTTP_POST , std : : bind ( & Web : : showWebApi , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " / " , HTTP_GET , std : : bind ( & Web : : onIndex , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /login " , HTTP_ANY , std : : bind ( & Web : : onLogin , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /logout " , HTTP_GET , std : : bind ( & Web : : onLogout , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /style.css " , HTTP_GET , std : : bind ( & Web : : onCss , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /api.js " , HTTP_GET , std : : bind ( & Web : : onApiJs , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /favicon.ico " , HTTP_GET , std : : bind ( & Web : : onFavicon , this , std : : placeholders : : _1 ) ) ;
mWeb . onNotFound ( std : : bind ( & Web : : showNotFound , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /reboot " , HTTP_ANY , std : : bind ( & Web : : onReboot , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /system " , HTTP_ANY , std : : bind ( & Web : : onSystem , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /erase " , HTTP_ANY , std : : bind ( & Web : : showErase , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /factory " , HTTP_ANY , std : : bind ( & Web : : showFactoryRst , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /setup " , HTTP_GET , std : : bind ( & Web : : onSetup , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /save " , HTTP_ANY , std : : bind ( & Web : : showSave , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /live " , HTTP_ANY , std : : bind ( & Web : : onLive , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /api1 " , HTTP_POST , std : : bind ( & Web : : showWebApi , this , std : : placeholders : : _1 ) ) ;
# ifdef ENABLE_JSON_EP
mWeb - > on ( " /json " , HTTP_ANY , std : : bind ( & Web : : showJson , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /json " , HTTP_ANY , std : : bind ( & Web : : showJson , this , std : : placeholders : : _1 ) ) ;
# endif
# ifdef ENABLE_PROMETHEUS_EP
mWeb - > on ( " /metrics " , HTTP_ANY , std : : bind ( & Web : : showMetrics , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /metrics " , HTTP_ANY , std : : bind ( & Web : : showMetrics , this , std : : placeholders : : _1 ) ) ;
# endif
mWeb - > on ( " /update " , HTTP_GET , std : : bind ( & Web : : onUpdate , this , std : : placeholders : : _1 ) ) ;
mWeb - > on ( " /update " , HTTP_POST , std : : bind ( & Web : : showUpdate , this , std : : placeholders : : _1 ) ,
mWeb . on ( " /update " , HTTP_GET , std : : bind ( & Web : : onUpdate , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /update " , HTTP_POST , std : : bind ( & Web : : showUpdate , this , std : : placeholders : : _1 ) ,
std : : bind ( & Web : : showUpdate2 , this , std : : placeholders : : _1 , std : : placeholders : : _2 , std : : placeholders : : _3 , std : : placeholders : : _4 , std : : placeholders : : _5 , std : : placeholders : : _6 ) ) ;
mWeb - > on ( " /serial " , HTTP_GET , std : : bind ( & Web : : onSerial , this , std : : placeholders : : _1 ) ) ;
mWeb . on ( " /serial " , HTTP_GET , std : : bind ( & Web : : onSerial , this , std : : placeholders : : _1 ) ) ;
mEvts - > onConnect ( std : : bind ( & Web : : onConnect , this , std : : placeholders : : _1 ) ) ;
mWeb - > addHandler ( mEvts ) ;
mEvts . onConnect ( std : : bind ( & Web : : onConnect , this , std : : placeholders : : _1 ) ) ;
mWeb . addHandler ( & mEvts ) ;
registerDebugCb ( std : : bind ( & Web : : serialCb , this , std : : placeholders : : _1 ) ) ; // dbg.h
}
mWeb . begin ( ) ;
void loop ( void ) {
if ( ah : : checkTicker ( & mWebSerialTicker , mWebSerialInterval ) ) {
if ( mSerialBufFill > 0 ) {
mEvts - > send ( mSerialBuf , " serial " , millis ( ) ) ;
memset ( mSerialBuf , 0 , WEB_SERIAL_BUF_SIZE ) ;
mSerialBufFill = 0 ;
}
}
registerDebugCb ( std : : bind ( & Web : : serialCb , this , std : : placeholders : : _1 ) ) ; // dbg.h
}
void tickSecond ( ) {
@ -117,10 +104,18 @@ class Web {
DPRINTLN ( DBG_DEBUG , " auto logout in " + String ( mLogoutTimeout ) ) ;
}
if ( mSerialClientConnnected ) {
if ( mSerialBufFill > 0 ) {
mEvts . send ( mSerialBuf , " serial " , millis ( ) ) ;
memset ( mSerialBuf , 0 , WEB_SERIAL_BUF_SIZE ) ;
mSerialBufFill = 0 ;
}
}
}
AsyncWebServer * getWebSrvPtr ( void ) {
return mWeb ;
return & mWeb ;
}
void setProtection ( bool protect ) {
@ -151,6 +146,9 @@ class Web {
}
void serialCb ( String msg ) {
if ( ! mSerialClientConnnected )
return ;
msg . replace ( " \r \n " , " <rn> " ) ;
if ( mSerialAddTime ) {
if ( ( 9 + mSerialBufFill ) < = WEB_SERIAL_BUF_SIZE ) {
@ -159,7 +157,7 @@ class Web {
}
else {
mSerialBufFill = 0 ;
mEvts - > send ( " webSerial, buffer overflow! " , " serial " , millis ( ) ) ;
mEvts . send ( " webSerial, buffer overflow! " , " serial " , millis ( ) ) ;
}
mSerialAddTime = false ;
}
@ -174,7 +172,7 @@ class Web {
}
else {
mSerialBufFill = 0 ;
mEvts - > send ( " webSerial, buffer overflow! " , " serial " , millis ( ) ) ;
mEvts . send ( " webSerial, buffer overflow! " , " serial " , millis ( ) ) ;
}
}
@ -212,6 +210,8 @@ class Web {
void onConnect ( AsyncEventSourceClient * client ) {
DPRINTLN ( DBG_VERBOSE , " onConnect " ) ;
mSerialClientConnnected = true ;
if ( client - > lastId ( ) )
DPRINTLN ( DBG_VERBOSE , " Client reconnected! Last message ID that it got is: " + String ( client - > lastId ( ) ) ) ;
@ -262,6 +262,7 @@ class Web {
}
void onCss ( AsyncWebServerRequest * request ) {
DPRINTLN ( DBG_VERBOSE , F ( " onCss " ) ) ;
mLogoutTimeout = LOGOUT_TIMEOUT ;
AsyncWebServerResponse * response = request - > beginResponse_P ( 200 , F ( " text/css " ) , style_css , style_css_len ) ;
response - > addHeader ( F ( " Content-Encoding " ) , " gzip " ) ;
@ -373,7 +374,9 @@ class Web {
return ;
}
if ( request - > args ( ) > 0 ) {
if ( request - > args ( ) = = 0 )
return ;
char buf [ 20 ] = { 0 } ;
// general
@ -504,7 +507,6 @@ class Web {
request - > send ( response ) ;
}
}
}
void onLive ( AsyncWebServerRequest * request ) {
DPRINTLN ( DBG_VERBOSE , F ( " onLive " ) ) ;
@ -632,7 +634,7 @@ class Web {
}
modJson + = F ( " \" json_ts \" : \" " ) + String ( ah : : getDateTimeStr ( mMain - > mTimestamp ) ) + F ( " \" \n } \n " ) ;
mWeb - > send ( 200 , F ( " application/json " ) , modJson ) ;
mWeb . send ( 200 , F ( " application/json " ) , modJson ) ;
}
# endif
@ -663,7 +665,7 @@ class Web {
}
}
mWeb - > send ( 200 , F ( " text/plain " ) , metrics ) ;
mWeb . send ( 200 , F ( " text/plain " ) , metrics ) ;
}
std : : pair < String , String > convertToPromUnits ( String shortUnit ) {
@ -679,8 +681,8 @@ class Web {
}
# endif
AsyncWebServer * mWeb ;
AsyncEventSource * mEvts ;
AsyncWebServer mWeb ;
AsyncEventSource mEvts ;
bool mProtected ;
uint32_t mLogoutTimeout ;
IApp * mApp ;
@ -691,8 +693,7 @@ class Web {
bool mSerialAddTime ;
char mSerialBuf [ WEB_SERIAL_BUF_SIZE ] ;
uint16_t mSerialBufFill ;
uint32_t mWebSerialTicker ;
uint32_t mWebSerialInterval ;
bool mSerialClientConnnected ;
} ;
# endif /*__WEB_H__*/