Skip to content

Commit

Permalink
Digital Twin Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbeitz committed Apr 18, 2024
1 parent 6a448bb commit 3144fec
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 48 deletions.
7 changes: 3 additions & 4 deletions digital-twin-model/src/sdv_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub mod basic_airbag_seat_massager {
#[derivative(Default(value = "crate::sdv_v1::context()"))]
pub context: Vec<String>,
#[serde(rename = "@id")]
pub instance_id: String,
pub instance_id: String,
#[serde(rename = "@type")]
#[derivative(Default(
value = "crate::sdv_v1::basic_airbag_seat_massager::ID.to_string()"
Expand Down Expand Up @@ -215,8 +215,7 @@ pub mod cabin {

#[derive(derivative::Derivative)]
#[derivative(Default)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize, Debug)]
#[derive(PartialEq)]
#[derive(serde_derive::Serialize, serde_derive::Deserialize, Debug, PartialEq)]
pub enum SEAT_POSITION_TYPE {
#[derivative(Default)]
/// No value
Expand Down Expand Up @@ -394,7 +393,7 @@ pub mod premium_airbag_seat_massager {
#[derivative(Default(value = "crate::sdv_v1::context()"))]
pub context: Vec<String>,
#[serde(rename = "@id")]
pub instance_id: String,
pub instance_id: String,
#[serde(rename = "@type")]
#[derivative(Default(
value = "crate::sdv_v1::premium_airbag_seat_massager::ID.to_string()"
Expand Down
59 changes: 45 additions & 14 deletions samples/graph/consumer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul

// Connect to the digital twin graph service.
let client = Retry::spawn(retry_strategy.clone(), || async {
DigitalTwinGraphClient::connect(invehicle_digital_twin_uri.clone()).await.map_err(|err_msg| format!("Unable to connect to the digital twin graph service due to: {err_msg}"))
DigitalTwinGraphClient::connect(invehicle_digital_twin_uri.clone()).await.map_err(
|err_msg| {
format!("Unable to connect to the digital twin graph service due to: {err_msg}")
},
)
})
.await?;

Expand All @@ -35,7 +39,10 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul

let request: FindRequest = FindRequest { model_id: sdv::vehicle::ID.to_string() };

client.find(request).await.map_err(|err_msg| format!("Unable to call find due to: {err_msg}"))
client
.find(request)
.await
.map_err(|err_msg| format!("Unable to call find due to: {err_msg}"))
})
.await?;
let find_vehicle_response_inner = find_vehicle_response.into_inner();
Expand All @@ -45,12 +52,13 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul
}

// For now, we will just use the first vehicle instance.
let vehicle: sdv::vehicle::TYPE = serde_json::from_str(&find_vehicle_response_inner.values[0]).unwrap();
let vehicle: sdv::vehicle::TYPE =
serde_json::from_str(&find_vehicle_response_inner.values[0]).unwrap();
// info!("The vehicle is: {:?}", vehicle);
info!("The vehicle's instance id is: {:?}", vehicle.instance_id);
if vehicle.cabin.is_empty() {
return Err("The vehicle does not have a cabin".to_string());
}
}
let cabin_instance_id = vehicle.cabin[0].instance_id.clone();
info!("The cabin's instance id is: {:?}", cabin_instance_id);

Expand All @@ -59,9 +67,13 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul
// Make a local mutable copy for use only within this closure body.
let mut client = client.clone();

let request: GetRequest = GetRequest { instance_id: cabin_instance_id.clone(), member_path: "".to_string() };
let request: GetRequest =
GetRequest { instance_id: cabin_instance_id.clone(), member_path: "".to_string() };

