From 058d6b0c965d3636638bbf3495e2b4e3c9e3a0ca Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 8 May 2022 15:27:50 -0500 Subject: [PATCH] Completed destructors for SpanAccessory, SpanService, and SpanCharacteristics Tested to ensure any combination can be deleted. To do: must add logic to destruct any PushButtons that were added to a Service --- src/HAP.cpp | 13 ++----------- src/HomeSpan.cpp | 18 ++++++++++++++++-- src/HomeSpan.h | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 9599503..89fe7e7 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -141,23 +141,14 @@ void HAPClient::init(){ if(!nvs_get_blob(hapNVS,"HAPHASH",NULL,&len)){ // if found HAP HASH structure nvs_get_blob(hapNVS,"HAPHASH",&homeSpan.hapConfig,&len); // retrieve data } else { - Serial.print("Resetting Accessory Configuration number...\n"); + Serial.print("Resetting Database Hash...\n"); nvs_set_blob(hapNVS,"HAPHASH",&homeSpan.hapConfig,sizeof(homeSpan.hapConfig)); // save data (will default to all zero values, which will then be updated below) nvs_commit(hapNVS); // commit to NVS } Serial.print("\n"); - homeSpan.updateConfigNum(); - - for(int i=0;iServices.size();j++){ - SpanService *s=homeSpan.Accessories[i]->Services[j]; - if((void(*)())(s->*(&SpanService::loop)) != (void(*)())(&SpanService::loop)) // save pointers to services in Loops vector - homeSpan.Loops.push_back(s); - } - } - + homeSpan.updateConfigNum(); // create Configuration Cumber and Loop vector } ////////////////////////////////////// diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index c2d2a01..553142c 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1492,6 +1492,15 @@ boolean Span::updateConfigNum(){ Serial.print("\n\n"); } + Loops.clear(); + + for(auto acc=Accessories.begin(); acc!=Accessories.end(); acc++){ // identify all services with over-ridden loop() methods + for(auto svc=(*acc)->Services.begin(); svc!=(*acc)->Services.end(); svc++){ + if((void(*)())((*svc)->*(&SpanService::loop)) != (void(*)())(&SpanService::loop)) // save pointers to services in Loops vector + homeSpan.Loops.push_back((*svc)); + } + } + return(changed); } @@ -1574,7 +1583,7 @@ SpanService::SpanService(const char *type, const char *hapName, boolean isCustom homeSpan.Accessories.back()->Services.push_back(this); accessory=homeSpan.Accessories.back(); - iid=++(homeSpan.Accessories.back()->iidCount); + iid=++(homeSpan.Accessories.back()->iidCount); } /////////////////////////////// @@ -1595,7 +1604,7 @@ SpanService::~SpanService(){ homeSpan.Loops.erase(svc); Serial.printf("Deleted Loop Entry\n"); } - + Serial.printf("Deleted Service AID=%d IID=%d\n",accessory->aid,iid); } @@ -1698,6 +1707,11 @@ SpanCharacteristic::~SpanCharacteristic(){ free(unit); free(validValues); free(nvsKey); + + if(format==FORMAT::STRING){ + free(value.STRING); + free(newValue.STRING); + } Serial.printf("Deleted Characteristic AID=%d IID=%d\n",aid,iid); } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index c6e9bbc..2ebda74 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -315,7 +315,7 @@ struct SpanService{ int sprintfAttributes(char *cBuf, int flags); // prints Service JSON records into buf; return number of characters printed, excluding null terminator virtual boolean update() {return(true);} // placeholder for code that is called when a Service is updated via a Controller. Must return true/false depending on success of update - virtual void loop(){} // loops for each Service - called every cycle and can be over-ridden with user-defined code + virtual void loop(){} // loops for each Service - called every cycle if over-ridden with user-defined code virtual void button(int pin, int pressType){} // method called for a Service when a button attached to "pin" has a Single, Double, or Long Press, according to pressType };