Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 11 additions & 8 deletions crates/connector/serial/src/serial/serial_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub trait Serial {

impl Serial for SerialDevice {
fn new(device_name: String, trs: bool) -> Result<Self, Error> {
let ports = match serialport::available_ports() {
Ok(ports) => ports,
Err(_) => Vec::new(),
};
let ports = serialport::available_ports().unwrap_or_else(|_| Vec::new());
let ports_output = ports
.iter()
.map(|port| {
Expand Down Expand Up @@ -91,10 +88,16 @@ impl Serial for SerialDevice {
}
}
fn write(&mut self, data: &[u8]) {
match self.port.write_all(data) {
Ok(_) => {}
Err(e) => {
error!(target: "connections", "Failed to send data: {}", e);
let mut remaining_data = data;
while !remaining_data.is_empty() {
match self.port.write(remaining_data) {
Ok(written) => {
remaining_data = &remaining_data[written..];
}
Err(e) => {
error!(target: "connections", "Failed to send data: {}", e);
break;
}
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/connector/serial/src/serial/serial_utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use serialport::SerialPortType;

pub fn get_serial_devices() -> Vec<String> {
let ports = match serialport::available_ports() {
Ok(ports) => ports,
Err(_) => Vec::new(),
};
let ports = serialport::available_ports().unwrap_or_else(|_| Vec::new());
let ports_output = ports
.iter()
.map(|port| {
Expand Down
2 changes: 1 addition & 1 deletion crates/connector/src-tauri/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn get_wasm_events(app: tauri::AppHandle) -> Vec<WasmEvent> {
pub async fn get_latest_custom_event_version(app: tauri::AppHandle) -> String {
let mut wasm_registry = events::wasm_registry::WASMRegistry::new();
wasm_registry.load_wasm(&app);
wasm_registry.get_latest_custom_event_version(&app)
wasm_registry.get_latest_custom_event_version()
}

#[tauri::command]
Expand Down
20 changes: 9 additions & 11 deletions crates/connector/src-tauri/src/events/action.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use simconnect::SimConnector;

pub struct Action {
pub id: u32,
pub name: ActionName,
pub _id: u32,
pub _name: ActionName,
pub excecute_action: Box<dyn Fn(&SimConnector, String, f32, i32)>,
}

impl Action {
pub const fn new(
id: u32,
name: ActionName,
_id: u32,
_name: ActionName,
excecute_action: Box<dyn Fn(&SimConnector, String, f32, i32)>,
) -> Action {
Action {
id,
name,
_id,
_name,
excecute_action,
}
}
Expand All @@ -31,9 +31,7 @@ impl Action {
}

pub enum ActionName {
THROTTLE,
PROP,
MIXTURE,
RUDDER,
AILERON,
Throttle,
Prop,
Mixture,
}
2 changes: 1 addition & 1 deletion crates/connector/src-tauri/src/events/action_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ impl ActionRegistry {
}

pub fn get_action_by_id(&self, id: u32) -> Option<&Action> {
return self.actions.get(&id);
self.actions.get(&id)
}
}
6 changes: 3 additions & 3 deletions crates/connector/src-tauri/src/events/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ pub fn get_actions() -> HashMap<u32, Action> {
let mut action_map = HashMap::new();
action_map.insert(
199,
Action::new(199, ActionName::THROTTLE, Box::new(throttle_action)),
Action::new(199, ActionName::Throttle, Box::new(throttle_action)),
);
action_map.insert(
115,
Action::new(115, ActionName::MIXTURE, Box::new(mixture_action)),
Action::new(115, ActionName::Mixture, Box::new(mixture_action)),
);
action_map.insert(
198,
Action::new(198, ActionName::PROP, Box::new(propeller_action)),
Action::new(198, ActionName::Prop, Box::new(propeller_action)),
);
action_map
}
4 changes: 0 additions & 4 deletions crates/connector/src-tauri/src/events/input_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ impl InputRegistry {
}
}

pub fn get_inputs(&self) -> &HashMap<u32, Input> {
&self.inputs
}

pub fn get_input(&self, input_id: u32) -> Option<&Input> {
self.inputs.get(&input_id)
}
Expand Down
4 changes: 1 addition & 3 deletions crates/connector/src-tauri/src/events/output_registry.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::collections::HashMap;

use connector_types::types::{category::Category, output::Output, wasm_event::WasmEvent};
use connector_types::types::{output::Output, wasm_event::WasmEvent};
use file_parsers::parsers::output_parser;

#[derive(Clone, Debug)]
pub struct OutputRegistry {
pub categories: Vec<Category>,
pub outputs: HashMap<u32, Output>,
output_path: String,
}

impl OutputRegistry {
pub fn new() -> OutputRegistry {
OutputRegistry {
categories: Vec::new(),
outputs: HashMap::new(),
output_path: String::from("src/events/outputs.json"),
}
Expand Down
24 changes: 5 additions & 19 deletions crates/connector/src-tauri/src/events/wasm_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use connector_types::types::{output::Output, wasm_event::WasmEvent};
use serde_json::json;
use tauri_plugin_store::StoreExt;

use crate::simconnect_mod::wasm::send_wasm_command;
use crate::{simconnect_mod::wasm::send_wasm_command, utils::store::save_store};
#[derive(Debug, Clone)]
pub struct WASMRegistry {
wasm_outputs: HashMap<u32, WasmEvent>,
Expand Down Expand Up @@ -37,11 +37,11 @@ impl WASMRegistry {
for event in &self.wasm_default_events {
store.set(event.id.to_string().clone(), json!(event));
}
store.save();
save_store(store);
self.load_wasm(&app);
}

pub fn get_latest_custom_event_version(&mut self, app: &tauri::AppHandle) -> String {
pub fn get_latest_custom_event_version(&mut self) -> String {
let parsed_custom_event_file =
file_parsers::parsers::wasm_event_parser::parse_events_from_file(&self.wasm_file_path);
parsed_custom_event_file.version
Expand All @@ -64,11 +64,7 @@ impl WASMRegistry {
self.wasm_inputs.insert(wasm_event.id, wasm_event);
}
}
store.save();
}

pub fn get_wasm_output_by_id(&mut self, output_id: u32) -> Option<&Output> {
self.parsed_wasm_outputs.get(&output_id)
save_store(store);
}

pub fn get_wasm_event_by_id(&self, event_id: u32) -> Option<&WasmEvent> {
Expand All @@ -79,10 +75,6 @@ impl WASMRegistry {
&self.wasm_outputs
}

pub fn get_wasm_inputs(&self) -> &HashMap<u32, WasmEvent> {
&self.wasm_inputs
}

pub fn get_wasm_events(&self) -> HashMap<u32, WasmEvent> {
let mut events = self.wasm_outputs.clone();
events.extend(self.wasm_inputs.clone());
Expand All @@ -93,20 +85,14 @@ impl WASMRegistry {
self.wasm_default_events.clone()
}

pub fn set_wasm_output_value(&mut self, output_id: u32, value: f64) {
if let Some(output) = self.wasm_outputs.get_mut(&output_id) {
output.value = value;
}
}

pub fn init_custom_events_to_store(&mut self, app: &tauri::AppHandle) {
let store = app.store(".events.dat").unwrap();
self.load_default_events();
let events = self.get_default_wasm_events();
for event in events {
store.set(event.id.to_string().clone(), json!(event));
}
store.save();
save_store(store);
}

pub fn register_wasm_inputs_to_simconnect(&self, conn: &mut simconnect::SimConnector) {
Expand Down
9 changes: 5 additions & 4 deletions crates/connector/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use utils::library_handler::generate_library;
use utils::library_handler::get_library_header_content;
use utils::library_handler::get_library_outputs;
use utils::library_handler::get_library_source_content;
use utils::store::save_store;
use utils::wasm_installer::{check_if_wasm_up_to_date, install_wasm};

use std::{env, thread};
Expand Down Expand Up @@ -106,14 +107,14 @@ async fn get_outputs(app: tauri::AppHandle) -> Vec<Output> {
}

#[tauri::command]
fn send_debug_message(app: tauri::AppHandle, message: Message) {
fn send_debug_message(message: Message) {
println!("Received message: {:?}", message);
let sender = SENDER.lock().unwrap().deref().clone().unwrap();
sender.send(message).unwrap();
}

#[tauri::command]
fn start_simconnect_connection(app: tauri::AppHandle, run_bundles: Vec<RunBundle>, debug: bool) {
fn start_simconnect_connection(app: tauri::AppHandle, run_bundles: Vec<RunBundle>) {
let (tx, rx) = mpsc::channel();
*SENDER.lock().unwrap() = Some(tx);
*RECEIVER.lock().unwrap() = Some(rx);
Expand All @@ -122,7 +123,7 @@ fn start_simconnect_connection(app: tauri::AppHandle, run_bundles: Vec<RunBundle
thread::spawn(|| {
#[cfg(target_os = "windows")]
let mut simconnect_handler =
simconnect_mod::simconnect_handler::SimconnectHandler::new(app, receiver, true);
simconnect_mod::simconnect_handler::SimconnectHandler::new(app, receiver);
#[cfg(target_os = "windows")]
simconnect_handler.start_connection(run_bundles);
});
Expand All @@ -143,7 +144,7 @@ fn init_wasm_events_to_store(app: tauri::AppHandle) {
let mut wasm_registry = events::wasm_registry::WASMRegistry::new();
wasm_registry.init_custom_events_to_store(&app);
}
store.save();
save_store(store);
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn create_new_exe_xml(
.resolve("resources/exe.xml", BaseDirectory::Resource)
{
Ok(path) => path,
Err(e) => {
Err(_) => {
Copy link

Copilot AI Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discarding the error information by replacing 'Err(e)' with 'Err(_)' removes potentially useful details for debugging. Consider logging or preserving the error details to aid in troubleshooting.

Suggested change
Err(_) => {
Err(e) => {
error!("Failed to resolve exe.xml resource path: {:?}", e);

Copilot uses AI. Check for mistakes.
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Failed to resolve exe.xml resource path",
Expand Down
36 changes: 26 additions & 10 deletions crates/connector/src-tauri/src/simconnect_mod/simconnect_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use connector_types::types::connector_settings::ConnectorSettings;
use connector_types::types::input::InputType;
use connector_types::types::simvar_update::SimvarUpdate;
use lazy_static::lazy_static;
use log::warn;
use log::{error, info};
Expand Down Expand Up @@ -80,17 +81,10 @@ pub struct SimconnectHandler {
active_com_ports: HashMap<String, Box<dyn Serial>>,
run_bundles: Vec<RunBundle>,
connector_settings: ConnectorSettings,
debug: bool,
}

// define the payload struct
#[derive(Clone, serde::Serialize)]
struct Payload {
message: String,
}

impl SimconnectHandler {
pub fn new(app_handle: tauri::AppHandle, rx: mpsc::Receiver<Message>, debug: bool) -> Self {
pub fn new(app_handle: tauri::AppHandle, rx: mpsc::Receiver<Message>) -> Self {
let mut simconnect = simconnect::SimConnector::new();
simconnect.connect("Tauri Simconnect");
let input_registry = InputRegistry::new();
Expand All @@ -110,7 +104,6 @@ impl SimconnectHandler {
active_com_ports: HashMap::new(),
run_bundles: vec![],
connector_settings,
debug,
}
}

Expand Down Expand Up @@ -171,6 +164,10 @@ impl SimconnectHandler {

pub fn start_connection(&mut self, run_bundles: Vec<RunBundle>) {
self.run_bundles = run_bundles;
println!(
"Starting connection with run bundles: {:?}",
self.run_bundles
);
self.set_settings();
self.set_com_ports();
self.connect_to_devices();
Expand Down Expand Up @@ -265,6 +262,15 @@ impl SimconnectHandler {
})
.collect();

self.app_handle
.emit(
"simvar_update",
SimvarUpdate {
id: output_id,
value,
},
)
.expect("Something went wrong emitting value");
for com_port in com_ports {
self.send_output_to_device(output_exists.1, &com_port, value);
}
Expand All @@ -275,6 +281,16 @@ impl SimconnectHandler {
"Sending output to device: {}, {}, {}",
output_id, com_port, value
);
self.app_handle
.emit_to(
"logWindow",
"simvar_update",
SimvarUpdate {
id: output_id,
value,
},
)
.expect("Something went wrong emitting value");

// Mutably borrow self.output_registry to set the value
// TODO: refactor to return result instead
Expand Down Expand Up @@ -573,7 +589,7 @@ impl SimconnectHandler {
&latest_output.metric,
simconnect::SIMCONNECT_DATATYPE_SIMCONNECT_DATATYPE_FLOAT64,
latest_output.id,
latest_output.update_every,
output.update_every,
);
}
}
Expand Down
4 changes: 0 additions & 4 deletions crates/connector/src-tauri/src/simconnect_mod/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct ClientDataProperties {
id: u32,
name: &'static str,
definition_id: u32,
request_id: u32,
data_size: DWORD,
}

Expand All @@ -22,7 +21,6 @@ pub fn register_wasm_data(conn: &mut simconnect::SimConnector) {
id: 1,
name: "shared",
definition_id: 101,
request_id: 102,
data_size: 256,
};
create_wasm_client(conn, &mut input_client);
Expand All @@ -31,7 +29,6 @@ pub fn register_wasm_data(conn: &mut simconnect::SimConnector) {
id: 2,
name: "messages",
definition_id: 103,
request_id: 104,
data_size: 4096,
};
create_wasm_client(conn, &mut output_client);
Expand All @@ -40,7 +37,6 @@ pub fn register_wasm_data(conn: &mut simconnect::SimConnector) {
id: 3,
name: "command_client",
definition_id: 105,
request_id: 106,
data_size: 4096,
};
create_wasm_client(conn, &mut command_client);
Expand Down
2 changes: 1 addition & 1 deletion crates/connector/src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod library_handler;
pub mod semver_matcher;
pub mod store;
pub mod wasm_installer;
Loading