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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::time::{Duration, Instant};

use async_trait::async_trait;
use thiserror::Error;
use tokio::sync::{
    mpsc::{Receiver, Sender},
    Mutex,
};

use crate::{
    image::{RawImage, RawImageError},
    models::Color,
};

use super::EffectMessageKind;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ControlMessage {
    Abort,
}

struct InstanceMethodsData {
    crx: Receiver<ControlMessage>,
    aborted: bool,
}

pub struct InstanceMethods {
    tx: Sender<EffectMessageKind>,
    led_count: usize,
    deadline: Option<Instant>,
    data: Mutex<InstanceMethodsData>,
}

impl InstanceMethods {
    pub fn new(
        tx: Sender<EffectMessageKind>,
        crx: Receiver<ControlMessage>,
        led_count: usize,
        duration: Option<Duration>,
    ) -> Self {
        Self {
            tx,
            led_count,
            deadline: duration.map(|d| Instant::now() + d),
            data: Mutex::new(InstanceMethodsData {
                crx,
                aborted: false,
            }),
        }
    }

    fn completed(&self, data: &InstanceMethodsData) -> bool {
        data.aborted || self.deadline.map(|d| Instant::now() > d).unwrap_or(false)
    }

    /// Returns true if the should abort
    async fn poll_control(&self) -> Result<(), RuntimeMethodError> {
        let mut data = self.data.lock().await;
        match data.crx.try_recv() {
            Ok(m) => match m {
                ControlMessage::Abort => {
                    data.aborted = true;
                    return Err(RuntimeMethodError::EffectAborted);
                }
            },
            Err(err) => {
                match err {
                    tokio::sync::mpsc::error::TryRecvError::Empty => {
                        // No control messages pending
                    }
                    tokio::sync::mpsc::error::TryRecvError::Disconnected => {
                        // We were disconnected
                        data.aborted = true;
                        return Err(RuntimeMethodError::EffectAborted);
                    }
                }
            }
        }

        if self.completed(&data) {
            Err(RuntimeMethodError::EffectAborted)
        } else {
            Ok(())
        }
    }

    async fn wrap_result<T, E: Into<RuntimeMethodError>>(
        &self,
        res: Result<T, E>,
    ) -> Result<T, RuntimeMethodError> {
        match res {
            Ok(t) => Ok(t),
            Err(err) => {
                // TODO: Log error?
                self.data.lock().await.aborted = true;
                Err(err.into())
            }
        }
    }
}

#[async_trait]
impl RuntimeMethods for InstanceMethods {
    fn get_led_count(&self) -> usize {
        self.led_count
    }

    async fn abort(&self) -> bool {
        self.poll_control().await.is_err()
    }

    async fn set_color(&self, color: crate::models::Color) -> Result<(), RuntimeMethodError> {
        self.poll_control().await?;

        self.wrap_result(self.tx.send(EffectMessageKind::SetColor { color }).await)
            .await
    }

    async fn set_led_colors(
        &self,
        colors: Vec<crate::models::Color>,
    ) -> Result<(), RuntimeMethodError> {
        self.poll_control().await?;

        self.wrap_result(
            self.tx
                .send(EffectMessageKind::SetLedColors {
                    colors: colors.into(),
                })
                .await,
        )
        .await
    }

    async fn set_image(&self, image: RawImage) -> Result<(), RuntimeMethodError> {
        self.poll_control().await?;

        self.wrap_result(
            self.tx
                .send(EffectMessageKind::SetImage {
                    image: image.into(),
                })
                .await,
        )
        .await
    }
}

#[async_trait]
pub trait RuntimeMethods: Send {
    fn get_led_count(&self) -> usize;
    async fn abort(&self) -> bool;

    async fn set_color(&self, color: Color) -> Result<(), RuntimeMethodError>;
    async fn set_led_colors(&self, colors: Vec<Color>) -> Result<(), RuntimeMethodError>;
    async fn set_image(&self, image: RawImage) -> Result<(), RuntimeMethodError>;
}

#[derive(Debug, Error)]
pub enum RuntimeMethodError {
    #[cfg(feature = "python")]
    #[error("Invalid arguments to hyperion.{name}")]
    InvalidArguments { name: &'static str },
    #[cfg(feature = "python")]
    #[error("Length of bytearray argument should be 3*ledCount")]
    InvalidByteArray,
    #[error("Effect aborted")]
    EffectAborted,
    #[error(transparent)]
    InvalidImageData(#[from] RawImageError),
}

impl<T> From<tokio::sync::mpsc::error::SendError<T>> for RuntimeMethodError {
    fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
        Self::EffectAborted
    }
}