flip_weather_parse.c 6.4 KB

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