From 4f6f2493570571f8ba5b5c5aa8115042600b468b Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 11 Aug 2020 20:57:40 -0500 Subject: [PATCH] completed update to checkEvent() checkEvents() now allows for multiple characteristics to be updated by service->notify(). This change included the use of vector to dynamically create the the temporary storage needed. To do: re-work similar routines to use vector instead of needing to perform two passes each time to gauge size of SpanBuf array needed. --- .../13-EventNotifications/DEV_Sensors.h | 8 ++-- src/HAP.cpp | 38 ++++++++----------- src/HAP.h | 1 - 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/examples/Expert/13-EventNotifications/DEV_Sensors.h b/examples/Expert/13-EventNotifications/DEV_Sensors.h index 3816868..8225b51 100644 --- a/examples/Expert/13-EventNotifications/DEV_Sensors.h +++ b/examples/Expert/13-EventNotifications/DEV_Sensors.h @@ -18,7 +18,7 @@ struct DEV_TempSensor : Service::TemperatureSensor { // A standalone Tempera void event(){ - temp->setVal(22.0); + temp->setVal((double)random(15,25)); } // event }; @@ -48,11 +48,11 @@ struct DEV_AirQualitySensor : Service::AirQualitySensor { // A standalone Ai void event(){ - airQuality->setVal(1); - o3Density->setVal(10.8); + airQuality->setVal((int)random(1,6)); + o3Density->setVal((double)random(200,500)); if(!random(2)){ Serial.println("HERE\n"); - no2Density->setVal(50.3); + no2Density->setVal((double)random(600,800)); } } // event diff --git a/src/HAP.cpp b/src/HAP.cpp index a072177..9a2332a 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1093,8 +1093,7 @@ int HAPClient::putCharacteristicsURL(char *json){ void HAPClient::checkEvents(){ unsigned long cTime=millis(); // current time - int nObj=0; - SpanBuf pObj[homeSpan.Events.size()]; // maximum number of objects needed if every Event is triggered and each requires a Notification + vector spanBuf; // vector to SpanBuf objects for(int i=0;ihomeSpan.Events[i]->alarmTime){ // if alarm time has passed @@ -1104,7 +1103,14 @@ void HAPClient::checkEvents(){ for(int j=0;jservice->Characteristics.size();j++){ // loop over all characteristics if(homeSpan.Events[i]->service->Characteristics[j]->isUpdated){ // if characteristic is updated - + + SpanBuf sb; // create SpanBuf object + sb.characteristic=homeSpan.Events[i]->service->Characteristics[j]; // set characteristic + sb.status=StatusCode::OK; // set status + sb.val=""; // set dummy "val" so that sprintfNotify knows to consider this "update" + + spanBuf.push_back(sb); + Serial.print("Event for aid="); Serial.print(homeSpan.Events[i]->service->Characteristics[j]->aid); Serial.print(" iid="); @@ -1112,29 +1118,17 @@ void HAPClient::checkEvents(){ Serial.print("\n"); homeSpan.Events[i]->service->Characteristics[j]->isUpdated=false; // reset isUpdated flag - } - } - } - } -} + + } // if characteristic is updated + } // characteristic loop + } // alarm triggered + } // events loop -/* - if(characteristic){ // if the service has responded with a characteristic to update - pObj[nObj].status=StatusCode::OK; // populate pObj - pObj[nObj].characteristic=characteristic; - pObj[nObj].val=""; // dummy object needed to ensure sprintfNotify knows to consider this "update" - nObj++; // increment number of characteristics found that need to be turned off - } - } - } - - if(nObj>0) - eventNotify(pObj,nObj); // transmit EVENT Notification for "n" pObj objects + if(spanBuf.size()>0) // if updated items are found + eventNotify(&spanBuf[0],spanBuf.size()); // transmit EVENT Notifications } -*/ - ////////////////////////////////////// void HAPClient::checkTimedResets(){ diff --git a/src/HAP.h b/src/HAP.h index f9de4df..2e1d179 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -114,7 +114,6 @@ struct HAPClient { static void checkTimedResets(); // checks for Timed Resets and reports to controllers as needed (HAP Section 6.8) static void checkEvents(); // checks for Event Notifications and reports to controllers as needed (HAP Section 6.8) static void eventNotify(SpanBuf *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanBuf objects, pObj, with optional flag to ignore a specific client - }; /////////////////////////////////////////////////