Skip to content

Commit da76edb

Browse files
committed
create init event to commonize linux and windows
1 parent fc7be68 commit da76edb

File tree

3 files changed

+118
-150
lines changed

3 files changed

+118
-150
lines changed

src/event_handler/init.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use crate::slint_generatedAppWindow::{socket_info, AppWindow};
2+
#[cfg(target_os = "windows")]
3+
use pcan_basic::hw::attached_channels as available_interfaces;
4+
use slint::{ComponentHandle, ModelRc, SharedString, VecModel, Weak};
5+
#[cfg(target_os = "linux")]
6+
use socketcan::available_interfaces;
7+
use std::{process::exit, time::Duration};
8+
pub struct Init<'a> {
9+
pub ui_handle: &'a Weak<AppWindow>,
10+
}
11+
12+
impl<'a> Init<'a> {
13+
pub fn run(&self) {
14+
let mut previous_interfaces = Vec::default();
15+
loop {
16+
match available_interfaces() {
17+
Ok(interface) => {
18+
if interface.is_empty() {
19+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
20+
let socket_info = socket_info {
21+
index: ModelRc::new(VecModel::from(Vec::default())),
22+
name: ModelRc::new(VecModel::from(Vec::default())),
23+
};
24+
ui.set_can_sockets(socket_info);
25+
ui.set_init_string(SharedString::from("No CAN device found !"));
26+
});
27+
} else {
28+
#[cfg(target_os = "linux")]
29+
if previous_interfaces != interface {
30+
let interface_clone = interface.clone();
31+
previous_interfaces = interface.clone();
32+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
33+
ui.set_init_string(SharedString::from(format!(
34+
"Found {} CAN devices\n Please select your device ",
35+
interface.len()
36+
)));
37+
});
38+
39+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
40+
let convert_shared_string: Vec<SharedString> = interface_clone
41+
.into_iter()
42+
.map(SharedString::from)
43+
.collect();
44+
let socket_info = socket_info {
45+
index: ModelRc::new(VecModel::from(Vec::default())),
46+
name: ModelRc::new(VecModel::from(convert_shared_string)),
47+
};
48+
ui.set_can_sockets(socket_info);
49+
});
50+
}
51+
#[cfg(target_os = "windows")]
52+
{
53+
let mut interface_names = Vec::default();
54+
let mut interface_index = Vec::default();
55+
let mut count = 0;
56+
for channel in channels {
57+
let interface_name = SharedString::from(format!(
58+
"{}(0x{:02X})",
59+
channel.device_name(),
60+
channel.channel_information.device_id
61+
));
62+
interface_names.push(socket_name);
63+
interface_index
64+
.push(channel.channel_information.channel_handle as i32);
65+
count += 1;
66+
}
67+
if previous_interfaces != interface_names {
68+
previous_interfaces = interface_names.clone();
69+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
70+
ui.set_init_string(SharedString::from(format!(
71+
"Found {} CAN devices\n Please select your device ",
72+
count
73+
)));
74+
let socket_info = socket_info {
75+
index: ModelRc::new(VecModel::from(interface_index)),
76+
name: ModelRc::new(VecModel::from(interface_names)),
77+
};
78+
ui.set_can_sockets(socket_info);
79+
});
80+
}
81+
}
82+
}
83+
}
84+
Err(e) => {
85+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
86+
ui.set_init_string(SharedString::from(format!(
87+
"Can't get device list: {}",
88+
e
89+
)));
90+
let socket_info = socket_info {
91+
index: ModelRc::new(VecModel::from(Vec::default())),
92+
name: ModelRc::new(VecModel::from(Vec::default())),
93+
};
94+
ui.set_can_sockets(socket_info);
95+
});
96+
}
97+
};
98+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
99+
if !ui.window().is_visible() {
100+
exit(1);
101+
}
102+
});
103+
std::thread::sleep(Duration::from_micros(50));
104+
}
105+
}
106+
}

