flip_weather_callback.h 12 KB

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