-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Prepare MQTT and serialization structure
- Loading branch information
1 parent
a113154
commit dc31c82
Showing
4 changed files
with
96 additions
and
183 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use eskf::ESKF; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use rumqttc::{Client, Event, Incoming, MqttOptions, QoS}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct Euler { | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct Imu { | ||
euler: Euler, | ||
// TODO: Add fields | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct Gps { | ||
fix_type: i32, | ||
// TODO: Add fields | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct Boat { | ||
sog: f32, | ||
// TODO: Add fields | ||
} | ||
|
||
fn on_message_imu(filter: ESKF, message: Imu) { | ||
println!("Received IMU message: {message:?}"); | ||
// TODO: Update filter | ||
} | ||
|
||
fn on_message_gps(filter: ESKF, message: Gps) { | ||
println!("Received GPS message: {message:?}"); | ||
// TODO: Update filter | ||
} | ||
|
||
fn main() { | ||
// TODO: Add username and password authentication | ||
let mqqt_opts = MqttOptions::new("sailtrack-aggregator", "localhost", 1883); | ||
|
||
let mut filter = eskf::Builder::new() | ||
.acceleration_variance(0.01) // FIXME | ||
.rotation_variance(0.01) // FIXME | ||
.build(); | ||
|
||
let (client, mut connection) = Client::new(mqqt_opts, 10); | ||
client.subscribe("sensor/gps0", QoS::AtMostOnce).unwrap(); | ||
client.subscribe("sensor/imu0", QoS::AtMostOnce).unwrap(); | ||
|
||
thread::spawn(move || { | ||
loop { | ||
let message = Boat { | ||
sog: 10.0, // FIXME | ||
}; | ||
client | ||
.publish( | ||
"boat", | ||
QoS::AtLeastOnce, | ||
false, | ||
serde_json::to_vec(&message).unwrap(), | ||
) | ||
.unwrap(); | ||
println!("Published boat measurement: {message:?}"); | ||
thread::sleep(Duration::from_millis(200)); | ||
} | ||
}); | ||
|
||
for notification in connection.iter().flatten() { | ||
if let Event::Incoming(Incoming::Publish(packet)) = notification { | ||
match packet.topic.as_str() { | ||
"sensor/imu0" => on_message_imu( | ||
filter, | ||
serde_json::from_slice(packet.payload.as_ref()).unwrap(), | ||
), | ||
"sensor/gps0" => on_message_gps( | ||
filter, | ||
serde_json::from_slice(packet.payload.as_ref()).unwrap(), | ||
), | ||
_ => (), | ||
} | ||
} | ||
} | ||
} |