| 9 | use chirpstack_api::integration as integration_pb; |
| 10 | |
| 11 | pub async fn handle( |
| 12 | uplink_frame_set: &UplinkFrameSet, |
| 13 | tenant: &tenant::Tenant, |
| 14 | app: &application::Application, |
| 15 | dp: &device_profile::DeviceProfile, |
| 16 | dev: &device::Device, |
| 17 | block: &lrwn::MACCommandSet, |
| 18 | ) -> Result<Option<lrwn::MACCommandSet>> { |
| 19 | let mac = (**block) |
| 20 | .first() |
| 21 | .ok_or_else(|| anyhow!("Expected DevStatusAns"))?; |
| 22 | if let lrwn::MACCommand::DevStatusAns(pl) = mac { |
| 23 | info!(dev_eui = %dev.dev_eui, battery = pl.battery, margin = pl.margin, "DevStatusAns received"); |
| 24 | |
| 25 | device::partial_update( |
| 26 | dev.dev_eui, |
| 27 | &device::DeviceChangeset { |
| 28 | margin: Some(pl.margin as i32), |
| 29 | external_power_source: Some(pl.battery == 0), |
| 30 | battery_level: Some(if pl.battery > 0 && pl.battery < 255 { |
| 31 | let v: fields::BigDecimal = ((pl.battery as f32) / 254.0 * 100.0).try_into()?; |
| 32 | Some(v.with_scale(2).into()) |
| 33 | } else { |
| 34 | None |
| 35 | }), |
| 36 | ..Default::default() |
| 37 | }, |
| 38 | ) |
| 39 | .await?; |
| 40 | |
| 41 | let mut tags = (*app.tags).clone(); |
| 42 | tags.extend((*dp.tags).clone()); |
| 43 | tags.extend((*dev.tags).clone()); |
| 44 | |
| 45 | let rx_time: DateTime<Utc> = |
| 46 | helpers::get_rx_timestamp(&uplink_frame_set.rx_info_set).into(); |
| 47 | |
| 48 | integration::status_event( |
| 49 | app.id.into(), |
| 50 | &dev.variables, |
| 51 | &integration_pb::StatusEvent { |
| 52 | deduplication_id: uplink_frame_set.uplink_set_id.to_string(), |
| 53 | time: Some(rx_time.into()), |
| 54 | device_info: Some(integration_pb::DeviceInfo { |
| 55 | tenant_id: tenant.id.to_string(), |
| 56 | tenant_name: tenant.name.clone(), |
| 57 | application_id: app.id.to_string(), |
| 58 | application_name: app.name.to_string(), |
| 59 | device_profile_id: dp.id.to_string(), |
| 60 | device_profile_name: dp.name.clone(), |
| 61 | device_name: dev.name.clone(), |
| 62 | device_class_enabled: dev.enabled_class.to_proto().into(), |
| 63 | dev_eui: dev.dev_eui.to_string(), |
| 64 | tags, |
| 65 | }), |
| 66 | margin: pl.margin as i32, |
| 67 | external_power_source: pl.battery == 0, |
| 68 | battery_level_unavailable: pl.battery == 255, |