diff --git a/scripts/api_test_device_publisher.py b/scripts/api_test_device_publisher.py index 7777d1f..61562c1 100755 --- a/scripts/api_test_device_publisher.py +++ b/scripts/api_test_device_publisher.py @@ -7,7 +7,9 @@ # Predefined response cell_data = [ { - "id": 62, + "cid": None, + "enodeB": 100344, + "pci": 62, "type": "LTE", "arfcn": 3350, "band": "7", diff --git a/src/cell_info.rs b/src/cell_info.rs index 8c32750..bd86b1a 100644 --- a/src/cell_info.rs +++ b/src/cell_info.rs @@ -48,7 +48,9 @@ pub struct SingleCell { #[derive(Debug, Clone, Deserialize)] #[allow(non_snake_case, dead_code)] pub struct CellData { - pub id: u64, + pub nodeB: Option, + pub cid: Option, + pub pci: Option, pub r#type: String, pub arfcn: u64, pub band: String, @@ -178,6 +180,21 @@ pub fn arfcn_to_frequency(arfcn: u64, cell_type: &CellularType) -> Result { } } +impl CellData { + /// Returns the first non-`None` identifier among `cid`, `pci`, and `nodeB`. + /// Returns `0` as fallback if all are `None`. + /// + /// # Examples + /// + /// ``` + /// let cell_data = CellData { cid: None, pci: Some(456), nodeB: None }; + /// assert_eq!(cell_data.safe_id(), 456); + /// ``` + pub fn safe_id(&self) -> u64 { + self.cid.or(self.pci).or(self.nodeB).unwrap_or(0) + } +} + impl CellInfo { // Do not rely on cell_id, just check if frequency and cell_type are the same pub fn equal_content(info_a: &CellInfo, info_b: &CellInfo) -> bool { @@ -318,7 +335,7 @@ impl CellInfo { let mut cell_info = CellInfo { cells: vec![] }; for cell in cell_data.iter() { let mut single_cell = SingleCell { - cell_id: cell.id, + cell_id: cell.safe_id(), cell_type: CellularType::from_str(&cell.r#type)?, frequency: 0, rssi: cell.rssi, @@ -474,7 +491,9 @@ mod tests { const DUMMY_DEVICEPUBLISHER_RESPONSE: &str = r#"[ { - "id": 10, + "nodeB": 20321, + "cid": null, + "pci": null, "type": "LTE", "arfcn": 1801, "band": "1800", @@ -612,7 +631,7 @@ mod tests { fn test_devpub_from_celldata() -> Result<()> { let cell_data = serde_json::from_str::>(DUMMY_DEVICEPUBLISHER_RESPONSE)?; let cell_info = CellInfo::from_devpub_celldata(cell_data)?; - assert_eq!(cell_info.cells.first().unwrap().cell_id, 10); + assert_eq!(cell_info.cells.first().unwrap().cell_id, 20321); assert_eq!( cell_info.cells.first().unwrap().cell_type, CellularType::LTE