Skip to content

Commit 7372d47

Browse files
committed
feat: implement getting firmware release
- grabs releases from repo - checks if releases are newer - writes to config file
1 parent f2e4e33 commit 7372d47

File tree

23 files changed

+704
-93
lines changed

23 files changed

+704
-93
lines changed

GUI/ETVR/src-tauri/Cargo.lock

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/ETVR/src-tauri/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "eyetrackvr"
33
version = "1.0.0"
44
description = "Eye tracking for VR"
5-
authors = [ "DaOfficialWizard", "Luckmer", "RedHawk989",]
5+
authors = [ "DaOfficialWizard", "Luckmer", "RedHawk989"]
66
license = "MIT"
77
repository = "https://github.com/RedHawk989/EyeTrackVR"
88
default-run = "eyetrackvr"
@@ -21,16 +21,16 @@ futures-util = "0.3.25"
2121
mdns-sd = "0.5.9"
2222

2323
[features]
24-
default = [ "custom-protocol",]
25-
custom-protocol = [ "tauri/custom-protocol",]
24+
default = [ "custom-protocol"]
25+
custom-protocol = [ "tauri/custom-protocol"]
2626

2727
[dependencies.serde]
2828
version = "1.0"
29-
features = [ "derive",]
29+
features = [ "derive"]
3030

3131
[dependencies.tauri]
3232
version = "1.1.1"
33-
features = [ "dialog-all", "fs-create-dir", "fs-read-dir", "fs-write-file", "http-all", "icon-ico", "notification-all", "os-all", "path-all", "process-relaunch", "shell-open", "system-tray", "window-center", "window-close", "window-hide", "window-maximize", "window-minimize", "window-set-decorations", "window-set-focus", "window-set-fullscreen", "window-set-size", "window-start-dragging", "window-unmaximize", "window-unminimize",]
33+
features = [ "fs-all", "shell-sidecar", "dialog-all", "http-all", "icon-ico", "notification-all", "os-all", "path-all", "process-relaunch", "shell-open", "system-tray", "window-center", "window-close", "window-hide", "window-maximize", "window-minimize", "window-set-decorations", "window-set-focus", "window-set-fullscreen", "window-set-size", "window-start-dragging", "window-unmaximize", "window-unminimize"]
3434

3535
[dependencies.tauri-plugin-window-state]
3636
git = "https://github.com/tauri-apps/tauri-plugin-window-state/"
Binary file not shown.

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

+1
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,6 @@ pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error
193193
// write the json object to a file
194194
let to_string_json = serde_json::to_string_pretty(&config)?;
195195
// return the json object as a string
196+
196197
Ok(to_string_json)
197198
}

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

+159-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use crate::modules::m_dnsquery;
1+
use futures_util::future::ok;
22
use log::{debug, error, info, warn};
33
use reqwest::Client;
44
use serde::Deserialize;
5+
use serde_json::{Map, Value};
56
use std::collections::hash_map::HashMap;
7+
use std::io::Read;
68

