flip_weather_callback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "callback/flip_weather_callback.h"
  2. bool weather_request_success = false;
  3. bool sent_weather_request = false;
  4. bool got_weather_data = false;
  5. void flip_weather_request_error(Canvas *canvas)
  6. {
  7. if (fhttp.last_response != NULL)
  8. {
  9. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  10. {
  11. canvas_clear(canvas);
  12. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  13. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  14. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  15. }
  16. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  17. {
  18. canvas_clear(canvas);
  19. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  20. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  21. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  22. }
  23. else
  24. {
  25. canvas_clear(canvas);
  26. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  27. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  28. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  29. }
  30. }
  31. else
  32. {
  33. canvas_clear(canvas);
  34. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  35. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  36. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  37. }
  38. }
  39. void flip_weather_handle_gps_draw(Canvas *canvas, bool show_gps_data)
  40. {
  41. if (sent_get_request)
  42. {
  43. if (fhttp.state == RECEIVING)
  44. {
  45. if (show_gps_data)
  46. {
  47. canvas_clear(canvas);
  48. canvas_draw_str(canvas, 0, 10, "Loading GPS...");
  49. canvas_draw_str(canvas, 0, 22, "Receiving...");
  50. }
  51. }
  52. // check status
  53. else if (fhttp.state == ISSUE || !get_request_success || fhttp.last_response == NULL)
  54. {
  55. flip_weather_request_error(canvas);
  56. }
  57. else if (fhttp.state == IDLE && fhttp.last_response != NULL)
  58. {
  59. // success, draw GPS
  60. process_geo_location();
  61. if (show_gps_data)
  62. {
  63. canvas_clear(canvas);
  64. canvas_draw_str(canvas, 0, 10, city_data);
  65. canvas_draw_str(canvas, 0, 20, region_data);
  66. canvas_draw_str(canvas, 0, 30, country_data);
  67. canvas_draw_str(canvas, 0, 40, lat_data);
  68. canvas_draw_str(canvas, 0, 50, lon_data);
  69. canvas_draw_str(canvas, 0, 60, ip_data);
  70. }
  71. }
  72. }
  73. }
  74. // Callback for drawing the weather screen
  75. void flip_weather_view_draw_callback_weather(Canvas *canvas, void *model)
  76. {
  77. if (!canvas)
  78. {
  79. return;
  80. }
  81. UNUSED(model);
  82. canvas_set_font(canvas, FontSecondary);
  83. if (fhttp.state == INACTIVE)
  84. {
  85. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  86. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  87. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  88. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  89. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  90. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  91. return;
  92. }
  93. canvas_draw_str(canvas, 0, 10, "Loading Weather...");
  94. // handle geo location until it's processed and then handle weather
  95. // start the process
  96. if (!send_geo_location_request())
  97. {
  98. flip_weather_request_error(canvas);
  99. }
  100. // wait until geo location is processed
  101. if (!sent_get_request || !get_request_success || fhttp.state == RECEIVING)
  102. {
  103. return;
  104. }
  105. // get/set geo lcoation once
  106. if (!geo_information_processed)
  107. {
  108. flip_weather_handle_gps_draw(canvas, false);
  109. }
  110. // start the weather process
  111. if (!sent_weather_request && fhttp.state == IDLE)
  112. {
  113. sent_weather_request = true;
  114. char url[512];
  115. char *lattitude = lat_data + 10;
  116. char *longitude = lon_data + 11;
  117. char *headers = jsmn("Content-Type", "application/json");
  118. 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);
  119. weather_request_success = flipper_http_get_request_with_headers(url, headers);
  120. free(headers);
  121. if (!weather_request_success)
  122. {
  123. FURI_LOG_E(TAG, "Failed to send GET request");
  124. fhttp.state = ISSUE;
  125. flip_weather_request_error(canvas);
  126. }
  127. fhttp.state = RECEIVING;
  128. }
  129. else
  130. {
  131. if (fhttp.state == RECEIVING)
  132. {
  133. canvas_draw_str(canvas, 0, 10, "Loading Weather...");
  134. canvas_draw_str(canvas, 0, 22, "Receiving...");
  135. return;
  136. }
  137. // check status
  138. if (fhttp.state == ISSUE || !weather_request_success || fhttp.last_response == NULL)
  139. {
  140. flip_weather_request_error(canvas);
  141. }
  142. else
  143. {
  144. // success, draw weather
  145. process_weather();
  146. canvas_clear(canvas);
  147. canvas_draw_str(canvas, 0, 10, temperature_data);
  148. canvas_draw_str(canvas, 0, 20, precipitation_data);
  149. canvas_draw_str(canvas, 0, 30, rain_data);
  150. canvas_draw_str(canvas, 0, 40, showers_data);
  151. canvas_draw_str(canvas, 0, 50, snowfall_data);
  152. canvas_draw_str(canvas, 0, 60, time_data);
  153. }
  154. }
  155. }
  156. // Callback for drawing the GPS screen
  157. void flip_weather_view_draw_callback_gps(Canvas *canvas, void *model)
  158. {
  159. if (!canvas)
  160. {
  161. return;
  162. }
  163. UNUSED(model);
  164. if (fhttp.state == INACTIVE)
  165. {
  166. canvas_set_font(canvas, FontSecondary);
  167. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  168. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  169. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  170. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  171. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  172. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  173. return;
  174. }
  175. if (!send_geo_location_request())
  176. {
  177. flip_weather_request_error(canvas);
  178. }
  179. flip_weather_handle_gps_draw(canvas, true);
  180. }
  181. void callback_submenu_choices(void *context, uint32_t index)
  182. {
  183. FlipWeatherApp *app = (FlipWeatherApp *)context;
  184. if (!app)
  185. {
  186. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  187. return;
  188. }
  189. switch (index)
  190. {
  191. case FlipWeatherSubmenuIndexWeather:
  192. if (!flip_weather_handle_ip_address())
  193. {
  194. return;
  195. }
  196. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewWeather);
  197. break;
  198. case FlipWeatherSubmenuIndexGPS:
  199. if (!flip_weather_handle_ip_address())
  200. {
  201. return;
  202. }
  203. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewGPS);
  204. break;
  205. case FlipWeatherSubmenuIndexAbout:
  206. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewAbout);
  207. break;
  208. case FlipWeatherSubmenuIndexSettings:
  209. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215. void text_updated_ssid(void *context)
  216. {
  217. FlipWeatherApp *app = (FlipWeatherApp *)context;
  218. if (!app)
  219. {
  220. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  221. return;
  222. }
  223. // store the entered text
  224. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  225. // Ensure null-termination
  226. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  227. // update the variable item text
  228. if (app->variable_item_ssid)
  229. {
  230. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  231. }
  232. // save settings
  233. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  234. // save wifi settings to devboard
  235. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  236. {
  237. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  238. {
  239. FURI_LOG_E(TAG, "Failed to save wifi settings");
  240. }
  241. }
  242. // switch to the settings view
  243. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  244. }
  245. void text_updated_password(void *context)
  246. {
  247. FlipWeatherApp *app = (FlipWeatherApp *)context;
  248. if (!app)
  249. {
  250. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  251. return;
  252. }
  253. // store the entered text
  254. strncpy(app->uart_text_input_buffer_password, app->uart_text_input_temp_buffer_password, app->uart_text_input_buffer_size_password);
  255. // Ensure null-termination
  256. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  257. // update the variable item text
  258. if (app->variable_item_password)
  259. {
  260. variable_item_set_current_value_text(app->variable_item_password, app->uart_text_input_buffer_password);
  261. }
  262. // save settings
  263. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  264. // save wifi settings to devboard
  265. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  266. {
  267. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  268. {
  269. FURI_LOG_E(TAG, "Failed to save wifi settings");
  270. }
  271. }
  272. // switch to the settings view
  273. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  274. }
  275. uint32_t callback_to_submenu(void *context)
  276. {
  277. if (!context)
  278. {
  279. FURI_LOG_E(TAG, "Context is NULL");
  280. return VIEW_NONE;
  281. }
  282. UNUSED(context);
  283. sent_get_request = false;
  284. get_request_success = false;
  285. got_ip_address = false;
  286. got_weather_data = false;
  287. geo_information_processed = false;
  288. weather_information_processed = false;
  289. sent_weather_request = false;
  290. weather_request_success = false;
  291. return FlipWeatherViewSubmenu;
  292. }
  293. void settings_item_selected(void *context, uint32_t index)
  294. {
  295. FlipWeatherApp *app = (FlipWeatherApp *)context;
  296. if (!app)
  297. {
  298. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  299. return;
  300. }
  301. switch (index)
  302. {
  303. case 0: // Input SSID
  304. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputSSID);
  305. break;
  306. case 1: // Input Password
  307. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputPassword);
  308. break;
  309. default:
  310. FURI_LOG_E(TAG, "Unknown configuration item index");
  311. break;
  312. }
  313. }
  314. /**
  315. * @brief Navigation callback for exiting the application
  316. * @param context The context - unused
  317. * @return next view id (VIEW_NONE to exit the app)
  318. */
  319. uint32_t callback_exit_app(void *context)
  320. {
  321. // Exit the application
  322. if (!context)
  323. {
  324. FURI_LOG_E(TAG, "Context is NULL");
  325. return VIEW_NONE;
  326. }
  327. UNUSED(context);
  328. return VIEW_NONE; // Return VIEW_NONE to exit the app
  329. }
  330. uint32_t callback_to_wifi_settings(void *context)
  331. {
  332. if (!context)
  333. {
  334. FURI_LOG_E(TAG, "Context is NULL");
  335. return VIEW_NONE;
  336. }
  337. UNUSED(context);
  338. return FlipWeatherViewSettings;
  339. }