Skip to content

Commit

Permalink
working triggering miner
Browse files Browse the repository at this point in the history
  • Loading branch information
Misieq01 committed Aug 8, 2024
1 parent 1e99d44 commit 42ae5b1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src-tauri/src/auto_miner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use device_query::{ DeviceEvents, DeviceQuery, DeviceState, MouseState };
use device_query::{ DeviceQuery, DeviceState };
use log::info;
use tokio::{ spawn, time::{ sleep, Duration, Timeout } };
use tauri::{ window, Manager };
use tokio::{ time::{ sleep, Duration } };
use tokio_util::sync::CancellationToken;

#[derive(Debug, Clone)]
Expand All @@ -11,6 +12,11 @@ pub struct AutoMiner {
pub cancelation_token: Option<CancellationToken>,
}

#[derive(Clone, serde::Serialize)]
pub struct Payload {
event_type: String,
}

impl AutoMiner {
pub fn new() -> Self {
Self {
Expand All @@ -21,25 +27,22 @@ impl AutoMiner {
}
}

pub fn set_auto_mining_listening(&mut self, is_listening: bool) {
self.is_listening = is_listening;
}

pub fn read_user_mouse_coords() -> (i32, i32) {
let device_state = DeviceState::new();
let mouse = device_state.get_mouse();

return mouse.coords;
}

pub fn start_listening_to_mouse_poisition_change(&mut self) {
pub fn start_listening_to_mouse_poisition_change(&mut self, _window: tauri::Window) {
let idle_timeout = self.idle_timeout;

let cancellation_token = CancellationToken::new();
self.cancelation_token = Some(cancellation_token.clone());
self.is_listening = true;

let mut auto_miner = self.to_owned();
let window = _window.clone();

let mut timeout_counter: u64 = 0;
let mut last_mouse_coords = AutoMiner::read_user_mouse_coords();
Expand All @@ -61,12 +64,12 @@ impl AutoMiner {


if timeout_counter >= idle_timeout && !auto_miner.is_mining {
AutoMiner::start_auto_mining();
AutoMiner::start_auto_mining(&window);
auto_miner.is_mining = true;
}

if timeout_counter < idle_timeout && auto_miner.is_mining {
AutoMiner::stop_auto_mining();
AutoMiner::stop_auto_mining(&window);
auto_miner.is_mining = false;
}

Expand All @@ -75,7 +78,7 @@ impl AutoMiner {
} => {},
_ = cancellation_token.cancelled() => {
info!("AutoMiner::start_listening_to_mouse_poisition_change() has been cancelled");
AutoMiner::stop_auto_mining();
AutoMiner::stop_auto_mining(&window);
auto_miner.is_mining = false;
}
}
Expand All @@ -94,11 +97,13 @@ impl AutoMiner {
}
}

pub fn start_auto_mining() {
pub fn start_auto_mining(window: &tauri::Window) {
window.emit("message", Payload { event_type: "user_idle".to_string() }).unwrap();
println!("Mining started");
}

pub fn stop_auto_mining() {
pub fn stop_auto_mining(window: &tauri::Window) {
window.emit("message", Payload { event_type: "user_active".to_string() }).unwrap();
println!("Mining stopped");
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn start_auto_mining<'r>(
app: tauri::AppHandle
) -> Result<(), String> {
let mut auto_miner = state.auto_miner.write().await;
auto_miner.start_listening_to_mouse_poisition_change();
auto_miner.start_listening_to_mouse_poisition_change(window);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"windows": [
{
"title": "tari-universe",
"label": "main",
"width": 1200,
"height": 800,
"resizable": true,
Expand Down
11 changes: 11 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { listen } from '@tauri-apps/api/event';
import { TauriEvent } from './types.ts';
import useAppStateStore from './store/appStateStore.ts';
import { useUserMousePosition } from './hooks/useUserMousePosition.ts';
import { useMining } from './hooks/useMining.ts';

function App() {
const background = useUIStore((s) => s.background);
Expand All @@ -36,6 +37,16 @@ function App() {
settingUpFinished();
}
break;
case 'user_idle':
invoke('start_mining').then(() => {
console.log('Mining started');
});
break;
case 'user_active':
invoke('stop_mining').then(() => {
console.log('Mining stopped');
});
break;
default:
console.log('Unknown tauri event: ', {
event,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Use union type
type TauriEventPayload = {
event_type: 'setup_status';
event_type: 'setup_status' | 'user_idle' | 'user_active';
title: string;
progress: number;
};
Expand Down

0 comments on commit 42ae5b1

Please sign in to comment.