-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a963782
Showing
42 changed files
with
730 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
# Build | ||
build/ | ||
target/ | ||
|
||
# Lock | ||
package-lock.json | ||
Cargo.lock | ||
|
||
# Editor directories and files | ||
.vscode/ | ||
.idea/ | ||
.fleet/ | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
# Other | ||
node_modules/ | ||
dist | ||
dist-ssr | ||
*.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Tauri + React + Typescript | ||
|
||
This template should help get you started developing with Tauri, React and Typescript in Vite. | ||
|
||
## Recommended IDE Setup | ||
|
||
- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Tauri + React + TS</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "mascoty-taurine", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview", | ||
"tauri": "tauri" | ||
}, | ||
"dependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"@tauri-apps/api": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.7.10", | ||
"@types/react": "^18.0.15", | ||
"@types/react-dom": "^18.0.6", | ||
"@vitejs/plugin-react": "^3.0.0", | ||
"typescript": "^4.6.4", | ||
"vite": "^4.0.0", | ||
"@tauri-apps/cli": "^1.2.2" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[package] | ||
name = "mascoty-taurine" | ||
version = "0.0.0" | ||
description = "Backend of Mascoty Taurine app" | ||
authors = [ "offluck" ] | ||
license = "" | ||
repository = "" | ||
edition = "2021" | ||
rust-version = "1.57" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[build-dependencies] | ||
tauri-build = { version = "1.2", features = [] } | ||
|
||
[dependencies] | ||
serde = { version = "1.0.152", features = [ "derive" ] } | ||
serde_json = "1.0.91" | ||
serde_yaml = "0.9.17" | ||
|
||
log = "0.4.17" | ||
simple_logger = "4.0.0" | ||
|
||
tauri = { version = "1.2", features = ["shell-open"] } | ||
rand = "0.8.4" | ||
dotenv = "0.15.0" | ||
|
||
[dependencies.nokhwa] | ||
version = "0.10.3" | ||
features = [ | ||
"input-avfoundation", | ||
"input-msmf", | ||
"output-wgpu", | ||
] | ||
|
||
[features] | ||
# by default Tauri runs in production mode | ||
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL | ||
default = ["custom-protocol"] | ||
# this feature is used used for production builds where `devPath` points to the filesystem | ||
# DO NOT remove this | ||
custom-protocol = ["tauri/custom-protocol"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
tauri_build::build() | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
service: | ||
url: 127.0.0.1 | ||
port: 50051 | ||
camera: | ||
height: 720 | ||
width: 1280 | ||
fps: 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
use std::fs::File; | ||
|
||
use log::{debug, info, warn, error}; | ||
use serde_yaml; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub service: Service, | ||
pub camera: Camera, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Service { | ||
pub url: String, | ||
pub port: u16, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Camera { | ||
pub height: u32, | ||
pub width: u32, | ||
pub fps: u32, | ||
} | ||
|
||
pub fn import_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> { | ||
debug!("Reading config file"); | ||
let file = File::open(path)?; | ||
|
||
debug!("Deserializing YAML"); | ||
match serde_yaml::from_reader(file) { | ||
Ok(conf) => Ok(conf), | ||
Err(err) => Err(Box::new(err)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use super::{import_config}; | ||
|
||
#[test] | ||
fn check_config_right_path() -> Result<(), Box<dyn std::error::Error>> { | ||
import_config("src/config/config.yaml")?; | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn check_config_wrong_path() { | ||
import_config("it is not even a path lol").unwrap(); | ||
} | ||
|
||
#[test] | ||
fn check_url() { | ||
let conf = import_config("src/config/config.yaml").unwrap(); | ||
assert!(conf.service.url.len() > 0); | ||
} | ||
|
||
#[test] | ||
fn check_port() { | ||
let conf = import_config("src/config/config.yaml").unwrap(); | ||
assert!(conf.service.port > 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::config::Config; | ||
use crate::panic_error; | ||
|
||
use std::sync::Mutex; | ||
// use std::cell::RefCell; | ||
|
||
use nokhwa::pixel_format::RgbFormat; | ||
use nokhwa::utils::{ApiBackend, CameraFormat, FrameFormat, RequestedFormat, RequestedFormatType}; | ||
use nokhwa::{query, Camera, NokhwaError}; | ||
|
||
use log::{debug, error, info, warn}; | ||
|
||
pub struct Devices { | ||
camera: Mutex<Camera>, | ||
} | ||
|
||
unsafe impl Sync for Devices {} | ||
|
||
unsafe impl Send for Devices {} | ||
|
||
pub struct Input {} | ||
|
||
pub fn get_devices(config: &Config) -> Result<Devices, NokhwaError> { | ||
debug!("Getting devices"); | ||
let cams = query(ApiBackend::Auto)?; | ||
|
||
info!("Number of cameras: {}", cams.len()); | ||
if cams.len() == 0 { | ||
return Err( | ||
NokhwaError::GeneralError( | ||
"Cannot find any connected camera".to_string(), | ||
), | ||
); | ||
} | ||
|
||
info!("First camera index: {}", cams[0].index()); | ||
debug!("Connecting to camera"); | ||
let format_type = RequestedFormatType::Exact( | ||
CameraFormat::new_from( | ||
config.camera.width, | ||
config.camera.height, | ||
FrameFormat::MJPEG, | ||
config.camera.fps, | ||
), | ||
); | ||
let format = RequestedFormat::new::<RgbFormat>(format_type); | ||
let mut camera = Camera::new(cams[0].index().to_owned(), format)?; | ||
info!("Camera info: {}", camera.info()); | ||
|
||
debug!("Openning stream"); | ||
camera.open_stream()?; | ||
|
||
debug!("Camera has been initialized"); | ||
Ok( | ||
Devices { | ||
camera: Mutex::new(camera), | ||
}, | ||
) | ||
} | ||
|
||
pub fn get_input(devices: &Devices) -> Result<Input, NokhwaError> { | ||
debug!("Getting input"); | ||
debug!("Getting camera instance"); | ||
let mut camera = panic_error!(devices.camera.lock(), "failed to lock mutex"); | ||
|
||
debug!("Getting frame"); | ||
let frame = camera.frame()?; | ||
|
||
debug!("Decoding image"); | ||
let rgb = frame.decode_image::<RgbFormat>()?; | ||
rgb.get_pixel(10, 10); | ||
info!( | ||
"Frame resolution: {}; Pixel: {:?}", | ||
frame.resolution(), | ||
rgb.get_pixel(10, 10), | ||
); | ||
|
||
debug!("Sending info"); | ||
Ok(Input {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#![cfg_attr( | ||
all(not(debug_assertions), target_os = "windows"), | ||
windows_subsystem = "windows" | ||
)] | ||
|
||
extern crate dotenv; | ||
extern crate log; | ||
extern crate nokhwa; | ||
extern crate rand; | ||
extern crate serde; | ||
extern crate serde_json; | ||
extern crate serde_yaml; | ||
extern crate simple_logger; | ||
|
||
mod config; | ||
mod input; | ||
mod mascot; | ||
mod utils; | ||
|
||
use crate::config::import_config; | ||
use crate::input::get_devices; | ||
|
||
use dotenv::dotenv; | ||
use input::Devices; | ||
use log::{debug, error, info, warn}; | ||
use simple_logger::SimpleLogger; | ||
|
||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command | ||
#[tauri::command] | ||
fn get_mascot(state: tauri::State<Devices>) -> mascot::Mascot { | ||
mascot::get_mascot(&state) | ||
} | ||
|
||
fn main() { | ||
match SimpleLogger::new() | ||
.with_level(log::LevelFilter::Debug) | ||
.init() | ||
{ | ||
Ok(()) => {} | ||
Err(err) => panic!("Cannot initialize logger: {:?}", err), | ||
}; | ||
|
||
match dotenv().ok() { | ||
Some(_) => debug!("Successfully imported data from .env"), | ||
None => debug!("Failed to work with .env"), | ||
}; | ||
|
||
debug!("Config parsing"); | ||
let conf = panic_error!(import_config("src/config/config.yaml"), "config parsing"); | ||
info!("Config: {:?}", conf); | ||
|
||
debug!("Getting devices"); | ||
|
||
let devices = panic_error!(get_devices(&conf), "getting devices"); | ||
|
||
tauri::Builder::default() | ||
.manage(devices) | ||
.invoke_handler(tauri::generate_handler![get_mascot]) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} |
Oops, something went wrong.