Skip to content

Commit

Permalink
migrated to react
Browse files Browse the repository at this point in the history
  • Loading branch information
olegdayo committed Jan 23, 2023
0 parents commit a963782
Show file tree
Hide file tree
Showing 42 changed files with 730 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
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
7 changes: 7 additions & 0 deletions README.md
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)
14 changes: 14 additions & 0 deletions index.html
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>
26 changes: 26 additions & 0 deletions package.json
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"
}
}
6 changes: 6 additions & 0 deletions public/tauri.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src-tauri/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/

42 changes: 42 additions & 0 deletions src-tauri/Cargo.toml
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"]
3 changes: 3 additions & 0 deletions src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}
Binary file added src-tauri/icons/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square107x107Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square142x142Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square150x150Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square284x284Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square30x30Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square310x310Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square44x44Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square71x71Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/Square89x89Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/StoreLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/icons/icon.icns
Binary file not shown.
Binary file added src-tauri/icons/icon.ico
Binary file not shown.
Binary file added src-tauri/icons/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src-tauri/src/config/config.yaml
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
38 changes: 38 additions & 0 deletions src-tauri/src/config/mod.rs
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)),
}
}
25 changes: 25 additions & 0 deletions src-tauri/src/config/tests.rs
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);
}
80 changes: 80 additions & 0 deletions src-tauri/src/input.rs
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 {})
}
61 changes: 61 additions & 0 deletions src-tauri/src/main.rs
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");
}
Loading

0 comments on commit a963782

Please sign in to comment.