flip_weather_callback.c 13 KB

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