flip_weather_parse.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. static bool sent_get_request = false;
  3. static bool get_request_success = false;
  4. static bool got_ip_address = false;
  5. static bool geo_information_processed = false;
  6. static bool weather_information_processed = false;
  7. static bool flip_weather_parse_ip_address() {
  8. // load the received data from the saved file
  9. FuriString* returned_data = flipper_http_load_from_file(fhttp.file_path);
  10. if(returned_data == NULL) {
  11. FURI_LOG_E(TAG, "Failed to load received data from file.");
  12. return false;
  13. }
  14. const char* data_cstr = furi_string_get_cstr(returned_data);
  15. if(data_cstr == NULL) {
  16. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  17. furi_string_free(returned_data);
  18. return false;
  19. }
  20. char* ip = get_json_value("origin", (char*)data_cstr, MAX_TOKENS);
  21. if(ip == NULL) {
  22. FURI_LOG_E(TAG, "Failed to get IP address");
  23. sent_get_request = true;
  24. get_request_success = false;
  25. fhttp.state = ISSUE;
  26. free(ip);
  27. furi_string_free(returned_data);
  28. return false;
  29. }
  30. snprintf(ip_address, 16, "%s", ip);
  31. ip_address[15] = '\0';
  32. free(ip);
  33. furi_string_free(returned_data);
  34. return true;
  35. }
  36. // handle the async-to-sync process to get and set the IP address
  37. static bool flip_weather_handle_ip_address() {
  38. if(!got_ip_address) {
  39. got_ip_address = true;
  40. snprintf(
  41. fhttp.file_path,
  42. sizeof(fhttp.file_path),
  43. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_weather/ip.txt");
  44. fhttp.save_received_data = true;
  45. if(!flipper_http_get_request("https://httpbin.org/get")) {
  46. FURI_LOG_E(TAG, "Failed to get IP address");
  47. return false;
  48. } else {
  49. fhttp.state = RECEIVING;
  50. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  51. }
  52. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  53. // Wait for the feed to be received
  54. furi_delay_ms(100);
  55. }
  56. furi_timer_stop(fhttp.get_timeout_timer);
  57. if(!flip_weather_parse_ip_address()) {
  58. FURI_LOG_E(TAG, "Failed to get IP address");
  59. sent_get_request = true;
  60. get_request_success = false;
  61. fhttp.state = ISSUE;
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. static bool send_geo_location_request() {
  68. if(!sent_get_request && fhttp.state == IDLE) {
  69. sent_get_request = true;
  70. get_request_success = flipper_http_get_request_with_headers(
  71. "https://ipwhois.app/json/", jsmn("Content-Type", "application/json"));
  72. if(!get_request_success) {
  73. FURI_LOG_E(TAG, "Failed to send GET request");
  74. return false;
  75. }
  76. fhttp.state = RECEIVING;
  77. }
  78. return true;
  79. }
  80. static void process_geo_location() {
  81. if(!geo_information_processed && fhttp.last_response != NULL) {
  82. geo_information_processed = true;
  83. char* city = get_json_value("city", fhttp.last_response, MAX_TOKENS);
  84. char* region = get_json_value("region", fhttp.last_response, MAX_TOKENS);
  85. char* country = get_json_value("country", fhttp.last_response, MAX_TOKENS);
  86. char* latitude = get_json_value("latitude", fhttp.last_response, MAX_TOKENS);
  87. char* longitude = get_json_value("longitude", fhttp.last_response, MAX_TOKENS);
  88. snprintf(city_data, 64, "City: %s", city);
  89. snprintf(region_data, 64, "Region: %s", region);
  90. snprintf(country_data, 64, "Country: %s", country);
  91. snprintf(lat_data, 64, "Latitude: %s", latitude);
  92. snprintf(lon_data, 64, "Longitude: %s", longitude);
  93. snprintf(ip_data, 64, "IP Address: %s", ip_address);
  94. fhttp.state = IDLE;
  95. }
  96. }
  97. static void process_weather() {
  98. if(!weather_information_processed && fhttp.last_response != NULL) {
  99. weather_information_processed = true;
  100. char* current_data = get_json_value("current", fhttp.last_response, MAX_TOKENS);
  101. char* temperature = get_json_value("temperature_2m", current_data, MAX_TOKENS);
  102. char* precipitation = get_json_value("precipitation", current_data, MAX_TOKENS);
  103. char* rain = get_json_value("rain", current_data, MAX_TOKENS);
  104. char* showers = get_json_value("showers", current_data, MAX_TOKENS);
  105. char* snowfall = get_json_value("snowfall", current_data, MAX_TOKENS);
  106. char* time = get_json_value("time", current_data, MAX_TOKENS);
  107. // replace the "T" in time with a space
  108. char* ptr = strstr(time, "T");
  109. if(ptr != NULL) {
  110. *ptr = ' ';
  111. }
  112. snprintf(temperature_data, 64, "Temperature (C): %s", temperature);
  113. snprintf(precipitation_data, 64, "Precipitation: %s", precipitation);
  114. snprintf(rain_data, 64, "Rain: %s", rain);
  115. snprintf(showers_data, 64, "Showers: %s", showers);
  116. snprintf(snowfall_data, 64, "Snowfall: %s", snowfall);
  117. snprintf(time_data, 64, "Time: %s", time);
  118. fhttp.state = IDLE;
  119. } else if(!weather_information_processed && fhttp.last_response == NULL) {
  120. FURI_LOG_E(TAG, "Failed to process weather data");
  121. // store error message
  122. snprintf(temperature_data, 64, "Failed. Update WiFi settings.");
  123. fhttp.state = ISSUE;
  124. }
  125. }