@@ -36,6 +36,10 @@ pub fn router(api_channel: watch::Sender<ApiChannelMessage>) -> Router<()> {
3636 "/api/settings/:setting_id" ,
3737 patch ( self :: patch:: camera_settings) ,
3838 )
39+ . route (
40+ "/api/cameras/:camera_id/restart" ,
41+ post ( self :: post:: camera_restart) ,
42+ )
3943 . with_state ( api_channel)
4044}
4145
@@ -221,10 +225,13 @@ mod get {
221225
222226mod post {
223227 use super :: { AuthSession , IntoResponse , StatusCode } ;
228+ use crate :: ApiChannelMessage ;
224229 use crate :: { Camera , CameraPermission , CameraSetting , Model } ;
230+ use axum:: extract:: { Path , State } ;
225231 use axum:: Form ;
226232 use axum:: Json ;
227233 use serde:: Deserialize ;
234+ use tokio:: sync:: watch;
228235
229236 #[ derive( Debug , Clone , Deserialize ) ]
230237 pub struct AddCameraForm {
@@ -260,7 +267,7 @@ mod post {
260267 setting_id : CameraSetting :: DEFAULT . setting_id ,
261268 camera_id : camera. camera_id ,
262269 flashlight_enabled : CameraSetting :: DEFAULT . flashlight_enabled ,
263- resolution : "800x600 " . to_string ( ) ,
270+ resolution : "SVGA " . to_string ( ) ,
264271 framerate : 5 ,
265272 last_modified : CameraSetting :: DEFAULT . last_modified ( ) ,
266273 modified_by : Some ( user. user_id ) ,
@@ -295,6 +302,32 @@ mod post {
295302 None => StatusCode :: UNAUTHORIZED . into_response ( ) ,
296303 }
297304 }
305+
306+ pub async fn camera_restart (
307+ auth_session : AuthSession ,
308+ Path ( camera_id) : Path < i64 > ,
309+ state : State < watch:: Sender < ApiChannelMessage > > ,
310+ ) -> impl IntoResponse {
311+ match auth_session. user {
312+ Some ( user) => {
313+ if user. username != "admin" {
314+ return StatusCode :: FORBIDDEN . into_response ( ) ;
315+ }
316+
317+ let api_message = ApiChannelMessage :: CameraRelated {
318+ camera_id,
319+ message : crate :: web:: CameraMessage :: Restart ,
320+ } ;
321+
322+ if state. send ( api_message) . is_err ( ) {
323+ return StatusCode :: INTERNAL_SERVER_ERROR . into_response ( ) ;
324+ }
325+
326+ StatusCode :: OK . into_response ( )
327+ }
328+ None => StatusCode :: UNAUTHORIZED . into_response ( ) ,
329+ }
330+ }
298331}
299332
300333// TODO: Don't always return the same error
@@ -352,8 +385,8 @@ mod patch {
352385 #[ derive( Debug , Clone , Deserialize ) ]
353386 pub struct UpdateSettingsForm {
354387 pub flashlight_enabled : bool ,
355- // pub resolution: String,
356- // pub framerate: i64
388+ pub resolution : String ,
389+ pub framerate : i64 ,
357390 }
358391
359392 pub async fn camera_settings (
@@ -384,10 +417,23 @@ mod patch {
384417 return StatusCode :: FORBIDDEN . into_response ( ) ;
385418 }
386419
387- // TODO: Update resolution and framerate
420+ // TODO: resolution
388421 setting. flashlight_enabled = settings_form. flashlight_enabled ;
389- // setting.resolution = settings_form.resolution;
390- // setting.framerate = settings_form.framerate;
422+
423+ // ? Maybe allow any framerate/resolution for admin but give warning
424+ if user. username == "admin" {
425+ if ( settings_form. framerate < 1 ) || ( settings_form. framerate > 60 ) {
426+ return StatusCode :: BAD_REQUEST . into_response ( ) ;
427+ }
428+
429+ if ![ "SVGA" , "VGA" ] . contains ( & settings_form. resolution . as_str ( ) ) {
430+ return StatusCode :: BAD_REQUEST . into_response ( ) ;
431+ }
432+
433+ setting. resolution = settings_form. resolution ;
434+ setting. framerate = settings_form. framerate ;
435+ }
436+
391437 setting. last_modified = CameraSetting :: DEFAULT . last_modified ( ) ;
392438 setting. modified_by = Some ( user. user_id ) ;
393439
0 commit comments