From db620e497e5f2231b6341005b90ac822ba253a98 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 3 Oct 2020 22:21:38 -0500 Subject: [PATCH] Added code to output configuration log and error messages Config Log is output if logLevel>1 OR there is a fatal error in configuration --- src/HomeSpan.cpp | 40 +++++++++++++++++++++++++++------------- src/HomeSpan.h | 2 ++ src/src.ino | 4 ++-- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index d712a54..e597143 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -79,6 +79,16 @@ void Span::poll() { if(!isInitialized){ + if(logLevel>1 || isFatalError){ + Serial.print(configLog); + Serial.print("\n*** End Config Log ***\n"); + } + + if(isFatalError){ + Serial.print("\n*** PROGRAM HALTED DUE TO FATAL ERRORS IN CONFIGURATION! ***\n\n"); + while(1); + } + Serial.print("\n"); nvs_flash_init(); // initialize non-volatile-storage partition in flash @@ -919,6 +929,8 @@ SpanAccessory::SpanAccessory(){ homeSpan.Accessories.push_back(this); aid=homeSpan.Accessories.size(); + + homeSpan.configLog+="+Accessory " + String(aid) + "\n"; } /////////////////////////////// @@ -948,16 +960,17 @@ SpanService::SpanService(const char *type, const char *hapName){ this->type=type; this->hapName=hapName; + homeSpan.configLog+="-->Service " + String(hapName); + if(homeSpan.Accessories.empty()){ - Serial.print("*** FATAL ERROR: Can't create new Service '"); - Serial.print(hapName); - Serial.print("' without a defined Accessory. Program halted!\n\n"); - while(1); + homeSpan.configLog+=" *** ERROR! Missing Accessory! ***\n"; + homeSpan.isFatalError=true; + return; } - + + homeSpan.configLog+="\n"; homeSpan.Accessories.back()->Services.push_back(this); - iid=++(homeSpan.Accessories.back()->iidCount); - + iid=++(homeSpan.Accessories.back()->iidCount); } /////////////////////////////// @@ -1009,20 +1022,21 @@ SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, char *hapName) this->perms=perms; this->hapName=hapName; + homeSpan.configLog+="---->Characteristic " + String(hapName); + if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){ - Serial.print("*** FATAL ERROR: Can't create new Characteristic '"); - Serial.print(hapName); - Serial.print("' without a defined Service. Program halted!\n\n"); - while(1); + homeSpan.configLog+=" *** ERROR! Missing Service! ***\n"; + homeSpan.isFatalError=true; + return; } - + + homeSpan.configLog+="\n"; homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this); iid=++(homeSpan.Accessories.back()->iidCount); service=homeSpan.Accessories.back()->Services.back(); aid=homeSpan.Accessories.back()->aid; ev=(boolean *)calloc(homeSpan.maxConnections,sizeof(boolean)); - } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 3728059..5dd8977 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -53,6 +53,8 @@ struct Span{ 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() boolean isInitialized=false; // flag indicating HomeSpan has been initialized + boolean isFatalError=false; // flag indicating a fatal error in user-defined configuration + String configLog="*** Config Log ***\n\n"; // log of configuration process, including any errors char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED diff --git a/src/src.ino b/src/src.ino index 6911652..87fd0a3 100644 --- a/src/src.ino +++ b/src/src.ino @@ -8,12 +8,12 @@ void setup() { Serial.begin(115200); + homeSpan.setLogLevel(2); + homeSpan.begin(Category::Lighting,"HomeSpan Benchmark"); new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments - new Characteristic::Model("HSL Test"); // Model of the Accessory (arbitrary text string, and can be the same for every Accessory) - new Service::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics new Characteristic::Name("HomeSpan Test"); // Name of the Accessory, which shows up on the HomeKit "tiles", and should be unique across Accessories new Characteristic::Manufacturer("HomeSpan"); // Manufacturer of the Accessory (arbitrary text string, and can be the same for every Accessory)