Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ clap = { version = "4.6.1", features = ["derive"] }
fs_extra = "1.3.0"
futures = "0.3.32"
futures-util = "0.3.32"
hopper = { git = "https://github.com/systemetric/hopper", rev = "v0.1.3", version = "0.1.3" }
hopper = { git = "https://github.com/systemetric/hopper", rev = "v0.1.5", version = "0.1.5" }
nix = { version = "0.31.3", features=["fs", "user"] }
rumqttc = { version = "0.25.1", default-features = false }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
Expand All @@ -39,4 +40,5 @@ tower-http = { version = "0.6.8", features = ["fs", "trace"] }
tower-service = "0.3.3"
tracing = "0.1.44"
tracing-subscriber = "0.3.23"
walkdir = "2.5.0"
zip = "8.5.1"
30 changes: 10 additions & 20 deletions crates/shepherd-app/src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use axum::{
http::StatusCode,
routing::post,
};
use shepherd_common::config::Config;
use shepherd_common::{Mode, Zone, config::Config};
use shepherd_mqtt::{
MqttAsyncClient,
messages::{PatchStatus, PatchStatusMessage},
messages::{ControlMessage, ControlMessageType},
};
use tracing::info;
use zip::ZipArchive;
Expand All @@ -20,7 +20,7 @@ use crate::error::{ShepherdError, ShepherdResult};
struct PatchState {
patch_dir: PathBuf,
mqttc: MqttAsyncClient,
patch_status: String,
robot_control: String,
}

