1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! State of gled
//! * list of renderers
//! * main configuration
//! * current bpm (duration_milliseconds)
//! * master dimmer
//! * blackout state

use super::{Configuration, Renderer};
use crossbeam_channel::Sender;
use lazy_static::lazy_static;
use serde_derive::{Deserialize, Serialize};
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
    sync::{Arc, RwLock},
    time::SystemTime,
};

lazy_static! {
    pub static ref STATE: RwLock<State> = RwLock::new(State {
        ..Default::default()
    });
}

#[derive(Serialize, Deserialize)]
#[serde(default)]
pub struct State {
    pub config: Configuration,
    pub renderers: Vec<Arc<RwLock<Renderer>>>,

    /// BPM
    pub duration_milliseconds: f64,
    pub master_dimmer: f64,
    pub blackout: bool,

    #[serde(skip)]
    /// Last time a tap button was activated
    pub last_tap: Option<SystemTime>,

    #[serde(skip)]
    /// List of Senders which are called every time the user interface should be updated
    pub update_uis: Vec<Sender<()>>,
}

impl Default for State {
    fn default() -> Self {
        State {
            config: Configuration {
                ..Default::default()
            },
            renderers: vec![Arc::new(RwLock::new(Renderer {
                ..Default::default()
            }))],
            duration_milliseconds: 1_000.0,
            master_dimmer: 1.0,
            blackout: false,

            last_tap: None,
            update_uis: vec![],
        }
    }
}

impl State {
    pub fn hash(&self) -> u64 {
        serde_json::to_string_pretty(self).ok().map_or(0, |json| {
            let mut s = DefaultHasher::new();
            json.hash(&mut s);
            s.finish()
        })
    }
}