Skip to content

Commit

Permalink
feat: implement getting firmware release
Browse files Browse the repository at this point in the history
- grabs releases from repo
- checks if releases are newer
- writes to config file
  • Loading branch information
ZanzyTHEbar committed Mar 22, 2023
1 parent f2e4e33 commit 7372d47
Show file tree
Hide file tree
Showing 23 changed files with 704 additions and 93 deletions.
22 changes: 22 additions & 0 deletions GUI/ETVR/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions GUI/ETVR/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "eyetrackvr"
version = "1.0.0"
description = "Eye tracking for VR"
authors = [ "DaOfficialWizard", "Luckmer", "RedHawk989",]
authors = [ "DaOfficialWizard", "Luckmer", "RedHawk989"]
license = "MIT"
repository = "https://github.com/RedHawk989/EyeTrackVR"
default-run = "eyetrackvr"
Expand All @@ -21,16 +21,16 @@ futures-util = "0.3.25"
mdns-sd = "0.5.9"

[features]
default = [ "custom-protocol",]
custom-protocol = [ "tauri/custom-protocol",]
default = [ "custom-protocol"]
custom-protocol = [ "tauri/custom-protocol"]

[dependencies.serde]
version = "1.0"
features = [ "derive",]
features = [ "derive"]

[dependencies.tauri]
version = "1.1.1"
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",]
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"]

[dependencies.tauri-plugin-window-state]
git = "https://github.com/tauri-apps/tauri-plugin-window-state/"
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions GUI/ETVR/src-tauri/src/modules/m_dnsquery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,6 @@ pub async fn generate_json(instance: &Mdns) -> Result<String, Box<dyn std::error
// write the json object to a file
let to_string_json = serde_json::to_string_pretty(&config)?;
// return the json object as a string

Ok(to_string_json)
}
177 changes: 159 additions & 18 deletions GUI/ETVR/src-tauri/src/modules/rest_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::modules::m_dnsquery;
use futures_util::future::ok;
use log::{debug, error, info, warn};
use reqwest::Client;
use serde::Deserialize;
use serde_json::{Map, Value};
use std::collections::hash_map::HashMap;
use std::io::Read;

