From f4c9c430ef40de2f76a15e822b8327861951c304 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 13 Jun 2021 09:20:13 -0500 Subject: [PATCH] Replace restore() method with second optional parameter when instantiating Characteristic To enable save/restore for a Characteristic, set second parameter to TRUE when instantiating. Since first parameter was optional as well, this requires setting it as well. Next up: Must add logic to setVal() to store new value as well. --- src/HomeSpan.cpp | 19 ------------ src/HomeSpan.h | 78 +++++++++++++++++++++++++++++------------------- src/Span.h | 2 +- src/src.ino | 8 ++--- 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 71b58ae..7fc6a47 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1525,25 +1525,6 @@ SpanCharacteristic::SpanCharacteristic(HapChar *hapChar){ /////////////////////////////// -void SpanCharacteristic::restore(){ - - nvsKey=(char *)malloc(16); - uint16_t t; - sscanf(type,"%x",&t); - sprintf(nvsKey,"%04X%08X%03X",t,aid,iid&0xFFF); - size_t len; - - if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){ - nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len); - } - else { - nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data - nvs_commit(homeSpan.charNVS); // commit to NVS - } -} - -/////////////////////////////// - int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ int nBytes=0; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 1525ef8..1ed6c64 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -266,7 +266,6 @@ struct SpanCharacteristic{ int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.) - void restore(); // loads previous value of Characteristic from NVS (if found) boolean updated(){return(isUpdated);} // returns isUpdated unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated @@ -379,7 +378,7 @@ struct SpanCharacteristic{ } // setRange() - template void init(T val, A min=0, B max=1){ + template void init(T val, boolean nvsStore, A min=0, B max=1){ uvSet(value,val); uvSet(newValue,val); @@ -387,36 +386,55 @@ struct SpanCharacteristic{ uvSet(maxValue,max); uvSet(stepValue,0); - homeSpan.configLog+="(" + uvPrint(value) + ")" + ": IID=" + String(iid) + ", UUID=0x" + String(type); - if(format!=STRING && format!=BOOL) - homeSpan.configLog+= " Range=[" + String(uvPrint(minValue)) + "," + String(uvPrint(maxValue)) + "]"; - - boolean valid=false; - - for(int i=0; !valid && iServices.back()->req.size(); i++) - valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->req[i]->type); - - for(int i=0; !valid && iServices.back()->opt.size(); i++) - valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->opt[i]->type); - - if(!valid){ - homeSpan.configLog+=" *** ERROR! Service does not support this Characteristic. ***"; - homeSpan.nFatalErrors++; - } - - boolean repeated=false; + if(nvsStore){ + nvsKey=(char *)malloc(16); + uint16_t t; + sscanf(type,"%x",&t); + sprintf(nvsKey,"%04X%08X%03X",t,aid,iid&0xFFF); + size_t len; + + if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){ + nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len); + } + else { + nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data + nvs_commit(homeSpan.charNVS); // commit to NVS + } + } - for(int i=0; !repeated && iServices.back()->Characteristics.size(); i++) - repeated=!strcmp(type,homeSpan.Accessories.back()->Services.back()->Characteristics[i]->type); + homeSpan.configLog+="(" + uvPrint(value) + ")" + ": IID=" + String(iid) + ", UUID=0x" + String(type); + if(format!=STRING && format!=BOOL) + homeSpan.configLog+= " Range=[" + String(uvPrint(minValue)) + "," + String(uvPrint(maxValue)) + "]"; + + if(nvsStore) + homeSpan.configLog+=" (restored)"; - if(valid && repeated){ - homeSpan.configLog+=" *** ERROR! Characteristic already defined for this Service. ***"; - homeSpan.nFatalErrors++; - } - - homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this); - - homeSpan.configLog+="\n"; + boolean valid=false; + + for(int i=0; !valid && iServices.back()->req.size(); i++) + valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->req[i]->type); + + for(int i=0; !valid && iServices.back()->opt.size(); i++) + valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->opt[i]->type); + + if(!valid){ + homeSpan.configLog+=" *** ERROR! Service does not support this Characteristic. ***"; + homeSpan.nFatalErrors++; + } + + boolean repeated=false; + + for(int i=0; !repeated && iServices.back()->Characteristics.size(); i++) + repeated=!strcmp(type,homeSpan.Accessories.back()->Services.back()->Characteristics[i]->type); + + if(valid && repeated){ + homeSpan.configLog+=" *** ERROR! Characteristic already defined for this Service. ***"; + homeSpan.nFatalErrors++; + } + + homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this); + + homeSpan.configLog+="\n"; } // init() diff --git a/src/Span.h b/src/Span.h index f8eb3ce..64695af 100644 --- a/src/Span.h +++ b/src/Span.h @@ -384,7 +384,7 @@ namespace Service { // Macro to define Span Characteristic structures based on name of HAP Characteristic, default value, and mix/max value (not applicable for STRING or BOOL which default to min=0, max=1) #define CREATE_CHAR(TYPE,HAPCHAR,DEFVAL,MINVAL,MAXVAL) \ - struct HAPCHAR : SpanCharacteristic { HAPCHAR(TYPE val=DEFVAL) : SpanCharacteristic {&hapChars.HAPCHAR} { init(val,(TYPE)MINVAL,(TYPE)MAXVAL); } }; + struct HAPCHAR : SpanCharacteristic { HAPCHAR(TYPE val=DEFVAL, boolean nvsStore=false) : SpanCharacteristic {&hapChars.HAPCHAR} { init(val,nvsStore,(TYPE)MINVAL,(TYPE)MAXVAL); } }; namespace Characteristic { diff --git a/src/src.ino b/src/src.ino index e103ba0..9e67e91 100644 --- a/src/src.ino +++ b/src/src.ino @@ -39,13 +39,13 @@ void setup() { new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP new Service::LightBulb(); - (new Characteristic::On())->restore(); - (new Characteristic::Brightness())->restore(); + new Characteristic::On(0); + new Characteristic::Brightness(); new Characteristic::Name("Light 1"); new Characteristic::ColorTemperature(); new Service::LightBulb(); - (new Characteristic::On())->restore(); - (new Characteristic::Brightness(50))->setRange(10,100,5)->restore(); + new Characteristic::On(0,true); + (new Characteristic::Brightness(50,true))->setRange(10,100,5); new Characteristic::Name("Light 2"); } // end of setup()