flip_weather_callback.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #ifndef FLIP_WEATHER_CALLBACK_H
  2. #define FLIP_WEATHER_CALLBACK_H
  3. static bool sent_get_request = false;
  4. static bool get_request_success = false;
  5. static bool weather_request_success = false;
  6. static bool got_ip_address = false;
  7. static bool sent_weather_request = false;
  8. static bool got_weather_data = false;
  9. static bool geo_information_processed = false;
  10. static bool weather_information_processed = false;
  11. static char ip_address[16];
  12. static char city_data[48];
  13. static char region_data[48];
  14. static char country_data[48];
  15. static char lat_data[32];
  16. static char lon_data[32];
  17. static char ip_data[32];
  18. static char temperature_data[32];
  19. static char precipitation_data[32];
  20. static char rain_data[32];
  21. static char showers_data[32];
  22. static char snowfall_data[32];
  23. static char time_data[32];
  24. #define MAX_TOKENS 64 // Adjust based on expected JSON size (50)
  25. void flip_weather_request_error(Canvas *canvas)
  26. {
  27. if (fhttp.received_data == NULL)
  28. {
  29. if (fhttp.last_response != NULL)
  30. {
  31. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  32. {
  33. canvas_clear(canvas);
  34. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  35. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  36. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  37. }
  38. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  39. {
  40. canvas_clear(canvas);
  41. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  42. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  43. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  44. }
  45. else
  46. {
  47. canvas_clear(canvas);
  48. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  49. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  50. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  51. }
  52. }
  53. else
  54. {
  55. canvas_clear(canvas);
  56. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  57. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  58. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  59. }
  60. }
  61. else
  62. {
  63. canvas_clear(canvas);
  64. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  65. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  66. }
  67. }
  68. static bool send_geo_location_request()
  69. {
  70. if (!sent_get_request && fhttp.state == IDLE)
  71. {
  72. sent_get_request = true;
  73. get_request_success = flipper_http_get_request_with_headers("https://ipwhois.app/json/", "{\"Content-Type\": \"application/json\"}");
  74. if (!get_request_success)
  75. {
  76. FURI_LOG_E(TAG, "Failed to send GET request");
  77. return false;
  78. }
  79. fhttp.state = RECEIVING;
  80. }
  81. return true;
  82. }
  83. static void process_geo_location()
  84. {
  85. if (!geo_information_processed && fhttp.received_data != NULL)
  86. {
  87. geo_information_processed = true;
  88. char *city = get_json_value("city", fhttp.received_data, MAX_TOKENS);
  89. char *region = get_json_value("region", fhttp.received_data, MAX_TOKENS);
  90. char *country = get_json_value("country", fhttp.received_data, MAX_TOKENS);
  91. char *latitude = get_json_value("latitude", fhttp.received_data, MAX_TOKENS);
  92. char *longitude = get_json_value("longitude", fhttp.received_data, MAX_TOKENS);
  93. snprintf(city_data, 64, "City: %s", city);
  94. snprintf(region_data, 64, "Region: %s", region);
  95. snprintf(country_data, 64, "Country: %s", country);
  96. snprintf(lat_data, 64, "Latitude: %s", latitude);
  97. snprintf(lon_data, 64, "Longitude: %s", longitude);
  98. snprintf(ip_data, 64, "IP Address: %s", ip_address);
  99. fhttp.state = IDLE;
  100. }
  101. }
  102. static void process_weather()
  103. {
  104. if (!weather_information_processed && fhttp.received_data != NULL)
  105. {
  106. weather_information_processed = true;
  107. char *current_data = get_json_value("current", fhttp.received_data, MAX_TOKENS);
  108. char *temperature = get_json_value("temperature_2m", current_data, MAX_TOKENS);
  109. char *precipitation = get_json_value("precipitation", current_data, MAX_TOKENS);
  110. char *rain = get_json_value("rain", current_data, MAX_TOKENS);
  111. char *showers = get_json_value("showers", current_data, MAX_TOKENS);
  112. char *snowfall = get_json_value("snowfall", current_data, MAX_TOKENS);
  113. char *time = get_json_value("time", current_data, MAX_TOKENS);
  114. // replace the "T" in time with a space
  115. char *ptr = strstr(time, "T");
  116. if (ptr != NULL)
  117. {
  118. *ptr = ' ';
  119. }
  120. snprintf(temperature_data, 64, "Temperature (C): %s", temperature);
  121. snprintf(precipitation_data, 64, "Precipitation: %s", precipitation);
  122. snprintf(rain_data, 64, "Rain: %s", rain);
  123. snprintf(showers_data, 64, "Showers: %s", showers);
  124. snprintf(snowfall_data, 64, "Snowfall: %s", snowfall);
  125. snprintf(time_data, 64, "Time: %s", time);
  126. fhttp.state = IDLE;
  127. }
  128. else if (!weather_information_processed && fhttp.received_data == NULL)
  129. {
  130. FURI_LOG_E(TAG, "Failed to process weather data");
  131. // store error message
  132. snprintf(temperature_data, 64, "Failed. Update WiFi settings.");
  133. fhttp.state = ISSUE;
  134. }
  135. }
  136. static void flip_weather_handle_gps_draw(Canvas *canvas, bool show_gps_data)
  137. {
  138. if (sent_get_request)
  139. {
  140. if (fhttp.state == RECEIVING)
  141. {
  142. if (show_gps_data)
  143. {
  144. canvas_clear(canvas);
  145. canvas_draw_str(canvas, 0, 10, "Loading GPS...");
  146. canvas_draw_str(canvas, 0, 22, "Receiving...");
  147. }
  148. }
  149. // check status
  150. else if (fhttp.state == ISSUE || !get_request_success || fhttp.received_data == NULL)
  151. {
  152. flip_weather_request_error(canvas);
  153. }
  154. else if (fhttp.state == IDLE && fhttp.received_data != NULL)
  155. {
  156. // success, draw GPS
  157. process_geo_location();
  158. if (show_gps_data)
  159. {
  160. canvas_clear(canvas);
  161. canvas_draw_str(canvas, 0, 10, city_data);
  162. canvas_draw_str(canvas, 0, 20, region_data);
  163. canvas_draw_str(canvas, 0, 30, country_data);
  164. canvas_draw_str(canvas, 0, 40, lat_data);
  165. canvas_draw_str(canvas, 0, 50, lon_data);
  166. canvas_draw_str(canvas, 0, 60, ip_data);
  167. }
  168. }
  169. }
  170. }
  171. // Callback for drawing the weather screen
  172. static void flip_weather_view_draw_callback_weather(Canvas *canvas, void *model)
  173. {
  174. if (!canvas)
  175. {
  176. return;
  177. }
  178. UNUSED(model);
  179. canvas_set_font(canvas, FontSecondary);
  180. if (fhttp.state == INACTIVE)
  181. {
  182. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  183. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  184. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  185. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  186. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  187. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  188. return;
  189. }
  190. canvas_draw_str(canvas, 0, 10, "Loading Weather...");
  191. // handle geo location until it's processed and then handle weather
  192. // start the process
  193. if (!send_geo_location_request())
  194. {
  195. flip_weather_request_error(canvas);
  196. }
  197. // wait until geo location is processed
  198. if (!sent_get_request || !get_request_success || fhttp.state == RECEIVING)
  199. {
  200. return;
  201. }
  202. // get/set geo lcoation once
  203. if (!geo_information_processed)
  204. {
  205. flip_weather_handle_gps_draw(canvas, false);
  206. }
  207. // start the weather process
  208. if (!sent_weather_request && fhttp.state == IDLE)
  209. {
  210. sent_weather_request = true;
  211. char url[256];
  212. char *lattitude = lat_data + 10;
  213. char *longitude = lon_data + 11;
  214. snprintf(url, 256, "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);
  215. weather_request_success = flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}");
  216. if (!weather_request_success)
  217. {
  218. FURI_LOG_E(TAG, "Failed to send GET request");
  219. flip_weather_request_error(canvas);
  220. }
  221. fhttp.state = RECEIVING;
  222. }
  223. else
  224. {
  225. if (fhttp.state == RECEIVING)
  226. {
  227. canvas_draw_str(canvas, 0, 10, "Loading Weather...");
  228. canvas_draw_str(canvas, 0, 22, "Receiving...");
  229. return;
  230. }
  231. // check status
  232. else if (fhttp.state == ISSUE || !weather_request_success || fhttp.received_data == NULL)
  233. {
  234. flip_weather_request_error(canvas);
  235. }
  236. else
  237. {
  238. // success, draw weather
  239. process_weather();
  240. canvas_clear(canvas);
  241. canvas_draw_str(canvas, 0, 10, temperature_data);
  242. canvas_draw_str(canvas, 0, 20, precipitation_data);
  243. canvas_draw_str(canvas, 0, 30, rain_data);
  244. canvas_draw_str(canvas, 0, 40, showers_data);
  245. canvas_draw_str(canvas, 0, 50, snowfall_data);
  246. canvas_draw_str(canvas, 0, 60, time_data);
  247. }
  248. }
  249. }
  250. // Callback for drawing the GPS screen
  251. static void flip_weather_view_draw_callback_gps(Canvas *canvas, void *model)
  252. {
  253. if (!canvas)
  254. {
  255. return;
  256. }
  257. UNUSED(model);
  258. if (fhttp.state == INACTIVE)
  259. {
  260. canvas_set_font(canvas, FontSecondary);
  261. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  262. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  263. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  264. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  265. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  266. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  267. return;
  268. }
  269. if (!send_geo_location_request())
  270. {
  271. flip_weather_request_error(canvas);
  272. }
  273. flip_weather_handle_gps_draw(canvas, true);
  274. }
  275. // Input callback for the view (async input handling)
  276. bool flip_weather_view_input_callback_weather(InputEvent *event, void *context)
  277. {
  278. FlipWeatherApp *app = (FlipWeatherApp *)context;
  279. UNUSED(app);
  280. if (event->type == InputTypePress && event->key == InputKeyBack)
  281. {
  282. // Exit the app when the back button is pressed
  283. // view_dispatcher_stop(app->view_dispatcher);
  284. // return true;
  285. }
  286. return false;
  287. }
  288. // Input callback for the view (async input handling)
  289. bool flip_weather_view_input_callback_gps(InputEvent *event, void *context)
  290. {
  291. FlipWeatherApp *app = (FlipWeatherApp *)context;
  292. UNUSED(app);
  293. if (event->type == InputTypePress && event->key == InputKeyBack)
  294. {
  295. // Exit the app when the back button is pressed
  296. // view_dispatcher_stop(app->view_dispatcher);
  297. // return true;
  298. }
  299. return false;
  300. }
  301. // handle the async-to-sync process to get and set the IP address
  302. static bool flip_weather_handle_ip_address()
  303. {
  304. if (!got_ip_address)
  305. {
  306. got_ip_address = true;
  307. if (!flipper_http_get_request("https://httpbin.org/get"))
  308. {
  309. FURI_LOG_E(TAG, "Failed to get IP address");
  310. return false;
  311. }
  312. else
  313. {
  314. fhttp.state = RECEIVING;
  315. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  316. }
  317. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  318. {
  319. // Wait for the feed to be received
  320. furi_delay_ms(100);
  321. }
  322. furi_timer_stop(fhttp.get_timeout_timer);
  323. ip_address[0] = '\0';
  324. if (fhttp.received_data != NULL)
  325. {
  326. char *ip = get_json_value("origin", fhttp.received_data, MAX_TOKENS);
  327. if (ip == NULL)
  328. {
  329. FURI_LOG_E(TAG, "Failed to get IP address");
  330. sent_get_request = true;
  331. get_request_success = false;
  332. fhttp.state = ISSUE;
  333. return false;
  334. }
  335. strncpy(ip_address, ip, 15);
  336. ip_address[15] = '\0';
  337. }
  338. else
  339. {
  340. FURI_LOG_E(TAG, "Failed to get IP address");
  341. sent_get_request = true;
  342. get_request_success = false;
  343. fhttp.state = ISSUE;
  344. return false;
  345. }
  346. }
  347. return true;
  348. }
  349. static void callback_submenu_choices(void *context, uint32_t index)
  350. {
  351. FlipWeatherApp *app = (FlipWeatherApp *)context;
  352. if (!app)
  353. {
  354. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  355. return;
  356. }
  357. switch (index)
  358. {
  359. case FlipWeatherSubmenuIndexWeather:
  360. if (!flip_weather_handle_ip_address())
  361. {
  362. return;
  363. }
  364. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewWeather);
  365. break;
  366. case FlipWeatherSubmenuIndexGPS:
  367. if (!flip_weather_handle_ip_address())
  368. {
  369. return;
  370. }
  371. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewGPS);
  372. break;
  373. case FlipWeatherSubmenuIndexAbout:
  374. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewAbout);
  375. break;
  376. case FlipWeatherSubmenuIndexSettings:
  377. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. static void text_updated_ssid(void *context)
  384. {
  385. FlipWeatherApp *app = (FlipWeatherApp *)context;
  386. if (!app)
  387. {
  388. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  389. return;
  390. }
  391. // store the entered text
  392. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  393. // Ensure null-termination
  394. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  395. // update the variable item text
  396. if (app->variable_item_ssid)
  397. {
  398. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  399. }
  400. // save settings
  401. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  402. // save wifi settings to devboard
  403. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  404. {
  405. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  406. {
  407. FURI_LOG_E(TAG, "Failed to save wifi settings");
  408. }
  409. }
  410. // switch to the settings view
  411. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  412. }
  413. static void text_updated_password(void *context)
  414. {
  415. FlipWeatherApp *app = (FlipWeatherApp *)context;
  416. if (!app)
  417. {
  418. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  419. return;
  420. }
  421. // store the entered text
  422. strncpy(app->uart_text_input_buffer_password, app->uart_text_input_temp_buffer_password, app->uart_text_input_buffer_size_password);
  423. // Ensure null-termination
  424. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  425. // update the variable item text
  426. if (app->variable_item_password)
  427. {
  428. variable_item_set_current_value_text(app->variable_item_password, app->uart_text_input_buffer_password);
  429. }
  430. // save settings
  431. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  432. // save wifi settings to devboard
  433. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  434. {
  435. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  436. {
  437. FURI_LOG_E(TAG, "Failed to save wifi settings");
  438. }
  439. }
  440. // switch to the settings view
  441. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewSettings);
  442. }
  443. static uint32_t callback_to_submenu(void *context)
  444. {
  445. if (!context)
  446. {
  447. FURI_LOG_E(TAG, "Context is NULL");
  448. return VIEW_NONE;
  449. }
  450. UNUSED(context);
  451. sent_get_request = false;
  452. get_request_success = false;
  453. got_ip_address = false;
  454. got_weather_data = false;
  455. geo_information_processed = false;
  456. weather_information_processed = false;
  457. sent_weather_request = false;
  458. weather_request_success = false;
  459. return FlipWeatherViewSubmenu;
  460. }
  461. static void settings_item_selected(void *context, uint32_t index)
  462. {
  463. FlipWeatherApp *app = (FlipWeatherApp *)context;
  464. if (!app)
  465. {
  466. FURI_LOG_E(TAG, "FlipWeatherApp is NULL");
  467. return;
  468. }
  469. switch (index)
  470. {
  471. case 0: // Input SSID
  472. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputSSID);
  473. break;
  474. case 1: // Input Password
  475. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewTextInputPassword);
  476. break;
  477. default:
  478. FURI_LOG_E(TAG, "Unknown configuration item index");
  479. break;
  480. }
  481. }
  482. /**
  483. * @brief Navigation callback for exiting the application
  484. * @param context The context - unused
  485. * @return next view id (VIEW_NONE to exit the app)
  486. */
  487. static uint32_t callback_exit_app(void *context)
  488. {
  489. // Exit the application
  490. if (!context)
  491. {
  492. FURI_LOG_E(TAG, "Context is NULL");
  493. return VIEW_NONE;
  494. }
  495. UNUSED(context);
  496. return VIEW_NONE; // Return VIEW_NONE to exit the app
  497. }
  498. #endif