Skip to content

Commit

Permalink
Merge branch 'main' into barebones
Browse files Browse the repository at this point in the history
  • Loading branch information
CaspianA1 committed Jul 29, 2024
2 parents afb0aa8 + 5de5b04 commit 7ad3189
Show file tree
Hide file tree
Showing 45 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ RUST_LOG=wbor_studio_dashboard cargo run --release
- Format all debug types with `<varname>:?` when possible
- Use the max durations of Spinitron spins to reduce the number of API calls
- Make a small window that shows the dashboard uptime (`chrono::Duration` should work for a long, long time)
- Merge app_config.json and api_keys.json

- Fun ideas:
- Run the dashboard on a PVM/BVM (less burn-in), or an original iMac, eventually?
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
36 changes: 30 additions & 6 deletions src/dashboard_defs/dashboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::sync::atomic::{AtomicUsize, Ordering};

use chrono::Duration;
use sdl2::{render::BlendMode, ttf::{FontStyle, Hinting}};
Expand Down Expand Up @@ -34,6 +35,33 @@ use crate::{
}
};

//////////

static FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX: AtomicUsize = AtomicUsize::new(0);

lazy_static::lazy_static!(
static ref FALLBACK_TEXTURE_PATHS: Vec<String> =
std::fs::read_dir("assets/fallback_textures").unwrap()
.map(|maybe_dir_entry| maybe_dir_entry.map(|dir_entry| {
let path = dir_entry.path();
assert!(path.is_file());
path.to_str().unwrap().to_owned()
}))
.collect::<Result<Vec<_>, _>>().unwrap();
);

fn get_fallback_texture_creation_info() -> TextureCreationInfo<'static> {
let ordering = Ordering::SeqCst;
let mut index = FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX.fetch_add(1, ordering);

if index >= FALLBACK_TEXTURE_PATHS.len() {
index = 0;
FALLBACK_TEXTURE_CREATION_INFO_PATH_INDEX.store(0, ordering);
}

TextureCreationInfo::Path(Cow::Borrowed(&FALLBACK_TEXTURE_PATHS[index]))
}

////////// TODO: maybe split `make_dashboard` into some smaller sub-functions

/* TODO:
Expand Down Expand Up @@ -384,10 +412,6 @@ pub fn make_dashboard(

////////// Defining the shared state

// TODO: make it possible to get different variants of this texture (randomly chosen)
const FALLBACK_TEXTURE_CREATION_INFO: TextureCreationInfo<'static> =
TextureCreationInfo::Path(Cow::Borrowed("assets/no_texture_available.png"));

let initial_spin_window_size_guess = (1024, 1024);

let custom_model_expiry_durations = [
Expand All @@ -398,7 +422,7 @@ pub fn make_dashboard(
];

let spinitron_state = SpinitronState::new(
(&api_keys.spinitron, &FALLBACK_TEXTURE_CREATION_INFO,
(&api_keys.spinitron, get_fallback_texture_creation_info,
custom_model_expiry_durations, initial_spin_window_size_guess)
)?;

Expand All @@ -408,7 +432,7 @@ pub fn make_dashboard(
spinitron_state,
twilio_state,
font_info: &FONT_INFO,
fallback_texture_creation_info: &FALLBACK_TEXTURE_CREATION_INFO,
get_fallback_texture_creation_info,
curr_dashboard_error: None,
rand_generator: rand::thread_rng()
}
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/shared_window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct SharedWindowState<'a> {
pub font_info: &'a FontInfo,

// This is used whenever a texture can't be loaded
pub fallback_texture_creation_info: &'a TextureCreationInfo<'a>,
pub get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'a>,

pub curr_dashboard_error: Option<String>,

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/spinitron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn make_spinitron_windows(
true,
params.texture_pool,
&texture_creation_info,
inner_shared_state.fallback_texture_creation_info
inner_shared_state.get_fallback_texture_creation_info
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/updatable_text_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn make_window<IndividualState: UpdatableTextWindowMethods + Clone + 'static
);

texture_contents.update_as_texture(true, params.texture_pool,
&texture_creation_info, &texture_creation_info)
&texture_creation_info, inner_shared_state.get_fallback_texture_creation_info)
}

//////////
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn weather_updater_fn(params: WindowUpdaterParams) -> MaybeError {
weather_changed,
params.texture_pool,
&texture_creation_info,
inner_shared_state.fallback_texture_creation_info
inner_shared_state.get_fallback_texture_creation_info
)
}

Expand Down
8 changes: 5 additions & 3 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::borrow::Cow;
use crate::utility_types::generic_result::*;

type Response = GenericResult<minreq::Response>;

pub fn build_url(base_url: &str, path_params: &[Cow<str>],
query_params: &[(&str, Cow<str>)]) -> String {

Expand All @@ -21,7 +23,7 @@ pub fn build_url(base_url: &str, path_params: &[Cow<str>],

/* TODO: in order to effectively do request stuff, maybe eliminate this wrapper
code altogether? Or just keep this wrapper layer as request submitting code? */
pub fn get_with_maybe_header(url: &str, maybe_header: Option<(&str, &str)>) -> GenericResult<minreq::Response> {
pub fn get_with_maybe_header(url: &str, maybe_header: Option<(&str, &str)>) -> Response {
const EXPECTED_STATUS_CODE: i32 = 200;
const DEFAULT_TIMEOUT_SECONDS: u64 = 20;

Expand All @@ -44,12 +46,12 @@ pub fn get_with_maybe_header(url: &str, maybe_header: Option<(&str, &str)>) -> G
}
}

