hyperion/global/
input_message.rs

1use std::sync::Arc;
2
3use tokio::sync::{oneshot, Mutex};
4
5use crate::{
6    api::json::message::EffectRequest, component::ComponentName, image::RawImage,
7    instance::StartEffectError, models::Color,
8};
9
10use super::Message;
11
12#[derive(Debug, Clone)]
13pub struct InputMessage {
14    source_id: usize,
15    component: ComponentName,
16    data: InputMessageData,
17}
18
19impl Message for InputMessage {
20    type Data = InputMessageData;
21
22    fn new(source_id: usize, component: ComponentName, data: Self::Data) -> Self {
23        Self {
24            source_id,
25            component,
26            data,
27        }
28    }
29
30    fn source_id(&self) -> usize {
31        self.source_id
32    }
33
34    fn component(&self) -> ComponentName {
35        self.component
36    }
37
38    fn data(&self) -> &Self::Data {
39        &self.data
40    }
41
42    fn unregister_source(global: &mut super::GlobalData, input_source: &super::InputSource<Self>) {
43        global.unregister_input_source(input_source);
44    }
45}
46
47pub type StartEffectResponseCallback = Mutex<Option<oneshot::Sender<Result<(), StartEffectError>>>>;
48
49#[derive(Debug, Clone)]
50pub enum InputMessageData {
51    ClearAll,
52    Clear {
53        priority: i32,
54    },
55    SolidColor {
56        priority: i32,
57        duration: Option<chrono::Duration>,
58        color: Color,
59    },
60    Image {
61        priority: i32,
62        duration: Option<chrono::Duration>,
63        image: Arc<RawImage>,
64    },
65    LedColors {
66        priority: i32,
67        duration: Option<chrono::Duration>,
68        led_colors: Arc<Vec<Color>>,
69    },
70    Effect {
71        priority: i32,
72        duration: Option<chrono::Duration>,
73        effect: Arc<EffectRequest>,
74        response: Arc<StartEffectResponseCallback>,
75    },
76}
77
78impl InputMessageData {
79    pub fn priority(&self) -> Option<i32> {
80        match self {
81            InputMessageData::ClearAll => None,
82            InputMessageData::Clear { priority } => Some(*priority),
83            InputMessageData::SolidColor { priority, .. } => Some(*priority),
84            InputMessageData::Image { priority, .. } => Some(*priority),
85            InputMessageData::LedColors { priority, .. } => Some(*priority),
86            InputMessageData::Effect { priority, .. } => Some(*priority),
87        }
88    }
89
90    pub fn duration(&self) -> Option<chrono::Duration> {
91        match self {
92            InputMessageData::ClearAll => None,
93            InputMessageData::Clear { .. } => None,
94            InputMessageData::SolidColor { duration, .. } => *duration,
95            InputMessageData::Image { duration, .. } => *duration,
96            InputMessageData::LedColors { duration, .. } => *duration,
97            InputMessageData::Effect { duration, .. } => *duration,
98        }
99    }
100}