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.
This commit is contained in:
parent
40798b15cf
commit
f4c9c430ef
|
|
@ -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 SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){
|
||||||
int nBytes=0;
|
int nBytes=0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
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.)
|
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
|
boolean updated(){return(isUpdated);} // returns isUpdated
|
||||||
unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated
|
unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated
|
||||||
|
|
@ -379,7 +378,7 @@ struct SpanCharacteristic{
|
||||||
|
|
||||||
} // setRange()
|
} // setRange()
|
||||||
|
|
||||||
template <typename T, typename A=boolean, typename B=boolean> void init(T val, A min=0, B max=1){
|
template <typename T, typename A=boolean, typename B=boolean> void init(T val, boolean nvsStore, A min=0, B max=1){
|
||||||
|
|
||||||
uvSet(value,val);
|
uvSet(value,val);
|
||||||
uvSet(newValue,val);
|
uvSet(newValue,val);
|
||||||
|
|
@ -387,36 +386,55 @@ struct SpanCharacteristic{
|
||||||
uvSet(maxValue,max);
|
uvSet(maxValue,max);
|
||||||
uvSet(stepValue,0);
|
uvSet(stepValue,0);
|
||||||
|
|
||||||
homeSpan.configLog+="(" + uvPrint(value) + ")" + ": IID=" + String(iid) + ", UUID=0x" + String(type);
|
if(nvsStore){
|
||||||
if(format!=STRING && format!=BOOL)
|
nvsKey=(char *)malloc(16);
|
||||||
homeSpan.configLog+= " Range=[" + String(uvPrint(minValue)) + "," + String(uvPrint(maxValue)) + "]";
|
uint16_t t;
|
||||||
|
sscanf(type,"%x",&t);
|
||||||
boolean valid=false;
|
sprintf(nvsKey,"%04X%08X%03X",t,aid,iid&0xFFF);
|
||||||
|
size_t len;
|
||||||
for(int i=0; !valid && i<homeSpan.Accessories.back()->Services.back()->req.size(); i++)
|
|
||||||
valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->req[i]->type);
|
if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){
|
||||||
|
nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len);
|
||||||
for(int i=0; !valid && i<homeSpan.Accessories.back()->Services.back()->opt.size(); i++)
|
}
|
||||||
valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->opt[i]->type);
|
else {
|
||||||
|
nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data
|
||||||
if(!valid){
|
nvs_commit(homeSpan.charNVS); // commit to NVS
|
||||||
homeSpan.configLog+=" *** ERROR! Service does not support this Characteristic. ***";
|
}
|
||||||
homeSpan.nFatalErrors++;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
boolean repeated=false;
|
|
||||||
|
|
||||||
for(int i=0; !repeated && i<homeSpan.Accessories.back()->Services.back()->Characteristics.size(); i++)
|
homeSpan.configLog+="(" + uvPrint(value) + ")" + ": IID=" + String(iid) + ", UUID=0x" + String(type);
|
||||||
repeated=!strcmp(type,homeSpan.Accessories.back()->Services.back()->Characteristics[i]->type);
|
if(format!=STRING && format!=BOOL)
|
||||||
|
homeSpan.configLog+= " Range=[" + String(uvPrint(minValue)) + "," + String(uvPrint(maxValue)) + "]";
|
||||||
|
|
||||||
|
if(nvsStore)
|
||||||
|
homeSpan.configLog+=" (restored)";
|
||||||
|
|
||||||
if(valid && repeated){
|
boolean valid=false;
|
||||||
homeSpan.configLog+=" *** ERROR! Characteristic already defined for this Service. ***";
|
|
||||||
homeSpan.nFatalErrors++;
|
for(int i=0; !valid && i<homeSpan.Accessories.back()->Services.back()->req.size(); i++)
|
||||||
}
|
valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->req[i]->type);
|
||||||
|
|
||||||
homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this);
|
for(int i=0; !valid && i<homeSpan.Accessories.back()->Services.back()->opt.size(); i++)
|
||||||
|
valid=!strcmp(type,homeSpan.Accessories.back()->Services.back()->opt[i]->type);
|
||||||
homeSpan.configLog+="\n";
|
|
||||||
|
if(!valid){
|
||||||
|
homeSpan.configLog+=" *** ERROR! Service does not support this Characteristic. ***";
|
||||||
|
homeSpan.nFatalErrors++;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean repeated=false;
|
||||||
|
|
||||||
|
for(int i=0; !repeated && i<homeSpan.Accessories.back()->Services.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()
|
} // init()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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)
|
// 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) \
|
#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 {
|
namespace Characteristic {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP
|
||||||
|
|
||||||
new Service::LightBulb();
|
new Service::LightBulb();
|
||||||
(new Characteristic::On())->restore();
|
new Characteristic::On(0);
|
||||||
(new Characteristic::Brightness())->restore();
|
new Characteristic::Brightness();
|
||||||
new Characteristic::Name("Light 1");
|
new Characteristic::Name("Light 1");
|
||||||
new Characteristic::ColorTemperature();
|
new Characteristic::ColorTemperature();
|
||||||
new Service::LightBulb();
|
new Service::LightBulb();
|
||||||
(new Characteristic::On())->restore();
|
new Characteristic::On(0,true);
|
||||||
(new Characteristic::Brightness(50))->setRange(10,100,5)->restore();
|
(new Characteristic::Brightness(50,true))->setRange(10,100,5);
|
||||||
new Characteristic::Name("Light 2");
|
new Characteristic::Name("Light 2");
|
||||||
|
|
||||||
} // end of setup()
|
} // end of setup()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue