flip_weather_parse.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 send_geo_location_request() {
  8. if(fhttp.state == INACTIVE) {
  9. FURI_LOG_E(TAG, "Board is INACTIVE");
  10. flipper_http_ping(); // ping the device
  11. fhttp.state = ISSUE;
  12. return false;
  13. }
  14. if(!flipper_http_get_request_with_headers(
  15. "https://ipwhois.app/json/", "{\"Content-Type\": \"application/json\"}")) {
  16. FURI_LOG_E(TAG, "Failed to send GET request");
  17. fhttp.state = ISSUE;
  18. return false;
  19. }
  20. fhttp.state = RECEIVING;
  21. return true;
  22. }
  23. bool send_geo_weather_request(DataLoaderModel* model) {
  24. UNUSED(model);
  25. char url[512];
  26. char* lattitude = lat_data + 10;
  27. char* longitude = lon_data + 11;
  28. snprintf(
  29. url,
  30. 512,
  31. "https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&current=temperature_2m,precipitation,rain,showers,snowfall&temperature_unit=celsius&wind_speed_unit=mph&precipitation_unit=inch&forecast_days=1",
  32. lattitude,
  33. longitude);
  34. if(!flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}")) {
  35. FURI_LOG_E(TAG, "Failed to send GET request");
  36. fhttp.state = ISSUE;
  37. return false;
  38. }
  39. fhttp.state = RECEIVING;
  40. return true;
  41. }
  42. char* process_geo_location(DataLoaderModel* model) {
  43. UNUSED(model);
  44. if(fhttp.last_response != NULL) {
  45. char* city = get_json_value("city", fhttp.last_response, MAX_TOKENS);
  46. char* region = get_json_value("region", fhttp.last_response, MAX_TOKENS);
  47. char* country = get_json_value("country", fhttp.last_response, MAX_TOKENS);
  48. char* latitude = get_json_value("latitude", fhttp.last_response, MAX_TOKENS);
  49. char* longitude = get_json_value("longitude", fhttp.last_response, MAX_TOKENS);
  50. if(city == NULL || region == NULL || country == NULL || latitude == NULL ||
  51. longitude == NULL) {
  52. FURI_LOG_E(TAG, "Failed to get geo location data");
  53. fhttp.state = ISSUE;
  54. return NULL;
  55. }
  56. snprintf(lat_data, sizeof(lat_data), "Latitude: %s", latitude);
  57. snprintf(lon_data, sizeof(lon_data), "Longitude: %s", longitude);
  58. if(!total_data) {
  59. total_data = (char*)malloc(512);
  60. if(!total_data) {
  61. FURI_LOG_E(TAG, "Failed to allocate memory for total_data");
  62. fhttp.state = ISSUE;
  63. return NULL;
  64. }
  65. }
  66. snprintf(
  67. total_data,
  68. 512,
  69. "You are in %s, %s, %s. \nLatitude: %s, Longitude: %s",
  70. city,
  71. region,
  72. country,
  73. latitude,
  74. longitude);
  75. fhttp.state = IDLE;
  76. free(city);
  77. free(region);
  78. free(country);
  79. free(latitude);
  80. free(longitude);
  81. }
  82. return total_data;
  83. }
  84. bool process_geo_location_2() {
  85. if(fhttp.last_response != NULL) {
  86. char* city = get_json_value("city", fhttp.last_response, MAX_TOKENS);
  87. char* region = get_json_value("region", fhttp.last_response, MAX_TOKENS);
  88. char* country = get_json_value("country", fhttp.last_response, MAX_TOKENS);
  89. char* latitude = get_json_value("latitude", fhttp.last_response, MAX_TOKENS);
  90. char* longitude = get_json_value("longitude", fhttp.last_response, MAX_TOKENS);
  91. if(city == NULL || region == NULL || country == NULL || latitude == NULL ||
  92. longitude == NULL) {
  93. FURI_LOG_E(TAG, "Failed to get geo location data");
  94. fhttp.state = ISSUE;
  95. return false;
  96. }
  97. snprintf(lat_data, sizeof(lat_data), "Latitude: %s", latitude);
  98. snprintf(lon_data, sizeof(lon_data), "Longitude: %s", longitude);
  99. fhttp.state = IDLE;
  100. free(city);
  101. free(region);
  102. free(country);
  103. free(latitude);
  104. free(longitude);
  105. return true;
  106. }
  107. return false;
  108. }
  109. char* process_weather(DataLoaderModel* model) {
  110. UNUSED(model);
  111. if(fhttp.last_response != NULL) {
  112. char* current_data = get_json_value("current", fhttp.last_response, MAX_TOKENS);
  113. char* temperature = get_json_value("temperature_2m", current_data, MAX_TOKENS);
  114. char* precipitation = get_json_value("precipitation", current_data, MAX_TOKENS);
  115. char* rain = get_json_value("rain", current_data, MAX_TOKENS);
  116. char* showers = get_json_value("showers", current_data, MAX_TOKENS);
  117. char* snowfall = get_json_value("snowfall", current_data, MAX_TOKENS);
  118. char* time = get_json_value("time", current_data, MAX_TOKENS);
  119. if(current_data == NULL || temperature == NULL || precipitation == NULL || rain == NULL ||
  120. showers == NULL || snowfall == NULL || time == NULL) {
  121. FURI_LOG_E(TAG, "Failed to get weather data");
  122. fhttp.state = ISSUE;
  123. return NULL;
  124. }
  125. // replace the "T" in time with a space
  126. char* ptr = strstr(time, "T");
  127. if(ptr != NULL) {
  128. *ptr = ' ';
  129. }
  130. if(!weather_data) {
  131. weather_data = (char*)malloc(512);
  132. if(!weather_data) {
  133. FURI_LOG_E(TAG, "Failed to allocate memory for weather_data");
  134. fhttp.state = ISSUE;
  135. return NULL;
  136. }
  137. }
  138. snprintf(
  139. weather_data,
  140. 512,
  141. "Temperature: %s C\nPrecipitation: %s\nRain: %s\nShowers: %s\nSnowfall: %s\nTime: %s",
  142. temperature,
  143. precipitation,
  144. rain,
  145. showers,
  146. snowfall,
  147. time);
  148. fhttp.state = IDLE;
  149. free(current_data);
  150. free(temperature);
  151. free(precipitation);
  152. free(rain);
  153. free(showers);
  154. free(snowfall);
  155. free(time);
  156. }
  157. return weather_data;
  158. }