Skip to content

Commit

Permalink
don't make the user specify sensor IDs in config
Browse files Browse the repository at this point in the history
While working on #6354, it turned out that having to manually assign
these was really easy to mess up when you want to simulate a lot of
sensors, and there wasn't an easy way to get "real" ID values from an
inventory of a real life SP, so the config writer has to assign them
sequentially anyway. #6354 ended up using `component_details`
rather than `read_sensor`, so being able to assign sensor IDs identical
to those from Hubris turned out to not be that important, since (IIUC)
the only way to get the `SensorId`s is to look at the Hubris archive,
while *component* IDs can be discovered dynamically from the inventory
APIs.
  • Loading branch information
hawkw committed Aug 19, 2024
1 parent 866af8f commit 871bd72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
4 changes: 2 additions & 2 deletions sp-sim/examples/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ description = "FAKE Southwest temperature sensor"
capabilities = 2
presence = "Present"
sensors = [
{ name = "Southwest", kind = "Temperature", sensor_id = 0, last_data.value = 41.7890625, last_data.timestamp = 1234 },
{ name = "Southwest", kind = "Temperature", last_data.value = 41.7890625, last_data.timestamp = 1234 },
]

[[simulated_sps.gimlet]]
Expand All @@ -56,7 +56,7 @@ description = "FAKE Southwest temperature sensor"
capabilities = 2
presence = "Present"
sensors = [
{ name = "Southwest", kind = "Temperature", sensor_id = 0, last_data.value = 41.7890625, last_data.timestamp = 1234 },
{ name = "Southwest", kind = "Temperature", last_data.value = 41.7890625, last_data.timestamp = 1234 },
]


Expand Down
31 changes: 11 additions & 20 deletions sp-sim/src/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use std::collections::HashMap;

pub(crate) struct Sensors {
by_component: HashMap<SpComponent, Vec<u32>>,

sensors: HashMap<u32, Sensor>,
sensors: Vec<Sensor>,
}

#[derive(Debug)]
Expand All @@ -35,7 +34,6 @@ struct Sensor {
pub struct SensorDef {
pub name: String,
pub kind: MeasurementKind,
pub sensor_id: u32,
}

// TODO(eliza): note that currently, we just hardcode these in
Expand Down Expand Up @@ -104,7 +102,7 @@ impl Sensors {
pub(crate) fn from_component_configs<'a>(
cfgs: impl IntoIterator<Item = &'a SpComponentConfig>,
) -> Self {
let mut sensors = HashMap::new();
let mut sensors = Vec::new();
let mut by_component = HashMap::new();
for cfg in cfgs {
if cfg.sensors.is_empty() {
Expand All @@ -122,19 +120,11 @@ impl Sensors {

let mut ids = Vec::with_capacity(cfg.sensors.len());
for sensor in &cfg.sensors {
let sensor_id = sensor.def.sensor_id;
let prev = sensors.insert(
sensor_id,
Sensor {
def: sensor.def.clone(),
state: sensor.state.clone(),
},
);
assert!(
prev.is_none(),
"invalid config: sensor ID {sensor_id} already exists!\n
previous sensor: {prev:#?}\nnew sensor: {sensor:#?}"
);
let sensor_id = sensors.len() as u32;
sensors.push(Sensor {
def: sensor.def.clone(),
state: sensor.state.clone(),
});
ids.push(sensor_id)
}

Expand All @@ -150,8 +140,8 @@ impl Sensors {
component: &SpComponent,
index: BoundsChecked,
) -> Option<&'sensors Sensor> {
let id = self.by_component.get(component)?.get(index.0 as usize)?;
self.sensors.get(id)
let &id = self.by_component.get(component)?.get(index.0 as usize)?;
self.sensors.get(id as usize)
}

pub(crate) fn num_component_details(
Expand Down Expand Up @@ -201,7 +191,8 @@ impl Sensors {
&self,
SensorRequest { id, kind }: SensorRequest,
) -> Result<SensorResponse, SensorError> {
let sensor = self.sensors.get(&id).ok_or(SensorError::InvalidSensor)?;
let sensor =
self.sensors.get(id as usize).ok_or(SensorError::InvalidSensor)?;
match kind {
SensorRequestKind::LastReading => {
Ok(SensorResponse::LastReading(sensor.state.last_reading()))
Expand Down

0 comments on commit 871bd72

Please sign in to comment.