Add setValidValues(int n, ...) method to Characteristic

Allows user to explicitly set the valid values for a Characteristic of type UINT8.  Throws an error if used with any other Characteristic type.
This commit is contained in:
Gregg 2021-09-05 08:54:34 -05:00
parent 2e9539a115
commit 6a74ce9283
3 changed files with 42 additions and 2 deletions

View File

@ -1600,6 +1600,10 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){
if(uvGet<float>(stepValue)>0)
nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minStep\":%s",uvPrint(stepValue).c_str());
}
if(validValues){
nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"valid-values\":%s",validValues);
}
}
if(desc && (flags&GET_DESC)){
@ -1720,6 +1724,40 @@ unsigned long SpanCharacteristic::timeVal(){
return(homeSpan.snapTime-updateTime);
}
///////////////////////////////
void SpanCharacteristic::setValidValues(int n, ...){
char c[256];
String *s = new String("[");
va_list vl;
va_start(vl,n);
for(int i=0;i<n;i++){
*s+=va_arg(vl,int);
if(i!=n-1)
*s+=",";
}
va_end(vl);
*s+="]";
homeSpan.configLog+=String(" \u2b0c Set Valid Values for ") + String(hapName) + " with IID=" + String(iid);
if(validValues){
sprintf(c," *** ERROR! Valid Values already set for this Characteristic! ***\n");
homeSpan.nFatalErrors++;
} else
if(format!=UINT8){
sprintf(c," *** ERROR! Can't set Valid Values for this Characteristic! ***\n");
homeSpan.nFatalErrors++;
} else {
validValues=s->c_str();
sprintf(c,": ValidValues=%s\n",validValues);
}
homeSpan.configLog+=c;
}
///////////////////////////////
// SpanRange //
///////////////////////////////

View File

@ -256,8 +256,9 @@ struct SpanCharacteristic{
UVal minValue; // Characteristic minimum (not applicable for STRING)
UVal maxValue; // Characteristic maximum (not applicable for STRING)
UVal stepValue; // Characteristic step size (not applicable for STRING)
boolean staticRange; // Flag that indiates whether Range is static and cannot be changed with setRange()
boolean staticRange; // Flag that indicates whether Range is static and cannot be changed with setRange()
boolean customRange=false; // Flag for custom ranges
const char *validValues=NULL; // Optional JSON array of valid values. Applicable only to uint8 Characteristics
boolean *ev; // Characteristic Event Notify Enable (per-connection)
char *nvsKey=NULL; // key for NVS storage of Characteristic value
@ -274,6 +275,7 @@ struct SpanCharacteristic{
boolean updated(){return(isUpdated);} // returns isUpdated
unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated
void setValidValues(int n, ...); // sets a list of 'n' valid values allowed for a Characteristic. Only applicable if format=uint8
String uvPrint(UVal &u){
char c[64];

View File

@ -381,7 +381,7 @@ namespace Service {
// SPAN CHARACTERISTICS (HAP Chapter 9) //
//////////////////////////////////////////
// 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 min/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, boolean nvsStore=false) : SpanCharacteristic {&hapChars.HAPCHAR} { init(val,nvsStore,(TYPE)MINVAL,(TYPE)MAXVAL); } };