Преглед изворни кода

Web Crawler - v0.7

Improved memory allocation
jblanked пре 1 година
родитељ
комит
54cfb749be
9 измењених фајлова са 366 додато и 446 уклоњено
  1. 3 0
      CHANGELOG.md
  2. 1 1
      application.fam
  3. BIN
      assets/.DS_Store
  4. BIN
      assets/FlipperHTTP/.DS_Store
  5. 328 310
      flipper_http.h
  6. 33 3
      web_crawler_callback.h
  7. 0 7
      web_crawler_free.h
  8. 1 1
      web_crawler_i.h
  9. 0 124
      web_crawler_storage.h

+ 3 - 0
CHANGELOG.md

@@ -1,3 +1,6 @@
+## 0.7
+- Improved memory allocation
+
 ## 0.6
 - Added a DOWNLOAD method, which downloads the file at the specified path. This is best used for downloading images, binary files, and other non-text files.
 - Increased buffer size to handle larger responses.

+ 1 - 1
application.fam

@@ -10,5 +10,5 @@ App(
     fap_description="Use Wi-Fi to access the internet and scrape data from the web.",
     fap_author="JBlanked",
     fap_weburl="https://github.com/jblanked/WebCrawler-FlipperZero",
-    fap_version = "0.6",
+    fap_version = "0.7",
 )


BIN
assets/FlipperHTTP/.DS_Store


Разлика између датотеке није приказан због своје велике величине
+ 328 - 310
flipper_http.h


+ 33 - 3
web_crawler_callback.h

