web_crawler_callback.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. #include <callback/web_crawler_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. bool sent_http_request = false;
  12. bool get_success = false;
  13. bool already_success = false;
  14. static void web_crawler_draw_error(Canvas *canvas)
  15. {
  16. if (!canvas)
  17. {
  18. FURI_LOG_E(TAG, "Canvas is NULL");
  19. return;
  20. }
  21. canvas_clear(canvas);
  22. canvas_set_font(canvas, FontSecondary);
  23. if (fhttp.state == INACTIVE)
  24. {
  25. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  26. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  27. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  28. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  29. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  30. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  31. return;
  32. }
  33. if (fhttp.last_response)
  34. {
  35. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  36. {
  37. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  38. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  39. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  40. return;
  41. }
  42. if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  43. {
  44. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  45. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  46. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  47. return;
  48. }
  49. if (strstr(fhttp.last_response, "[ERROR] GET request failed with error: connection refused") != NULL)
  50. {
  51. canvas_draw_str(canvas, 0, 10, "[ERROR] Connection refused.");
  52. canvas_draw_str(canvas, 0, 50, "Choose another URL.");
  53. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  54. return;
  55. }
  56. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  57. {
  58. canvas_clear(canvas);
  59. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  60. return;
  61. }
  62. canvas_draw_str(canvas, 0, 10, "[ERROR] Failed to sync data.");
  63. canvas_draw_str(canvas, 0, 30, "If this is your third attempt,");
  64. canvas_draw_str(canvas, 0, 40, "it's likely your URL is not");
  65. canvas_draw_str(canvas, 0, 50, "compabilbe or correct.");
  66. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  67. return;
  68. }
  69. canvas_draw_str(canvas, 0, 10, "HTTP request failed.");
  70. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  71. }
  72. void web_crawler_http_method_change(VariableItem *item)
  73. {
  74. uint8_t index = variable_item_get_current_value_index(item);
  75. variable_item_set_current_value_text(item, http_method_names[index]);
  76. // save the http method
  77. if (app_instance)
  78. {
  79. strncpy(app_instance->http_method, http_method_names[index], strlen(http_method_names[index]) + 1);
  80. // save the settings
  81. save_settings(
  82. app_instance->path,
  83. app_instance->ssid,
  84. app_instance->password,
  85. app_instance->file_rename,
  86. app_instance->file_type,
  87. app_instance->http_method,
  88. app_instance->headers,
  89. app_instance->payload);
  90. }
  91. }
  92. static bool web_crawler_fetch(DataLoaderModel *model)
  93. {
  94. UNUSED(model);
  95. if (app_instance->file_type && app_instance->file_rename)
  96. {
  97. snprintf(
  98. fhttp.file_path,
  99. sizeof(fhttp.file_path),
  100. STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/%s%s",
  101. app_instance->file_rename,
  102. app_instance->file_type);
  103. }
  104. else
  105. {
  106. snprintf(
  107. fhttp.file_path,
  108. sizeof(fhttp.file_path),
  109. STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/received_data.txt");
  110. }
  111. if (strstr(app_instance->http_method, "GET") != NULL)
  112. {
  113. fhttp.save_received_data = true;
  114. fhttp.is_bytes_request = false;
  115. // Perform GET request and handle the response
  116. if (app_instance->headers == NULL || app_instance->headers[0] == '\0' || strstr(app_instance->headers, " ") == NULL)
  117. {
  118. get_success = flipper_http_get_request(app_instance->path);
  119. }
  120. else
  121. {
  122. get_success = flipper_http_get_request_with_headers(app_instance->path, app_instance->headers);
  123. }
  124. }
  125. else if (strstr(app_instance->http_method, "POST") != NULL)
  126. {
  127. fhttp.save_received_data = true;
  128. fhttp.is_bytes_request = false;
  129. // Perform POST request and handle the response
  130. get_success = flipper_http_post_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  131. }
  132. else if (strstr(app_instance->http_method, "PUT") != NULL)
  133. {
  134. fhttp.save_received_data = true;
  135. fhttp.is_bytes_request = false;
  136. // Perform PUT request and handle the response
  137. get_success = flipper_http_put_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  138. }
  139. else if (strstr(app_instance->http_method, "DELETE") != NULL)
  140. {
  141. fhttp.save_received_data = true;
  142. fhttp.is_bytes_request = false;
  143. // Perform DELETE request and handle the response
  144. get_success = flipper_http_delete_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  145. }
  146. else
  147. {
  148. fhttp.save_received_data = false;
  149. fhttp.is_bytes_request = true;
  150. // Perform GET request and handle the response
  151. get_success = flipper_http_get_request_bytes(app_instance->path, app_instance->headers);
  152. }
  153. return get_success;
  154. }
  155. static char *web_crawler_parse(DataLoaderModel *model)
  156. {
  157. UNUSED(model);
  158. // there is no parsing since everything is saved to file
  159. return "Data saved to file.\nPress BACK to return.";
  160. }
  161. static void web_crawler_data_switch_to_view(WebCrawlerApp *app)
  162. {
  163. char *title = "GET Request";
  164. if (strstr(app_instance->http_method, "GET") != NULL)
  165. {
  166. title = "GET Request";
  167. }
  168. else if (strstr(app_instance->http_method, "POST") != NULL)
  169. {
  170. title = "POST Request";
  171. }
  172. else if (strstr(app_instance->http_method, "PUT") != NULL)
  173. {
  174. title = "PUT Request";
  175. }
  176. else if (strstr(app_instance->http_method, "DELETE") != NULL)
  177. {
  178. title = "DELETE Request";
  179. }
  180. else
  181. {
  182. title = "File Download";
  183. }
  184. web_crawler_generic_switch_to_view(app, title, web_crawler_fetch, web_crawler_parse, 1, web_crawler_back_to_main_callback, WebCrawlerViewLoader);
  185. }
  186. /**
  187. * @brief Navigation callback to handle exiting from other views to the submenu.
  188. * @param context The context - WebCrawlerApp object.
  189. * @return WebCrawlerViewSubmenu
  190. */
  191. uint32_t web_crawler_back_to_configure_callback(void *context)
  192. {
  193. UNUSED(context);
  194. // free file read widget if it exists
  195. if (app_instance->widget_file_read)
  196. {
  197. widget_reset(app_instance->widget_file_read);
  198. }
  199. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  200. }
  201. /**
  202. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  203. * @param context The context - WebCrawlerApp object.
  204. * @return WebCrawlerViewSubmenu
  205. */
  206. uint32_t web_crawler_back_to_main_callback(void *context)
  207. {
  208. UNUSED(context);
  209. // reset GET request flags
  210. sent_http_request = false;
  211. get_success = false;
  212. already_success = false;
  213. // free file read widget if it exists
  214. if (app_instance->widget_file_read)
  215. {
  216. widget_reset(app_instance->widget_file_read);
  217. }
  218. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  219. }
  220. uint32_t web_crawler_back_to_file_callback(void *context)
  221. {
  222. UNUSED(context);
  223. return WebCrawlerViewVariableItemListFile; // Return to the file submenu
  224. }
  225. uint32_t web_crawler_back_to_wifi_callback(void *context)
  226. {
  227. UNUSED(context);
  228. return WebCrawlerViewVariableItemListWifi; // Return to the wifi submenu
  229. }
  230. uint32_t web_crawler_back_to_request_callback(void *context)
  231. {
  232. UNUSED(context);
  233. return WebCrawlerViewVariableItemListRequest; // Return to the request submenu
  234. }
  235. /**
  236. * @brief Navigation callback to handle exiting the app from the main submenu.
  237. * @param context The context - unused
  238. * @return VIEW_NONE to exit the app
  239. */
  240. uint32_t web_crawler_exit_app_callback(void *context)
  241. {
  242. UNUSED(context);
  243. return VIEW_NONE;
  244. }
  245. /**
  246. * @brief Handle submenu item selection.
  247. * @param context The context - WebCrawlerApp object.
  248. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  249. */
  250. void web_crawler_submenu_callback(void *context, uint32_t index)
  251. {
  252. WebCrawlerApp *app = (WebCrawlerApp *)context;
  253. if (app->view_dispatcher)
  254. {
  255. switch (index)
  256. {
  257. case WebCrawlerSubmenuIndexRun:
  258. sent_http_request = false; // Reset the flag
  259. web_crawler_data_switch_to_view(app);
  260. break;
  261. case WebCrawlerSubmenuIndexAbout:
  262. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewAbout);
  263. break;
  264. case WebCrawlerSubmenuIndexConfig:
  265. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  266. break;
  267. case WebCrawlerSubmenuIndexWifi:
  268. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  269. break;
  270. case WebCrawlerSubmenuIndexRequest:
  271. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  272. break;
  273. case WebCrawlerSubmenuIndexFile:
  274. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  275. break;
  276. default:
  277. FURI_LOG_E(TAG, "Unknown submenu index");
  278. break;
  279. }
  280. }
  281. }
  282. /**
  283. * @brief Configuration enter callback to handle different items.
  284. * @param context The context - WebCrawlerApp object.
  285. * @param index The index of the item that was clicked.
  286. */
  287. void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  288. {
  289. switch (index)
  290. {
  291. case 0: // SSID
  292. web_crawler_setting_item_ssid_clicked(context, index);
  293. break;
  294. case 1: // Password
  295. web_crawler_setting_item_password_clicked(context, index);
  296. break;
  297. default:
  298. FURI_LOG_E(TAG, "Unknown configuration item index");
  299. break;
  300. }
  301. }
  302. /**
  303. * @brief Configuration enter callback to handle different items.
  304. * @param context The context - WebCrawlerApp object.
  305. * @param index The index of the item that was clicked.
  306. */
  307. void web_crawler_file_enter_callback(void *context, uint32_t index)
  308. {
  309. switch (index)
  310. {
  311. case 0: // File Read
  312. web_crawler_setting_item_file_read_clicked(context, index);
  313. break;
  314. case 1: // FIle Type
  315. web_crawler_setting_item_file_type_clicked(context, index);
  316. break;
  317. case 2: // File Rename
  318. web_crawler_setting_item_file_rename_clicked(context, index);
  319. break;
  320. case 3: // File Delete
  321. web_crawler_setting_item_file_delete_clicked(context, index);
  322. break;
  323. default:
  324. FURI_LOG_E(TAG, "Unknown configuration item index");
  325. break;
  326. }
  327. }
  328. /**
  329. * @brief Configuration enter callback to handle different items.
  330. * @param context The context - WebCrawlerApp object.
  331. * @param index The index of the item that was clicked.
  332. */
  333. void web_crawler_request_enter_callback(void *context, uint32_t index)
  334. {
  335. switch (index)
  336. {
  337. case 0: // URL
  338. web_crawler_setting_item_path_clicked(context, index);
  339. break;
  340. case 1:
  341. // HTTP Method
  342. break;
  343. case 2:
  344. // Headers
  345. web_crawler_setting_item_headers_clicked(context, index);
  346. break;
  347. case 3:
  348. // Payload
  349. web_crawler_setting_item_payload_clicked(context, index);
  350. break;
  351. default:
  352. FURI_LOG_E(TAG, "Unknown configuration item index");
  353. break;
  354. }
  355. }
  356. /**
  357. * @brief Callback for when the user finishes entering the URL.
  358. * @param context The context - WebCrawlerApp object.
  359. */
  360. void web_crawler_set_path_updated(void *context)
  361. {
  362. WebCrawlerApp *app = (WebCrawlerApp *)context;
  363. if (!app)
  364. {
  365. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  366. return;
  367. }
  368. if (!app->path || !app->temp_buffer_path || !app->temp_buffer_size_path || !app->path_item)
  369. {
  370. FURI_LOG_E(TAG, "Invalid path buffer");
  371. return;
  372. }
  373. // Store the entered URL from temp_buffer_path to path
  374. strncpy(app->path, app->temp_buffer_path, app->temp_buffer_size_path - 1);
  375. if (app->path_item)
  376. {
  377. variable_item_set_current_value_text(app->path_item, app->path);
  378. // Save the URL to the settings
  379. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  380. }
  381. // Return to the Configure view
  382. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  383. }
  384. /**
  385. * @brief Callback for when the user finishes entering the headers
  386. * @param context The context - WebCrawlerApp object.
  387. */
  388. void web_crawler_set_headers_updated(void *context)
  389. {
  390. WebCrawlerApp *app = (WebCrawlerApp *)context;
  391. if (!app)
  392. {
  393. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  394. return;
  395. }
  396. if (!app->temp_buffer_headers || !app->temp_buffer_size_headers || !app->headers_item)
  397. {
  398. FURI_LOG_E(TAG, "Invalid headers buffer");
  399. return;
  400. }
  401. // Store the entered headers from temp_buffer_headers to headers
  402. strncpy(app->headers, app->temp_buffer_headers, app->temp_buffer_size_headers - 1);
  403. if (app->headers_item)
  404. {
  405. variable_item_set_current_value_text(app->headers_item, app->headers);
  406. // Save the headers to the settings
  407. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  408. }
  409. // Return to the Configure view
  410. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  411. }
  412. /**
  413. * @brief Callback for when the user finishes entering the payload.
  414. * @param context The context - WebCrawlerApp object.
  415. */
  416. void web_crawler_set_payload_updated(void *context)
  417. {
  418. WebCrawlerApp *app = (WebCrawlerApp *)context;
  419. if (!app)
  420. {
  421. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  422. return;
  423. }
  424. if (!app->temp_buffer_payload || !app->temp_buffer_size_payload || !app->payload_item)
  425. {
  426. FURI_LOG_E(TAG, "Invalid payload buffer");
  427. return;
  428. }
  429. // Store the entered payload from temp_buffer_payload to payload
  430. strncpy(app->payload, app->temp_buffer_payload, app->temp_buffer_size_payload - 1);
  431. if (app->payload_item)
  432. {
  433. variable_item_set_current_value_text(app->payload_item, app->payload);
  434. // Save the payload to the settings
  435. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  436. }
  437. // Return to the Configure view
  438. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  439. }
  440. /**
  441. * @brief Callback for when the user finishes entering the SSID.
  442. * @param context The context - WebCrawlerApp object.
  443. */
  444. void web_crawler_set_ssid_updated(void *context)
  445. {
  446. WebCrawlerApp *app = (WebCrawlerApp *)context;
  447. if (!app)
  448. {
  449. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  450. return;
  451. }
  452. if (!app->temp_buffer_ssid || !app->temp_buffer_size_ssid || !app->ssid || !app->ssid_item)
  453. {
  454. FURI_LOG_E(TAG, "Invalid SSID buffer");
  455. return;
  456. }
  457. // Store the entered SSID from temp_buffer_ssid to ssid
  458. strncpy(app->ssid, app->temp_buffer_ssid, app->temp_buffer_size_ssid - 1);
  459. if (app->ssid_item)
  460. {
  461. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  462. // Save the SSID to the settings
  463. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  464. // send to UART
  465. if (!flipper_http_save_wifi(app->ssid, app->password))
  466. {
  467. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  468. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  469. }
  470. }
  471. // Return to the Configure view
  472. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  473. }
  474. /**
  475. * @brief Callback for when the user finishes entering the Password.
  476. * @param context The context - WebCrawlerApp object.
  477. */
  478. void web_crawler_set_password_update(void *context)
  479. {
  480. WebCrawlerApp *app = (WebCrawlerApp *)context;
  481. if (!app)
  482. {
  483. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  484. return;
  485. }
  486. if (!app->temp_buffer_password || !app->temp_buffer_size_password || !app->password || !app->password_item)
  487. {
  488. FURI_LOG_E(TAG, "Invalid password buffer");
  489. return;
  490. }
  491. // Store the entered Password from temp_buffer_password to password
  492. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  493. if (app->password_item)
  494. {
  495. variable_item_set_current_value_text(app->password_item, app->password);
  496. // Save the Password to the settings
  497. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  498. // send to UART
  499. if (!flipper_http_save_wifi(app->ssid, app->password))
  500. {
  501. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  502. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  503. }
  504. }
  505. // Return to the Configure view
  506. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  507. }
  508. /**
  509. * @brief Callback for when the user finishes entering the File Type.
  510. * @param context The context - WebCrawlerApp object.
  511. */
  512. void web_crawler_set_file_type_update(void *context)
  513. {
  514. WebCrawlerApp *app = (WebCrawlerApp *)context;
  515. if (!app)
  516. {
  517. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  518. return;
  519. }
  520. if (!app->temp_buffer_file_type || !app->temp_buffer_size_file_type || !app->file_type || !app->file_type_item)
  521. {
  522. FURI_LOG_E(TAG, "Invalid file type buffer");
  523. return;
  524. }
  525. // Temporary buffer to store the old name
  526. char old_file_type[256];
  527. strncpy(old_file_type, app->file_type, sizeof(old_file_type) - 1);
  528. old_file_type[sizeof(old_file_type) - 1] = '\0'; // Null-terminate
  529. strncpy(app->file_type, app->temp_buffer_file_type, app->temp_buffer_size_file_type - 1);
  530. if (app->file_type_item)
  531. {
  532. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  533. // Save the File Type to the settings
  534. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  535. }
  536. rename_received_data(app->file_rename, app->file_rename, app->file_type, old_file_type);
  537. // set the file path for fhttp.file_path
  538. if (app->file_rename && app->file_type)
  539. {
  540. char file_path[256];
  541. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  542. file_path[sizeof(file_path) - 1] = '\0'; // Null-terminate
  543. strncpy(fhttp.file_path, file_path, sizeof(fhttp.file_path) - 1);
  544. fhttp.file_path[sizeof(fhttp.file_path) - 1] = '\0'; // Null-terminate
  545. }
  546. // Return to the Configure view
  547. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  548. }
  549. /**
  550. * @brief Callback for when the user finishes entering the File Rename.
  551. * @param context The context - WebCrawlerApp object.
  552. */
  553. void web_crawler_set_file_rename_update(void *context)
  554. {
  555. WebCrawlerApp *app = (WebCrawlerApp *)context;
  556. if (!app)
  557. {
  558. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  559. return;
  560. }
  561. if (!app->temp_buffer_file_rename || !app->temp_buffer_size_file_rename || !app->file_rename || !app->file_rename_item)
  562. {
  563. FURI_LOG_E(TAG, "Invalid file rename buffer");
  564. return;
  565. }
  566. // Temporary buffer to store the old name
  567. char old_name[256];
  568. // Ensure that app->file_rename is null-terminated
  569. strncpy(old_name, app->file_rename, sizeof(old_name) - 1);
  570. old_name[sizeof(old_name) - 1] = '\0'; // Null-terminate
  571. // Store the entered File Rename from temp_buffer_file_rename to file_rename
  572. strncpy(app->file_rename, app->temp_buffer_file_rename, app->temp_buffer_size_file_rename - 1);
  573. if (app->file_rename_item)
  574. {
  575. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  576. // Save the File Rename to the settings
  577. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  578. }
  579. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  580. // set the file path for fhttp.file_path
  581. if (app->file_rename && app->file_type)
  582. {
  583. char file_path[256];
  584. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  585. file_path[sizeof(file_path) - 1] = '\0'; // Null-terminate
  586. strncpy(fhttp.file_path, file_path, sizeof(fhttp.file_path) - 1);
  587. fhttp.file_path[sizeof(fhttp.file_path) - 1] = '\0'; // Null-terminate
  588. }
  589. // Return to the Configure view
  590. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  591. }
  592. /**
  593. * @brief Handler for Path configuration item click.
  594. * @param context The context - WebCrawlerApp object.
  595. * @param index The index of the item that was clicked.
  596. */
  597. void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  598. {
  599. WebCrawlerApp *app = (WebCrawlerApp *)context;
  600. if (!app)
  601. {
  602. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  603. return;
  604. }
  605. if (!app->text_input_path)
  606. {
  607. FURI_LOG_E(TAG, "Text input is NULL");
  608. return;
  609. }
  610. UNUSED(index);
  611. // Initialize temp_buffer with existing path
  612. if (app->path && strlen(app->path) > 0)
  613. {
  614. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  615. }
  616. else
  617. {
  618. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  619. }
  620. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  621. // Configure the text input
  622. bool clear_previous_text = false;
  623. text_input_set_result_callback(
  624. app->text_input_path,
  625. web_crawler_set_path_updated,
  626. app,
  627. app->temp_buffer_path,
  628. app->temp_buffer_size_path,
  629. clear_previous_text);
  630. // Set the previous callback to return to Configure screen
  631. view_set_previous_callback(
  632. text_input_get_view(app->text_input_path),
  633. web_crawler_back_to_request_callback);
  634. // Show text input dialog
  635. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  636. }
  637. /**
  638. * @brief Handler for headers configuration item click.
  639. * @param context The context - WebCrawlerApp object.
  640. * @param index The index of the item that was clicked.
  641. */
  642. void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  643. {
  644. WebCrawlerApp *app = (WebCrawlerApp *)context;
  645. if (!app)
  646. {
  647. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  648. return;
  649. }
  650. UNUSED(index);
  651. if (!app->text_input_headers)
  652. {
  653. FURI_LOG_E(TAG, "Text input is NULL");
  654. return;
  655. }
  656. if (!app->headers)
  657. {
  658. FURI_LOG_E(TAG, "Headers is NULL");
  659. return;
  660. }
  661. if (!app->temp_buffer_headers)
  662. {
  663. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  664. return;
  665. }
  666. // Initialize temp_buffer with existing headers
  667. if (app->headers && strlen(app->headers) > 0)
  668. {
  669. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  670. }
  671. else
  672. {
  673. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  674. }
  675. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  676. // Configure the text input
  677. bool clear_previous_text = false;
  678. text_input_set_result_callback(
  679. app->text_input_headers,
  680. web_crawler_set_headers_updated,
  681. app,
  682. app->temp_buffer_headers,
  683. app->temp_buffer_size_headers,
  684. clear_previous_text);
  685. // Set the previous callback to return to Configure screen
  686. view_set_previous_callback(
  687. text_input_get_view(app->text_input_headers),
  688. web_crawler_back_to_request_callback);
  689. // Show text input dialog
  690. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputHeaders);
  691. }
  692. /**
  693. * @brief Handler for payload configuration item click.
  694. * @param context The context - WebCrawlerApp object.
  695. * @param index The index of the item that was clicked.
  696. */
  697. void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  698. {
  699. WebCrawlerApp *app = (WebCrawlerApp *)context;
  700. if (!app)
  701. {
  702. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  703. return;
  704. }
  705. UNUSED(index);
  706. if (!app->text_input_payload)
  707. {
  708. FURI_LOG_E(TAG, "Text input is NULL");
  709. return;
  710. }
  711. // Initialize temp_buffer with existing payload
  712. if (app->payload && strlen(app->payload) > 0)
  713. {
  714. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  715. }
  716. else
  717. {
  718. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  719. }
  720. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  721. // Configure the text input
  722. bool clear_previous_text = false;
  723. text_input_set_result_callback(
  724. app->text_input_payload,
  725. web_crawler_set_payload_updated,
  726. app,
  727. app->temp_buffer_payload,
  728. app->temp_buffer_size_payload,
  729. clear_previous_text);
  730. // Set the previous callback to return to Configure screen
  731. view_set_previous_callback(
  732. text_input_get_view(app->text_input_payload),
  733. web_crawler_back_to_request_callback);
  734. // Show text input dialog
  735. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPayload);
  736. }
  737. /**
  738. * @brief Handler for SSID configuration item click.
  739. * @param context The context - WebCrawlerApp object.
  740. * @param index The index of the item that was clicked.
  741. */
  742. void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  743. {
  744. WebCrawlerApp *app = (WebCrawlerApp *)context;
  745. if (!app)
  746. {
  747. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  748. return;
  749. }
  750. UNUSED(index);
  751. if (!app->text_input_ssid)
  752. {
  753. FURI_LOG_E(TAG, "Text input is NULL");
  754. return;
  755. }
  756. // Initialize temp_buffer with existing SSID
  757. if (app->ssid && strlen(app->ssid) > 0)
  758. {
  759. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  760. }
  761. else
  762. {
  763. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  764. }
  765. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  766. // Configure the text input
  767. bool clear_previous_text = false;
  768. text_input_set_result_callback(
  769. app->text_input_ssid,
  770. web_crawler_set_ssid_updated,
  771. app,
  772. app->temp_buffer_ssid,
  773. app->temp_buffer_size_ssid,
  774. clear_previous_text);
  775. // Set the previous callback to return to Configure screen
  776. view_set_previous_callback(
  777. text_input_get_view(app->text_input_ssid),
  778. web_crawler_back_to_wifi_callback);
  779. // Show text input dialog
  780. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  781. }
  782. /**
  783. * @brief Handler for Password configuration item click.
  784. * @param context The context - WebCrawlerApp object.
  785. * @param index The index of the item that was clicked.
  786. */
  787. void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  788. {
  789. WebCrawlerApp *app = (WebCrawlerApp *)context;
  790. if (!app)
  791. {
  792. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  793. return;
  794. }
  795. UNUSED(index);
  796. if (!app->text_input_password)
  797. {
  798. FURI_LOG_E(TAG, "Text input is NULL");
  799. return;
  800. }
  801. // Initialize temp_buffer with existing password
  802. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  803. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  804. // Configure the text input
  805. bool clear_previous_text = false;
  806. text_input_set_result_callback(
  807. app->text_input_password,
  808. web_crawler_set_password_update,
  809. app,
  810. app->temp_buffer_password,
  811. app->temp_buffer_size_password,
  812. clear_previous_text);
  813. // Set the previous callback to return to Configure screen
  814. view_set_previous_callback(
  815. text_input_get_view(app->text_input_password),
  816. web_crawler_back_to_wifi_callback);
  817. // Show text input dialog
  818. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  819. }
  820. /**
  821. * @brief Handler for File Type configuration item click.
  822. * @param context The context - WebCrawlerApp object.
  823. * @param index The index of the item that was clicked.
  824. */
  825. void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  826. {
  827. WebCrawlerApp *app = (WebCrawlerApp *)context;
  828. if (!app)
  829. {
  830. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  831. return;
  832. }
  833. UNUSED(index);
  834. if (!app->text_input_file_type)
  835. {
  836. FURI_LOG_E(TAG, "Text input is NULL");
  837. return;
  838. }
  839. // Initialize temp_buffer with existing file_type
  840. if (app->file_type && strlen(app->file_type) > 0)
  841. {
  842. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  843. }
  844. else
  845. {
  846. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  847. }
  848. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  849. // Configure the text input
  850. bool clear_previous_text = false;
  851. text_input_set_result_callback(
  852. app->text_input_file_type,
  853. web_crawler_set_file_type_update,
  854. app,
  855. app->temp_buffer_file_type,
  856. app->temp_buffer_size_file_type,
  857. clear_previous_text);
  858. // Set the previous callback to return to Configure screen
  859. view_set_previous_callback(
  860. text_input_get_view(app->text_input_file_type),
  861. web_crawler_back_to_file_callback);
  862. // Show text input dialog
  863. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileType);
  864. }
  865. /**
  866. * @brief Handler for File Rename configuration item click.
  867. * @param context The context - WebCrawlerApp object.
  868. * @param index The index of the item that was clicked.
  869. */
  870. void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  871. {
  872. WebCrawlerApp *app = (WebCrawlerApp *)context;
  873. if (!app)
  874. {
  875. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  876. return;
  877. }
  878. UNUSED(index);
  879. if (!app->text_input_file_rename)
  880. {
  881. FURI_LOG_E(TAG, "Text input is NULL");
  882. return;
  883. }
  884. // Initialize temp_buffer with existing file_rename
  885. if (app->file_rename && strlen(app->file_rename) > 0)
  886. {
  887. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  888. }
  889. else
  890. {
  891. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  892. }
  893. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  894. // Configure the text input
  895. bool clear_previous_text = false;
  896. text_input_set_result_callback(
  897. app->text_input_file_rename,
  898. web_crawler_set_file_rename_update,
  899. app,
  900. app->temp_buffer_file_rename,
  901. app->temp_buffer_size_file_rename,
  902. clear_previous_text);
  903. // Set the previous callback to return to Configure screen
  904. view_set_previous_callback(
  905. text_input_get_view(app->text_input_file_rename),
  906. web_crawler_back_to_file_callback);
  907. // Show text input dialog
  908. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileRename);
  909. }
  910. /**
  911. * @brief Handler for File Delete configuration item click.
  912. * @param context The context - WebCrawlerApp object.
  913. * @param index The index of the item that was clicked.
  914. */
  915. void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  916. {
  917. WebCrawlerApp *app = (WebCrawlerApp *)context;
  918. if (!app)
  919. {
  920. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  921. return;
  922. }
  923. UNUSED(index);
  924. if (!delete_received_data(app))
  925. {
  926. FURI_LOG_E(TAG, "Failed to delete file");
  927. }
  928. // Set the previous callback to return to Configure screen
  929. view_set_previous_callback(
  930. widget_get_view(app->widget_file_delete),
  931. web_crawler_back_to_file_callback);
  932. // Show text input dialog
  933. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileDelete);
  934. }
  935. void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  936. {
  937. WebCrawlerApp *app = (WebCrawlerApp *)context;
  938. if (!app)
  939. {
  940. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  941. return;
  942. }
  943. UNUSED(index);
  944. widget_reset(app->widget_file_read);
  945. if (app->file_rename && app->file_type)
  946. {
  947. snprintf(fhttp.file_path, sizeof(fhttp.file_path), "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  948. }
  949. else
  950. {
  951. snprintf(fhttp.file_path, sizeof(fhttp.file_path), "%s%s%s", RECEIVED_DATA_PATH, "received_data", ".txt");
  952. }
  953. // load the received data from the saved file
  954. FuriString *received_data = flipper_http_load_from_file(fhttp.file_path);
  955. if (received_data == NULL)
  956. {
  957. FURI_LOG_E(TAG, "Failed to load received data from file.");
  958. if (app->widget_file_read)
  959. {
  960. widget_add_text_scroll_element(
  961. app->widget_file_read,
  962. 0,
  963. 0,
  964. 128,
  965. 64, "File is empty.");
  966. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  967. }
  968. return;
  969. }
  970. const char *data_cstr = furi_string_get_cstr(received_data);
  971. if (data_cstr == NULL)
  972. {
  973. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  974. furi_string_free(received_data);
  975. if (app->widget_file_read)
  976. {
  977. widget_add_text_scroll_element(
  978. app->widget_file_read,
  979. 0,
  980. 0,
  981. 128,
  982. 64, "File is empty.");
  983. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  984. }
  985. return;
  986. }
  987. widget_add_text_scroll_element(app_instance->widget_file_read, 0, 0, 128, 64, data_cstr);
  988. furi_string_free(received_data);
  989. // Set the previous callback to return to Configure screen
  990. view_set_previous_callback(
  991. widget_get_view(app->widget_file_read),
  992. web_crawler_back_to_file_callback);
  993. // Show text input dialog
  994. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  995. }
  996. static void web_crawler_widget_set_text(char *message, Widget **widget)
  997. {
  998. if (widget == NULL)
  999. {
  1000. FURI_LOG_E(TAG, "flip_weather_set_widget_text - widget is NULL");
  1001. DEV_CRASH();
  1002. return;
  1003. }
  1004. if (message == NULL)
  1005. {
  1006. FURI_LOG_E(TAG, "flip_weather_set_widget_text - message is NULL");
  1007. DEV_CRASH();
  1008. return;
  1009. }
  1010. widget_reset(*widget);
  1011. uint32_t message_length = strlen(message); // Length of the message
  1012. uint32_t i = 0; // Index tracker
  1013. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1014. char *formatted_message; // Buffer to hold the final formatted message
  1015. // Allocate buffer with double the message length plus one for safety
  1016. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1017. {
  1018. return;
  1019. }
  1020. while (i < message_length)
  1021. {
  1022. uint32_t max_line_length = 31; // Maximum characters per line
  1023. uint32_t remaining_length = message_length - i; // Remaining characters
  1024. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1025. // Check for newline character within the current segment
  1026. uint32_t newline_pos = i;
  1027. bool found_newline = false;
  1028. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1029. {
  1030. if (message[newline_pos] == '\n')
  1031. {
  1032. found_newline = true;
  1033. break;
  1034. }
  1035. }
  1036. if (found_newline)
  1037. {
  1038. // If newline found, set line_length up to the newline
  1039. line_length = newline_pos - i;
  1040. }
  1041. // Temporary buffer to hold the current line
  1042. char line[32];
  1043. strncpy(line, message + i, line_length);
  1044. line[line_length] = '\0';
  1045. // If newline was found, skip it for the next iteration
  1046. if (found_newline)
  1047. {
  1048. i += line_length + 1; // +1 to skip the '\n' character
  1049. }
  1050. else
  1051. {
  1052. // Check if the line ends in the middle of a word and adjust accordingly
  1053. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1054. {
  1055. // Find the last space within the current line to avoid breaking a word
  1056. char *last_space = strrchr(line, ' ');
  1057. if (last_space != NULL)
  1058. {
  1059. // Adjust the line_length to avoid cutting the word
  1060. line_length = last_space - line;
  1061. line[line_length] = '\0'; // Null-terminate at the space
  1062. }
  1063. }
  1064. // Move the index forward by the determined line_length
  1065. i += line_length;
  1066. // Skip any spaces at the beginning of the next line
  1067. while (i < message_length && message[i] == ' ')
  1068. {
  1069. i++;
  1070. }
  1071. }
  1072. // Manually copy the fixed line into the formatted_message buffer
  1073. for (uint32_t j = 0; j < line_length; j++)
  1074. {
  1075. formatted_message[formatted_index++] = line[j];
  1076. }
  1077. // Add a newline character for line spacing
  1078. formatted_message[formatted_index++] = '\n';
  1079. }
  1080. // Null-terminate the formatted_message
  1081. formatted_message[formatted_index] = '\0';
  1082. // Add the formatted message to the widget
  1083. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1084. }
  1085. void web_crawler_loader_draw_callback(Canvas *canvas, void *model)
  1086. {
  1087. if (!canvas || !model)
  1088. {
  1089. FURI_LOG_E(TAG, "web_crawler_loader_draw_callback - canvas or model is NULL");
  1090. return;
  1091. }
  1092. SerialState http_state = fhttp.state;
  1093. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1094. DataState data_state = data_loader_model->data_state;
  1095. char *title = data_loader_model->title;
  1096. canvas_set_font(canvas, FontSecondary);
  1097. if (http_state == INACTIVE)
  1098. {
  1099. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1100. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1101. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1102. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1103. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1104. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1105. return;
  1106. }
  1107. if (data_state == DataStateError || data_state == DataStateParseError)
  1108. {
  1109. web_crawler_draw_error(canvas);
  1110. return;
  1111. }
  1112. canvas_draw_str(canvas, 0, 7, title);
  1113. canvas_draw_str(canvas, 0, 17, "Loading...");
  1114. if (data_state == DataStateInitial)
  1115. {
  1116. return;
  1117. }
  1118. if (http_state == SENDING)
  1119. {
  1120. canvas_draw_str(canvas, 0, 27, "Sending...");
  1121. return;
  1122. }
  1123. if (http_state == RECEIVING || data_state == DataStateRequested)
  1124. {
  1125. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1126. return;
  1127. }
  1128. if (http_state == IDLE && data_state == DataStateReceived)
  1129. {
  1130. canvas_draw_str(canvas, 0, 27, "Processing...");
  1131. return;
  1132. }
  1133. if (http_state == IDLE && data_state == DataStateParsed)
  1134. {
  1135. canvas_draw_str(canvas, 0, 27, "Processed...");
  1136. return;
  1137. }
  1138. }
  1139. static void web_crawler_loader_process_callback(void *context)
  1140. {
  1141. if (context == NULL)
  1142. {
  1143. FURI_LOG_E(TAG, "web_crawler_loader_process_callback - context is NULL");
  1144. DEV_CRASH();
  1145. return;
  1146. }
  1147. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1148. View *view = app->view_loader;
  1149. DataState current_data_state;
  1150. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1151. if (current_data_state == DataStateInitial)
  1152. {
  1153. with_view_model(
  1154. view,
  1155. DataLoaderModel * model,
  1156. {
  1157. model->data_state = DataStateRequested;
  1158. DataLoaderFetch fetch = model->fetcher;
  1159. if (fetch == NULL)
  1160. {
  1161. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1162. model->data_state = DataStateError;
  1163. return;
  1164. }
  1165. // Clear any previous responses
  1166. strncpy(fhttp.last_response, "", 1);
  1167. bool request_status = fetch(model);
  1168. if (!request_status)
  1169. {
  1170. model->data_state = DataStateError;
  1171. }
  1172. },
  1173. true);
  1174. }
  1175. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1176. {
  1177. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1178. {
  1179. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1180. {
  1181. FURI_LOG_DEV(TAG, "PONG received.");
  1182. }
  1183. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1184. {
  1185. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1186. }
  1187. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1188. {
  1189. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1190. }
  1191. else if (strlen(fhttp.last_response) == 0)
  1192. {
  1193. // Still waiting on response
  1194. }
  1195. else
  1196. {
  1197. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1198. }
  1199. }
  1200. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1201. {
  1202. // continue waiting
  1203. }
  1204. else if (fhttp.state == INACTIVE)
  1205. {
  1206. // inactive. try again
  1207. }
  1208. else if (fhttp.state == ISSUE)
  1209. {
  1210. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1211. }
  1212. else
  1213. {
  1214. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1215. DEV_CRASH();
  1216. }
  1217. }
  1218. else if (current_data_state == DataStateReceived)
  1219. {
  1220. with_view_model(
  1221. view,
  1222. DataLoaderModel * model,
  1223. {
  1224. char *data_text;
  1225. if (model->parser == NULL)
  1226. {
  1227. data_text = NULL;
  1228. FURI_LOG_DEV(TAG, "Parser is NULL");
  1229. DEV_CRASH();
  1230. }
  1231. else
  1232. {
  1233. data_text = model->parser(model);
  1234. }
  1235. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1236. model->data_text = data_text;
  1237. if (data_text == NULL)
  1238. {
  1239. model->data_state = DataStateParseError;
  1240. }
  1241. else
  1242. {
  1243. model->data_state = DataStateParsed;
  1244. }
  1245. },
  1246. true);
  1247. }
  1248. else if (current_data_state == DataStateParsed)
  1249. {
  1250. with_view_model(
  1251. view,
  1252. DataLoaderModel * model,
  1253. {
  1254. if (++model->request_index < model->request_count)
  1255. {
  1256. model->data_state = DataStateInitial;
  1257. }
  1258. else
  1259. {
  1260. web_crawler_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app_instance->widget_result);
  1261. if (model->data_text != NULL)
  1262. {
  1263. free(model->data_text);
  1264. model->data_text = NULL;
  1265. }
  1266. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  1267. view_dispatcher_switch_to_view(app_instance->view_dispatcher, WebCrawlerViewWidgetResult);
  1268. }
  1269. },
  1270. true);
  1271. }
  1272. }
  1273. static void web_crawler_loader_timer_callback(void *context)
  1274. {
  1275. if (context == NULL)
  1276. {
  1277. FURI_LOG_E(TAG, "web_crawler_loader_timer_callback - context is NULL");
  1278. DEV_CRASH();
  1279. return;
  1280. }
  1281. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1282. view_dispatcher_send_custom_event(app->view_dispatcher, WebCrawlerCustomEventProcess);
  1283. }
  1284. static void web_crawler_loader_on_enter(void *context)
  1285. {
  1286. if (context == NULL)
  1287. {
  1288. FURI_LOG_E(TAG, "web_crawler_loader_on_enter - context is NULL");
  1289. DEV_CRASH();
  1290. return;
  1291. }
  1292. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1293. View *view = app->view_loader;
  1294. with_view_model(
  1295. view,
  1296. DataLoaderModel * model,
  1297. {
  1298. view_set_previous_callback(view, model->back_callback);
  1299. if (model->timer == NULL)
  1300. {
  1301. model->timer = furi_timer_alloc(web_crawler_loader_timer_callback, FuriTimerTypePeriodic, app);
  1302. }
  1303. furi_timer_start(model->timer, 250);
  1304. },
  1305. true);
  1306. }
  1307. static void web_crawler_loader_on_exit(void *context)
  1308. {
  1309. if (context == NULL)
  1310. {
  1311. FURI_LOG_E(TAG, "web_crawler_loader_on_exit - context is NULL");
  1312. DEV_CRASH();
  1313. return;
  1314. }
  1315. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1316. View *view = app->view_loader;
  1317. with_view_model(
  1318. view,
  1319. DataLoaderModel * model,
  1320. {
  1321. if (model->timer)
  1322. {
  1323. furi_timer_stop(model->timer);
  1324. }
  1325. },
  1326. false);
  1327. }
  1328. void web_crawler_loader_init(View *view)
  1329. {
  1330. if (view == NULL)
  1331. {
  1332. FURI_LOG_E(TAG, "web_crawler_loader_init - view is NULL");
  1333. DEV_CRASH();
  1334. return;
  1335. }
  1336. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1337. view_set_enter_callback(view, web_crawler_loader_on_enter);
  1338. view_set_exit_callback(view, web_crawler_loader_on_exit);
  1339. }
  1340. void web_crawler_loader_free_model(View *view)
  1341. {
  1342. if (view == NULL)
  1343. {
  1344. FURI_LOG_E(TAG, "web_crawler_loader_free_model - view is NULL");
  1345. DEV_CRASH();
  1346. return;
  1347. }
  1348. with_view_model(
  1349. view,
  1350. DataLoaderModel * model,
  1351. {
  1352. if (model->timer)
  1353. {
  1354. furi_timer_free(model->timer);
  1355. model->timer = NULL;
  1356. }
  1357. if (model->parser_context)
  1358. {
  1359. free(model->parser_context);
  1360. model->parser_context = NULL;
  1361. }
  1362. },
  1363. false);
  1364. }
  1365. bool web_crawler_custom_event_callback(void *context, uint32_t index)
  1366. {
  1367. if (context == NULL)
  1368. {
  1369. FURI_LOG_E(TAG, "web_crawler_custom_event_callback - context is NULL");
  1370. DEV_CRASH();
  1371. return false;
  1372. }
  1373. switch (index)
  1374. {
  1375. case WebCrawlerCustomEventProcess:
  1376. web_crawler_loader_process_callback(context);
  1377. return true;
  1378. default:
  1379. FURI_LOG_DEV(TAG, "web_crawler_custom_event_callback. Unknown index: %ld", index);
  1380. return false;
  1381. }
  1382. }
  1383. void web_crawler_generic_switch_to_view(WebCrawlerApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1384. {
  1385. if (app == NULL)
  1386. {
  1387. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - app is NULL");
  1388. DEV_CRASH();
  1389. return;
  1390. }
  1391. View *view = app->view_loader;
  1392. if (view == NULL)
  1393. {
  1394. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - view is NULL");
  1395. DEV_CRASH();
  1396. return;
  1397. }
  1398. with_view_model(
  1399. view,
  1400. DataLoaderModel * model,
  1401. {
  1402. model->title = title;
  1403. model->fetcher = fetcher;
  1404. model->parser = parser;
  1405. model->request_index = 0;
  1406. model->request_count = request_count;
  1407. model->back_callback = back;
  1408. model->data_state = DataStateInitial;
  1409. model->data_text = NULL;
  1410. },
  1411. true);
  1412. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1413. }