Skip to content

Commit

Permalink
implement reids review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed Aug 2, 2024
1 parent 2e68c61 commit b4eda5b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ use tracing::{info, warn};

use crate::{command_data::CommandData, error::ScyllaError};

/// the prefix for the calypso topic, so topic of cmd is this plus the key appended on
pub const CALYPSO_BIDIR_CMD_PREFIX: &str = "Calypso/Bidir/Command/";

#[derive(Deserialize, Debug)]
pub struct ConfigRequest {
pub data: Vec<f32>,
}

/// This controller recieves two peices of info: the key from the path, and the list of float values from the query params
/// The key is used in accordance with the calypso specification: a command_data object is sent over siren to CALYPSO_BIDIR_CMD_PREFIX/<key>
/// That command_data object is processed and matched via the key to a YAML specification for the encoding and sending of a CAN message
/// TLDR: This triggers a command <key> with the values <data_query.0.data> for calypso to update the CAN packet sent to the bus.
pub async fn send_config(
Path(key): Path<String>,
data_query: Query<ConfigRequest>,
Expand All @@ -25,16 +30,19 @@ pub async fn send_config(
"Sending car config with key: {}, and values: {:?}",
key, data_query.0.data
);
// disable scylla if not prod, as there will be None mqtt client
let Some(client) = client else {
return Err(ScyllaError::NotProd);
};

// create a payload object of the values to be parsed by calypso into a CAN packet
let mut payload = CommandData::new();
payload.data = data_query.0.data;
let Ok(bytes) = payload.write_to_bytes() else {
return Err(ScyllaError::ImpossibleEncoding);
};

// publish the message to the topic that calypso's encoder is susbcribed to
if let Err(err) = client
.publish(
format!("{}{}", CALYPSO_BIDIR_CMD_PREFIX, key),
Expand All @@ -47,5 +55,6 @@ pub async fn send_config(
warn!("Could not publish instruction: {}", err);
return Err(ScyllaError::CommFailure);
}

Ok(())
}
2 changes: 1 addition & 1 deletion scylla-server-rust/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod car_command_controller;
pub mod data_controller;
pub mod data_type_controller;
pub mod driver_controller;
pub mod location_controller;
pub mod node_controller;
pub mod run_controller;
pub mod send_config_controller;
pub mod system_controller;
12 changes: 6 additions & 6 deletions scylla-server-rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use prisma_client_rust::chrono;
use rumqttc::v5::AsyncClient;
use scylla_server_rust::{
controllers::{
self, data_type_controller, driver_controller, location_controller, node_controller,
run_controller,
send_config_controller::{self},
system_controller,
self,
car_command_controller::{self},
data_type_controller, driver_controller, location_controller, node_controller,
run_controller, system_controller,
},
prisma::PrismaClient,
processors::{
Expand Down Expand Up @@ -200,8 +200,8 @@ async fn main() {
.route("/systems", get(system_controller::get_all_systems))
// CONFIG
.route(
"/config/:key",
post(send_config_controller::send_config).layer(Extension(client)),
"/config/set/:configKey",
post(car_command_controller::send_config).layer(Extension(client)),
)
// for CORS handling
.layer(
Expand Down
2 changes: 1 addition & 1 deletion scylla-server-rust/src/processors/mqtt_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, instrument, trace, warn, Level};

use crate::{
controllers::send_config_controller::CALYPSO_BIDIR_CMD_PREFIX, serverdata,
controllers::car_command_controller::CALYPSO_BIDIR_CMD_PREFIX, serverdata,
services::run_service,
};

Expand Down

0 comments on commit b4eda5b

Please sign in to comment.