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()
This commit is contained in:
Gregg 2020-08-16 08:45:24 -05:00
parent e3d1a9b566
commit 53f51cc11b
5 changed files with 30 additions and 0 deletions

View File

@ -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
};
//////////////////////////////////

View File

@ -93,6 +93,15 @@ void HAPClient::init(){
Serial.print(homeSpan.hapConfig.configNumber);
Serial.print("\n\n");
}
for(int i=0;i<homeSpan.Accessories.size();i++){ // identify all services with over-ridden loop() methods
for(int j=0;j<homeSpan.Accessories[i]->Services.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;i<homeSpan.Loops.size();i++) // loop over all services with over-ridden loop() methods
homeSpan.Loops[i]->loop(); // call the loop() method
}
//////////////////////////////////////
void HAPClient::checkEvents(){
unsigned long cTime=millis(); // current time

View File

@ -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

View File

@ -148,6 +148,7 @@ void Span::poll() {
} // for-loop over connection slots
HAPClient::checkTimedResets();
HAPClient::callServiceLoops();
HAPClient::checkEvents();
HAPClient::checkTimedWrites();

View File

@ -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<SpanAccessory *> Accessories; // vector of pointers to all Accessories
vector<SpanTimedReset *> TimedResets; // vector of pointers to all TimedResets
vector<SpanEvent *> Events; // vector of pointer to all Events
vector<SpanService *> Loops; // vector of pointer to all Services that have over-ridden loop() methods
unordered_map<uint64_t, uint32_t> 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
};
///////////////////////////////