@@ -7,7 +7,7 @@ use validator::Validate;
7
7
8
8
use crate :: {
9
9
component:: ComponentName ,
10
- global:: { Global , InputMessage , InputMessageData , InputSourceHandle } ,
10
+ global:: { Global , InputMessage , InputMessageData , InputSourceHandle , Message } ,
11
11
image:: { RawImage , RawImageError } ,
12
12
instance:: { InstanceHandle , InstanceHandleError } ,
13
13
} ;
@@ -30,6 +30,8 @@ pub enum JsonApiError {
30
30
Recv ( #[ from] tokio:: sync:: oneshot:: error:: RecvError ) ,
31
31
#[ error( "error accessing the current instance: {0}" ) ]
32
32
Instance ( #[ from] InstanceHandleError ) ,
33
+ #[ error( "no current instance found" ) ]
34
+ InstanceNotFound ,
33
35
}
34
36
35
37
/// A client connected to the JSON endpoint
@@ -46,10 +48,10 @@ impl ClientConnection {
46
48
}
47
49
}
48
50
49
- async fn current_instance ( & mut self , global : & Global ) -> Option < InstanceHandle > {
51
+ async fn current_instance ( & mut self , global : & Global ) -> Result < InstanceHandle , JsonApiError > {
50
52
if let Some ( current_instance) = self . current_instance {
51
53
if let Some ( instance) = global. get_instance ( current_instance) . await {
52
- return Some ( instance) ;
54
+ return Ok ( instance) ;
53
55
} else {
54
56
// Instance id now invalid, reset
55
57
self . current_instance = None ;
@@ -58,10 +60,10 @@ impl ClientConnection {
58
60
59
61
if let Some ( ( id, inst) ) = global. default_instance ( ) . await {
60
62
self . set_current_instance ( id) ;
61
- return Some ( inst) ;
63
+ return Ok ( inst) ;
62
64
}
63
65
64
- None
66
+ Err ( JsonApiError :: InstanceNotFound )
65
67
}
66
68
67
69
fn set_current_instance ( & mut self , id : i32 ) {
@@ -144,36 +146,47 @@ impl ClientConnection {
144
146
} ) => {
145
147
// TODO: Handle origin, python_script, image_data
146
148
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
+ }
170
183
}
171
184
172
185
HyperionCommand :: ServerInfo ( message:: ServerInfoRequest { subscribe : _ } ) => {
173
186
// TODO: Handle subscribe field
174
187
175
188
let ( adjustments, priorities) =
176
- if let Some ( handle) = self . current_instance ( global) . await {
189
+ if let Ok ( handle) = self . current_instance ( global) . await {
177
190
(
178
191
handle
179
192
. config ( )
0 commit comments