/// A struct to hold the REST client
/// ## Fields
Expand Down Expand Up @@ -38,6 +40,7 @@ pub async fn request(rest_client: &RESTClient) -> Result<String, String> {
response = rest_client
.http_client
.get(&rest_client.base_url)
.header("User-Agent", "EyeTrackVR")
.send()
.await
.map_err(|e| e.to_string())?
Expand Down Expand Up @@ -77,31 +80,169 @@ pub async fn run_rest_client(
method: String,
) -> Result<String, String> {
info!("Starting REST client");
// read the json config file
let data = std::fs::read_to_string("config/config.json").expect("Unable to read config file");
// parse the json config file
let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
debug!("Current Config: {:?}", config);
let full_url = format!("{}{}", device_name, endpoint);
let mut request_response: String = String::new();
let mut url = config["urls"][device_name].as_str();
let full_url_result = match url {
Some(url) => url,
None => {
error!("Unable to get url");
url = Some("");
return Err(format!("Unable to get url: {:?}", url));
}
};
let full_url = format!("{}{}", full_url_result, endpoint);
//info!("Full url: {}", full_url);
let rest_client = RESTClient::new(full_url, method);
let request_result = request(&rest_client).await;
match request_result {
Ok(response) => {
request_response = response;
println!("Request response: {:?}", request_response);
println!("[REST Client]: Request response: {:?}", request_response);
}
Err(e) => println!("[REST Client]: Request failed: {}", e),
}
Ok(request_response)
}

async fn run_gh_release_latest() -> Result<String, String> {
info!("Starting GitHub release client");
let endpoint = "/releases/latest";
let device_name = "https://api.github.com/repos/lorow/OpenIris";
let method = "GET".to_string();
let mut request_response: String = String::new();
let request_result = run_rest_client(endpoint.to_string(), device_name.to_string(), method).await;

match request_result {
Ok(response) => {
request_response = response;
/* println!(
"[Github Release Latest]: Request response: {:?}",
request_response
); */
}
Err(e) => println!("Request failed: {}", e),
Err(e) => println!("[Github Release Latest]: Request failed: {}", e),
}
Ok(request_response)
}

/*
release_id: 0,
new_release: false,
assets: [],
*/
///! DEPRICATED - but it works ....
pub async fn run_gh_release_assets() -> Result<String, String> {
info!("Starting GitHub release client");
let gh_response = run_gh_release_latest().await;
let mut request_response: String = String::new();
match gh_response {
Ok(response) => {
request_response = response;
/* println!(
"[Github Release Asset]: Request Response: {:?}",
request_response
); */
}
Err(e) => println!("[Github Release Asset]: Request failed: {}", e),
}

let json_response = serde_json::from_str::<Map<String, Value>>(&request_response);

match json_response {
Ok(response) => {
println!("[Github Release]: JSON response: {:?}", response);
// check if the json object is empty
if response.is_empty() {
warn!("[Github Release]: JSON object is empty");
return Err("[Github Release]: JSON object is empty".into());
}

// 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
if response.contains_key("id") {
let id = response["id"].to_string();
// read the json config file and create it if it doesn't exist
let mut data = String::new();
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("config/config.json");

match file {
Ok(ref mut file) => {
file.read_to_string(&mut data).unwrap();
}
Err(e) => {
error!("[Github Release]: Unable to open config file: {}", e);
return Err("[Github Release]: Unable to open config file".into());
}
}

// if the file is empty, create a new file with the id, and assets array
if data.is_empty() {
println!("[Github Release]: File is empty - creating new file");
let mut config = Map::new();
let mut assets = Map::new();
let mut asset = Map::new();
asset.insert("id".to_string(), Value::String(id));
asset.insert("new_release".to_string(), Value::Bool(true));

let mut assets_array = Vec::new();
// get the assets array from the json response and add it to the assets_array
let assets_array_json = response["assets"].as_array();
let assets_array_json_result = match assets_array_json {
Some(assets_array_json) => assets_array_json,
None => {
error!("[Github Release]: Unable to get assets array");
return Err("[Github Release]: Unable to get assets array".into());
}
};
for asset in assets_array_json_result {
assets_array.push(asset.clone());
}
asset.insert("assets".to_string(), Value::Array(assets_array));
assets.insert("OpenIris".to_string(), Value::Object(asset));
config.insert("assets".to_string(), Value::Object(assets));
let config_json = serde_json::to_string_pretty(&config).unwrap();
std::fs::write("config/config.json", config_json).expect("Unable to write file");
return Ok("[Github Release]: Grabbed Newest Github Asset Config - Created new config file".to_string());
}

// we got hre because the file is not empty - so parse the json config file

// check if the id is the same as the one in the config file

// if the id is the same, return and do nothing

// if the id is different, update the config file with the new id and assets array
// parse the json config file
let config: serde_json::Value = serde_json::from_str(&data).map_err(|e| e.to_string())?;
debug!("[Github Release]: Current Config: {:?}", config);

if id == config["assets"]["OpenIris"]["id"] {
println!("[Github Release]: No new release - using cached config");
return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string());
}

let mut config = Map::new();
let mut assets = Map::new();
let mut asset = Map::new();
asset.insert("id".to_string(), Value::String(id));
asset.insert("new_release".to_string(), Value::Bool(true));

let mut assets_array = Vec::new();
// get the assets array from the json response and add it to the assets_array
let assets_array_json = response["assets"].as_array();
let assets_array_json_result = match assets_array_json {
Some(assets_array_json) => assets_array_json,
None => {
error!("Unable to get assets array");
return Err("Unable to get assets array".into());
}
};
for asset in assets_array_json_result {
assets_array.push(asset.clone());
}
asset.insert("assets".to_string(), Value::Array(assets_array));
assets.insert("OpenIris".to_string(), Value::Object(asset));
config.insert("assets".to_string(), Value::Object(assets));
let config_json = serde_json::to_string_pretty(&config).unwrap();
std::fs::write("config/config.json", config_json).expect("Unable to write file");
return Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string());
}
}
Err(e) => println!("JSON parse failed: {}", e),
}
Ok("[Github Release]: Grabbed Newest Github Asset Config".to_string())
}
48 changes: 33 additions & 15 deletions GUI/ETVR/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@
"save": true
},
"fs": {
"all": false,
"copyFile": false,
"createDir": true,
"exists": false,
"all": true,
"readFile": true,
"writeFile": true,
"readDir": true,
"readFile": false,
"removeDir": false,
"removeFile": false,
"renameFile": false,
"copyFile": true,
"createDir": true,
"removeDir": true,
"removeFile": true,
"renameFile": true,
"exists": true,
"scope": [
"$APP/*",
"$APPDATA/*",
"$APPCONFIG/*",
"$DOCUMENT/*",
"$DOWNLOAD/*"
],
"writeFile": true
]
},
"globalShortcut": {
"all": false
Expand All @@ -50,7 +52,8 @@
"all": true,
"request": true,
"scope": [
"http://127.0.0.1:7856/*"
"http://127.0.0.1:7856/*",
"https://api.github.com/repos/lorow/OpenIris/*"
]
},
"notification": {
Expand All @@ -77,8 +80,21 @@
"all": false,
"execute": false,
"open": ".*",
"scope": [],
"sidecar": false
"scope": [
{
"name": "binaries/dra",
"sidecar": true,
"args": [
"download",
"--select",
{
"validator": "^\"[A-Za-z0-9]+_[A-Za-z]+-v\\{[A-Za-z]+\\}||[A-Za-z0-9]+-[A-Za-z]+\\.[A-Za-z]+\"$"
},
"lorow/OpenIris"
]
}
],
"sidecar": true
},
"window": {
"all": false,
Expand Down Expand Up @@ -128,7 +144,9 @@
"identifier": "com.eyetrackvr.dev",
"longDescription": "",
"resources": [],
"externalBin": [],
"externalBin": [
"binaries/dra"
],
"shortDescription": "",
"targets": "all",
"macOS": {
Expand Down Expand Up @@ -192,4 +210,4 @@
"iconAsTemplate": true
}
}
}
}
Loading

0 comments on commit 7372d47

Please sign in to comment.