From f75596f3cd77cfe1e05f019fb6a41569817e984a Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 22 May 2022 13:13:34 -0500 Subject: [PATCH] Changed deleteAccessory from int to boolean Returns true on success, otherwise false --- .../20-AdvancedTechniques.ino | 16 +++++++++------- src/HomeSpan.cpp | 6 +++--- src/HomeSpan.h | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/20-AdvancedTechniques/20-AdvancedTechniques.ino b/examples/20-AdvancedTechniques/20-AdvancedTechniques.ino index dd120f5..4fa9647 100644 --- a/examples/20-AdvancedTechniques/20-AdvancedTechniques.ino +++ b/examples/20-AdvancedTechniques/20-AdvancedTechniques.ino @@ -123,17 +123,19 @@ void deleteAccessory(const char *buf){ return; } - if(homeSpan.deleteAccessory(n+1)!=0){ + if(homeSpan.deleteAccessory(n+1)){ + Serial.printf("Deleting Accessory: Light-%d\n",n); + + auto it=std::remove(lights.begin(),lights.end(),n); // remove entry from lights array + *it=0; // overwrite end with a 0 + nvs_set_blob(savedData,"LIGHTS",&lights,sizeof(lights)); // update data + nvs_commit(savedData); + + } else { Serial.printf("No such Accessory: Light-%d\n",n); - return; } - Serial.printf("Deleting Accessory: Light-%d\n",n); - auto it=std::remove(lights.begin(),lights.end(),n); - *it=0; // overwrite end with a 0 - nvs_set_blob(savedData,"LIGHTS",&lights,sizeof(lights)); // update data - nvs_commit(savedData); } /////////////////////////// diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 05e7de0..f881467 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1170,16 +1170,16 @@ void Span::prettyPrint(char *buf, int nsp){ /////////////////////////// -int Span::deleteAccessory(uint32_t n){ +boolean Span::deleteAccessory(uint32_t n){ auto it=homeSpan.Accessories.begin(); for(;it!=homeSpan.Accessories.end() && (*it)->aid!=n; it++); if(it==homeSpan.Accessories.end()) - return(-1); + return(false); delete *it; - return(0); + return(true); } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 4e936e9..85bda61 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -215,13 +215,13 @@ struct Span{ void commandMode(); // allows user to control and reset HomeSpan settings with the control button void processSerialCommand(const char *c); // process command 'c' (typically from readSerial, though can be called with any 'c') - boolean updateDatabase(boolean updateMDNS=true); // updates HAP Configuration Number and Loop vector; iF updateMDNS=true and config number has changed, re-broadcasts MDNS 'c#' record; returns true if config number changed + boolean updateDatabase(boolean updateMDNS=true); // updates HAP Configuration Number and Loop vector; if updateMDNS=true and config number has changed, re-broadcasts MDNS 'c#' record; returns true if config number changed int sprintfAttributes(char *cBuf, int flags=GET_VALUE|GET_META|GET_PERMS|GET_TYPE|GET_DESC); // prints Attributes JSON database into buf, unless buf=NULL; return number of characters printed, excluding null terminator void prettyPrint(char *buf, int nsp=2); // print arbitrary JSON from buf to serial monitor, formatted with indentions of 'nsp' spaces SpanCharacteristic *find(uint32_t aid, int iid); // return Characteristic with matching aid and iid (else NULL if not found) - int deleteAccessory(uint32_t aid); // deletes Accessory with matching aid, if found. Returns 0 on success, -1 on fail (aid not found) + boolean deleteAccessory(uint32_t aid); // deletes Accessory with matching aid; returns true if found, else returns false int countCharacteristics(char *buf); // return number of characteristic objects referenced in PUT /characteristics JSON request int updateCharacteristics(char *buf, SpanBuf *pObj); // parses PUT /characteristics JSON request 'buf into 'pObj' and updates referenced characteristics; returns 1 on success, 0 on fail