| 90 | } |
| 91 | |
| 92 | async fn get( |
| 93 | &self, |
| 94 | request: Request<api::GetDeviceRequest>, |
| 95 | ) -> Result<Response<api::GetDeviceResponse>, Status> { |
| 96 | let req = request.get_ref(); |
| 97 | let dev_eui = EUI64::from_str(&req.dev_eui).map_err(|e| e.status())?; |
| 98 | |
| 99 | self.validator |
| 100 | .validate( |
| 101 | request.extensions(), |
| 102 | validator::ValidateDeviceAccess::new(validator::Flag::Read, dev_eui), |
| 103 | ) |
| 104 | .await?; |
| 105 | |
| 106 | let d = device::get(&dev_eui).await.map_err(|e| e.status())?; |
| 107 | |
| 108 | let mut resp = Response::new(api::GetDeviceResponse { |
| 109 | device: Some(api::Device { |
| 110 | dev_eui: d.dev_eui.to_string(), |
| 111 | name: d.name.clone(), |
| 112 | description: d.description.clone(), |
| 113 | application_id: d.application_id.to_string(), |
| 114 | device_profile_id: d.device_profile_id.to_string(), |
| 115 | skip_fcnt_check: d.skip_fcnt_check, |
| 116 | is_disabled: d.is_disabled, |
| 117 | variables: d.variables.into_hashmap(), |
| 118 | tags: d.tags.into_hashmap(), |
| 119 | join_eui: d.join_eui.to_string(), |
| 120 | }), |
| 121 | created_at: Some(helpers::datetime_to_prost_timestamp(&d.created_at)), |
| 122 | updated_at: Some(helpers::datetime_to_prost_timestamp(&d.updated_at)), |
| 123 | last_seen_at: d |
| 124 | .last_seen_at |
| 125 | .as_ref() |
| 126 | .map(helpers::datetime_to_prost_timestamp), |
| 127 | device_status: match d.margin.is_some() { |
| 128 | true => Some(api::DeviceStatus { |
| 129 | margin: d.margin.unwrap(), // we already know it is Some(v) |
| 130 | external_power_source: d.external_power_source, |
| 131 | battery_level: match d.battery_level { |
| 132 | Some(v) => v.to_f32().unwrap(), |
| 133 | None => -1.0, |
| 134 | }, |
| 135 | }), |
| 136 | false => None, |
| 137 | }, |
| 138 | class_enabled: d.enabled_class.to_proto().into(), |
| 139 | }); |
| 140 | resp.metadata_mut() |
| 141 | .insert("x-log-dev_eui", req.dev_eui.parse().unwrap()); |
| 142 | |
| 143 | Ok(resp) |
| 144 | } |
| 145 | |
| 146 | async fn update( |
| 147 | &self, |