Skip to content

Commit

Permalink
fix(requests): changed models library to use copied internal structs …
Browse files Browse the repository at this point in the history
…to save space and because rspotify doesn't work well on esp
  • Loading branch information
SreeDan committed Jun 29, 2024
1 parent e256084 commit c070b65
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
66 changes: 62 additions & 4 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Track {
name: String,
artists: Vec<Artist>,
Expand All @@ -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,
Expand All @@ -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<String>,
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<u32>,
}

/// 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,
}
}
29 changes: 12 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,27 +44,32 @@ 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)
.expect("could not send current_playback request");

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<Option<CurrentlyPlaying>> =
serde_json::from_slice(&buf).unwrap();
let playing_json: Result<Option<CurrentlyPlaying>, 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);
}
Expand Down Expand Up @@ -102,7 +98,6 @@ fn wifi(wifi_driver: &mut BlockingWifi<EspWifi>) {

wifi_driver.start().unwrap();
wifi_driver.connect().unwrap();
let mut seconds = 1;

println!("Waiting for wifi connection");
wifi_driver.wait_netif_up().unwrap();
Expand Down

0 comments on commit c070b65

Please sign in to comment.