1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//!
//! JSON API used by the web UI and possibly third-party applications
//!

use minint::NtConn;
use utopia::OpenApi;
use actix_web::{web::{self, Data}, App, HttpServer, Responder};

use crate::config::CameraResolution;

#[derive(OpenApi)]
#[openapi(
    info(
        title = "Chalkydri",
    ),
    paths(
        info,
        configurations,
        configure,
    ),
)]
#[allow(dead_code)]
struct ApiDoc;
 
pub async fn run_api<'nt>(nt: NtConn) {
    HttpServer::new(move || {
        App::new().app_data(Data::new(nt.clone())).service(info).service(configurations)
    })
    .bind(("0.0.0.0", 6942)).unwrap()
    .run()
    .await
    .unwrap();
}


#[derive(Serialize)]
pub struct Info {
    pub version: &'static str,
}

/// Chalkydri version and info
#[utopia::path(
    responses(
        (status = 200, body = Info),
    ),
)]
#[get("/api/info")]
pub(super) async fn info() -> impl Responder {
    #[cfg(feature = "python")]
    let sys = "rpi";

    web::Json(Info {
        version: env!("CARGO_PKG_VERSION"),
    })
}

#[derive(Serialize, Deserialize)]
pub struct Configuration {
    dimensions: CameraResolution,
    frame_rate: f32,
}
#[derive(Serialize)]
pub struct Configurations {
    cfgs: Vec<Configuration>,
}


/// List possible configurations
#[utopia::path(
    responses(
        (status = 200, body = Configurations),
    ),
)]
#[get("/api/configurations")]
pub(super) async fn configurations() -> impl Responder {
    web::Json(Configurations {
        cfgs: Vec::new()
    })
}

/// Set configuration
#[utopia::path(
    responses(
        (status = 200, body = web::Json),
    ),
)]
#[post("/api/configure")]
pub(super) async fn configure(web::Json(cfgg): web::Json<Configuration>) -> impl Responder {
    web::Json(serde_json::json!({
        "a": {}
    }))
}