flip_weather_callback.h 20 KB

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