Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DevicePublisher API #10

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scripts/api_test_device_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# Predefined response
cell_data = [
{
"id": 62,
"cid": None,
"enodeB": 100344,
"pci": 62,
"type": "LTE",
"arfcn": 3350,
"band": "7",
Expand Down
27 changes: 23 additions & 4 deletions src/cell_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
pub cid: Option<u64>,
pub pci: Option<u64>,
pub r#type: String,
pub arfcn: u64,
pub band: String,
Expand Down Expand Up @@ -178,6 +180,21 @@ pub fn arfcn_to_frequency(arfcn: u64, cell_type: &CellularType) -> Result<u64> {
}
}

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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -612,7 +631,7 @@ mod tests {
fn test_devpub_from_celldata() -> Result<()> {
let cell_data = serde_json::from_str::<Vec<CellData>>(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
Expand Down
Loading