Explorar el Código

Add helper function

jblanked hace 1 año
padre
commit
465fb54562
Se han modificado 2 ficheros con 78 adiciones y 0 borrados
  1. 39 0
      assets/FlipperHTTP/flipper_http.h
  2. 39 0
      flipper_http.h

+ 39 - 0
assets/FlipperHTTP/flipper_http.h

@@ -53,6 +53,10 @@ bool flipper_http_post_request_bytes(const char *url, const char *headers, const
 //
 //
 bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
 bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
 static char *trim(const char *str);
 static char *trim(const char *str);
+//
+static bool flipper_http_process_response_async(
+    bool (*http_request)(void),
+    bool (*parse_json)(void));
 
 
 // State variable to track the UART state
 // State variable to track the UART state
 typedef enum
 typedef enum
@@ -1521,4 +1525,39 @@ char *trim(const char *str)
     return trimmed_str;
     return trimmed_str;
 }
 }
 
 
+/**
+ * @brief Process requests and parse JSON data asynchronously
+ * @param http_request The function to send the request
+ * @param parse_json The function to parse the JSON
+ * @return true if successful, false otherwise
+ */
+static bool flipper_http_process_response_async(
+    bool (*http_request)(void),
+    bool (*parse_json)(void))
+{
+    if (http_request()) // start the async request
+    {
+        furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
+        fhttp.state = RECEIVING;
+    }
+    else
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to send request");
+        return false;
+    }
+    while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
+    {
+        // Wait for the request to be received
+        furi_delay_ms(100);
+    }
+    furi_timer_stop(fhttp.get_timeout_timer);
+    if (!parse_json()) // parse the JSON before switching to the view (synchonous)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
+        return false;
+    }
+    furi_timer_stop(fhttp.get_timeout_timer);
+    return true;
+}
+
 #endif // FLIPPER_HTTP_H
 #endif // FLIPPER_HTTP_H

+ 39 - 0
flipper_http.h

@@ -52,6 +52,10 @@ bool flipper_http_post_request_bytes(const char *url, const char *headers, const
 //
 //
 bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
 bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
 static char *trim(const char *str);
 static char *trim(const char *str);
+//
+static bool flipper_http_process_response_async(
+    bool (*http_request)(void),
+    bool (*parse_json)(void));
 
 
 // State variable to track the UART state
 // State variable to track the UART state
 typedef enum
 typedef enum
@@ -1498,4 +1502,39 @@ char *trim(const char *str)
     return trimmed_str;
     return trimmed_str;
 }
 }
 
 
+/**
+ * @brief Process requests and parse JSON data asynchronously
+ * @param http_request The function to send the request
+ * @param parse_json The function to parse the JSON
+ * @return true if successful, false otherwise
+ */
+static bool flipper_http_process_response_async(
+    bool (*http_request)(void),
+    bool (*parse_json)(void))
+{
+    if (http_request()) // start the async request
+    {
+        furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
+        fhttp.state = RECEIVING;
+    }
+    else
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to send request");
+        return false;
+    }
+    while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
+    {
+        // Wait for the request to be received
+        furi_delay_ms(100);
+    }
+    furi_timer_stop(fhttp.get_timeout_timer);
+    if (!parse_json()) // parse the JSON before switching to the view (synchonous)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
+        return false;
+    }
+    furi_timer_stop(fhttp.get_timeout_timer);
+    return true;
+}
+
 #endif // FLIPPER_HTTP_H
 #endif // FLIPPER_HTTP_H