Initial work on adding destructors to Accessories, Services, and Characteristics

Will allow for dynamic changes to database without rebooting.
This commit is contained in:
Gregg 2022-05-08 07:02:19 -05:00
parent bddfb4658a
commit 554cefd3ce
2 changed files with 45 additions and 9 deletions

View File

@ -1526,6 +1526,20 @@ SpanAccessory::SpanAccessory(uint32_t aid){
///////////////////////////////
SpanAccessory::~SpanAccessory(){
while(Services.rbegin()!=Services.rend()) // delete all Services in this Accessory
delete *Services.rbegin();
auto acc=homeSpan.Accessories.begin(); // find Accessory in homeSpan vector and erase entry
while((*acc)!=this)
acc++;
homeSpan.Accessories.erase(acc);
Serial.printf("Deleted Accessory AID=%d\n",aid);
}
///////////////////////////////
int SpanAccessory::sprintfAttributes(char *cBuf, int flags){
int nBytes=0;
@ -1559,11 +1573,30 @@ SpanService::SpanService(const char *type, const char *hapName, boolean isCustom
this->isCustom=isCustom;
homeSpan.Accessories.back()->Services.push_back(this);
accessory=homeSpan.Accessories.back();
iid=++(homeSpan.Accessories.back()->iidCount);
}
///////////////////////////////
SpanService::~SpanService(){
auto svc=accessory->Services.begin(); // find Service in containing Accessory vector and erase entry
while((*svc)!=this)
svc++;
accessory->Services.erase(svc);
for(svc=homeSpan.Loops.begin(); (*svc)!=this && svc!=homeSpan.Loops.end(); svc++); // find and erase entry in Loop vector
if(svc!=homeSpan.Loops.end()){
homeSpan.Loops.erase(svc);
Serial.printf("Deleted Loop Entry\n");
}
Serial.printf("Deleted Service AID=%d IID=%d\n",accessory->aid,iid);
}
///////////////////////////////
SpanService *SpanService::setPrimary(){
primary=true;
return(this);
@ -1624,6 +1657,13 @@ int SpanService::sprintfAttributes(char *cBuf, int flags){
///////////////////////////////
SpanCharacteristic::SpanCharacteristic(HapChar *hapChar, boolean isCustom){
if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){
Serial.printf("\nFATAL ERROR! Can't create new Characteristic '%s' without a defined Service ***\n",hapName);
Serial.printf("\n=== PROGRAM HALTED ===");
while(1);
}
type=hapChar->type;
perms=hapChar->perms;
hapName=hapChar->hapName;
@ -1632,12 +1672,7 @@ SpanCharacteristic::SpanCharacteristic(HapChar *hapChar, boolean isCustom){
this->isCustom=isCustom;
this->hapChar=hapChar;
if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){
Serial.printf("\nFATAL ERROR! Can't create new Characteristic '%s' without a defined Service ***\n",hapName);
Serial.printf("\n=== PROGRAM HALTED ===");
while(1);
}
homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this);
iid=++(homeSpan.Accessories.back()->iidCount);
service=homeSpan.Accessories.back()->Services.back();
aid=homeSpan.Accessories.back()->aid;

View File

@ -283,6 +283,7 @@ struct SpanAccessory{
vector<SpanService *> Services; // vector of pointers to all Services in this Accessory
SpanAccessory(uint32_t aid=0);
~SpanAccessory();
int sprintfAttributes(char *cBuf, int flags); // prints Accessory JSON database into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL
};
@ -301,8 +302,10 @@ struct SpanService{
unordered_set<HapChar *> opt; // unordered set of pointers to all optional HAP Characteristic Types for this Service
vector<SpanService *> linkedServices; // vector of pointers to any optional linked Services
boolean isCustom; // flag to indicate this is a Custom Service
SpanAccessory *accessory=NULL; // pointer to Accessory containing this Service
SpanService(const char *type, const char *hapName, boolean isCustom=false); // constructor
~SpanService();
SpanService *setPrimary(); // sets the Service Type to be primary and returns pointer to self
SpanService *setHidden(); // sets the Service Type to be hidden and returns pointer to self
@ -511,9 +514,7 @@ struct SpanCharacteristic{
uvSet(maxValue,max);
uvSet(stepValue,0);
}
homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this);
} // init()