Skip to content

Commit

Permalink
✨ added remove sighting request to bluetooth
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Mar 4, 2022
1 parent 60f14f7 commit e8941f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bluetooth/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum Message {
LastResponse { last: Sighting },
#[serde(rename = "sighting_request")]
SightingRequest { uuid: String },
#[serde(rename = "remove_sighting_request")]
RemoveSightingRequest { uuid: String },
#[serde(rename = "sighting_response")]
SightingResponse { sighting: Sighting },
#[serde(rename = "image_request")]
Expand Down
24 changes: 24 additions & 0 deletions src/bluetooth/rfcomm_srv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Sighting;
use crate::sighting::save_to_file;
use base64;
use bluer::{
adv::Advertisement,
Expand Down Expand Up @@ -142,6 +143,29 @@ async fn handle_connection(
continue;
}
}
Ok(Message::RemoveSightingRequest { uuid }) => {
println!("remove sighting {}", uuid);
let sightings = {
let mut sightings = sightings.lock().unwrap();
let index = sightings.iter().position(|x| x.uuid == uuid).unwrap();
sightings.remove(index);
sightings.to_vec()
};
save_to_file(sightings.clone(), "sightings/sightings.db").unwrap();
let sightings = {
let sightings: Vec<String> = sightings.into_iter().map(|i| i.uuid).collect();
sightings
};
let response = serde_json::to_vec(&Message::SightingIdsResponse {
ids: sightings.clone(),
})
.unwrap();

if let Err(err) = stream.write_all(&response).await {
println!("Write failed: {}", &err);
continue;
}
}
Ok(Message::ImageRequest { uuid }) => {
println!("{}", uuid);
let filename = {
Expand Down

0 comments on commit e8941f9

Please sign in to comment.