Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.83 KB | None | 0 0
  1. fn v4l_page(_req: HttpRequest) -> impl Responder {
  2.     let cameras_path = fs::read_dir("/dev/").unwrap()
  3.     .filter(|f| f.as_ref().unwrap().file_name().to_str().unwrap().starts_with("video"))
  4.     .map(|f| String::from(f.unwrap().path().clone().to_str().unwrap()))
  5.     .collect::<Vec<_>>();
  6.  
  7.     println!("{:#?}", cameras_path);
  8.  
  9.     let mut main_json = json!({"cameras":{}});
  10.     let cameras_json = &mut main_json["cameras"];
  11.     'camera_loop: for camera_path in cameras_path {
  12.        println!("{:#?}", camera_path);
  13.        let camera = Camera::new(&camera_path);
  14.        if camera.is_err() {
  15.            continue;
  16.        }
  17.        let camera = camera.unwrap();
  18.        let mut controls = Vec::<serde_json::value::Value>::new();
  19.        for i in camera.controls() {
  20.            if i.is_err() {
  21.                println!("Error: {:#?}", i);
  22.                continue 'camera_loop;
  23.             }
  24.             let value = serde_json::to_value(i.unwrap());
  25.             if value.is_err() {
  26.                 println!("Error with value: {:#?}", value);
  27.                 continue 'camera_loop;
  28.            }
  29.            controls.push(value.unwrap());
  30.        }
  31.        cameras_json[&camera_path]["controls"] = serde_json::to_value(controls).unwrap();
  32.  
  33.        let mut formats = Vec::<serde_json::value::Value>::new();
  34.        for i in camera.formats() {
  35.            if i.is_err() {
  36.                println!("Error: {:#?}", i);
  37.                continue 'camera_loop;
  38.             }
  39.             let format = i.unwrap();
  40.  
  41.             let value = serde_json::to_value(&format);
  42.             if value.is_err() {
  43.                 println!("Error with value: {:#?}", value);
  44.                 continue 'camera_loop;
  45.            }
  46.            let mut value = value.unwrap();
  47.            let get_fourcc = |i: &String| -> [u8; 4] {
  48.                let mut array = [0; 4];
  49.                array.copy_from_slice(&i.as_bytes()[0..4]);
  50.                return array;
  51.            };
  52.            let fourcc = get_fourcc(&format.description);
  53.            let resolutions = camera.resolutions(fourcc);
  54.            if resolutions.is_err() {
  55.                println!("Error with value: {:#?} for format {}", resolutions, &format.description);
  56.            }
  57.  
  58.            let resolutions_interval = Vec::<serde_json::value::Value>::new();
  59.            for resolution in &resolutions.into_iter() {
  60.                //camera.intervals(fourcc, resolution.);
  61.                println!("> {:#?} > {:#?}", &format.description, resolution);
  62.                //resolutions_interval.push()
  63.            }
  64.  
  65.            value["resolutions"] = serde_json::to_value(resolutions.unwrap()).unwrap();
  66.            formats.push(value);
  67.        }
  68.        cameras_json[&camera_path]["formats"] = serde_json::to_value(formats).unwrap();
  69.    }
  70.    serde_json::to_string_pretty(&main_json)
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement