-
Notifications
You must be signed in to change notification settings - Fork 0
Implement sequences #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fweichselbaum
wants to merge
20
commits into
main
Choose a base branch
from
feat/sequences
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f15427b
add crates serde, toml & config
fweichselbaum f391e02
add json schema for sequences
fweichselbaum cb2f2aa
add derive feature for crate serde
fweichselbaum ef303b4
add toml sequences for sequence parsing tests
fweichselbaum ae11b14
add sequence control events
fweichselbaum 4fee8d2
add sequence definitions and parsing
fweichselbaum 1bc7a14
add sequence runner
fweichselbaum 564014a
spawn sequence runner thread
fweichselbaum 8f8d464
remove duplicate sequence loading logic
fweichselbaum 248d90e
fix shutdown behaviour of running sequences
fweichselbaum 79818ee
fix `minItems` for hold and set_param step
fweichselbaum bc25e8a
add check that sequence start time <= 0
fweichselbaum cecbadd
add check that sequence step needs at least 1 action
fweichselbaum 85633dd
add check that conditional hold needs at least 1 condition
fweichselbaum 5dc36f5
fixed test sequences syntax
fweichselbaum bf6226e
fixed tests in sequence definition by checking for error messages
fweichselbaum c81802a
improve sequence validation
fweichselbaum 0c964cc
add crate ntest for test timeouts
fweichselbaum 0f72f85
extract sequence validation function into separate functions
fweichselbaum 628f000
rework sequence building and running + tests
fweichselbaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "$defs": { | ||
| "setParam": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "timestamp", | ||
| "param", | ||
| "value" | ||
| ], | ||
| "properties": { | ||
| "timestamp": { | ||
| "type": "number" | ||
| }, | ||
| "param": { | ||
| "type": "string" | ||
| }, | ||
| "value": { | ||
| "type": "number" | ||
| } | ||
| } | ||
| }, | ||
| "holdcondition": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "field", | ||
| "is", | ||
| "value" | ||
| ], | ||
| "properties": { | ||
| "field": { | ||
| "type": "string" | ||
| }, | ||
| "is": { | ||
| "enum": [ | ||
| "equal", | ||
| "not_eq", | ||
| "less", | ||
| "less_eq", | ||
| "greater", | ||
| "greater_eq" | ||
| ] | ||
| }, | ||
| "value": { | ||
| "type": "number" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "title": "FerroFlow-Sequence", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "name", | ||
| "globals", | ||
| "steps" | ||
| ], | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string" | ||
| }, | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "globals": { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "start_time", | ||
| "end_time", | ||
| "interpolations", | ||
| "interpolation_interval" | ||
| ], | ||
| "properties": { | ||
| "start_time": { | ||
| "type": "number" | ||
| }, | ||
| "end_time": { | ||
| "type": "number" | ||
| }, | ||
| "interpolation_interval": { | ||
| "type": "number", | ||
| "exclusiveMinimum": 0 | ||
| }, | ||
| "interpolations": { | ||
| "type": "object", | ||
| "patternProperties": { | ||
| "^.+$": { | ||
| "type": "string", | ||
| "enum": [ | ||
| "linear", | ||
| "none" | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "steps": { | ||
| "type": "array", | ||
| "items": { | ||
| "anyOf": [ | ||
| { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "name", | ||
| "timestamp", | ||
| "hold" | ||
| ], | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "description": { | ||
| "type": "string" | ||
| }, | ||
| "timestamp": { | ||
| "type": "number" | ||
| }, | ||
| "hold": { | ||
| "type": "array", | ||
| "minItems": 1, | ||
| "items": { | ||
| "$ref": "#/$defs/holdcondition" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "name", | ||
| "timestamp", | ||
| "hold" | ||
| ], | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "description": { | ||
| "type": "string" | ||
| }, | ||
| "timestamp": { | ||
| "type": "number" | ||
| }, | ||
| "hold": { | ||
| "type": "string", | ||
| "const": "always" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": [ | ||
| "name", | ||
| "timestamp", | ||
| "set_params" | ||
| ], | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "description": { | ||
| "type": "string" | ||
| }, | ||
| "timestamp": { | ||
| "type": "number" | ||
| }, | ||
| "set_params": { | ||
| "type": "array", | ||
| "minItems": 1, | ||
| "items": { | ||
| "$ref": "#/$defs/setParam" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,77 @@ | ||
| //! Code for managing and running sequences. | ||
|
|
||
| #![allow(unused)] | ||
|
|
||
| use std::{ | ||
| sync::mpsc, | ||
| thread, | ||
| time::{Duration, Instant}, | ||
| mod sequence_builder; | ||
| mod sequence_definition; | ||
| mod sequence_runner; | ||
| mod sequence_validation; | ||
|
|
||
| use std::path::Path; | ||
|
|
||
| use crate::{ | ||
| events, | ||
| sequence::{ | ||
| sequence_definition::Sequence, | ||
| sequence_runner::{SequenceCmd, SequenceRunner}, | ||
| }, | ||
| }; | ||
|
|
||
| pub struct Sequence { | ||
| name: String, | ||
| steps: Vec<SequenceStep>, | ||
| } | ||
|
|
||
| struct SequenceStep { | ||
| description: String, | ||
| delay_from_start_ms: u64, | ||
| action: (), // TODO: how are steps defined? | ||
| } | ||
|
|
||
| pub struct SequenceHandle { | ||
| cancel_tx: mpsc::Sender<()>, | ||
| thread_handle: thread::JoinHandle<()>, | ||
| } | ||
|
|
||
| impl SequenceHandle { | ||
| /// Signals the sequence to stop executing further steps. | ||
| pub fn cancel(self) { | ||
| let _ = self.cancel_tx.send(()); | ||
| } | ||
|
|
||
| /// Blocks until the sequence finishes (or is cancelled). | ||
| pub fn wait(self) -> thread::Result<()> { | ||
| self.thread_handle.join() | ||
| } | ||
| } | ||
|
|
||
| pub fn run_sequence(mut seq: Sequence) -> SequenceHandle { | ||
| // Create a channel for our interrupt signal | ||
| let (cancel_tx, cancel_rx) = mpsc::channel(); | ||
|
|
||
| // Sort steps. | ||
| // TODO: We could probably require that they are already sorted at this point. | ||
| seq.steps.sort_by_key(|s| s.delay_from_start_ms); | ||
|
|
||
| let thread_handle = thread::spawn(move || { | ||
| println!("Starting sequence: {}", seq.name); | ||
|
|
||
| let start_time = Instant::now(); | ||
|
|
||
| for step in seq.steps { | ||
| // Calculate the absolute target time for this specific step | ||
| let target_time = start_time + Duration::from_millis(step.delay_from_start_ms); | ||
| let now = Instant::now(); | ||
|
|
||
| // If the target time is in the future, we need to wait | ||
| if target_time > now { | ||
| let wait_duration = target_time - now; | ||
|
|
||
| // recv_timeout blocks until a message is received OR the timeout is reached. | ||
| match cancel_rx.recv_timeout(wait_duration) { | ||
| Ok(_) => { | ||
| println!("Sequence '{}' interrupted! Aborting.", seq.name); | ||
| return; | ||
| } | ||
| Err(mpsc::RecvTimeoutError::Disconnected) => { | ||
| // The caller dropped the handle without explicitly calling cancel(). | ||
| println!("Sequence handle dropped. Aborting '{}'.", seq.name); | ||
| return; | ||
| } | ||
| Err(mpsc::RecvTimeoutError::Timeout) => { | ||
| // Timeout reached without interruption. Run the step. | ||
| pub fn spawn_sequence_runner_thread<'scope>( | ||
| event_dispatcher: &'scope events::EventDispatcher, | ||
| scope: &'scope std::thread::Scope<'scope, '_>, | ||
| ) { | ||
| scope.spawn(move || { | ||
| let (tx, rx) = std::sync::mpsc::channel::<events::Event>(); | ||
| event_dispatcher.subscribe(tx, "Sequence Runner thread"); | ||
| let mut sequence_runner = SequenceRunner::new(event_dispatcher, scope); | ||
|
|
||
| while let Ok(event) = rx.recv() { | ||
| match event { | ||
| events::Event::Shutdown => { | ||
| sequence_runner.control_sequence(SequenceCmd::Shutdown); | ||
| break; | ||
| } | ||
| events::Event::StartSequence { | ||
| seq_name, | ||
| abort_seq_name, | ||
| } => { | ||
| // TODO: replace with loading sequences from the frontend | ||
| let seq_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("sequences"); | ||
| let seq = Sequence::load_from_path(&seq_dir.join(&seq_name)); | ||
| let abort_seq = Sequence::load_from_path(&seq_dir.join(&abort_seq_name)); | ||
|
Comment on lines
+33
to
+40
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change the StartSequence Event to take the actual sequence string instead of path? Because that's how it's going to work with the socket later on and it shouldn't really change anything for the tests |
||
|
|
||
| let seq = match seq { | ||
| Ok(seq) => seq, | ||
| Err(err) => { | ||
| eprintln!("Error while loading sequence '{seq_name}': {err:#}"); | ||
| continue; | ||
| } | ||
| }; | ||
| let abort_seq = match abort_seq { | ||
| Ok(abort_seq) => abort_seq, | ||
| Err(err) => { | ||
| eprintln!( | ||
| "Error while loading abort sequence '{abort_seq_name}': {err:#}" | ||
| ); | ||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| let result = sequence_runner.run_sequence(seq, abort_seq); | ||
| if let Err(err) = result { | ||
| eprintln!("Error while running sequence: {err:#}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| println!("Executing step: {}", step.description); | ||
| events::Event::PauseSequence => { | ||
| sequence_runner.control_sequence(SequenceCmd::Pause) | ||
| } | ||
| events::Event::ResumeSequence => { | ||
| sequence_runner.control_sequence(SequenceCmd::Resume) | ||
| } | ||
| events::Event::AbortSequence => { | ||
| sequence_runner.control_sequence(SequenceCmd::Abort) | ||
| } | ||
| _ => continue, | ||
| }; | ||
| } | ||
|
|
||
| println!("Sequence '{}' completed successfully.", seq.name); | ||
| }); | ||
|
|
||
| SequenceHandle { | ||
| cancel_tx, | ||
| thread_handle, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.