Adding userData to getweblogs callback.

Allows user-specific data to be available in the callback that returns weblog HTML data.

Return HTTP response code and content type only when no weblog callback defined
This commit is contained in:
Francois 2024-02-07 21:30:54 -05:00
parent 27addd274e
commit 7f6db49d52
4 changed files with 16 additions and 13 deletions

View File

@ -256,7 +256,7 @@ void HAPClient::processRequest(){
}
if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())){ // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE
getStatusURL(this,NULL);
getStatusURL(this,NULL,NULL);
return;
}
@ -1095,7 +1095,7 @@ int HAPClient::putPrepareURL(char *json){
//////////////////////////////////////
void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *)){
void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *, void *), void *user_data){
char clocktime[33];
@ -1119,9 +1119,9 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
if(hapClient)
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hapClient->client.remoteIP().toString().c_str());
hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack);
hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack).setCallbackUserData(user_data);
hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n";
if(!callBack) hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n";
hapOut << "<html><head><title>" << homeSpan.displayName << "</title>\n";
hapOut << "<style>body {background-color:lightblue;} th, td {padding-right: 10px; padding-left: 10px; border:1px solid black;}" << homeSpan.webLog.css.c_str() << "</style></head>\n";
hapOut << "<body class=bod1><h2>" << homeSpan.displayName << "</h2>\n";
@ -1191,7 +1191,7 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
if(homeSpan.weblogCallback){
String usrString;
homeSpan.weblogCallback(usrString);
homeSpan.weblogCallback(usrString); // Callback to add user-defined html in top table.
hapOut << usrString.c_str();
}
@ -1593,7 +1593,7 @@ void HapOut::HapStreamBuffer::flushBuffer(){
buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer)
if(callBack)
callBack(buffer);
callBack(buffer,callBackUserData);
if(logLevel<=homeSpan.getLogLevel()){
if(enablePrettyPrint) // if pretty print needed, use formatted method
@ -1649,8 +1649,9 @@ int HapOut::HapStreamBuffer::sync(){
indent=0;
if(callBack){
callBack(NULL);
callBack(NULL,callBackUserData);
callBack=NULL;
callBackUserData=NULL;
}
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash

View File

@ -170,7 +170,7 @@ struct HAPClient {
static void checkTimedWrites(); // checks for expired Timed Write PIDs, and clears any found (HAP Section 6.7.2.4)
static void eventNotify(SpanBuf *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanBuf objects, pObj, with optional flag to ignore a specific client
static void getStatusURL(HAPClient *, void (*)(const char *)); // GET / status (an optional, non-HAP feature)
static void getStatusURL(HAPClient *, void (*)(const char *, void *), void *); // GET / status (an optional, non-HAP feature)
class HAPTLV : public TLV8 { // dedicated class for HAP TLV8 records
public:
@ -198,7 +198,8 @@ class HapOut : public std::ostream {
size_t indent=0;
uint8_t *hash;
mbedtls_sha512_context *ctx;
void (*callBack)(const char *)=NULL;
void (*callBack)(const char *, void *)=NULL;
void *callBackUserData = NULL;
void flushBuffer();
int_type overflow(int_type c) override;
@ -220,7 +221,8 @@ class HapOut : public std::ostream {
HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);}
HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);}
HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);}
HapOut& setCallback(void(*f)(const char *)){hapBuffer.callBack=f;return(*this);}
HapOut& setCallback(void(*f)(const char *, void *)){hapBuffer.callBack=f;return(*this);}
HapOut& setCallbackUserData(void *userData){hapBuffer.callBackUserData=userData;return(*this);}
uint8_t *getHash(){return(hapBuffer.hash);}
size_t getSize(){return(hapBuffer.getSize());}

View File

@ -1147,8 +1147,8 @@ void Span::processSerialCommand(const char *c){
///////////////////////////////
void Span::getWebLog(void (*f)(const char *)){
HAPClient::getStatusURL(NULL,f);
void Span::getWebLog(void (*f)(const char *, void *), void *user_data){
HAPClient::getStatusURL(NULL,f,user_data);
}
///////////////////////////////

View File

@ -358,7 +358,7 @@ class Span{
Span& setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";return(*this);}
Span& setWebLogCallback(void (*f)(String &)){weblogCallback=f;return(*this);}
void getWebLog(void (*f)(const char *));
void getWebLog(void (*f)(const char *, void *), void *);
Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);}