|
| 1 | +use crate::device::manager::ManagerActorHandler; |
| 2 | +use crate::server::protocols::v1::errors::Error; |
| 3 | +use actix_web::Responder; |
| 4 | +use mime_guess::from_path; |
| 5 | +use paperclip::actix::{ |
| 6 | + api_v2_operation, get, post, |
| 7 | + web::{self, HttpResponse, Json}, |
| 8 | + Apiv2Schema, |
| 9 | +}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | +use serde_json::json; |
| 12 | + |
| 13 | +#[derive(rust_embed::RustEmbed)] |
| 14 | +#[folder = "src/server/protocols/v1/frontend"] |
| 15 | +struct Asset; |
| 16 | + |
| 17 | +fn handle_embedded_file(path: &str) -> HttpResponse { |
| 18 | + match Asset::get(path) { |
| 19 | + Some(content) => HttpResponse::Ok() |
| 20 | + .content_type(from_path(path).first_or_octet_stream().as_ref()) |
| 21 | + .body(content.data.into_owned()), |
| 22 | + None => HttpResponse::NotFound().body("404 Not Found"), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[api_v2_operation(skip)] |
| 27 | +#[get("/")] |
| 28 | +async fn index() -> impl Responder { |
| 29 | + handle_embedded_file("index.html") |
| 30 | +} |
| 31 | + |
| 32 | +/// The "register_service" route is used by BlueOS extensions manager |
| 33 | +#[api_v2_operation] |
| 34 | +#[get("register_service")] |
| 35 | +async fn server_metadata() -> Result<Json<ServerMetadata>, Error> { |
| 36 | + let package = ServerMetadata::default(); |
| 37 | + Ok(Json(package)) |
| 38 | +} |
| 39 | + |
| 40 | +pub fn register_services(cfg: &mut web::ServiceConfig) { |
| 41 | + cfg.service(index) |
| 42 | + .service(post_request) |
| 43 | + .service(server_metadata); |
| 44 | +} |
| 45 | + |
| 46 | +#[api_v2_operation] |
| 47 | +#[post("device/request")] |
| 48 | +async fn post_request( |
| 49 | + manager_handler: web::Data<ManagerActorHandler>, |
| 50 | + json: web::Json<crate::device::manager::Request>, |
| 51 | +) -> Result<Json<crate::device::manager::Answer>, Error> { |
| 52 | + let request = json.into_inner(); |
| 53 | + |
| 54 | + let answer = manager_handler.send(request).await?; |
| 55 | + |
| 56 | + crate::server::protocols::v1::websocket::send_to_websockets(json!(answer), None); |
| 57 | + |
| 58 | + Ok(Json(answer)) |
| 59 | +} |
| 60 | +#[derive(Debug, Serialize, Deserialize, Apiv2Schema)] |
| 61 | +pub struct ServerMetadata { |
| 62 | + pub name: &'static str, |
| 63 | + pub description: &'static str, |
| 64 | + pub icon: &'static str, |
| 65 | + pub company: &'static str, |
| 66 | + pub version: &'static str, |
| 67 | + pub new_page: bool, |
| 68 | + pub webpage: &'static str, |
| 69 | + pub api: &'static str, |
| 70 | +} |
| 71 | + |
| 72 | +impl Default for ServerMetadata { |
| 73 | + fn default() -> Self { |
| 74 | + Self { |
| 75 | + name: "Ping Viewer Next", |
| 76 | + description: "A ping protocol extension for expose devices to web.", |
| 77 | + icon: "mdi-compass-outline", |
| 78 | + company: "BlueRobotics", |
| 79 | + version: "0.0.1", |
| 80 | + new_page: false, |
| 81 | + webpage: "https://github.com/RaulTrombin/navigator-assistant", |
| 82 | + api: "/docs", |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments