Added homeSpan.getWebLog()

This commit is contained in:
Gregg 2024-01-08 18:19:38 -06:00
parent 39b7b54d48
commit 392eac38b9
5 changed files with 54 additions and 24 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();
getStatusURL(this,NULL);
return;
}
@ -1095,7 +1095,7 @@ int HAPClient::putPrepareURL(char *json){
//////////////////////////////////////
int HAPClient::getStatusURL(){
void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *)){
char clocktime[33];
@ -1116,9 +1116,10 @@ int HAPClient::getStatusURL(){
sprintf(uptime,"%d:%02d:%02d:%02d",days,hours,mins,secs);
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",client.remoteIP().toString().c_str());
if(hapClient)
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hapClient->client.remoteIP().toString().c_str());
hapOut.setHapClient(this).setLogLevel(2);
hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(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";
@ -1225,10 +1226,10 @@ int HAPClient::getStatusURL(){
hapOut << "</body></html>\n";
hapOut.flush();
if(hapClient){
hapClient->client.stop();
LOG2("------------ SENT! --------------\n");
client.stop();
return(1);
}
}
//////////////////////////////////////
@ -1635,8 +1636,12 @@ void HapOut::HapStreamBuffer::flushBuffer(){
byteCount+=num;
if(logLevel<=homeSpan.getLogLevel()){
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);
if(logLevel<=homeSpan.getLogLevel()){
if(enablePrettyPrint) // if pretty print needed, use formatted method
printFormatted(buffer,num,2);
else // if not, just print
@ -1689,6 +1694,11 @@ int HapOut::HapStreamBuffer::sync(){
byteCount=0;
indent=0;
if(callBack){
callBack(NULL);
callBack=NULL;
}
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash
mbedtls_sha512_starts_ret(ctx,1); // re-start hash for next time

View File

@ -143,7 +143,6 @@ struct HAPClient {
int getCharacteristicsURL(char *urlBuf); // GET /characteristics (HAP Section 6.7.4)
int putCharacteristicsURL(char *json); // PUT /characteristics (HAP Section 6.7.2)
int putPrepareURL(char *json); // PUT /prepare (HAP Section 6.7.2.4)
int getStatusURL(); // GET / status (an optional, non-HAP feature)
void tlvRespond(TLV8 &tlv8); // respond to client with HTTP OK header and all defined TLV data records
void sendEncrypted(char *body, uint8_t *dataBuf, int dataLen); // send client complete ChaCha20-Poly1305 encrypted HTTP mesage comprising a null-terminated 'body' and 'dataBuf' with 'dataLen' bytes
@ -172,6 +171,8 @@ 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)
class HAPTLV : public TLV8 { // dedicated class for HAP TLV8 records
public:
HAPTLV() : TLV8(HAP_Names,11){}
@ -198,6 +199,7 @@ class HapOut : public std::ostream {
size_t indent=0;
uint8_t *hash;
mbedtls_sha512_context *ctx;
void (*callBack)(const char *)=NULL;
void flushBuffer();
int_type overflow(int_type c) override;
@ -219,6 +221,7 @@ 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);}
uint8_t *getHash(){return(hapBuffer.hash);}
size_t getSize(){return(hapBuffer.getSize());}

View File

@ -452,8 +452,6 @@ void Span::checkConnect(){
// create broadcast name from server base name plus accessory ID (without ':')
char *hostName;
if(!hostNameSuffix)
asprintf(&hostName,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15);
else
@ -554,9 +552,6 @@ void Span::checkConnect(){
if(wifiCallbackAll)
wifiCallbackAll((connected+1)/2);
free(hostName);
} // initWiFi
///////////////////////////////
@ -583,8 +578,10 @@ void Span::processSerialCommand(const char *c){
LOG0("\n*** HomeSpan Status ***\n\n");
LOG0("IP Address: %s\n\n",WiFi.localIP().toString().c_str());
LOG0("Accessory ID: ");
LOG0("IP Address: %s\n",WiFi.localIP().toString().c_str());
if(webLog.isEnabled && hostName!=NULL)
LOG0("Web Logging: http://%s.local:%d%s\n",hostName,tcpPortNum,webLog.statusURL.c_str()+4);
LOG0("\nAccessory ID: ");
HAPClient::charPrintRow(HAPClient::accessory.ID,17);
LOG0(" LTPK: ");
HAPClient::hexPrintRow(HAPClient::accessory.LTPK,32);
@ -1146,6 +1143,12 @@ void Span::processSerialCommand(const char *c){
///////////////////////////////
void Span::getWebLog(void (*f)(const char *)){
HAPClient::getStatusURL(NULL,f);
}
///////////////////////////////
void Span::resetStatus(){
if(strlen(network.wifiData.ssid)==0)
STATUS_UPDATE(start(LED_WIFI_NEEDED),HS_WIFI_NEEDED)

View File

@ -222,6 +222,7 @@ class Span{
const char *displayName; // display name for this device - broadcast as part of Bonjour MDNS
const char *hostNameBase; // base of hostName of this device - full host name broadcast by Bonjour MDNS will have 6-byte accessoryID as well as '.local' automatically appended
const char *hostNameSuffix=NULL; // optional "suffix" of hostName of this device. If specified, will be used as the hostName suffix instead of the 6-byte accessoryID
char *hostName=NULL; // derived full hostname
const char *modelName; // model name of this device - broadcast as Bonjour field "md"
char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13)
unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates()
@ -372,6 +373,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 *));
Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);}

View File

@ -27,14 +27,14 @@
#include "HomeSpan.h"
#define MAX_LIGHTS 4
#define MAX_LIGHTS 2
void setup() {
Serial.begin(115200);
homeSpan.setLogLevel(2);
homeSpan.enableWebLog(500);
homeSpan.enableWebLog(50);
homeSpan.begin(Category::Lighting,"HomeSpan Max");
@ -51,11 +51,13 @@ void setup() {
new Characteristic::Name(c);
new Service::LightBulb();
new Characteristic::On(0,false);
new Characteristic::Brightness(50,false);
new Characteristic::Hue(120,false);
new Characteristic::Saturation(100,false);
// new Characteristic::Brightness(50,false);
// new Characteristic::Hue(120,false);
// new Characteristic::Saturation(100,false);
}
new SpanUserCommand('w', " - get web log test",webLogTest);
}
//////////////////////////////////////
@ -67,3 +69,13 @@ void loop(){
}
//////////////////////////////////////
void webLogTest(const char *dummy){
Serial.printf("\n*** In Web Log Test. Starting Custom Web Log Handler\n");
homeSpan.getWebLog(webLogHandler);
Serial.printf("\n**** Done!");
}
void webLogHandler(const char *buf){
Serial.print("Here I am\n");
}