@@ -9,7 +9,7 @@ use crate::{
9
9
component:: ComponentName ,
10
10
global:: { Global , InputMessage , InputMessageData , InputSourceHandle , Message } ,
11
11
image:: { RawImage , RawImageError } ,
12
- instance:: { InstanceHandle , InstanceHandleError } ,
12
+ instance:: { InstanceHandle , InstanceHandleError , StartEffectError } ,
13
13
} ;
14
14
15
15
/// Schema definitions as Serde serializable structures and enums
@@ -27,11 +27,13 @@ pub enum JsonApiError {
27
27
#[ error( "error validating request: {0}" ) ]
28
28
Validation ( #[ from] validator:: ValidationErrors ) ,
29
29
#[ error( "error receiving system response: {0}" ) ]
30
- Recv ( #[ from] tokio :: sync :: oneshot:: error:: RecvError ) ,
30
+ Recv ( #[ from] oneshot:: error:: RecvError ) ,
31
31
#[ error( "error accessing the current instance: {0}" ) ]
32
32
Instance ( #[ from] InstanceHandleError ) ,
33
33
#[ error( "no current instance found" ) ]
34
34
InstanceNotFound ,
35
+ #[ error( transparent) ]
36
+ StartEffect ( #[ from] StartEffectError ) ,
35
37
}
36
38
37
39
/// A client connected to the JSON endpoint
@@ -76,7 +78,7 @@ impl ClientConnection {
76
78
& mut self ,
77
79
request : HyperionMessage ,
78
80
global : & Global ,
79
- ) -> Result < Option < HyperionResponse > , JsonApiError > {
81
+ ) -> Result < HyperionResponse , JsonApiError > {
80
82
request. validate ( ) ?;
81
83
82
84
match request. command {
@@ -146,40 +148,23 @@ impl ClientConnection {
146
148
} ) => {
147
149
// TODO: Handle origin, python_script, image_data
148
150
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
- }
151
+ let instance = self . current_instance ( global) . await ?;
152
+ let ( tx, rx) = oneshot:: channel ( ) ;
153
+
154
+ instance
155
+ . send ( InputMessage :: new (
156
+ self . source . id ( ) ,
157
+ ComponentName :: All ,
158
+ InputMessageData :: Effect {
159
+ priority,
160
+ duration : duration. 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 ( rx. await ?. map ( |_| HyperionResponse :: success ( ) ) ?) ;
183
168
}
184
169
185
170
HyperionCommand :: ServerInfo ( message:: ServerInfoRequest { subscribe : _ } ) => {
@@ -209,46 +194,37 @@ impl ClientConnection {
209
194
. await ;
210
195
211
196
// Just answer the serverinfo request, no need to update state
212
- return Ok ( Some (
213
- global
214
- . read_config ( |config| {
215
- let instances = config
216
- . instances
217
- . iter ( )
218
- . map ( |instance_config| ( & instance_config. 1 . instance ) . into ( ) )
219
- . collect ( ) ;
220
-
221
- HyperionResponse :: server_info (
222
- request. tan ,
223
- priorities,
224
- adjustments,
225
- effects,
226
- instances,
227
- )
228
- } )
229
- . await ,
230
- ) ) ;
197
+ return Ok ( global
198
+ . read_config ( |config| {
199
+ let instances = config
200
+ . instances
201
+ . iter ( )
202
+ . map ( |instance_config| ( & instance_config. 1 . instance ) . into ( ) )
203
+ . collect ( ) ;
204
+
205
+ HyperionResponse :: server_info ( priorities, adjustments, effects, instances)
206
+ } )
207
+ . await ) ;
231
208
}
232
209
233
210
HyperionCommand :: Authorize ( message:: Authorize { subcommand, .. } ) => match subcommand {
234
211
message:: AuthorizeCommand :: AdminRequired => {
235
212
// TODO: Perform actual authentication flow
236
- return Ok ( Some ( HyperionResponse :: admin_required ( request . tan , false ) ) ) ;
213
+ return Ok ( HyperionResponse :: admin_required ( false ) ) ;
237
214
}
238
215
message:: AuthorizeCommand :: TokenRequired => {
239
216
// TODO: Perform actual authentication flow
240
- return Ok ( Some ( HyperionResponse :: token_required ( request . tan , false ) ) ) ;
217
+ return Ok ( HyperionResponse :: token_required ( false ) ) ;
241
218
}
242
219
_ => {
243
220
return Err ( JsonApiError :: NotImplemented ) ;
244
221
}
245
222
} ,
246
223
247
224
HyperionCommand :: SysInfo => {
248
- return Ok ( Some ( HyperionResponse :: sys_info (
249
- request. tan ,
225
+ return Ok ( HyperionResponse :: sys_info (
250
226
global. read_config ( |config| config. uuid ( ) ) . await ,
251
- ) ) ) ;
227
+ ) ) ;
252
228
}
253
229
254
230
HyperionCommand :: Instance ( message:: Instance {
@@ -258,18 +234,18 @@ impl ClientConnection {
258
234
} ) => {
259
235
if global. get_instance ( id) . await . is_some ( ) {
260
236
self . set_current_instance ( id) ;
261
- return Ok ( Some ( HyperionResponse :: switch_to ( request . tan , Some ( id) ) ) ) ;
237
+ return Ok ( HyperionResponse :: switch_to ( Some ( id) ) ) ;
262
238
} else {
263
239
// Note: it's an "Ok" but should be an Err. Find out how to represent errors
264
240
// better
265
- return Ok ( Some ( HyperionResponse :: switch_to ( request . tan , None ) ) ) ;
241
+ return Ok ( HyperionResponse :: switch_to ( None ) ) ;
266
242
}
267
243
}
268
244
269
245
_ => return Err ( JsonApiError :: NotImplemented ) ,
270
246
} ;
271
247
272
- Ok ( None )
248
+ Ok ( HyperionResponse :: success ( ) )
273
249
}
274
250
}
275
251
0 commit comments