Skip to content

Commit

Permalink
test will
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Dec 9, 2024
1 parent a5d6964 commit c2ec6c9
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions src/server/protocols/v1/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,45 @@ async fn addons_handler() -> impl Responder {
/// The "register_service" route is used by BlueOS extensions manager
#[api_v2_operation]
#[get("register_service")]
async fn server_metadata() -> Result<Json<ServerMetadata>, Error> {
let package = ServerMetadata::default();
Ok(Json(package))
async fn server_metadata(
manager_handler: web::Data<ManagerActorHandler>,
) -> Result<Json<ServerMetadata>, Error> {
let devices = match manager_handler.send(Request::List).await {
Ok(crate::device::manager::Answer::DeviceInfo(devices)) => devices,
Ok(unexpected) => {
return Err(Error::Internal(format!(
"Unexpected response from device manager: {:?}",
unexpected
)))
}
Err(_) => return Ok(Json(ServerMetadata::default())),
};

let widgets = devices
.into_iter()
.map(|device| {
let name = match device.device_type {
crate::device::manager::DeviceSelection::Ping1D => "ping1d",
crate::device::manager::DeviceSelection::Ping360 => "ping360",
other => {
return Err(Error::Internal(format!(
"Unsupported device type: {:?}",
other
)))
}
};

Ok(CockpitWidget {
name: name.to_string(),
url: format!("/addons/widget/{}/?uuid={}", name, device.id),
})
})
.collect::<Result<Vec<_>, Error>>()?;

Ok(Json(ServerMetadata {
cockpit_widget: widgets,
..ServerMetadata::default()
}))
}

pub fn register_services(cfg: &mut web::ServiceConfig) {
Expand Down Expand Up @@ -264,6 +300,13 @@ pub struct ServerMetadata {
pub new_page: bool,
pub webpage: &'static str,
pub api: &'static str,
pub cockpit_widget: Vec<CockpitWidget>,
}

#[derive(Debug, Serialize, Deserialize, Apiv2Schema)]
pub struct CockpitWidget {
name: String,
url: String,
}

impl Default for ServerMetadata {
Expand All @@ -277,6 +320,7 @@ impl Default for ServerMetadata {
new_page: false,
webpage: "https://github.com/RaulTrombin/navigator-assistant",
api: "/docs",
cockpit_widget: vec![],
}
}
}

0 comments on commit c2ec6c9

Please sign in to comment.