src/event_handler/mod.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
11
pub(crate) mod can_handler;
22
pub(crate) mod dbc_file;
3+
pub(crate) mod init;
34
pub(crate) mod packet_filter;
45

56
pub use can_handler::CanHandler;
67
pub use dbc_file::DBCFile;
8+
pub use init::Init;
79
pub use packet_filter::PacketFilter;
8-
#[cfg(target_os = "windows")]
9-
use pcan_basic::socket::Baudrate;
1010
use slint::Color;
1111

1212
const ODD_COLOR: Color = Color::from_rgb_u8(0x18, 0x1c, 0x27);
1313
const EVEN_COLOR: Color = Color::from_rgb_u8(0x13, 0x16, 0x1f);
14-
15-
#[cfg(target_os = "windows")]
16-
pub fn p_can_bitrate(bitrate: &str) -> Option<Baudrate> {
17-
match bitrate {
18-
"1 Mbit/s" => Some(Baudrate::Baud1M),
19-
"800 kbit/s" => Some(Baudrate::Baud800K),
20-
"500 kbit/s" => Some(Baudrate::Baud500K),
21-
"250 kbit/s" => Some(Baudrate::Baud250K),
22-
"125 kbit/s" => Some(Baudrate::Baud125K),
23-
"100 kbit/s" => Some(Baudrate::Baud100K),
24-
"95.238 kbit/s" => Some(Baudrate::Baud95K),
25-
"83.333 kbit/s" => Some(Baudrate::Baud83),
26-
"50 kbit/s" => Some(Baudrate::Baud50K),
27-
"47.619 kbit/s" => Some(Baudrate::Baud47K),
28-
"33.333 kbit/s" => Some(Baudrate::Baud33K),
29-
"20 kbit/s" => Some(Baudrate::Baud20K),
30-
"10 kbit/s" => Some(Baudrate::Baud10K),
31-
"5 kbit/s" => Some(Baudrate::Baud5K),
32-
_ => None,
33-
}
34-
}

src/main.rs

Lines changed: 10 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
use std::io;
2-
use std::process::exit;
32
use std::sync::mpsc;
43
use std::sync::{Arc, Mutex};
5-
use std::time::Duration;
64

75
mod event_handler;
86
use can_dbc::DBC;
7+
use event_handler::{CanHandler, DBCFile, Init, PacketFilter};
98
#[cfg(target_os = "windows")]
10-
use event_handler::p_can_bitrate;
11-
use event_handler::{CanHandler, DBCFile, PacketFilter};
12-
#[cfg(target_os = "windows")]
13-
use pcan_basic::{bus::UsbBus, hw::attached_channels, socket::usb::UsbCanSocket};
9+
use pcan_basic::{
10+
bus::UsbBus, hw::attached_channels as available_interfaces, socket::usb::UsbCanSocket,
11+
};
1412
#[cfg(target_os = "linux")]
1513
use privilege_rs::privilege_request;
1614
#[cfg(target_os = "windows")]
1715
use slint::Model;
18-
use slint::{ModelRc, SharedString, VecModel};
19-
#[cfg(target_os = "linux")]
20-
use socketcan::available_interfaces;
16+
use slint::SharedString;
2117
#[cfg(target_os = "windows")]
2218
use winapi::um::wincon::FreeConsole;
2319

