Skip to content

Commit

Permalink
Min/Max Support
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbosch committed Oct 22, 2024
1 parent 7b47ed9 commit 671fc59
Show file tree
Hide file tree
Showing 11 changed files with 1,442 additions and 441 deletions.
280 changes: 268 additions & 12 deletions databroker/src/broker.rs

Large diffs are not rendered by default.

232 changes: 166 additions & 66 deletions databroker/src/grpc/kuksa_val_v1/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::broker::SubscriptionError;
use crate::broker::{AuthorizedAccess, EntryReadAccess};
use crate::glob::Matcher;
use crate::permissions::Permissions;
use crate::types::{DataType, DataValue};

const MAX_REQUEST_PATH_LENGTH: usize = 1000;

Expand Down Expand Up @@ -792,72 +793,159 @@ fn proto_entry_from_entry_and_fields(
}
if all || fields.contains(&proto::Field::MetadataValueRestriction) {
metadata_is_set = true;
metadata.value_restriction = match entry.metadata().allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::StringArray(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::String(
proto::ValueRestrictionString {
allowed_values: vec.clone(),
},
)),
}),
broker::DataValue::Int32Array(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: vec.iter().cloned().map(i64::from).collect(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
broker::DataValue::Int64Array(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: vec.clone(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
broker::DataValue::Uint32Array(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Unsigned(
proto::ValueRestrictionUint {
allowed_values: vec.iter().cloned().map(u64::from).collect(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
broker::DataValue::Uint64Array(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Unsigned(
proto::ValueRestrictionUint {
allowed_values: vec.clone(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
broker::DataValue::FloatArray(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::FloatingPoint(
proto::ValueRestrictionFloat {
allowed_values: vec.iter().cloned().map(f64::from).collect(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
broker::DataValue::DoubleArray(vec) => Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::FloatingPoint(
proto::ValueRestrictionFloat {
allowed_values: vec.clone(),
min: None, // TODO: Implement
max: None, // TODO: Implement
},
)),
}),
_ => None,
},
None => None,
debug!("Datatype {:?} to be handled", entry.metadata().data_type);
match entry.metadata().data_type {
DataType::String | DataType::StringArray => {
let allowed = match entry.metadata().allowed.as_ref() {
Some(broker::DataValue::StringArray(vec)) => vec.clone(),
_ => Vec::new(),
};

if !allowed.is_empty() {
metadata.value_restriction = Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::String(
proto::ValueRestrictionString {
allowed_values: allowed,
},
)),
});
};
}
DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::Int8Array
| DataType::Int16Array
| DataType::Int32Array
| DataType::Int64Array => {
let min_value = match entry.metadata().min {
Some(DataValue::Int32(value)) => Some(i64::from(value)),
Some(DataValue::Int64(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let max_value = match entry.metadata().max {
Some(DataValue::Int32(value)) => Some(i64::from(value)),
Some(DataValue::Int64(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let allowed = match entry.metadata().allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Int32Array(vec) => {
vec.iter().cloned().map(i64::from).collect()
}
broker::DataValue::Int64Array(vec) => vec.to_vec(),
_ => Vec::new(),
},
_ => Vec::new(),
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
metadata.value_restriction = Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Signed(
proto::ValueRestrictionInt {
allowed_values: allowed,
min: min_value,
max: max_value,
},
)),
});
};
}
DataType::Uint8
| DataType::Uint16
| DataType::Uint32
| DataType::Uint64
| DataType::Uint8Array
| DataType::Uint16Array
| DataType::Uint32Array
| DataType::Uint64Array => {
let min_value = match entry.metadata().min {
Some(DataValue::Uint32(value)) => Some(u64::from(value)),
Some(DataValue::Uint64(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let max_value = match entry.metadata().max {
Some(DataValue::Uint32(value)) => Some(u64::from(value)),
Some(DataValue::Uint64(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let allowed = match entry.metadata().allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::Uint32Array(vec) => {
vec.iter().cloned().map(u64::from).collect()
}
broker::DataValue::Uint64Array(vec) => vec.to_vec(),
_ => Vec::new(),
},
_ => Vec::new(),
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
metadata.value_restriction = Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::Unsigned(
proto::ValueRestrictionUint {
allowed_values: allowed,
min: min_value,
max: max_value,
},
)),
});
};
}
DataType::Float
| DataType::Double
| DataType::FloatArray
| DataType::DoubleArray => {
let min_value = match entry.metadata().min {
Some(DataValue::Float(value)) => Some(f64::from(value)),
Some(DataValue::Double(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let max_value = match entry.metadata().max {
Some(DataValue::Float(value)) => Some(f64::from(value)),
Some(DataValue::Double(value)) => Some(value),
// Assumption here that we already have checked types
// so wither Int64 or None
_ => None,
};
let allowed = match entry.metadata().allowed.as_ref() {
Some(allowed) => match allowed {
broker::DataValue::FloatArray(vec) => {
vec.iter().cloned().map(f64::from).collect()
}
broker::DataValue::DoubleArray(vec) => vec.to_vec(),
_ => Vec::new(),
},
_ => Vec::new(),
};

if min_value.is_some() | max_value.is_some() | !allowed.is_empty() {
metadata.value_restriction = Some(proto::ValueRestriction {
r#type: Some(proto::value_restriction::Type::FloatingPoint(
proto::ValueRestrictionFloat {
allowed_values: allowed,
min: min_value,
max: max_value,
},
)),
});
};
}

_ => {
debug!("Datatype {:?} not yet handled", entry.metadata().data_type);
}
}
}
if all || fields.contains(&proto::Field::MetadataActuator) {
Expand Down Expand Up @@ -984,6 +1072,8 @@ impl broker::EntryUpdate {
data_type: None,
description: None,
allowed: None,
min: None,
max: None,
unit: None,
}
}
Expand All @@ -1007,6 +1097,8 @@ mod tests {
broker::ChangeType::OnChange,
broker::EntryType::Sensor,
"Test datapoint 1".to_owned(),
None, // min
None, // max
None,
None,
)
Expand Down Expand Up @@ -1062,6 +1154,8 @@ mod tests {
broker::ChangeType::OnChange,
broker::EntryType::Sensor,
"Test datapoint 1".to_owned(),
None, // min
None, // max
None,
Some("km/h".to_owned()),
)
Expand Down Expand Up @@ -1158,6 +1252,8 @@ mod tests {
broker::ChangeType::OnChange,
broker::EntryType::Sensor,
"Test datapoint 1".to_owned(),
None, // min
None, // max
None,
None,
)
Expand All @@ -1171,6 +1267,8 @@ mod tests {
broker::ChangeType::OnChange,
broker::EntryType::Sensor,
"Test branch datapoint 2".to_owned(),
None, // min
None, // max
None,
None,
)
Expand Down Expand Up @@ -1219,6 +1317,8 @@ mod tests {
broker::ChangeType::OnChange,
broker::EntryType::Sensor,
"Test datapoint 1".to_owned(),
None, // min
None, // max
None,
None,
)
Expand Down
Loading

0 comments on commit 671fc59

Please sign in to comment.