79
/// A struct to hold the REST client
810
/// ## Fields
@@ -38,6 +40,7 @@ pub async fn request(rest_client: &RESTClient) -> Result<String, String> {
3840
response = rest_client
3941
.http_client
4042
.get(&rest_client.base_url)
43+
.header("User-Agent", "EyeTrackVR")
4144
.send()
4245
.await
4346
.map_err(|e| e.to_string())?
@@ -77,31 +80,169 @@ pub async fn run_rest_client(
7780
method: String,
7881
) -> Result<String, String> {
7982
info!("Starting REST client");
80-
// read the json config file
81-
let data = std::fs::read_to_string("config/config.json").expect("Unable to read config file");
82-
// parse the json config file
83-
let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
84-
debug!("Current Config: {:?}", config);
83+
let full_url = format!("{}{}", device_name, endpoint);
8584
let mut request_response: String = String::new();
86-
let mut url = config["urls"][device_name].as_str();
87-
let full_url_result = match url {
88-
Some(url) => url,
89-
None => {
90-
error!("Unable to get url");
91-
url = Some("");
92-
return Err(format!("Unable to get url: {:?}", url));
93-
}
94-
};
95-
let full_url = format!("{}{}", full_url_result, endpoint);
9685
//info!("Full url: {}", full_url);
9786
let rest_client = RESTClient::new(full_url, method);
9887
let request_result = request(&rest_client).await;
9988
match request_result {
10089
Ok(response) => {
10190
request_response = response;
102-
println!("Request response: {:?}", request_response);
91+
println!("[REST Client]: Request response: {:?}", request_response);
92+
}
93+
Err(e) => println!("[REST Client]: Request failed: {}", e),
94+
}
95+
Ok(request_response)
96+
}
97+
98+
async fn run_gh_release_latest() -> Result<String, String> {
99+
info!("Starting GitHub release client");
100+
let endpoint = "/releases/latest";
101+
let device_name = "https://api.github.com/repos/lorow/OpenIris";
102+
let method = "GET".to_string();
103+
let mut request_response: String = String::new();
104+
let request_result = run_rest_client(endpoint.to_string(), device_name.to_string(), method).await;
105+
106+
match request_result {
107+
Ok(response) => {
108+
request_response = response;
109+
/* println!(
110+
"[Github Release Latest]: Request response: {:?}",
111+
request_response
112+
); */
103113
}
104-
Err(e) => println!("Request failed: {}", e),
114+
Err(e) => println!("[Github Release Latest]: Request failed: {}", e),
105115
}
106116
Ok(request_response)
107117
}
118+
119+
/*
120+
release_id: 0,
121+
new_release: false,
122+
assets: [],
123+
*/
124+
///! DEPRICATED - but it works ....
125+
pub async fn run_gh_release_assets() -> Result<String, String> {
126+
info!("Starting GitHub release client");
127+
let gh_response = run_gh_release_latest().await;
128+
let mut request_response: String = String::new();
129+
match gh_response {
130+
Ok(response) => {
131+
request_response = response;
132+
/* println!(
133+
"[Github Release Asset]: Request Response: {:?}",
134+
request_response
135+
); */
136+
}
137+
Err(e) => println!("[Github Release Asset]: Request failed: {}", e),
138+
}
139+
140+
let json_response = serde_json::from_str::<Map<String, Value>>(&request_response);
141+
142+
match json_response {
143+
Ok(response) => {
144+
println!("[Github Release]: JSON response: {:?}", response);
145+
// check if the json object is empty
146+
if response.is_empty() {
147+
warn!("[Github Release]: JSON object is empty");
148+
return Err("[Github Release]: JSON object is empty".into());
149+
}
150+
151+
// check if the json object has the key "id" - and if it does check if the value is the same as the current value saved to the file
152+
if response.contains_key("id") {
153+
let id = response["id"].to_string();
154+
// read the json config file and create it if it doesn't exist
155+
let mut data = String::new();
156+
let mut file = std::fs::OpenOptions::new()
157+
.read(true)
158+
.write(true)
159+
.create(true)
160+
.open("config/config.json");
161+
162+
match file {
163+
Ok(ref mut file) => {
164+
file.read_to_string(&mut data).unwrap();
165+
}
166+
Err(e) => {
167+
error!("[Github Release]: Unable to open config file: {}", e);
168+
return Err("[Github Release]: Unable to open config file".into());
169+
}
170+
}
171+
172+
// if the file is empty, create a new file with the id, and assets array
173+
if data.is_empty() {
174+
println!("[Github Release]: File is empty - creating new file");
175+
let mut config = Map::new();
176+
let mut assets = Map::new();
177+
let mut asset = Map::new();
178+
asset.insert("id".to_string(), Value::String(id));
179+
asset.insert("new_release".to_string(), Value::Bool(true));
180+
181+
let mut assets_array = Vec::new();
182+
// get the assets array from the json response and add it to the assets_array
183+
let assets_array_json = response["assets"].as_array();
184+
let assets_array_json_result = match assets_array_json {
185+
Some(assets_array_json) => assets_array_json,
186+
None => {
187+
error!("[Github Release]: Unable to get assets array");
188+
return Err("[Github Release]: Unable to get assets array".into());
189+
}
190+
};
191+
for asset in assets_array_json_result {
192+
assets_array.push(asset.clone());
193+
}
194+
asset.insert("assets".to_string(), Value::Array(assets_array));
195+
assets.insert("OpenIris".to_string(), Value::Object(asset));
196+
config.insert("assets".to_string(), Value::Object(assets));
197+
let config_json = serde_json::to_string_pretty(&config).unwrap();
198+
std::fs::write("config/config.json", config_json).expect("Unable to write file");
199+
return Ok("[Github Release]: Grabbed Newest Github Asset Config - Created new config file".to_string());
200+
}
201+
202+
// we got hre because the file is not empty - so parse the json config file
203+
204+
// check if the id is the same as the one in the config file
205+
206+
// if the id is the same, return and do nothing
207+
208+
// if the id is different, update the config file with the new id and assets array
209+
// parse the json config file
210+
let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
211+
debug!("[Github Release]: Current Config: {:?}", config);
212+
213+
if id == config["assets"]["OpenIris"]["id"] {
214+
println!("[Github Release]: No new release - using cached config");
215+
return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string());
216+
}
217+
218+
let mut config = Map::new();
219+
let mut assets = Map::new();
220+
let mut asset = Map::new();
221+
asset.insert("id".to_string(), Value::String(id));
222+
asset.insert("new_release".to_string(), Value::Bool(true));
223+
224+
let mut assets_array = Vec::new();
225+
// get the assets array from the json response and add it to the assets_array
226+
let assets_array_json = response["assets"].as_array();
227+
let assets_array_json_result = match assets_array_json {
228+
Some(assets_array_json) => assets_array_json,
229+
None => {
230+
error!("Unable to get assets array");
231+
return Err("Unable to get assets array".into());
232+
}
233+
};
234+
for asset in assets_array_json_result {
235+
assets_array.push(asset.clone());
236+
}
237+
asset.insert("assets".to_string(), Value::Array(assets_array));
238+
assets.insert("OpenIris".to_string(), Value::Object(asset));
239+
config.insert("assets".to_string(), Value::Object(assets));
240+
let config_json = serde_json::to_string_pretty(&config).unwrap();
241+
std::fs::write("config/config.json", config_json).expect("Unable to write file");
242+
return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string());
243+
}
244+
}
245+
Err(e) => println!("JSON parse failed: {}", e),
246+
}
247+
Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string())
248+
}