@@ -39,125 +35,11 @@ async fn main() -> io::Result<()> {
3935

4036
// Find available socket CAN
4137
let ui_handle = ui.as_weak();
42-
#[cfg(target_os = "linux")]
43-
tokio::spawn(async move {
44-
let mut previous_interfaces = Vec::default();
45-
loop {
46-
match available_interfaces() {
47-
Ok(interface) => {
48-
if interface.is_empty() {
49-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
50-
let socket_info = socket_info {
51-
index: ModelRc::new(VecModel::from(Vec::default())),
52-
name: ModelRc::new(VecModel::from(Vec::default())),
53-
};
54-
ui.set_can_sockets(socket_info);
55-
ui.set_init_string(SharedString::from("No CAN device found !"));
56-
});
57-
} else if previous_interfaces != interface {
58-
let interface_clone = interface.clone();
59-
previous_interfaces = interface.clone();
60-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
61-
ui.set_init_string(SharedString::from(format!(
62-
"Found {} CAN devices\n Please select your device ",
63-
interface.len()
64-
)));
65-
});
66-
67-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
68-
let convert_shared_string: Vec<SharedString> = interface_clone
69-
.into_iter()
70-
.map(SharedString::from)
71-
.collect();
72-
let socket_info = socket_info {
73-
index: ModelRc::new(VecModel::from(Vec::default())),
74-
name: ModelRc::new(VecModel::from(convert_shared_string)),
75-
};
76-
ui.set_can_sockets(socket_info);
77-
});
78-
}
79-
}
80-
Err(e) => {
81-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
82-
ui.set_init_string(SharedString::from(format!(
83-
"Can't get device list: {}",
84-
e
85-
)));
86-
let socket_info = socket_info {
87-
index: ModelRc::new(VecModel::from(Vec::default())),
88-
name: ModelRc::new(VecModel::from(Vec::default())),
89-
};
90-
ui.set_can_sockets(socket_info);
91-
});
92-
}
93-
};
94-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
95-
if !ui.window().is_visible() {
96-
exit(1);
97-
}
98-
});
99-
tokio::time::sleep(Duration::from_micros(50)).await;
100-
}
101-
});
102-
103-
#[cfg(target_os = "windows")]
10438
tokio::spawn(async move {
105-
let mut previous_sockets = Vec::default();
106-
loop {
107-
// get channel_handle
108-
match attached_channels() {
109-
Ok(channels) => {
110-
if channels.is_empty() {
111-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
112-
ui.set_init_string(SharedString::from("No CAN device found !"));
113-
});
114-
} else {
115-
let mut can_socket_names = Vec::default();
116-
let mut can_socket_index = Vec::default();
117-
let mut count = 0;
118-
for channel in channels {
119-
let socket_name = SharedString::from(format!(
120-
"{}(0x{:02X})",
121-
channel.device_name(),
122-
channel.channel_information.device_id
123-
));
124-
can_socket_names.push(socket_name);
125-
can_socket_index
126-
.push(channel.channel_information.channel_handle as i32);
127-
count += 1;
128-
}
129-
if previous_sockets != can_socket_names {
130-
previous_sockets = can_socket_names.clone();
131-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
132-
ui.set_init_string(SharedString::from(format!(
133-
"Found {} CAN devices\n Please select your device ",
134-
count
135-
)));
136-
let socket_info = socket_info {
137-
index: ModelRc::new(VecModel::from(can_socket_index)),
138-
name: ModelRc::new(VecModel::from(can_socket_names)),
139-
};
140-
ui.set_can_sockets(socket_info);
141-
});
142-
}
143-
}
144-
}
145-
Err(e) => {
146-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
147-
ui.set_init_string(SharedString::from(format!(
148-
"Can't get device list: {:?}",
149-
e
150-
)));
151-
});
152-
}
153-
}
154-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
155-
if !ui.window().is_visible() {
156-
exit(1);
157-
}
158-
});
159-
tokio::time::sleep(Duration::from_micros(50)).await;
160-
}
39+
let init_event = Init {
40+
ui_handle: &ui_handle,
41+
};
42+
init_event.run();
16143
});
16244

16345
let (start_tx, start_rx) = mpsc::channel();
@@ -177,6 +59,7 @@ async fn main() -> io::Result<()> {
17759
}
17860
#[cfg(target_os = "windows")]
17961
{
62+
use event_handler::p_can_bitrate;
18063
let ui = ui_handle.unwrap();
18164
let get_device_handle = match ui.get_can_sockets().index.row_data(_index as usize) {
18265
Some(device) => device,

0 commit comments

Comments
 (0)