From 00ea4c6dd8dab1ed422696abdbac917c56c0d395 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 21 May 2022 12:08:03 -0500 Subject: [PATCH] Changes homeSpan.getAccessory(aid) to homeSpan.deleteAccessory() The only reason to have used getAccessory(aid) is to delete it, so the delete command is now invoked automatically. Function returns 0 on success (matching aid found) or -1 on fail (aid not found) --- examples/20-DynamicBridge/20-DynamicBridge.ino | 16 +++++++++++----- src/HomeSpan.cpp | 7 ++++--- src/HomeSpan.h | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/20-DynamicBridge/20-DynamicBridge.ino b/examples/20-DynamicBridge/20-DynamicBridge.ino index 1213926..97ca0ce 100644 --- a/examples/20-DynamicBridge/20-DynamicBridge.ino +++ b/examples/20-DynamicBridge/20-DynamicBridge.ino @@ -58,7 +58,8 @@ void setup() { new SpanUserCommand('a'," - add a new light accessory with id=",addAccessory); new SpanUserCommand('d'," - delete a light accessory with id=",deleteAccessory); - new SpanUserCommand('u',"update accessories database",updateAccessories); + new SpanUserCommand('D'," - delete ALL light accessories",deleteAllAccessories); + new SpanUserCommand('u',"- update accessories database",updateAccessories); } @@ -132,15 +133,12 @@ void deleteAccessory(const char *buf){ return; } - SpanAccessory *acc=homeSpan.getAccessory(n+1); - - if(!acc){ + if(homeSpan.deleteAccessory(n+1)){ Serial.printf("No such Accessory: Light-%d\n",n); return; } Serial.printf("Deleting Accessory: Light-%d\n",n); - delete acc; int i; // find entry in accNum for(i=0;accNum[i]!=n;i++); @@ -153,6 +151,14 @@ void deleteAccessory(const char *buf){ /////////////////////////// +void deleteAllAccessories(const char *buf){ + + nvs_erase_all(savedData); + nvs_commit(savedData); + } + +/////////////////////////// + void updateAccessories(const char *buf){ if(homeSpan.updateDatabase()) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 757eddd..d25f631 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1170,15 +1170,16 @@ void Span::prettyPrint(char *buf, int nsp){ /////////////////////////// -SpanAccessory *Span::getAccessory(uint32_t n){ +int 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(NULL); + return(-1); - return(*it); + delete *it; + return(0); } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 966de42..40b592b 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -221,7 +221,7 @@ struct Span{ 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) - SpanAccessory *getAccessory(uint32_t aid); // return Accessory with matching aid (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) 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