web_crawler_callback.c 50 KB

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