From 53f51cc11b16d1498372b5a74fd79a2a60ec070e Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 16 Aug 2020 08:45:24 -0500 Subject: [PATCH] Created new framework for Service loops() Service loops() called for only those Services with over-ridden loop() methods. TO DO: update event logic to follow new framework. Vector should point to all CHARACTERISTICS that are updated to setVal() --- .../13-EventNotifications/DEV_Sensors.h | 6 ++++++ src/HAP.cpp | 19 +++++++++++++++++++ src/HAP.h | 1 + src/HomeSpan.cpp | 1 + src/HomeSpan.h | 3 +++ 5 files changed, 30 insertions(+) diff --git a/examples/Expert/13-EventNotifications/DEV_Sensors.h b/examples/Expert/13-EventNotifications/DEV_Sensors.h index 66e74a3..f611988 100644 --- a/examples/Expert/13-EventNotifications/DEV_Sensors.h +++ b/examples/Expert/13-EventNotifications/DEV_Sensors.h @@ -52,6 +52,9 @@ struct DEV_TempSensor : Service::TemperatureSensor { // A standalone Tempera temp->setVal(temperature); // don't forgot to update the temperature Characteristic to the new value! } // event + + void loop(){ + } // loop }; @@ -87,6 +90,9 @@ struct DEV_AirQualitySensor : Service::AirQualitySensor { // A standalone Ai // Note we are NOT updating the Nitrogen Dioxide Density Characteristic. This should therefore remain steady at its initial value of 700.0 } // event + + void loop(){ + } // loop }; ////////////////////////////////// diff --git a/src/HAP.cpp b/src/HAP.cpp index c5a88da..2f051c2 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -93,6 +93,15 @@ void HAPClient::init(){ Serial.print(homeSpan.hapConfig.configNumber); Serial.print("\n\n"); } + + for(int i=0;iServices.size();j++){ + SpanService *s=homeSpan.Accessories[i]->Services[j]; + + if((void*)(s->*(&SpanService::loop)) != (void*)(&SpanService::loop)) // save pointers to services in Loops vector + homeSpan.Loops.push_back(s); + } + } } @@ -1156,6 +1165,16 @@ int HAPClient::putPrepareURL(char *json){ ////////////////////////////////////// +void HAPClient::callServiceLoops(){ + + homeSpan.snapTime=millis(); // snap the current time for use in ALL loop routines + + for(int i=0;iloop(); // call the loop() method +} + +////////////////////////////////////// + void HAPClient::checkEvents(){ unsigned long cTime=millis(); // current time diff --git a/src/HAP.h b/src/HAP.h index ee70e71..d80c341 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -113,6 +113,7 @@ struct HAPClient { static void removeController(uint8_t *id); // removes specific Controller. If no remaining admin Controllers, remove all others (if any) as per HAP requirements. static void printControllers(); // prints IDs of all allocated (paired) Controller static void checkTimedResets(); // checks for Timed Resets and reports to controllers as needed (HAP Section 6.8) + static void callServiceLoops(); // call the loop() method for any Service with that over-rode the default method static void checkEvents(); // checks for Event Notifications and reports to controllers as needed (HAP Section 6.8) static void checkTimedWrites(); // checks for expired Timed Write PIDs, and clears any found (HAP Section 6.7.2.4) 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 diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 12912ec..713619b 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -148,6 +148,7 @@ void Span::poll() { } // for-loop over connection slots HAPClient::checkTimedResets(); + HAPClient::callServiceLoops(); HAPClient::checkEvents(); HAPClient::checkTimedWrites(); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 8fb01bc..8c28048 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -43,6 +43,7 @@ struct Span{ char *hostNameBase; // base of host name of this device - full host name broadcast by Bonjour MDNS will have 6-byte accessoryID as well as '.local' automatically appended char *modelName; // model name of this device - broadcast as Bonjour field "md" char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13) + unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() int resetPin=21; // drive this pin low to "factory" reset NVS data on start-up @@ -50,6 +51,7 @@ struct Span{ vector Accessories; // vector of pointers to all Accessories vector TimedResets; // vector of pointers to all TimedResets vector Events; // vector of pointer to all Events + vector Loops; // vector of pointer to all Services that have over-ridden loop() methods unordered_map TimedWrites; // map of timed-write PIDs and Alarm Times (based on TTLs) void begin(Category catID, @@ -106,6 +108,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 void event(){} // event generation for Services that create their own events and need to notify HomeKit of a new Characteristic value(s) + virtual void loop(){} // loops for each service }; ///////////////////////////////