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)
This commit is contained in:
Gregg 2022-05-21 12:08:03 -05:00
parent 52a977d2f1
commit 00ea4c6dd8
3 changed files with 16 additions and 9 deletions

View File

@ -58,7 +58,8 @@ void setup() {
new SpanUserCommand('a',"<num> - add a new light accessory with id=<num>",addAccessory);
new SpanUserCommand('d',"<num> - delete a light accessory with id=<num>",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())

View File

@ -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);
}
///////////////////////////////

View File

@ -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