Can get a single device enrollment status

This commit is contained in:
2024-07-03 22:05:19 +02:00
parent e97ef6fe45
commit 8674d25512
7 changed files with 91 additions and 8 deletions

View File

@ -101,6 +101,11 @@ impl DevicesList {
self.0.clone().into_values().collect()
}
/// Get the information about a single device
pub fn get_single(&self, id: &DeviceId) -> Option<Device> {
self.0.get(id).cloned()
}
/// Validate a device
pub fn validate(&mut self, id: &DeviceId) -> anyhow::Result<()> {
let dev = self

View File

@ -137,3 +137,16 @@ impl Handler<GetDeviceLists> for EnergyActor {
self.devices.full_list()
}
}
/// Get the information about a single device
#[derive(Message)]
#[rtype(result = "Option<Device>")]
pub struct GetSingleDevice(pub DeviceId);
impl Handler<GetSingleDevice> for EnergyActor {
type Result = Option<Device>;
fn handle(&mut self, msg: GetSingleDevice, _ctx: &mut Context<Self>) -> Self::Result {
self.devices.get_single(&msg.0)
}
}

View File

@ -71,3 +71,34 @@ pub async fn enroll(req: web::Json<EnrollRequest>, actor: WebEnergyActor) -> Htt
Ok(HttpResponse::Accepted().json("Device successfully enrolled"))
}
#[derive(serde::Deserialize)]
pub struct EnrollmentStatusQuery {
id: DeviceId,
}
#[derive(serde::Serialize)]
#[serde(tag = "status")]
enum EnrollmentDeviceStatus {
Unknown,
Pending,
Validated,
}
/// Check device enrollment status
pub async fn enrollment_status(
query: web::Query<EnrollmentStatusQuery>,
actor: WebEnergyActor,
) -> HttpResult {
let dev = actor
.send(energy_actor::GetSingleDevice(query.id.clone()))
.await?;
let status = match dev {
None => EnrollmentDeviceStatus::Unknown,
Some(d) if d.validated => EnrollmentDeviceStatus::Validated,
_ => EnrollmentDeviceStatus::Pending,
};
Ok(HttpResponse::Ok().json(status))
}

View File

@ -156,7 +156,10 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/devices_api/mgmt/enroll",
web::post().to(mgmt_controller::enroll),
)
// TODO : check device status
.route(
"/devices_api/mgmt/enrollment_status",
web::get().to(mgmt_controller::enrollment_status),
)
})
.bind_openssl(&AppConfig::get().listen_address, builder)?
.run()