web_crawler_callback.c 50 KB

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