(application_id: String, pl: integration::DownlinkCommand)
| 480 | } |
| 481 | |
| 482 | async fn handle_down_command(application_id: String, pl: integration::DownlinkCommand) { |
| 483 | let err = async { |
| 484 | info!(dev_eui = %pl.dev_eui, "Handling downlink command for device"); |
| 485 | let dev_eui = EUI64::from_str(&pl.dev_eui)?; |
| 486 | let app_id = Uuid::from_str(&application_id)?; |
| 487 | |
| 488 | // Validate that the application_id from the topic is indeed the application ID to which |
| 489 | // the device belongs. |
| 490 | let dev = device::get(&dev_eui).await?; |
| 491 | if Into::<Uuid>::into(dev.application_id) != app_id { |
| 492 | return Err(anyhow!( |
| 493 | "Application ID from topic does not match application ID from device" |
| 494 | )); |
| 495 | } |
| 496 | |
| 497 | let mut data = pl.data.clone(); |
| 498 | let mut f_port = pl.f_port as u8; |
| 499 | |
| 500 | if let Some(obj) = &pl.object { |
| 501 | let dp = device_profile::get(&dev.device_profile_id).await?; |
| 502 | |
| 503 | (f_port, data) = codec::struct_to_binary( |
| 504 | dp.payload_codec_runtime, |
| 505 | pl.f_port as u8, |
| 506 | &dev.variables, |
| 507 | &dp.payload_codec_script, |
| 508 | &codec::convert::pb_json_to_prost(obj), |
| 509 | ) |
| 510 | .await?; |
| 511 | } |
| 512 | |
| 513 | let qi = device_queue::DeviceQueueItem { |
| 514 | id: match pl.id.is_empty() { |
| 515 | true => Uuid::new_v4().into(), |
| 516 | false => Uuid::from_str(&pl.id)?.into(), |
| 517 | }, |
| 518 | f_port: f_port as i16, |
| 519 | confirmed: pl.confirmed, |
| 520 | data, |
| 521 | dev_eui, |
| 522 | expires_at: if let Some(expires_at) = pl.expires_at { |
| 523 | Some( |
| 524 | expires_at |
| 525 | .try_into() |
| 526 | .map_err(|e| anyhow!("Parse expires_at error: {}", e))?, |
| 527 | ) |
| 528 | } else { |
| 529 | None |
| 530 | }, |
| 531 | ..Default::default() |
| 532 | }; |
| 533 | |
| 534 | device_queue::enqueue_item(qi).await?; |
| 535 | |
| 536 | Ok(()) |
| 537 | } |
| 538 | .await |
| 539 | .err(); |
no test coverage detected