Skip to content

Commit d6bd6a7

Browse files
committed
refactor: various tasks for refactoring
1 parent b5888bb commit d6bd6a7

File tree

11 files changed

+164
-130
lines changed

11 files changed

+164
-130
lines changed

GUI/ETVR/src-tauri/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async fn run_mdns_query(service_type: String, scan_time: u64) -> Result<String,
110110
); // get's an array of the base urls found
111111
let json = m_dnsquery::generate_json(&*ref_mdns)
112112
.await
113-
.expect("Failed to generate JSON object"); // generates a json file with the base urls foundµ
113+
.expect("Failed to generate JSON object"); // generates a json file with the base urls found
114114
//tokio::fs::write("config/config.json", json)
115115
// .await
116116
// .expect("Failed to write JSON file");

GUI/ETVR/src-tauri/src/modules/m_dnsquery/mod.rs

+111-110
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
#![allow(dead_code, unused_imports, unused_variables)]
44
#![cfg_attr(
5-
all(not(debug_assertions), target_os = "windows"),
6-
windows_subsystem = "windows"
5+
all(not(debug_assertions), target_os = "windows"),
6+
windows_subsystem = "windows"
77
)]
88

99
use log::{error, info};
@@ -19,9 +19,9 @@ pub type MdnsMap = Arc<Mutex<HashMap<String, String>>>; // Arc<Mutex<HashMap<Str
1919
/// - `name`: a vector of the names of the devices found
2020
#[derive(Debug)]
2121
pub struct Mdns {
22-
pub base_url: MdnsMap,
23-
pub names: Vec<String>,
24-
pub ip: Vec<String>,
22+
pub base_url: MdnsMap,
23+
pub names: Vec<String>,
24+
pub ip: Vec<String>,
2525
}
2626

