Skip to content

Commit 046336c

Browse files
committed
fix(api): Only send effect to current instance for JSON API
1 parent 667eea5 commit 046336c

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/api/json.rs

+42-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use validator::Validate;
77

88
use crate::{
99
component::ComponentName,
10-
global::{Global, InputMessage, InputMessageData, InputSourceHandle},
10+
global::{Global, InputMessage, InputMessageData, InputSourceHandle, Message},
1111
image::{RawImage, RawImageError},
1212
instance::{InstanceHandle, InstanceHandleError},
1313
};
@@ -30,6 +30,8 @@ pub enum JsonApiError {
3030
Recv(#[from] tokio::sync::oneshot::error::RecvError),
3131
#[error("error accessing the current instance: {0}")]
3232
Instance(#[from] InstanceHandleError),
33+
#[error("no current instance found")]
34+
InstanceNotFound,
3335
}
3436

3537
/// A client connected to the JSON endpoint
@@ -46,10 +48,10 @@ impl ClientConnection {
4648
}
4749
}
4850

49-
async fn current_instance(&mut self, global: &Global) -> Option<InstanceHandle> {
51+
async fn current_instance(&mut self, global: &Global) -> Result<InstanceHandle, JsonApiError> {
5052
if let Some(current_instance) = self.current_instance {
5153
if let Some(instance) = global.get_instance(current_instance).await {
52-
return Some(instance);
54+
return Ok(instance);
5355
} else {
5456
// Instance id now invalid, reset
5557
self.current_instance = None;
@@ -58,10 +60,10 @@ impl ClientConnection {
5860

5961
if let Some((id, inst)) = global.default_instance().await {
6062
self.set_current_instance(id);
61-
return Some(inst);
63+
return Ok(inst);
6264
}
6365

64-
None
66+
Err(JsonApiError::InstanceNotFound)
6567
}
6668

6769
fn set_current_instance(&mut self, id: i32) {
@@ -144,36 +146,47 @@ impl ClientConnection {
144146
}) => {
145147
// TODO: Handle origin, python_script, image_data
146148

147-
let (tx, rx) = oneshot::channel();
148-
149-
// TODO: This should only target one instance?
150-
self.source.send(
151-
ComponentName::All,
152-
InputMessageData::Effect {
153-
priority,
154-
duration: duration.map(|ms| chrono::Duration::milliseconds(ms as _)),
155-
effect: effect.into(),
156-
response: Arc::new(Mutex::new(Some(tx))),
157-
},
158-
)?;
159-
160-
return Ok(match rx.await {
161-
Ok(result) => match result {
162-
Ok(_) => None,
163-
Err(err) => Some(HyperionResponse::error(request.tan, err)),
164-
},
165-
Err(_) => Some(HyperionResponse::error(
166-
request.tan,
167-
"effect request dropped",
168-
)),
169-
});
149+
match self.current_instance(global).await {
150+
Ok(instance) => {
151+
let (tx, rx) = oneshot::channel();
152+
153+
instance
154+
.send(InputMessage::new(
155+
self.source.id(),
156+
ComponentName::All,
157+
InputMessageData::Effect {
158+
priority,
159+
duration: duration
160+
.map(|ms| chrono::Duration::milliseconds(ms as _)),
161+
effect: effect.into(),
162+
response: Arc::new(Mutex::new(Some(tx))),
163+
},
164+
))
165+
.await?;
166+
167+
return Ok(match rx.await {
168+
Ok(result) => match result {
169+
Ok(_) => None,
170+
Err(err) => Some(HyperionResponse::error(request.tan, err)),
171+
},
172+
Err(_) => Some(HyperionResponse::error(
173+
request.tan,
174+
"effect request dropped",
175+
)),
176+
});
177+
}
178+
179+
Err(err) => {
180+
return Ok(Some(HyperionResponse::error(request.tan, err)));
181+
}
182+
}
170183
}
171184

172185
HyperionCommand::ServerInfo(message::ServerInfoRequest { subscribe: _ }) => {
173186
// TODO: Handle subscribe field
174187

175188
let (adjustments, priorities) =
176-
if let Some(handle) = self.current_instance(global).await {
189+
if let Ok(handle) = self.current_instance(global).await {
177190
(
178191
handle
179192
.config()

0 commit comments

Comments
 (0)