flip_weather_parse.c 5.7 KB

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