client.get(request.clone()).await.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
client
.get(request.clone())
.await
.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
})
.await?;
let get_cabin_response_inner = get_cabin_response.into_inner();
Expand All @@ -70,7 +82,9 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul
let cabin: sdv::cabin::TYPE = serde_json::from_str(&get_cabin_response_inner.value).unwrap();
// info!("The cabin is: {:?}", cabin);
for seat_relationship in cabin.seat.iter() {
if (seat_relationship.seat_row == 1) && (seat_relationship.seat_position == sdv::cabin::seat::SEAT_POSITION_TYPE::left) {
if (seat_relationship.seat_row == 1)
&& (seat_relationship.seat_position == sdv::cabin::seat::SEAT_POSITION_TYPE::left)
{
info!("The front left seat's instance id is: {:?}", seat_relationship.instance_id);
front_left_seat_instance_id_option = Some(seat_relationship.instance_id.clone());
}
Expand All @@ -87,9 +101,15 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul
// Make a local mutable copy for use only within this closure body.
let mut client = client.clone();

let request: GetRequest = GetRequest { instance_id: front_left_seat_instance_id.clone(), member_path: "".to_string() };
let request: GetRequest = GetRequest {
instance_id: front_left_seat_instance_id.clone(),
member_path: "".to_string(),
};

client.get(request.clone()).await.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
client
.get(request.clone())
.await
.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
})
.await?;
let get_seat_response_inner = get_seat_response.into_inner();
Expand All @@ -107,19 +127,30 @@ async fn interact_with_digital_twin(invehicle_digital_twin_uri: String) -> Resul
// Make a local mutable copy for use only within this closure body.
let mut client = client.clone();

let request: GetRequest = GetRequest { instance_id: seat_massager_instance_id.clone(), member_path: "".to_string() };
let request: GetRequest = GetRequest {
instance_id: seat_massager_instance_id.clone(),
member_path: "".to_string(),
};

client.get(request.clone()).await.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
client
.get(request.clone())
.await
.map_err(|err_msg| format!("Unable to call get due to: {err_msg}"))
})
.await?;
let get_seat_massager_response_inner = get_seat_massager_response.into_inner();

let seat_massager_json: serde_json::Value = serde_json::from_str(&get_seat_massager_response_inner.value).unwrap();
let seat_massager_json: serde_json::Value =
serde_json::from_str(&get_seat_massager_response_inner.value).unwrap();

if seat_massager_json["@type"] != sdv::premium_airbag_seat_massager::ID {
return Err(format!("The seat massager instance is not of the expected model, instead it is a {0}", seat_massager_json["@type"]));
return Err(format!(
"The seat massager instance is not of the expected model, instead it is a {0}",
seat_massager_json["@type"]
));
}
let seat_massager: sdv::premium_airbag_seat_massager::TYPE = serde_json::from_value(seat_massager_json.clone()).unwrap();
let seat_massager: sdv::premium_airbag_seat_massager::TYPE =
serde_json::from_value(seat_massager_json.clone()).unwrap();

info!("The seat massager is: {:?}", seat_massager);

