From 718c10b7aa89ae4aed8edceb2690ea2b69f2565f Mon Sep 17 00:00:00 2001 From: open Date: Sat, 3 Feb 2024 16:07:29 +0100 Subject: [PATCH] wip: rework the preferences API --- esp32-timed-switch.ino | 71 ++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino index dd34585..0ea2726 100644 --- a/esp32-timed-switch.ino +++ b/esp32-timed-switch.ino @@ -13,7 +13,7 @@ // -------------------------------------------- -const char* html_app="ESP32 timed Switch

Scheduler

"; +const char* html_app="ESP32 timed Switch

Scheduler

"; // PREFERENCES @@ -239,13 +239,19 @@ const char* getPreferenceName( int plug_id){ } } -void saveSchedule( int plug_id, byte schedule[24]){ +void saveSchedule( int plug_id, unsigned int data[24]){ const char* preference_name = getPreferenceName( plug_id ); - preferences.putBytes(preference_name, schedule, sizeof(schedule)); + Serial.print("saving to "); + Serial.println(preference_name); + preferences.putBytes(preference_name, data, sizeof(data)); + } -void sendSchedule(WiFiClient &client, int schedule[24]) { - client.write((const uint8_t*)schedule, 24 * sizeof(int)); +void sendSchedule(WiFiClient &client, int plug_id) { + int buf[24]; + const char* preference_name = getPreferenceName( plug_id ); + preferences.getBytes(preference_name, buf, preferences.getBytesLength(preference_name)); + client.write((const uint8_t*)buf, 24 * sizeof(int)); } // -------------------------------------------- @@ -269,16 +275,18 @@ void setup(){ if (pref_init == false) { Serial.println("preferences not exist"); preferences.putBool("test", true); - preferences.putBytes(RELAY_1_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_1_default)); - preferences.putBytes(RELAY_2_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_3_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_4_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_5_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_6_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_7_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); - preferences.putBytes(RELAY_8_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + /* + saveSchedule(1,scheduler_default_deactivate); + saveSchedule(2,scheduler_default_deactivate); + saveSchedule(3,scheduler_default_deactivate); + saveSchedule(4,scheduler_default_deactivate); + saveSchedule(5,scheduler_default_deactivate); + saveSchedule(6,scheduler_default_deactivate); + saveSchedule(7,scheduler_default_deactivate); + saveSchedule(8,scheduler_default_deactivate); + */ } - bool test = preferences.getBool("test"); + /* preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE)); preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE)); preferences.getBytes(RELAY_3_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_3_SCHEDULE)); @@ -287,6 +295,7 @@ void setup(){ preferences.getBytes(RELAY_6_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_6_SCHEDULE)); preferences.getBytes(RELAY_7_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_7_SCHEDULE)); preferences.getBytes(RELAY_8_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_8_SCHEDULE)); + */ preferences.end(); Serial.println("Setup::PREFERENCES::end"); @@ -339,7 +348,6 @@ void loop(){ run_scheduler(); } - client = http_server.available(); if (!client) { // Serial.println("Server not available... skip..."); @@ -353,9 +361,8 @@ void loop(){ // GET request if (request.indexOf("GET /api/schedule/") >= 0) { - int apiIndex = 4; // request.indexOf("/api/schedule/"); - int startIdIndex = 18; // apiIndex + strlen("/api/schedule/"); - int endIdIndex = 19; // request.indexOf(" ", startIdIndex); + int startIdIndex = 18; + int endIdIndex = 19; String plug_idStr = request.substring(startIdIndex, endIdIndex); int plug_id = plug_idStr.toInt() - 1; // Adjust for 0 index @@ -366,10 +373,7 @@ void loop(){ client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); - - // @todo THIS IS NOT OK - // we need to use the preferences - sendSchedule(client, scheduler_list[plug_id]); + sendSchedule(client, plug_id); } else { client.println("HTTP/1.1 404 Not Found"); client.println("Connection: close"); @@ -396,30 +400,31 @@ void loop(){ Serial.print("plug_idStr "); Serial.println(plug_idStr); int plug_id = plug_idStr.toInt() - 1; - // Force a new line client.readStringUntil('\n'); - // Read the next line which should have the POST body/content String preBody ; while ( preBody = client.readStringUntil('\n') ){ - Serial.print("Read preBody: '"); - Serial.print(preBody); - Serial.println("'"); if( preBody.isEmpty() ){ break; } } Serial.println("Done reading. Going for binary."); - delay(500); - char postBody[24]; - client.readBytes(postBody, 24); - Serial.print("Read postBody: "); - Serial.println(postBody); + + char post_body[24]; + client.readBytes(post_body, 24); + Serial.print("Read post_body: "); + Serial.println(post_body); // Update the schedule for the specified plug if (plug_id >= 0 && plug_id < 8) { - saveSchedule(plug_id, byte( atoi(postBody))); + + unsigned int byte_value[24]; + for (int i=0; i < 24; i++) { + byte_value[i] = (int) post_body[i]; + } + + saveSchedule(plug_id, byte_value ); Serial.println("Schedule updated for plug " + plug_idStr); } else { Serial.println("Invalid plug ID");