| 32 | #[tonic::async_trait] |
| 33 | impl GatewayService for Gateway { |
| 34 | async fn create( |
| 35 | &self, |
| 36 | request: Request<api::CreateGatewayRequest>, |
| 37 | ) -> Result<Response<()>, Status> { |
| 38 | let req_gw = match &request.get_ref().gateway { |
| 39 | Some(v) => v, |
| 40 | None => { |
| 41 | return Err(Status::invalid_argument("gateway is missing")); |
| 42 | } |
| 43 | }; |
| 44 | let tenant_id = Uuid::from_str(&req_gw.tenant_id).map_err(|e| e.status())?; |
| 45 | |
| 46 | self.validator |
| 47 | .validate( |
| 48 | request.extensions(), |
| 49 | validator::ValidateGatewaysAccess::new(validator::Flag::Create, tenant_id), |
| 50 | ) |
| 51 | .await?; |
| 52 | |
| 53 | let (lat, lon, alt) = match &req_gw.location { |
| 54 | Some(v) => (v.latitude, v.longitude, v.altitude as f32), |
| 55 | None => (0.0, 0.0, 0.0), |
| 56 | }; |
| 57 | |
| 58 | let gw = gateway::Gateway { |
| 59 | gateway_id: EUI64::from_str(&req_gw.gateway_id).map_err(|e| e.status())?, |
| 60 | tenant_id: tenant_id.into(), |
| 61 | name: req_gw.name.clone(), |
| 62 | description: req_gw.description.clone(), |
| 63 | latitude: lat, |
| 64 | longitude: lon, |
| 65 | altitude: alt, |
| 66 | tags: fields::KeyValue::new(req_gw.tags.clone()), |
| 67 | stats_interval_secs: req_gw.stats_interval as i32, |
| 68 | downlink_priority: req_gw.downlink_priority as i16, |
| 69 | ..Default::default() |
| 70 | }; |
| 71 | |
| 72 | let _ = gateway::create(gw).await.map_err(|e| e.status())?; |
| 73 | |
| 74 | let mut resp = Response::new(()); |
| 75 | resp.metadata_mut() |
| 76 | .insert("x-log-gateway_id", req_gw.gateway_id.parse().unwrap()); |
| 77 | |
| 78 | Ok(resp) |
| 79 | } |
| 80 | |
| 81 | async fn get( |
| 82 | &self, |