Expand Down
58 changes: 33 additions & 25 deletions samples/graph/seat_massager_provider/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,27 @@ fn create_request_state() -> RequestState {
// Create the seat massagers.

let front_left_airbag_seat_massager_instance_id = "front_left_airbag_seat_massager".to_string();
let front_left_airbag_seat_massager: sdv::premium_airbag_seat_massager::TYPE = sdv::premium_airbag_seat_massager::TYPE {
instance_id: front_left_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
let front_left_airbag_seat_massager: sdv::premium_airbag_seat_massager::TYPE =
sdv::premium_airbag_seat_massager::TYPE {
instance_id: front_left_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
result.instance_map.insert(
front_left_airbag_seat_massager_instance_id.to_string(),
InstanceData {
model_id: sdv::premium_airbag_seat_massager::ID.to_string(),
description: sdv::premium_airbag_seat_massager::DESCRIPTION.to_string(),
serialized_value: serde_json::to_string(&front_left_airbag_seat_massager).unwrap(),
},
);
);

let front_right_airbag_seat_massager_instance_id = "front_right_airbag_seat_massager".to_string();
let front_right_airbag_seat_massager: sdv::premium_airbag_seat_massager::TYPE = sdv::premium_airbag_seat_massager::TYPE {
instance_id: front_right_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
let front_right_airbag_seat_massager_instance_id =
"front_right_airbag_seat_massager".to_string();
let front_right_airbag_seat_massager: sdv::premium_airbag_seat_massager::TYPE =
sdv::premium_airbag_seat_massager::TYPE {
instance_id: front_right_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
result.instance_map.insert(
front_right_airbag_seat_massager_instance_id.to_string(),
InstanceData {
Expand All @@ -58,24 +61,27 @@ fn create_request_state() -> RequestState {
);

let back_left_airbag_seat_massager_instance_id = "back_left_airbag_seat_massager".to_string();
let back_left_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE = sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_left_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
let back_left_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE =
sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_left_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
result.instance_map.insert(
back_left_airbag_seat_massager_instance_id.to_string(),
InstanceData {
model_id: sdv::basic_airbag_seat_massager::ID.to_string(),
description: sdv::basic_airbag_seat_massager::DESCRIPTION.to_string(),
serialized_value: serde_json::to_string(&back_left_airbag_seat_massager).unwrap(),
},
);
);

let back_center_airbag_seat_massager_instance_id = "back_center_airbag_seat_massager".to_string();
let back_center_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE = sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_center_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
let back_center_airbag_seat_massager_instance_id =
"back_center_airbag_seat_massager".to_string();
let back_center_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE =
sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_center_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
result.instance_map.insert(
back_center_airbag_seat_massager_instance_id.to_string(),
InstanceData {
Expand All @@ -86,10 +92,11 @@ fn create_request_state() -> RequestState {
);

let back_right_airbag_seat_massager_instance_id = "back_right_airbag_seat_massager".to_string();
let back_right_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE = sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_right_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
let back_right_airbag_seat_massager: sdv::basic_airbag_seat_massager::TYPE =
sdv::basic_airbag_seat_massager::TYPE {
instance_id: back_right_airbag_seat_massager_instance_id.clone(),
..Default::default()
};
result.instance_map.insert(
back_right_airbag_seat_massager_instance_id.to_string(),
InstanceData {
Expand Down Expand Up @@ -154,7 +161,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

info!("The Seat Massager Provider has started.");

let settings = provider_config::load_settings_with_config_filename("seat_massager_provider_settings");
let settings =
provider_config::load_settings_with_config_filename("seat_massager_provider_settings");

let provider_authority = settings.provider_authority;

Expand Down
12 changes: 7 additions & 5 deletions samples/graph/vehicle_core_provider/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ fn create_request_state() -> RequestState {

// Seat massager ids.

let front_left_airbag_seat_massager_instance_id = "front_left_airbag_seat_massager".to_string();
let front_left_airbag_seat_massager_instance_id = "front_left_airbag_seat_massager".to_string();

let front_right_airbag_seat_massager_instance_id = "front_right_airbag_seat_massager".to_string();
let front_right_airbag_seat_massager_instance_id =
"front_right_airbag_seat_massager".to_string();

let back_left_airbag_seat_massager_instance_id = "back_left_airbag_seat_massager".to_string();
let back_left_airbag_seat_massager_instance_id = "back_left_airbag_seat_massager".to_string();

let back_center_airbag_seat_massager_instance_id = "back_center_airbag_seat_massager".to_string();
let back_center_airbag_seat_massager_instance_id =
"back_center_airbag_seat_massager".to_string();

let back_right_airbag_seat_massager_instance_id = "back_right_airbag_seat_massager".to_string();

Expand Down Expand Up @@ -139,7 +141,7 @@ fn create_request_state() -> RequestState {
sdv::cabin::seat::RELATIONSHIP_TYPE {
instance_id: front_right_seat_instance_id.to_string(),
seat_row: 1,
seat_position: sdv::cabin::seat::SEAT_POSITION_TYPE::right,
seat_position: sdv::cabin::seat::SEAT_POSITION_TYPE::right,
},
sdv::cabin::seat::RELATIONSHIP_TYPE {
instance_id: back_left_seat_instance_id.to_string(),
Expand Down

0 comments on commit 3144fec

Please sign in to comment.