2727
/// Runs a mDNS query for X seconds
@@ -44,66 +44,66 @@ pub struct Mdns {
4444
/// ***The service type should not have a '.' or a 'local' at the end.*** <br>
4545
/// ***The program adds ".local." automatically.***
4646
pub async fn run_query(
47-
instance: &mut Mdns,
48-
mut service_type: String,
49-
scan_time: u64,
47+
instance: &mut Mdns,
48+
mut service_type: String,
49+
scan_time: u64,
5050
) -> Result<(), Box<dyn std::error::Error>> {
51-
let mdns = ServiceDaemon::new()
52-
.expect("Failed to create daemon. Please install Bonjour on your system");
53-
//* Browse for a service type.
54-
service_type.push_str(".local.");
55-
let receiver = mdns.browse(&service_type).map_err(|e| e.to_string())?;
56-
let now = std::time::Instant::now();
57-
//* listen for event then stop the event loop after 5 seconds.
58-
// while let Ok(event) = receiver.recv() {}
59-
while now.elapsed().as_secs() < scan_time {
60-
//* let event = receiver.recv().expect("Failed to receive event");
61-
if let Ok(event) = receiver.recv_async().await {
62-
match event {
63-
ServiceEvent::ServiceResolved(info) => {
64-
info!(
65-
"At {:?}: Resolved a new service: {} IP: {:#?}:{:#?} Hostname: {:#?}",
66-
now.elapsed(),
67-
info.get_fullname(),
68-
info.get_addresses(),
69-
info.get_port(),
70-
info.get_hostname(),
71-
);
72-
//* split the fullname by '.' and take the first element
73-
let name = info.get_hostname();
74-
info!("Service name: {}", name);
75-
//* remove the period at the end of the name
76-
let mut name = name.trim_end_matches('.');
77-
//* append name to 'http://' to get the base url
78-
let mut base_url = String::from("http://");
79-
base_url.push_str(name);
80-
info!("Base URL: {}", base_url);
81-
//* add the base url to the hashmap
82-
instance
83-
.base_url
84-
.lock()
85-
.map_err(|e| e.to_string())?
86-
.insert(name.to_string(), base_url);
87-
//* remove the `.local` from the name
88-
name = name.trim_end_matches(".local");
89-
instance.names.push(name.to_string());
90-
let ip = info.get_addresses();
91-
// grab the ip adress' from the hashset and add them to the vector
92-
for i in ip {
93-
instance.ip.push(i.to_string());
94-
}
95-
}
96-
other_event => {
97-
info!(
98-
"At {:?} : Received other event: {:?}",
99-
now.elapsed(),
100-
&other_event
101-
);
102-
}
103-
}
51+
let mdns =
52+
ServiceDaemon::new().expect("Failed to create daemon. Please install Bonjour on your system");
53+
//* Browse for a service type.
54+
service_type.push_str(".local.");
55+
let receiver = mdns.browse(&service_type).map_err(|e| e.to_string())?;
56+
let now = std::time::Instant::now();
57+
//* listen for event then stop the event loop after 5 seconds.
58+
// while let Ok(event) = receiver.recv() {}
59+
while now.elapsed().as_secs() < scan_time {
60+
//* let event = receiver.recv().expect("Failed to receive event");
61+
if let Ok(event) = receiver.recv_async().await {
62+
match event {
63+
ServiceEvent::ServiceResolved(info) => {
64+
println!(
65+
"At {:?}: Resolved a new service: {} IP: {:#?}:{:#?} Hostname: {:#?}",
66+
now.elapsed(),
67+
info.get_fullname(),
68+
info.get_addresses(),
69+
info.get_port(),
70+
info.get_hostname(),
71+
);
72+
//* split the fullname by '.' and take the first element
73+
let name = info.get_hostname();
74+
println!("Service name: {}", name);
75+
//* remove the period at the end of the name
76+
let mut name = name.trim_end_matches('.');
77+
//* append name to 'http://' to get the base url
78+
let mut base_url = String::from("http://");
79+
base_url.push_str(name);
80+
println!("Base URL: {}", base_url);
81+
//* add the base url to the hashmap
82+
instance
83+
.base_url
84+
.lock()
85+
.map_err(|e| e.to_string())?
86+
.insert(name.to_string(), base_url);
87+
//* remove the `.local` from the name
88+
name = name.trim_end_matches(".local");
89+
instance.names.push(name.to_string());
90+
let ip = info.get_addresses();
91+
// grab the ip adress' from the hashset and add them to the vector
92+
for i in ip {
93+
instance.ip.push(i.to_string());
94+
}
10495
}
96+
other_event => {
97+
println!(
98+
"At {:?} : Received other event: {:?}",
99+
now.elapsed(),
100+
&other_event
101+
);
102+
}
103+
}
105104
}
106-
Ok(())
105+
}
106+
Ok(())
107107
}
108108

109109
/// Returns a map of the base urls found
@@ -125,7 +125,8 @@ pub async fn run_query(
125125
///let urls_map = m_dnsquery::get_url_map(ref_mdns);
126126
/// ```
127127
pub fn get_url_map(instance: &mut Mdns) -> &mut MdnsMap {
128-
&mut instance.base_url
128+
println!("Base URL: {:?}", &instance.base_url);
129+
&mut instance.base_url
129130
}
130131

131132
/// Returns a vector of the base urls found
@@ -147,56 +148,56 @@ pub fn get_url_map(instance: &mut Mdns) -> &mut MdnsMap {
147148
/// let vec = m_dnsquery::get_urls(ref_mdns);
148149
/// ```
149150
pub fn get_urls(instance: &Mdns) -> Vec<String> {
150-
let mut urls: Vec<String> = Vec::new();
151-
152-
let instance_lock = instance.base_url.lock();
153-
let instance_check_result = match instance_lock {
154-
Ok(instance_lock) => instance_lock,
155-
Err(err) => {
156-
error!(
157-
"Failed to lock the instance: {:?} with error: {} in get_urls",
158-
instance, err
159-
);
160-
return urls;
161-
}
162-
};
151+
let mut urls: Vec<String> = Vec::new();
163152

164-
for (_, url) in instance_check_result.iter() {
165-
urls.push(url.to_string());
153+
let instance_lock = instance.base_url.lock();
154+
let instance_check_result = match instance_lock {
155+
Ok(instance_lock) => instance_lock,
156+
Err(err) => {
157+
error!(
158+
"Failed to lock the instance: {:?} with error: {} in get_urls",
159+
instance, err
160+
);
161+
return urls;
166162
}
167-
urls
163+
};
164+
165+
for (_, url) in instance_check_result.iter() {
166+
urls.push(url.to_string());
167+
}
168+
urls
168169
}
169170

170171
pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error::Error>> {
171-
let data = get_urls(instance);
172-
//let mut json: serde_json::Value = serde_json::from_str("{}").unwrap();
173-
let mut json: Option<serde_json::Value> = None;
174-
// create a data iterator
175-
for (i, url) in data.iter().enumerate() {
176-
json = Some(serde_json::json!({
177-
"ips": [instance.ip[i].to_string()],
178-
"urls": {
179-
instance.names[i].to_string(): url.to_string()
180-
},
181-
}));
182-
}
183-
let config: serde_json::Value;
184-
if let Some(json) = json {
185-
let _serde_json = serde_json::from_value(json);
186-
let serde_json_result = match _serde_json {
187-
Ok(serde_json) => serde_json,
188-
Err(err) => {
189-
error!("Error configuring JSON config file: {}", err);
190-
return Err("Error configuring JSON config file".into());
191-
}
192-
};
193-
config = serde_json_result;
194-
} else {
195-
config = serde_json::json!({});
196-
}
197-
info!("{:?}", config);
198-
// write the json object to a file
199-
let to_string_json = serde_json::to_string_pretty(&config)?;
200-
// return the json object as a string
201-
Ok(to_string_json)
172+
let data = get_urls(instance);
173+
//let mut json: serde_json::Value = serde_json::from_str("{}").unwrap();
174+
let mut json: Option<serde_json::Value> = None;
175+
// create a data iterator
176+
for (i, url) in data.iter().enumerate() {
177+
json = Some(serde_json::json!({
178+
"ips": [instance.ip[i].to_string()],
179+
"urls": {
180+
instance.names[i].to_string(): url.to_string()
181+
},
182+
}));
183+
}
184+
let config: serde_json::Value;
185+
if let Some(json) = json {
186+
let _serde_json = serde_json::from_value(json);
187+
let serde_json_result = match _serde_json {
188+
Ok(serde_json) => serde_json,
189+
Err(err) => {
190+
error!("Error configuring JSON config file: {}", err);
191+
return Err("Error configuring JSON config file".into());
192+
}
193+
};
194+
config = serde_json_result;
195+
} else {
196+
config = serde_json::json!({});
197+
}
198+
info!("{:?}", config);
199+
// write the json object to a file
200+
let to_string_json = serde_json::to_string_pretty(&config)?;
201+
// return the json object as a string
202+
Ok(to_string_json)
202203
}

GUI/ETVR/src/app.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { onMount, Suspense, lazy } from 'solid-js'
2+
import { MdnsProvider } from '@utils/context/mdns'
23
import { handleTitlebar, handleAppBoot } from '@utils/hooks/app'
34

45
const AppRoutes = lazy(() => import('@routes/Routes'))
@@ -17,14 +18,16 @@ const App = () => {
1718
return (
1819
<div class="App overflow-y-auto items-center">
1920
<Suspense>
20-
<AppRoutes />
21-
<NewWindow ref={ref} name="test">
22-
<ExampleMenu />
23-
</NewWindow>
24-
<ModalMenu>
25-
<CameraSettingsModal />
26-
</ModalMenu>
27-
<ToastNotificationWindow />
21+
<MdnsProvider>
22+
<AppRoutes />
23+
<NewWindow ref={ref} name="test">
24+
<ExampleMenu />
25+
</NewWindow>
26+
<ModalMenu>
27+
<CameraSettingsModal />
28+
</ModalMenu>
29+
<ToastNotificationWindow />
30+
</MdnsProvider>
2831
</Suspense>
2932
</div>
3033
)

GUI/ETVR/src/components/Loader/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ const Loader = (props: LoaderProps) => {
3535
)
3636
}
3737

38-
export const OrangeLoader = () => {
38+
export const OrangeLoader = (width: number, height: number) => {
3939
return (
4040
<div class="flex justify-center items-center">
4141
<Loader
4242
gradient="orange"
4343
gradientMid="rgba(255, 153, 0, 0.594)"
4444
gradientBot="rgba(255, 153, 0, 0.144)"
45-
width="100px"
46-
height="100px"
45+
width={`${width}px`}
46+
height={`${height}px`}
4747
/>
4848
</div>
4949
)

GUI/ETVR/src/components/RangeInput/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { RANGE_INPUT_FORMAT } from '@src/static/types/enums'
2-
import { BULLET_POSITION_ADJUSTMENT, getBulletPosition } from '@src/utils/utils'
31
import { createEffect, createSignal } from 'solid-js'
2+
import { RANGE_INPUT_FORMAT } from '@static/types/enums'
3+
import { BULLET_POSITION_ADJUSTMENT, getBulletPosition } from '@utils/utils'
44
import './styles.css'
55

66
export interface IProps {

GUI/ETVR/src/components/RangeInput/styles.css

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ body {
66
height: 100vh;
77
background-color: #000;
88
font-family: 'Roboto', sans-serif;
9-
background: linear-gradient(180deg, #db302a 0%, #62186b 100%) no-repeat;
109
}
1110

1211
.box-minmax {

GUI/ETVR/src/components/Settings/CameraSettings/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { For } from 'solid-js'
12
import RangeInput from '@components/RangeInput'
23
import { RANGE_INPUT_FORMAT } from '@src/static/types/enums'
3-
import { For } from 'solid-js'
44

55
export interface IProps {
66
header: string

GUI/ETVR/src/components/Settings/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { RANGE_INPUT_FORMAT } from '@src/static/types/enums'
2-
import { CameraStatus, CameraType } from '@store/camera/camera'
31
import CameraAddress from './CameraAddress/CameraAddress'
42
import CameraConfigOptions from './CameraConfigOptions'
53
import CameraInfo from './CameraInfo/CameraInfo'
64
import CameraSettings from './CameraSettings'
7-
import CamerasModal from './CamerasModal/index'
5+
import CamerasModal from './CamerasModal'
6+
import { RANGE_INPUT_FORMAT } from '@src/static/types/enums'
7+
import { CameraStatus, CameraType } from '@store/camera/camera'
88

99
// TODO: stuff todo requested by lorow
1010
// honestly it looks good, I like that preview window. The camera ID I'd rename to camera IP though I'm not really sure if that's gonna be necessary,
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createContext, JSX } from 'solid-js'
2+
import { useMDNSScanner } from '@utils/hooks/api/useMDNSScanner'
3+
4+
export const MdnsContext = createContext()
5+
6+
export const MdnsProvider = (props: {
7+
children:
8+
| number
9+
| boolean
10+
| Node
11+
| JSX.ArrayElement
12+
| JSX.FunctionElement
13+
| (string & object)
14+
| null
15+
| undefined
16+
}) => {
17+
const { data, mutate, refetch, resData, setResData } = useMDNSScanner('openiristracker', 30)
18+
return (
19+
<MdnsContext.Provider value={{ data, mutate, refetch, resData, setResData }}>
20+
{props.children}
21+
</MdnsContext.Provider>
22+
)
23+
}

GUI/ETVR/src/utils/context/mdns/provider.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)