pub fn get(url: &str) -> GenericResult<minreq::Response> {
pub fn get(url: &str) -> Response {
get_with_maybe_header(url, None)
}

// This function is monadic!
pub fn as_type<T: for<'de> serde::Deserialize<'de>>(response: GenericResult<minreq::Response>) -> GenericResult<T> {
pub fn as_type<T: for<'de> serde::Deserialize<'de>>(response: Response) -> GenericResult<T> {
let unpacked_response = response?;
serde_json::from_str(unpacked_response.as_str()?).to_generic()
}
13 changes: 7 additions & 6 deletions src/spinitron/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl ModelAgeData {
#[derive(Clone)]
struct SpinitronStateData {
api_key: String,
fallback_texture_creation_info: &'static TextureCreationInfo<'static>,
get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'static>,

spin: Spin,
playlist: Playlist,
Expand All @@ -110,15 +110,15 @@ type WindowSize = (u32, u32);
// The third param is the fallback texture creation info, and the fourth one is the spin window size
type SpinitronStateDataParams<'a> = (
&'a str, // API key
&'static TextureCreationInfo<'static>, // Fallback texture creation info
fn() -> TextureCreationInfo<'static>, // Fallback texture creation info getter
[chrono::Duration; NUM_SPINITRON_MODEL_TYPES], // Custom model expiry durations
WindowSize
);

//////////

impl SpinitronStateData {
fn new((api_key, fallback_texture_creation_info,
fn new((api_key, get_fallback_texture_creation_info,
custom_model_expiry_durations, spin_window_size):
SpinitronStateDataParams) -> GenericResult<Self> {

Expand Down Expand Up @@ -148,7 +148,7 @@ impl SpinitronStateData {

let mut data = Self {
api_key: api_key.to_owned(),
fallback_texture_creation_info,
get_fallback_texture_creation_info,

spin, playlist, persona, show,

Expand Down Expand Up @@ -186,15 +186,16 @@ impl SpinitronStateData {

let age_state = self.age_data[model_name as usize].curr_age_state.clone();
let model = self.get_model_by_name(model_name);
let get_fallback = || Cow::Owned((self.get_fallback_texture_creation_info)());

let info = match model.get_texture_creation_info(age_state, size_pixels) {
Some(texture_creation_info) => Cow::Owned(texture_creation_info),
None => Cow::Borrowed(self.fallback_texture_creation_info)
None => get_fallback()
};

load_for_info(info).or_else(|error| {
log::warn!("Reverting to fallback texture for Spinitron model. Error: '{error}'");
load_for_info(Cow::Borrowed(self.fallback_texture_creation_info))
load_for_info(get_fallback())
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/window_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl WindowContents {
should_remake: bool,
texture_pool: &mut TexturePool,
texture_creation_info: &TextureCreationInfo,
fallback_texture_creation_info: &TextureCreationInfo) -> MaybeError {
get_fallback_texture_creation_info: fn() -> TextureCreationInfo<'static>) -> MaybeError {

/* This is a macro for making or remaking a texture. If making or
remaking fails, a fallback texture is put into that texture's slot. */
Expand All @@ -105,7 +105,7 @@ impl WindowContents {
log::warn!("Unexpectedly failed while trying to {} texture, and reverting to a fallback \
texture. Reason: '{failure_reason}'.", $make_or_remake_description);

$make_or_remake(fallback_texture_creation_info, $($extra_args),*)
$make_or_remake(&get_fallback_texture_creation_info(), $($extra_args),*)
}
)
}};
Expand Down

0 comments on commit 7ad3189

Please sign in to comment.