From 12471308a3bece05f6503873a0b92a1a1533260b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Arg=C3=A9rus?= Date: Tue, 24 Sep 2024 17:20:22 +0200 Subject: [PATCH] Add allowed values to metadata --- databroker-cli/src/sdv_cli.rs | 3 + databroker/src/grpc/kuksa_val_v1/val.rs | 68 ++++++++++++++++++- .../src/grpc/sdv_databroker_v1/conversions.rs | 48 +++++++++++++ proto/sdv/databroker/v1/types.proto | 15 +++- 4 files changed, 131 insertions(+), 3 deletions(-) diff --git a/databroker-cli/src/sdv_cli.rs b/databroker-cli/src/sdv_cli.rs index 0bf91b0d..e52dd642 100644 --- a/databroker-cli/src/sdv_cli.rs +++ b/databroker-cli/src/sdv_cli.rs @@ -1262,6 +1262,7 @@ mod test { entry_type: proto::v1::EntryType::Sensor.into(), change_type: proto::v1::ChangeType::OnChange.into(), description: "".into(), + allowed: None, }, proto::v1::Metadata { id: 2, @@ -1270,6 +1271,7 @@ mod test { entry_type: proto::v1::EntryType::Sensor.into(), change_type: proto::v1::ChangeType::OnChange.into(), description: "".into(), + allowed: None, }, proto::v1::Metadata { id: 3, @@ -1278,6 +1280,7 @@ mod test { entry_type: proto::v1::EntryType::Sensor.into(), change_type: proto::v1::ChangeType::OnChange.into(), description: "".into(), + allowed: None, }, ] .to_vec(); diff --git a/databroker/src/grpc/kuksa_val_v1/val.rs b/databroker/src/grpc/kuksa_val_v1/val.rs index afb42f79..9cbbfc8d 100644 --- a/databroker/src/grpc/kuksa_val_v1/val.rs +++ b/databroker/src/grpc/kuksa_val_v1/val.rs @@ -792,7 +792,73 @@ fn proto_entry_from_entry_and_fields( } if all || fields.contains(&proto::Field::MetadataValueRestriction) { metadata_is_set = true; - // TODO: Add to Metadata + 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, + } } if all || fields.contains(&proto::Field::MetadataActuator) { metadata_is_set = true; diff --git a/databroker/src/grpc/sdv_databroker_v1/conversions.rs b/databroker/src/grpc/sdv_databroker_v1/conversions.rs index e028c390..d52600e6 100644 --- a/databroker/src/grpc/sdv_databroker_v1/conversions.rs +++ b/databroker/src/grpc/sdv_databroker_v1/conversions.rs @@ -311,6 +311,54 @@ impl From<&broker::Metadata> for proto::Metadata { data_type: proto::DataType::from(&metadata.data_type) as i32, change_type: proto::ChangeType::Continuous as i32, // TODO: Add to metadata description: metadata.description.to_owned(), + allowed: match metadata.allowed.as_ref() { + Some(broker::DataValue::StringArray(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::StringValues(proto::StringArray { + values: vec.clone(), + })), + }), + Some(broker::DataValue::Int32Array(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::Int32Values(proto::Int32Array { + values: vec.clone(), + })), + }), + Some(broker::DataValue::Int64Array(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::Int64Values(proto::Int64Array { + values: vec.clone(), + })), + }), + Some(broker::DataValue::Uint32Array(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::Uint32Values(proto::Uint32Array { + values: vec.clone(), + })), + }), + Some(broker::DataValue::Uint64Array(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::Uint64Values(proto::Uint64Array { + values: vec.clone(), + })), + }), + Some(broker::DataValue::FloatArray(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::FloatValues(proto::FloatArray { + values: vec.clone(), + })), + }), + Some(broker::DataValue::DoubleArray(vec)) => Some(proto::Allowed { + values: Some(proto::allowed::Values::DoubleValues(proto::DoubleArray { + values: vec.clone(), + })), + }), + Some(broker::DataValue::BoolArray(_)) + | Some(broker::DataValue::NotAvailable) + | Some(broker::DataValue::Bool(_)) + | Some(broker::DataValue::String(_)) + | Some(broker::DataValue::Int32(_)) + | Some(broker::DataValue::Int64(_)) + | Some(broker::DataValue::Uint32(_)) + | Some(broker::DataValue::Uint64(_)) + | Some(broker::DataValue::Float(_)) + | Some(broker::DataValue::Double(_)) + | None => None, + }, } } } diff --git a/proto/sdv/databroker/v1/types.proto b/proto/sdv/databroker/v1/types.proto index 6790377c..44988098 100644 --- a/proto/sdv/databroker/v1/types.proto +++ b/proto/sdv/databroker/v1/types.proto @@ -143,9 +143,19 @@ message Datapoint { } } +message Allowed { + oneof values { + StringArray string_values = 1; + Int32Array int32_values = 3; + Int64Array int64_values = 4; + Uint32Array uint32_values = 5; + Uint64Array uint64_values = 6; + FloatArray float_values = 7; + DoubleArray double_values = 8; + } +} + message Metadata { - // Id to be used in "get" and "subscribe" requests. Ids stay valid during - // one power cycle, only. int32 id = 1; EntryType entry_type = 2; string name = 4; @@ -153,6 +163,7 @@ message Metadata { ChangeType change_type = 6; // CONTINUOUS or STATIC or ON_CHANGE string description = 7; + Allowed allowed = 10; // int32 min_update_hz = 10; // Only for CONTINUOUS // int32 max_update_hz = 11; // Only for CONTINUOUS };