From 698592b7a3cf52cf76ff7100582b6028df96ebd7 Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 11 Aug 2020 21:26:13 -0500 Subject: [PATCH] Updated checkTimedResets() converted checkedTimeResets() from two-pass logic that first computes the required size of SpanBuf to a single-pass that utilizes vector instead. Much cleaner and easier. --- .../13-EventNotifications/DEV_Sensors.h | 1 - src/HAP.cpp | 63 +++++++------------ 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/examples/Expert/13-EventNotifications/DEV_Sensors.h b/examples/Expert/13-EventNotifications/DEV_Sensors.h index 8225b51..a914fae 100644 --- a/examples/Expert/13-EventNotifications/DEV_Sensors.h +++ b/examples/Expert/13-EventNotifications/DEV_Sensors.h @@ -51,7 +51,6 @@ struct DEV_AirQualitySensor : Service::AirQualitySensor { // A standalone Ai airQuality->setVal((int)random(1,6)); o3Density->setVal((double)random(200,500)); if(!random(2)){ - Serial.println("HERE\n"); no2Density->setVal((double)random(600,800)); } diff --git a/src/HAP.cpp b/src/HAP.cpp index 9a2332a..88ffa15 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1108,14 +1108,13 @@ void HAPClient::checkEvents(){ 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="); - Serial.print(homeSpan.Events[i]->service->Characteristics[j]->iid); - Serial.print("\n"); + LOG1("Event Notification for aid="); + LOG1(homeSpan.Events[i]->service->Characteristics[j]->aid); + LOG1(" iid="); + LOG1(homeSpan.Events[i]->service->Characteristics[j]->iid); + LOG1("\n"); homeSpan.Events[i]->service->Characteristics[j]->isUpdated=false; // reset isUpdated flag @@ -1133,51 +1132,37 @@ void HAPClient::checkEvents(){ void HAPClient::checkTimedResets(){ - int n=0; - SpanTimedReset *tReset; + unsigned long cTime=millis(); // current time + vector spanBuf; // vector to SpanBuf objects - for(int i=0;icharacteristic->value.BOOL){ // characteristic is off - tReset->start=false; // ensure timer is not started - tReset->trigger=false; // turn off trigger + for(int i=0;icharacteristic->value.BOOL){ // characteristic is off + homeSpan.TimedResets[i]->start=false; // ensure timer is not started } - else if(!tReset->start){ // else characteristic is on but timer is not started - tReset->start=true; // start timer - tReset->alarmTime=millis()+tReset->waitTime; // set alarm time + else if(!homeSpan.TimedResets[i]->start){ // else characteristic is on but timer is not started + homeSpan.TimedResets[i]->start=true; // start timer + homeSpan.TimedResets[i]->alarmTime=cTime+homeSpan.TimedResets[i]->waitTime; // set alarm time } - else if(millis()>tReset->alarmTime){ // else characteristic is on, timer is started, and timer is expired - tReset->trigger=true; // set trigger - n++; // increment number of characteristics found that need to be turned off - } - } - - if(!n) // nothing to do (either no Timed Reset characteristics, or none that need to be turned off) - return; - - SpanBuf pObj[n]; // use a SpanBuf object to load characteristics to be updated - n=0; // reset number of tResets found that need to be turned off - - for(int i=0;itrigger){ // characteristic is triggered + else if(cTime>homeSpan.TimedResets[i]->alarmTime){ // else characteristic is on, timer is started, and timer is expired LOG1("Resetting aid="); - LOG1(tReset->characteristic->aid); + LOG1(homeSpan.TimedResets[i]->characteristic->aid); LOG1(" iid="); - LOG1(tReset->characteristic->iid); + LOG1(homeSpan.TimedResets[i]->characteristic->iid); LOG1("\n"); - memset(&(tReset->characteristic->value),0,sizeof(tReset->characteristic->value)); + memset(&(homeSpan.TimedResets[i]->characteristic->value),0,sizeof(SpanCharacteristic::UVal)); - pObj[n].status=StatusCode::OK; // populate pObj - pObj[n].characteristic=tReset->characteristic; - pObj[n].val=""; // dummy object needed to ensure sprintfNotify knows to consider this "update" - n++; // increment number of characteristics found that need to be turned off + SpanBuf sb; // create SpanBuf object + sb.characteristic=homeSpan.TimedResets[i]->characteristic; // 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); } } - eventNotify(pObj,n); // transmit EVENT Notification for "n" pObj objects + if(spanBuf.size()>0) // if updated items are found + eventNotify(&spanBuf[0],spanBuf.size()); // transmit EVENT Notifications }