Skip to content

Commit

Permalink
Use API client
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Jan 5, 2025
1 parent 5212e41 commit f5d9c90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub struct APIClient<'a> {
}

impl<'a> APIClient<'a> {
pub fn new(endpoint: String, timeout: time::Duration) -> Self {
pub fn new(endpoint: &str, timeout: time::Duration) -> Self {
Self {
endpoint,
endpoint: endpoint.to_string(),
timeout,
config: EspWebSocketClientConfig {
// server_cert: Some(X509::pem_until_nul(SERVER_ROOT_CERT)),
Expand Down
48 changes: 6 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod api;
mod usb;
mod wifi;

use crate::api::client::APIClient;
use crate::usb::msc_device::MSCDevice;
use crate::wifi::session::WifiSession;
use embedded_svc::wifi::AuthMethod;
Expand All @@ -11,48 +12,11 @@ use esp_idf_svc::hal::prelude::Peripherals;
use esp_idf_svc::hal::task::block_on;
use esp_idf_svc::http::client::EspHttpConnection;
use std::time::Duration;
use std::{fs, thread};
use std::{fs, thread, time};

const SSID: &str = env!("WIFI_SSID");
const PASSWORD: &str = env!("WIFI_PASS");

fn post_chunked_request(
client: &mut HttpClient<EspHttpConnection>,
data: &[u8],
) -> anyhow::Result<()> {
// Prepare payload

// Prepare headers and URL
let headers = [("content-type", "text/plain")];
let url = "https://httpbin.org/post";

// Send request
let mut request = client.post(url, &headers)?;
request.write_all(data)?;
request.flush()?;
log::info!("-> CHUNKED POST {}", url);
let mut response = request.submit()?;

// Process response
let status = response.status();
log::info!("<- {}", status);
let mut buf = [0u8; 1024];
let bytes_read = io::try_read_full(&mut response, &mut buf).map_err(|e| e.0)?;
log::info!("Read {} bytes", bytes_read);
match std::str::from_utf8(&buf[0..bytes_read]) {
Ok(body_string) => log::info!(
"Response body (truncated to {} bytes): {:?}",
buf.len(),
body_string
),
Err(e) => log::error!("Error decoding response body: {}", e),
};

// Drain the remaining response bytes
while response.read(&mut buf)? > 0 {}

Ok(())
}

async fn run_async() -> Result<(), anyhow::Error> {
log::info!(
Expand All @@ -76,17 +40,17 @@ async fn run_async() -> Result<(), anyhow::Error> {
crt_bundle_attach: Some(esp_idf_svc::sys::esp_crt_bundle_attach),
..Default::default()
};
let mut client = HttpClient::wrap(EspHttpConnection::new(&config)?);
let mut client = APIClient::new("", time::Duration::from_secs(30));

loop {
// Asynchronously wait for GPIO events, allowing other tasks
// to run, or the core to sleep.
button.wait_for_low().await?;
log::info!("Button pressed!");

let contents = fs::read_to_string("/disk/myfile.txt").unwrap_or(String::from("N/A"));
log::info!("File content: {}", contents);
post_chunked_request(&mut client, contents.as_bytes())?;
client.connect();
// let contents = fs::read_to_string("/disk/myfile.txt").unwrap_or(String::from("N/A"));
// log::info!("File content: {}", contents);

button.wait_for_high().await?;
log::info!("Button released!");
Expand Down

0 comments on commit f5d9c90

Please sign in to comment.