diff --git a/Cargo.toml b/Cargo.toml index faa4eb6..2be05b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,8 +42,9 @@ heapless = "0.8.0" dotenv = "0.15.0" image = "0.25.1" serde_json = "1.0.118" +serde = { version = "1.0.203", features = ["derive"] } models = { path = "models" } -graphics = { path = "graphics" } +# graphics = { path = "graphics" } [build-dependencies] embuild = "0.31.3" diff --git a/models/Cargo.toml b/models/Cargo.toml index 76e7758..47ff4c8 100644 --- a/models/Cargo.toml +++ b/models/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rspotify = "0.13.2" +serde = { version = "1.0.203", features = ["derive"] } +strum = { version = "0.26.3", features = ["derive"] } diff --git a/models/src/lib.rs b/models/src/lib.rs index fd5711b..c1d37ba 100644 --- a/models/src/lib.rs +++ b/models/src/lib.rs @@ -1,12 +1,13 @@ -use rspotify::model::{Device, RepeatState}; +use rspotify::{Device, RepeatState}; +use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Artist { name: String, url: Option, } -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Track { name: String, artists: Vec, @@ -15,7 +16,7 @@ pub struct Track { duration: u32, } -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct CurrentlyPlaying { device: Device, track: Track, @@ -25,3 +26,60 @@ pub struct CurrentlyPlaying { repeat_status: RepeatState, } +// Holds the structs from the `rspotify` package. It's easier to just copy the structs because it +// saves space and there are some issues with using this package on the esp +mod rspotify { + + use serde::{Deserialize, Serialize}; + use strum::IntoStaticStr; + + /// Device object + #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] + pub struct Device { + pub id: Option, + pub is_active: bool, + pub is_private_session: bool, + pub is_restricted: bool, + pub name: String, + #[serde(rename = "type")] + pub _type: DeviceType, + pub volume_percent: Option, + } + + /// Device Type: `computer`, `smartphone`, `speaker`, `TV` + #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, IntoStaticStr)] + #[strum(serialize_all = "snake_case")] + pub enum DeviceType { + Computer, + Tablet, + Smartphone, + Smartwatch, + Speaker, + /// Though undocumented, it has been reported that the Web API returns both + /// 'Tv' and 'TV' as the type. + #[serde(alias = "TV")] + Tv, + /// Same as above, the Web API returns both 'AVR' and 'Avr' as the type. + #[serde(alias = "AVR")] + Avr, + /// Same as above, the Web API returns both 'STB' and 'Stb' as the type. + #[serde(alias = "STB")] + Stb, + AudioDongle, + GameConsole, + CastVideo, + CastAudio, + Automobile, + Unknown, + } + + /// Repeat state: `track`, `context` or `off`. + #[derive(Clone, Debug, Copy, Serialize, Deserialize, PartialEq, Eq, IntoStaticStr)] + #[serde(rename_all = "snake_case")] + #[strum(serialize_all = "snake_case")] + pub enum RepeatState { + Off, + Track, + Context, + } +} diff --git a/src/main.rs b/src/main.rs index 3958f8d..899bc67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,15 +33,6 @@ fn main() { wifi(&mut wifi_driver); - let httpconnection = EspHttpConnection::new(&HttpConfig { - // use_global_ca_store: true, - crt_bundle_attach: Some(esp_crt_bundle_attach), - ..Default::default() - }) - .expect("Could not establish http connection"); - - let mut httpclient = Client::wrap(httpconnection); - let check_playback = thread::spawn(|| { loop { let httpconnection = EspHttpConnection::new(&HttpConfig { @@ -53,7 +44,8 @@ fn main() { let mut httpclient = Client::wrap(httpconnection); let url_root = include_str!("../api_url.txt"); - let formatted_url = std::format!("{}/current_playback", url_root); + // let formatted_url = std::format!("{}/current_playback", url_root); + let formatted_url = "http://23.20.233.1:8080/current_playback"; let request = httpclient .get(&formatted_url) @@ -61,19 +53,23 @@ fn main() { let mut response = request.submit().expect("could not get response"); - let mut buf = vec![0u8; response.content_len().unwrap() as usize]; - response.read_exact(&mut buf).unwrap(); + let mut playing_buf = vec![0u8; response.content_len().unwrap() as usize]; + let mut image_buf = vec![0u8; 300 * 300]; + + response.read_exact(&mut playing_buf).unwrap(); + + let response_str = std::str::from_utf8(&playing_buf); - let response_str = std::str::from_utf8(&buf); - let playing_json: Result> = - serde_json::from_slice(&buf).unwrap(); + let playing_json: Result, serde_json::Error> = + serde_json::from_slice(&playing_buf); if let Err(_) = playing_json { Delay::new_default().delay_ms(5000); continue; } - println!("{:?}", playing_json); + println!("{:?}", response_str); + println!("{:#?}", playing_json); Delay::new_default().delay_ms(1000); } @@ -102,7 +98,6 @@ fn wifi(wifi_driver: &mut BlockingWifi) { wifi_driver.start().unwrap(); wifi_driver.connect().unwrap(); - let mut seconds = 1; println!("Waiting for wifi connection"); wifi_driver.wait_netif_up().unwrap();