Add unsubscribe to objects

This commit is contained in:
Mihai Dinculescu
2022-10-18 21:46:01 +01:00
parent a2002e02cd
commit 0db9847030
11 changed files with 211 additions and 109 deletions

View File

@@ -18,27 +18,40 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
let notification = client.get_next_dispatch()?;
Ok(mut client) => {
let mut notifications_received = 0;
match notification {
Some(Notification::Open) => {
println!("Open");
loop {
let notification = client.get_next_dispatch()?;
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
match notification {
Some(Notification::Open) => {
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
}
_ => (),
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
}
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
Err(e) => {
println!("Error: {e:?}")
}

View File

@@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open, we register the structs
client.register_object::<GpsData>()?;

View File

@@ -22,27 +22,40 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
let notification = client.get_next_dispatch()?;
Ok(mut client) => {
let mut notifications_received = 0;
match notification {
Some(Notification::Open) => {
info!("Open");
loop {
let notification = client.get_next_dispatch()?;
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
info!("{gps_data:?}");
match notification {
Some(Notification::Open) => {
info!("Open");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
}
_ => (),
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
info!("{gps_data:?}");
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
}
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
Err(e) => {
error!("{e:?}")
}

View File

@@ -37,27 +37,40 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
let notification = client.get_next_dispatch()?;
Ok(mut client) => {
let mut notifications_received = 0;
match notification {
Some(Notification::Open) => {
println!("Open");
loop {
let notification = client.get_next_dispatch()?;
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
match notification {
Some(Notification::Open) => {
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
}
}
_ => (),
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
println!("Subscription stopped.");
break;
}
}
}
_ => (),
}
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
Err(e) => {
println!("Error: {e:?}")
}

View File

@@ -9,7 +9,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match notification {
Some(Notification::Open) => {
println!("Open");
println!("Connection opened.");
// After the connection is successfully open