-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to simulate messages (#61)
* cangen simulation structure * moved simulate into a separate binary * added sim code genreation * improved new value generation * bug fixes * added struct for param passing, removed format and signed * fixed value initialization for boolean values * added rounding for nicer numbers and boolean cases * updated submodule pointer * ok cargo clippy * fixed NaN values * limit retry attempts; refactoring and cleanup * update embedded-base pointer * bump submodule --------- Co-authored-by: Jack Rubacha <[email protected]>
- Loading branch information
1 parent
0a2cf21
commit bcc1a76
Showing
10 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Submodule Embedded-Base
updated
72 files
This file contains 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 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 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,69 @@ | ||
use std::{ | ||
thread::{self}, | ||
time::Duration, | ||
}; | ||
|
||
use calypso::{ | ||
mqtt::MqttClient, serverdata, simulatable_message::SimulatedComponent, simulate_data::create_simulated_components | ||
}; | ||
use clap::Parser; | ||
|
||
|
||
/// Calypso command line arguments | ||
#[derive(Parser, Debug)] | ||
#[command(version)] | ||
struct CalypsoArgs { | ||
/// The host url of the siren, including port and excluding protocol prefix | ||
#[arg( | ||
short = 'u', | ||
long, | ||
env = "CALYPSO_SIREN_HOST_URL", | ||
default_value = "localhost:1883" | ||
)] | ||
siren_host_url: String, | ||
} | ||
|
||
|
||
fn simulate_out(pub_path: &str) { | ||
let mut client = MqttClient::new(pub_path, "calypso-simulator"); | ||
let _ = client.connect(); // todo: add error handling | ||
let sleep_time = Duration::from_millis(10); | ||
|
||
// todo: a way to turn individual components on and off | ||
let mut simulated_components: Vec<SimulatedComponent> = create_simulated_components(); | ||
|
||
// loop through the simulated components, if they should update, update them and publish the data | ||
loop { | ||
for component in simulated_components.iter_mut() { | ||
if component.should_update() { | ||
component.update(); | ||
let data: calypso::data::DecodeData = component.get_data(); | ||
let mut payload = serverdata::ServerData::new(); | ||
payload.unit = data.unit.to_string(); | ||
payload.value = data.value.iter().map(|x| x.to_string()).collect(); | ||
|
||
client | ||
.publish( | ||
data.topic.to_string(), | ||
protobuf::Message::write_to_bytes(&payload).unwrap_or_else(|e| { | ||
format!("failed to serialize {}", e).as_bytes().to_vec() | ||
}), | ||
) | ||
.expect("Could not publish!"); | ||
} | ||
} | ||
// sleep for a bit | ||
thread::sleep(sleep_time); | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* Main Function | ||
* Calls the `simulate_out` function with the siren host URL from the command line arguments. | ||
*/ | ||
fn main() { | ||
let cli = CalypsoArgs::parse(); | ||
simulate_out(&cli.siren_host_url); | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -283,4 +283,4 @@ fn main() { | |
|
||
can_handle.join().expect("Decoder failed with "); | ||
println!("Decoder ended"); | ||
} | ||
} |
Oops, something went wrong.