Merge pull request #2 from couscousdude/refactor-rust

refactor: use idiomatic way to store IP instead of tuple
This commit is contained in:
Youwen Wu 2024-03-17 20:37:36 -07:00 committed by GitHub
commit 6d5ef6809a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 11 deletions

View file

@ -1,6 +1,8 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!! // Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::net::Ipv4Addr;
use tauri::Manager; use tauri::Manager;
mod telemetry; mod telemetry;
use tracing_subscriber::FmtSubscriber; use tracing_subscriber::FmtSubscriber;
@ -12,7 +14,7 @@ struct Payload {
message: String, message: String,
} }
const NTABLE_IP: (u8, u8, u8, u8) = (10, 12, 80, 2); const NTABLE_IP: Ipv4Addr = Ipv4Addr::new(10, 12, 80, 2);
const NTABLE_PORT: u16 = 5810; const NTABLE_PORT: u16 = 5810;
fn main() { fn main() {

View file

@ -13,15 +13,12 @@ use tokio::time::{sleep, Duration};
/// `telemetry_status` event with the payload `"disconnected"` instead. /// `telemetry_status` event with the payload `"disconnected"` instead.
pub async fn create_client( pub async fn create_client(
app_handle: &AppHandle, app_handle: &AppHandle,
ntable_ip: &(u8, u8, u8, u8), ntable_ip: &Ipv4Addr,
ntable_port: &u16, ntable_port: &u16,
) -> Client { ) -> Client {
loop { loop {
let client_attempt = Client::try_new_w_config( let client_attempt = Client::try_new_w_config(
SocketAddrV4::new( SocketAddrV4::new(*ntable_ip, *ntable_port),
Ipv4Addr::new(ntable_ip.0, ntable_ip.1, ntable_ip.2, ntable_ip.3),
*ntable_port,
),
Config { Config {
..Default::default() ..Default::default()
}, },

View file

@ -1,3 +1,5 @@
use std::net::Ipv4Addr;
use network_tables::v4::{MessageData, Subscription}; use network_tables::v4::{MessageData, Subscription};
use serde_json::to_string; use serde_json::to_string;
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
@ -16,11 +18,7 @@ use create_subscription::create_subscription;
/// to all connected frontends using the "telemetry_data" event. /// to all connected frontends using the "telemetry_data" event.
/// ///
/// The function loops forever, retrying connection every 3 seconds, reconnecting if the client disconnects. /// The function loops forever, retrying connection every 3 seconds, reconnecting if the client disconnects.
pub async fn subscribe_topics( pub async fn subscribe_topics(app_handle: AppHandle, ntable_ip: Ipv4Addr, ntable_port: u16) {
app_handle: AppHandle,
ntable_ip: (u8, u8, u8, u8),
ntable_port: u16,
) {
let mut previous_gpws: bool = false; let mut previous_gpws: bool = false;
loop { loop {