flip_weather_callback.h 18 KB

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