gled-core 2.32.1

The engine of gled without a user interface: inputs, timing, rendering, Art-Net/DMX output and storage
Documentation
//! What the engine reports about the machine besides the show, and the
//! machine settings interfaces may change.

use std::sync::atomic::Ordering::Relaxed;

use gled_protocol::state::Devices;

use super::{Engine, ServerSettings, ServerStatus};
use crate::{input::artnet::ARTNET_CONFIG, runtime::timing::CONNECTED_PEERS, storage};

impl Engine {
    pub(super) fn server_settings(&self) -> ServerSettings {
        let credentials = self.persistent_state.git_credentials();
        ServerSettings {
            fps_limit: self.persistent_state.fps_limit_setting(),
            ableton_link_read_only: self.persistent_state.ableton_link_read_only(),
            git_url: self.persistent_state.git_url(),
            git_private_key: credentials
                .private_key_path()
                .map(|path| path.display().to_string()),
            git_use_passphrase: credentials.use_passphrase(),
        }
    }

    pub(super) fn apply_server_settings(&mut self, settings: ServerSettings) {
        let state = &mut self.persistent_state;
        state.set_fps_limit(settings.fps_limit);
        state.set_ableton_link_read_only(settings.ableton_link_read_only);
        state.set_git_url(settings.git_url);
        let credentials = state.git_credentials_mut();
        if let Some(key) = settings.git_private_key {
            credentials.set_private_key_path(key.into());
        }
        credentials.set_use_passphrase(settings.git_use_passphrase);
        state.save();
    }

    pub(super) fn server_status(&mut self) -> ServerStatus {
        let branches = storage::branches();
        let task = storage::loading();
        let migration = storage::migration::progress();
        ServerStatus {
            storage_task: (task != storage::Loading::No).then(|| match migration {
                Some((done, total)) => format!("{task}\n{done} of {total}"),
                None => task.to_string(),
            }),
            storage_progress: migration.map(|(done, total)| done as f32 / total as f32),
            storage_error: storage::error(),
            storage_working: storage::working(),
            staged_files: storage::staged_files(),
            current_branch: branches.as_ref().map(|branches| branches.current.clone()),
            branches: branches
                .map(|branches| branches.available)
                .unwrap_or_default(),
            storage_dir: storage::STORAGE_DIR.display().to_string(),
            artnet: ARTNET_CONFIG.lock().clone(),
            devices: Devices {
                midi_outputs: self.midi.output_ports().to_vec(),
                ..self.device_scan.devices()
            },
            settings: self.server_settings(),
            network_in: self.network_stats.0 as f32,
            network_out: self.network_stats.1 as f32,
            temperature: self.temperature,
            host_name: self.host_name.clone(),
            link_peers: CONNECTED_PEERS.load(Relaxed),
        }
    }
}