async fn process_zip(state: PatchState, mut field: Field<'_>) -> ShepherdResult<()> {
Expand Down Expand Up @@ -55,11 +55,13 @@ async fn process_zip(state: PatchState, mut field: Field<'_>) -> ShepherdResult<
state
.mqttc
.publish(
&state.patch_status,
PatchStatusMessage {
status: PatchStatus::Apply,
&state.robot_control,
ControlMessage {
_type: ControlMessageType::Patch,
mode: Mode::Dev,
zone: Zone::Red,
},
true,
false,
)
.await
.map_err(|e| ShepherdError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Expand All @@ -68,18 +70,6 @@ async fn process_zip(state: PatchState, mut field: Field<'_>) -> ShepherdResult<
}

async fn upload(State(state): State<PatchState>, mut multipart: Multipart) -> ShepherdResult<()> {
state
.mqttc
.publish(
&state.patch_status,
PatchStatusMessage {
status: PatchStatus::Receive,
},
true,
)
.await
.map_err(|e| ShepherdError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

while let Some(field) = multipart
.next_field()
.await
Expand Down Expand Up @@ -110,6 +100,6 @@ pub fn router(config: &Config, mqttc: MqttAsyncClient) -> Router {
.with_state(PatchState {
patch_dir: config.app.patch_dir.clone(),
mqttc,
patch_status: config.channel.patch_status.clone(),
robot_control: config.channel.robot_control.clone(),
})
}
59 changes: 52 additions & 7 deletions crates/shepherd-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct Config {
pub channel: ChannelConfig,
#[serde(default)]
pub path: PathConfig,
#[serde(default)]
pub patch: PatchConfig,
#[serde(default)]
pub hopper: HopperConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -250,8 +254,6 @@ pub struct ChannelConfig {
pub user_state: String,
#[serde(default = "default_status")]
pub status: String,
#[serde(default = "default_patch_status")]
pub patch_status: String,
}

fn default_channel_robot_control() -> String {
Expand All @@ -269,9 +271,6 @@ fn default_user_status() -> String {
fn default_status() -> String {
"status".to_string()
}
fn default_patch_status() -> String {
"patch/status".to_string()
}

impl Default for ChannelConfig {
fn default() -> Self {
Expand All @@ -281,7 +280,6 @@ impl Default for ChannelConfig {
camera: default_channel_camera(),
user_state: default_user_status(),
status: default_status(),
patch_status: default_patch_status(),
}
}
}
Expand Down Expand Up @@ -340,6 +338,54 @@ impl Default for PathConfig {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PatchConfig {
#[serde(default = "default_patch_uid")]
pub uid: u32,
#[serde(default = "default_patch_gid")]
pub gid: u32,
#[serde(default = "default_patch_working_dir")]
pub working_dir: PathBuf,
}

fn default_patch_uid() -> u32 {
0u32
}
fn default_patch_gid() -> u32 {
0u32
}
fn default_patch_working_dir() -> PathBuf {
PathBuf::from("/")
}

impl Default for PatchConfig {
fn default() -> Self {
Self {
uid: default_patch_uid(),
gid: default_patch_gid(),
working_dir: default_patch_working_dir(),
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HopperConfig {
#[serde(default = "default_hopper_gid")]
pub gid: Option<u32>,
}

fn default_hopper_gid() -> Option<u32> {
None
}

impl Default for HopperConfig {
fn default() -> Self {
Self {
gid: default_hopper_gid(),
}
}
}

impl Config {
pub fn from_file(path: Option<&Path>) -> Result<Self> {
let path = path.unwrap_or(Path::new(DEFAULT_CONFIG_PATH));
Expand Down Expand Up @@ -375,7 +421,6 @@ impl Config {
}

_create_dir(&self.path.root);
_create_dir(&self.path.hopper);
_create_dir(&self.path.user_cur_dir);

_create_dir(&self.app.static_dir);
Expand Down
14 changes: 1 addition & 13 deletions crates/shepherd-mqtt/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum ControlMessageType {
Start,
Stop,
Reset,
Patch,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -41,16 +42,3 @@ pub struct StatusMessage {
pub struct StatusSummary {
pub statuses: Vec<StatusMessage>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PatchStatus {
Receive,
Apply,
Failed,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PatchStatusMessage {
pub status: PatchStatus,
}
2 changes: 2 additions & 0 deletions crates/shepherd-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ license.workspace = true
anyhow.workspace = true
base64.workspace = true
hopper.workspace = true
nix.workspace = true
serde.workspace = true
serde_json.workspace = true
shepherd-common = { path = "../shepherd-common" }
shepherd-mqtt = { path = "../shepherd-mqtt" }
tokio.workspace = true
tokio-gpiod.workspace = true
tracing.workspace = true
walkdir.workspace = true
25 changes: 25 additions & 0 deletions crates/shepherd-run/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::usercode::{Usercode, UsercodeHandle};
pub enum StateEvent {
Transition(RunState, Option<RunState>),
SetTarget(Mode, Zone),
SpawnPatch,
}

pub struct Runner {
Expand Down Expand Up @@ -209,6 +210,24 @@ impl Runner {

info!("update (mode, zone) to ({:?}, {:?})", mode, zone);
}
StateEvent::SpawnPatch => {
if self.state != RunState::PostRun {
warn!(
"cannot spawn patch, requires {:?}, got {:?}",
RunState::PostRun,
self.state
);
continue;
}

if let Some(uh) = &self.usercode_handle {
uh.start_patch()?;
} else {
return Err(anyhow!(
"tried to start patch, but usercode handle was not set?"
));
}
}
}
}

Expand All @@ -232,6 +251,11 @@ impl Runner {
ControlMessageType::Reset => {
sender.send(StateEvent::Transition(RunState::Ready, None))?
}
ControlMessageType::Patch => {
// patch should overrides user code
sender.send(StateEvent::Transition(RunState::PostRun, None))?;
sender.send(StateEvent::SpawnPatch)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -284,6 +308,7 @@ impl Runner {
&self.config.run.service_id,
&self.config.channel.camera,
Some(&self.config.path.hopper),
self.config.hopper.gid,
)?;
image_pipe.open()?;
self.image_pipe = Some(Arc::new(image_pipe));
Expand Down
Loading
Loading