Skip to content
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

allow read_can to reconnect on Siren connection failure #51

Merged
merged 6 commits into from
Sep 29, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,23 @@ struct CalypsoArgs {
fn read_can(pub_path: &str, can_interface: &str) -> JoinHandle<u32> {
//open can socket channel at name can_interface
let mut client = MqttClient::new(pub_path, "calypso-decoder");
client.connect().expect("Could not connect to Siren!");
if client.connect().is_err() {
println!("Unable to connect to Siren, going into reconnection mode.");
if client.reconnect().is_ok() {
println!("Reconnected to Siren!");
}
}

jr1221 marked this conversation as resolved.
Show resolved Hide resolved
let socket = CanSocket::open(can_interface).expect("Failed to open CAN socket!");

thread::spawn(move || loop {
if !client.is_connected() {
println!("[read_can] Unable to connect to Siren, going into reconnection mode.");
if client.reconnect().is_ok() {
println!("[read_can] Reconnected to Siren!");
}
}

let msg = match socket.read_frame() {
Ok(CanFrame::Data(msg)) => msg,
Ok(CanFrame::Remote(_)) => {
Expand Down Expand Up @@ -82,14 +95,15 @@ fn read_can(pub_path: &str, can_interface: &str) -> JoinHandle<u32> {
payload.unit = data.unit.to_string();
payload.value = data.value.iter().map(|x| x.to_string()).collect();

client
.publish(
if 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!");
).is_err() {
println!("[read_can] Failed to publish to Siren.");
}

// TODO: investigate disabling this
thread::sleep(Duration::from_micros(100));
}
Expand All @@ -101,7 +115,15 @@ fn read_can(pub_path: &str, can_interface: &str) -> JoinHandle<u32> {
*/
fn read_siren(pub_path: &str, send_map: Arc<RwLock<HashMap<u32, EncodeData>>>) -> JoinHandle<()> {
let mut client = MqttClient::new(pub_path, "calypso-encoder");
client.connect().expect("Could not connect to Siren!");
// client.connect().expect("Could not connect to Siren!");

while !client.is_connected() {
println!("[read_siren] Unable to connect to Siren, going into reconnection mode.");
if client.reconnect().is_ok() {
println!("[read_siren] Reconnected to Siren!");
}
}

jr1221 marked this conversation as resolved.
Show resolved Hide resolved
let reciever = client.start_consumer().expect("Could not begin consuming");
client
.subscribe(ENCODER_MAP_SUB)
Expand Down
Loading