From 24cc2486e278e2d743a8044613d52944ff0b1291 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 14 Feb 2021 18:32:54 -0600 Subject: [PATCH] Added Linked Services logic New SpanService method addLink(SpanService *svc), where svc is a pointer to the SpanService that is being specified as a Linked Service for the current Service. addLink() returns a pointer to "this" so may be chained --- src/HomeSpan.cpp | 35 ++++++++++++++++++++++++++++------- src/HomeSpan.h | 2 ++ src/src.ino | 16 ++++++++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index dd4f453..4c30e43 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -831,20 +831,24 @@ void Span::processSerialCommand(const char *c){ Serial.print("\n\n"); char d[]="------------------------------"; - char cBuf[256]; - sprintf(cBuf,"%-30s %s %10s %s %s %s %s\n","Service","Type","AID","IID","Update","Loop","Button"); - Serial.print(cBuf); - sprintf(cBuf,"%.30s %.4s %.10s %.3s %.6s %.4s %.6s\n",d,d,d,d,d,d,d); - Serial.print(cBuf); + Serial.printf("%-30s %s %10s %s %s %s %s %s\n","Service","Type","AID","IID","Update","Loop","Button","Linked Services"); + Serial.printf("%.30s %.4s %.10s %.3s %.6s %.4s %.6s %.15s\n",d,d,d,d,d,d,d,d); for(int i=0;iServices.size();j++){ SpanService *s=Accessories[i]->Services[j]; - sprintf(cBuf,"%-30s %4s %10u %3d %6s %4s %6s\n",s->hapName,s->type,Accessories[i]->aid,s->iid, + Serial.printf("%-30s %4s %10u %3d %6s %4s %6s ",s->hapName,s->type,Accessories[i]->aid,s->iid, (void(*)())(s->*(&SpanService::update))!=(void(*)())(&SpanService::update)?"YES":"NO", (void(*)())(s->*(&SpanService::loop))!=(void(*)())(&SpanService::loop)?"YES":"NO", (void(*)(int,boolean))(s->*(&SpanService::button))!=(void(*)(int,boolean))(&SpanService::button)?"YES":"NO" ); - Serial.print(cBuf); + if(s->linkedServices.empty()) + Serial.print("-"); + for(int k=0;klinkedServices.size();k++){ + Serial.print(s->linkedServices[k]->iid); + if(klinkedServices.size()-1) + Serial.print(","); + } + Serial.print("\n"); } } Serial.print("\n*** End Info ***\n"); @@ -1378,6 +1382,13 @@ SpanService *SpanService::setHidden(){ /////////////////////////////// +SpanService *SpanService::addLink(SpanService *svc){ + linkedServices.push_back(svc); + return(this); +} + +/////////////////////////////// + int SpanService::sprintfAttributes(char *cBuf){ int nBytes=0; @@ -1388,6 +1399,16 @@ int SpanService::sprintfAttributes(char *cBuf){ if(primary) nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,"\"primary\":true,"); + + if(!linkedServices.empty()){ + nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,"\"linked\":["); + for(int i=0;iiid); + if(i+1 Characteristics; // vector of pointers to all Characteristics in this Service vector req; // vector of pointers to all required HAP Characteristic Types for this Service vector opt; // vector of pointers to all optional HAP Characteristic Types for this Service + vector linkedServices; // vector of pointers to any optional linked Services SpanService(const char *type, const char *hapName); SpanService *setPrimary(); // sets the Service Type to be primary and returns pointer to self SpanService *setHidden(); // sets the Service Type to be hidden and returns pointer to self + SpanService *addLink(SpanService *svc); // adds svc as a Linked Service int sprintfAttributes(char *cBuf); // prints Service JSON records into buf; return number of characters printed, excluding null terminator void validate(); // error-checks Service diff --git a/src/src.ino b/src/src.ino index cf8bb6f..434b93d 100644 --- a/src/src.ino +++ b/src/src.ino @@ -20,10 +20,6 @@ void setup() { homeSpan.begin(Category::Lighting,"HomeSpanTest"); - Serial.printf("\nFD_SETSIZE: %d\n",FD_SETSIZE); - Serial.printf("CONFIG_LWIP_MAX_SOCKETS: %d\n",CONFIG_LWIP_MAX_SOCKETS); - Serial.printf("LWIP_SOCKET_OFFSET: %d\n\n",LWIP_SOCKET_OFFSET); - new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments new Service::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics @@ -37,8 +33,16 @@ void setup() { new Service::HAPProtocolInformation(); // Create the HAP Protcol Information Service new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP - new Service::LightBulb(); // Create the Light Bulb Service - new Characteristic::On(); // This Service requires the "On" Characteristic to turn the light on and off + SpanService *v1=new Service::Valve(); + new Characteristic::Active(); + new Characteristic::InUse(); + new Characteristic::ValveType(); + SpanService *v2=new Service::Valve(); + new Characteristic::Active(); + new Characteristic::InUse(); + new Characteristic::ValveType(); + (new Service::Faucet())->addLink(v1)->addLink(v2); + new Characteristic::Active(); } // end of setup()