From 8d6db796cb30dd5f35641657662a6e551388a9e8 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 10 Aug 2020 21:59:30 -0500 Subject: [PATCH] Reworking checkEvents() creating code to allow for multiple characteristics to be updated within event() call. To Do: define anogther sensor that supports multiple characteristics. also need to check that "new SpanEvent" works as expected from within a defined service. --- .../13-EventNotifications.ino | 2 +- .../13-EventNotifications/DEV_Temperature.h | 14 ++---- src/HAP.cpp | 23 +++++++++- src/HomeSpan.cpp | 43 +++++++++++++++++++ src/HomeSpan.h | 7 ++- 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/examples/Expert/13-EventNotifications/13-EventNotifications.ino b/examples/Expert/13-EventNotifications/13-EventNotifications.ino index 056d41c..e7af742 100644 --- a/examples/Expert/13-EventNotifications/13-EventNotifications.ino +++ b/examples/Expert/13-EventNotifications/13-EventNotifications.ino @@ -47,7 +47,7 @@ void setup() { new SpanAccessory(); new DEV_Identify("Temp Sensor","HomeSpan","123-ABC","Celsius","0.9",0); new DEV_TempSensor(); // Create a Temperature Sensor - new SpanEvent(60000); + new SpanEvent(5000); } // end of setup() diff --git a/examples/Expert/13-EventNotifications/DEV_Temperature.h b/examples/Expert/13-EventNotifications/DEV_Temperature.h index 5b7c60b..4095f94 100644 --- a/examples/Expert/13-EventNotifications/DEV_Temperature.h +++ b/examples/Expert/13-EventNotifications/DEV_Temperature.h @@ -6,28 +6,20 @@ struct DEV_TempSensor : Service::TemperatureSensor { // A standalone temperature sensor SpanCharacteristic *temp; // reference to the Current Temperature Characteristic - float step=0.5; DEV_TempSensor(ServiceType sType=ServiceType::Regular) : Service::TemperatureSensor(sType){ // constructor() method temp=new Characteristic::CurrentTemperature(); - temp->value.FLOAT=22.0; Serial.print("Configuring Temperature Sensor"); // initialization message Serial.print("\n"); } // end constructor - SpanCharacteristic *event(){ + void event(){ - temp->value.FLOAT+=step; - if(temp->value.FLOAT>28.0) - step=-step; - else if(temp->value.FLOAT<18.0) - step=-step; - - return(temp); - + temp->setVal(22.0); + } // event }; diff --git a/src/HAP.cpp b/src/HAP.cpp index f01149c..0d0ba6c 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1098,8 +1098,27 @@ void HAPClient::checkEvents(){ for(int i=0;ihomeSpan.Events[i]->alarmTime){ // if alarm time has passed + homeSpan.Events[i]->alarmTime=cTime+homeSpan.Events[i]->period; // set new alarm time to current time plus alarm period - SpanCharacteristic *characteristic=homeSpan.Events[i]->service->event(); // check service for new EVENT + homeSpan.Events[i]->service->event(); // check service for new EVENTS + + for(int j=0;jservice->Characteristics.size();j++){ // loop over all characteristics + if(homeSpan.Events[i]->service->Characteristics[j]->isUpdated){ // if characteristic is updated + + Serial.print("Event for aid="); + Serial.print(homeSpan.Events[i]->service->Characteristics[j]->aid); + Serial.print(" iid="); + Serial.print(homeSpan.Events[i]->service->Characteristics[j]->iid); + Serial.print("\n"); + + homeSpan.Events[i]->service->Characteristics[j]->isUpdated=false; // reset isUpdated flag + } + } + } + } +} + +/* if(characteristic){ // if the service has responded with a characteristic to update pObj[nObj].status=StatusCode::OK; // populate pObj pObj[nObj].characteristic=characteristic; @@ -1114,6 +1133,8 @@ void HAPClient::checkEvents(){ } +*/ + ////////////////////////////////////// void HAPClient::checkTimedResets(){ diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index b8840f6..36aaa39 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1054,6 +1054,49 @@ StatusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ return(StatusCode::TBD); } +/////////////////////////////// + +void SpanCharacteristic::setVal(int val){ + + switch(format){ + + case BOOL: + value.BOOL=(boolean)val; + break; + + case INT: + value.INT=(int)val; + break; + + case UINT8: + value.UINT8=(uint8_t)val; + break; + + case UINT16: + value.UINT16=(uint16_t)val; + break; + + case UINT32: + value.UINT32=(uint32_t)val; + break; + + case UINT64: + value.UINT64=(uint64_t)val; + break; + } + + isUpdated=true; + +} + +/////////////////////////////// + +void SpanCharacteristic::setVal(double val){ + + value.FLOAT=(double)val; + isUpdated=true; +} + /////////////////////////////// // SpanTimedReset // /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 4a18f15..a1c66ee 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -102,7 +102,7 @@ struct SpanService{ int sprintfAttributes(char *cBuf); // prints Service JSON records into buf; return number of characters printed, excluding null terminator virtual StatusCode update() {return(StatusCode::OK);} // update Service and return final statusCode based on updated Characteristics - should be overridden by DEVICE-SPECIFIC Services - virtual SpanCharacteristic* event(){return(NULL);} // event generation for Services that create their own events and need to notify HomeKit of a new Characteristic value + virtual void event(){} // event generation for Services that create their own events and need to notify HomeKit of a new Characteristic value(s) }; /////////////////////////////// @@ -151,7 +151,7 @@ struct SpanCharacteristic{ boolean ev[MAX_CONNECTIONS]={false}; // Characteristic Event Notify Enable (per-connection) int aid=0; // Accessory ID - passed through from Service containing this Characteristic - boolean isUpdated=false; // set to true when new value has been requested by PUT /characteristic + boolean isUpdated=false; // set to true when new value has been requested by PUT /characteristic, or when value is updated with setVal() UVal newValue; // the updated value requested by PUT /characteristic SpanService *service=NULL; // pointer to Service containing this Characteristic @@ -172,6 +172,9 @@ struct SpanCharacteristic{ template T getNewVal(){return(getValue(newValue));} // returns UVal newValue template T getValue(UVal v); // returns UVal v + void setVal(int value); // sets value of UVal value for all integer-based Characterstic types + void setVal(double value); // sets value of UVal value for FLOAT Characteristic type + boolean updated(){return(isUpdated);} // returns isUpdated };