GUI/ETVR/src-tauri/tauri.conf.json

+33-15
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@
2727
"save": true
2828
},
2929
"fs": {
30-
"all": false,
31-
"copyFile": false,
32-
"createDir": true,
33-
"exists": false,
30+
"all": true,
31+
"readFile": true,
32+
"writeFile": true,
3433
"readDir": true,
35-
"readFile": false,
36-
"removeDir": false,
37-
"removeFile": false,
38-
"renameFile": false,
34+
"copyFile": true,
35+
"createDir": true,
36+
"removeDir": true,
37+
"removeFile": true,
38+
"renameFile": true,
39+
"exists": true,
3940
"scope": [
4041
"$APP/*",
42+
"$APPDATA/*",
43+
"$APPCONFIG/*",
4144
"$DOCUMENT/*",
4245
"$DOWNLOAD/*"
43-
],
44-
"writeFile": true
46+
]
4547
},
4648
"globalShortcut": {
4749
"all": false
@@ -50,7 +52,8 @@
5052
"all": true,
5153
"request": true,
5254
"scope": [
53-
"http://127.0.0.1:7856/*"
55+
"http://127.0.0.1:7856/*",
56+
"https://api.github.com/repos/lorow/OpenIris/*"
5457
]
5558
},
5659
"notification": {
@@ -77,8 +80,21 @@
7780
"all": false,
7881
"execute": false,
7982
"open": ".*",
80-
"scope": [],
81-
"sidecar": false
83+
"scope": [
84+
{
85+
"name": "binaries/dra",
86+
"sidecar": true,
87+
"args": [
88+
"download",
89+
"--select",
90+
{
91+
"validator": "^\"[A-Za-z0-9]+_[A-Za-z]+-v\\{[A-Za-z]+\\}||[A-Za-z0-9]+-[A-Za-z]+\\.[A-Za-z]+\"$"
92+
},
93+
"lorow/OpenIris"
94+
]
95+
}
96+
],
97+
"sidecar": true
8298
},
8399
"window": {
84100
"all": false,
@@ -128,7 +144,9 @@
128144
"identifier": "com.eyetrackvr.dev",
129145
"longDescription": "",
130146
"resources": [],
131-
"externalBin": [],
147+
"externalBin": [
148+
"binaries/dra"
149+
],
132150
"shortDescription": "",
133151
"targets": "all",
134152
"macOS": {
@@ -192,4 +210,4 @@
192210
"iconAsTemplate": true
193211
}
194212
}
195-
}
213+
}

0 commit comments

Comments
 (0)