@@ -70,6 +70,13 @@ static void web_crawler_view_draw_callback(Canvas *canvas, void *context)
     {
         if (!sent_http_request)
         {
+            snprintf(
+                fhttp.file_path,
+                sizeof(fhttp.file_path),
+                STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/received_data.txt");
+
+            fhttp.save_received_data = true;
+
             if (strstr(app_instance->http_method, "GET") != NULL)
             {
                 canvas_draw_str(canvas, 0, 10, "Sending GET request...");
@@ -1050,20 +1057,43 @@ static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t i
         return;
     }
     UNUSED(index);
-
-    if (!load_received_data(app))
+    widget_reset(app->widget_file_read);
+    // load the received data from the saved file
+    FuriString *received_data = flipper_http_load_from_file(fhttp.file_path);
+    if (received_data == NULL)
+    {
+        FURI_LOG_E(TAG, "Failed to load received data from file.");
+        if (app->widget_file_read)
+        {
+            widget_add_text_scroll_element(
+                app->widget_file_read,
+                0,
+                0,
+                128,
+                64, "File is empty.");
+            view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
+        }
+        return;
+    }
+    const char *data_cstr = furi_string_get_cstr(received_data);
+    if (data_cstr == NULL)
     {
+        FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
+        furi_string_free(received_data);
         if (app->widget_file_read)
         {
-            widget_reset(app->widget_file_read);
             widget_add_text_scroll_element(
                 app->widget_file_read,
                 0,
                 0,
                 128,
                 64, "File is empty.");
+            view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
         }
+        return;
     }
+    widget_add_text_scroll_element(app_instance->widget_file_read, 0, 0, 128, 64, data_cstr);
+    furi_string_free(received_data);
 
     // Set the previous callback to return to Configure screen
     view_set_previous_callback(

+ 0 - 7
web_crawler_free.h

@@ -194,13 +194,6 @@ static void web_crawler_app_free(WebCrawlerApp *app)
         view_dispatcher_remove_view(app->view_dispatcher, WebCrawlerViewRun);
         view_free(app->view_run);
     }
-
-    // diconnect wifi
-    if (!flipper_http_disconnect_wifi())
-    {
-        FURI_LOG_E(TAG, "Failed to disconnect from WiFi");
-    }
-
     // Deinitialize UART
     flipper_http_deinit();
 

+ 1 - 1
web_crawler_i.h

@@ -176,7 +176,7 @@ WebCrawlerApp *web_crawler_app_alloc()
     variable_item_set_current_value_text(app->file_delete_item, ""); // Initialize
 
     // Allocate Submenu views
-    if (!easy_flipper_set_submenu(&app->submenu_main, WebCrawlerViewSubmenuMain, "Web Crawler v0.6", web_crawler_exit_app_callback, &app->view_dispatcher))
+    if (!easy_flipper_set_submenu(&app->submenu_main, WebCrawlerViewSubmenuMain, "Web Crawler v0.7", web_crawler_exit_app_callback, &app->view_dispatcher))
     {
         return NULL;
     }

+ 0 - 124
web_crawler_storage.h

@@ -404,128 +404,4 @@ static bool rename_received_data(const char *old_name, const char *new_name, con
         return renamed;
     }
 }
-
-static bool text_show_read_lines(File *file, FuriString *str_result)
-{
-    // Reset the FuriString to ensure it's empty before reading
-    furi_string_reset(str_result);
-
-    // Define a buffer to hold the read data
-    uint8_t buffer[SHOW_MAX_FILE_SIZE];
-
-    // Read data into the buffer
-    size_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
-    if (storage_file_get_error(file) != FSE_OK)
-    {
-        FURI_LOG_E(TAG, "Error reading from file.");
-        return false;
-    }
-
-    // Append each byte to the FuriString
-    for (size_t i = 0; i < read_count; i++)
-    {
-        furi_string_push_back(str_result, buffer[i]);
-    }
-
-    return true;
-}
-
-static bool load_received_data(WebCrawlerApp *app)
-{
-    if (app == NULL)
-    {
-        FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
-        return false;
-    }
-
-    if (!app->widget_file_read)
-    {
-        FURI_LOG_E(TAG, "Textbox is NULL");
-        return false;
-    }
-
-    // Open the storage record
-    Storage *storage = furi_record_open(RECORD_STORAGE);
-    if (!storage)
-    {
-        FURI_LOG_E(TAG, "Failed to open storage record");
-        return false;
-    }
-
-    // Allocate a file handle
-    File *file = storage_file_alloc(storage);
-    if (!file)
-    {
-        FURI_LOG_E(TAG, "Failed to allocate storage file");
-        furi_record_close(RECORD_STORAGE);
-        return false;
-    }
-
-    // Open the file for reading
-    if (!storage_file_open(file, RECEIVED_DATA_PATH "received_data.txt", FSAM_READ, FSOM_OPEN_EXISTING))
-    {
-        storage_file_free(file);
-        furi_record_close(RECORD_STORAGE);
-        return false; // Return false if the file does not exist
-    }
-
-    // Allocate a FuriString to hold the received data
-    FuriString *str_result = furi_string_alloc();
-    if (!str_result)
-    {
-        FURI_LOG_E(TAG, "Failed to allocate FuriString");
-        storage_file_close(file);
-        storage_file_free(file);
-        furi_record_close(RECORD_STORAGE);
-        return false;
-    }
-
-    // Read data into the FuriString
-    bool read_success = text_show_read_lines(file, str_result);
-    if (!read_success)
-    {
-        FURI_LOG_E(TAG, "Failed to read data from file");
-        furi_string_free(str_result);
-        storage_file_close(file);
-        storage_file_free(file);
-        furi_record_close(RECORD_STORAGE);
-        return false;
-    }
-
-    // Check if there is more data beyond the maximum size
-    char extra_byte;
-    storage_file_read(file, &extra_byte, 1);
-
-    // Retrieve the C-string from FuriString
-    const char *data_cstr = furi_string_get_cstr(str_result);
-    // Set the text box with the received data
-
-    widget_reset(app->widget_file_read);
-
-    if (str_result != NULL)
-    {
-        widget_add_text_scroll_element(
-            app->widget_file_read,
-            0,
-            0,
-            128,
-            64, data_cstr);
-    }
-    else
-    {
-        widget_add_text_scroll_element(
-            app->widget_file_read,
-            0,
-            0,
-            128,
-            64, "File is empty.");
-    }
-    // Clean up
-    furi_string_free(str_result);
-    storage_file_close(file);
-    storage_file_free(file);
-    furi_record_close(RECORD_STORAGE);
-    return true;
-}
-
 #endif // WEB_CRAWLER_STORAGE_H

Неке датотеке нису приказане због велике количине промена