flip_weather_parse.c 6.0 KB

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