feat(Device): add device presence check

This commit is contained in:
AkiChase 2024-05-14 19:45:01 +08:00
parent db4f51e9e2
commit fa0c9fedd2
10 changed files with 102 additions and 4 deletions

View File

@ -16,6 +16,7 @@ tauri-plugin-store = "2.0.0-beta"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
anyhow = "1.0" anyhow = "1.0"
lazy_static = "1.4.0"
tokio = { version = "1.36.0", features = ["rt-multi-thread", "net", "macros", "io-util", "time", "sync"] } tokio = { version = "1.36.0", features = ["rt-multi-thread", "net", "macros", "io-util", "time", "sync"] }
tauri-plugin-process = "2.0.0-beta" tauri-plugin-process = "2.0.0-beta"
tauri-plugin-shell = "2.0.0-beta" tauri-plugin-shell = "2.0.0-beta"

View File

@ -3,7 +3,7 @@ use std::{io::BufRead, path::PathBuf};
use crate::{ use crate::{
adb::{Adb, Device}, adb::{Adb, Device},
resource::{ResHelper, ResourceName}, resource::{ResHelper, ResourceName}, share,
}; };
/** /**
@ -119,6 +119,8 @@ impl ScrcpyClient {
// clear string to store new line only // clear string to store new line only
s.clear(); s.clear();
} }
*share::CLIENT_INFO.lock().unwrap() = None;
println!("Scrcpy server closed"); println!("Scrcpy server closed");
Ok(()) Ok(())
} }

View File

@ -5,3 +5,4 @@ pub mod control_msg;
pub mod resource; pub mod resource;
pub mod scrcpy_mask_cmd; pub mod scrcpy_mask_cmd;
pub mod socket; pub mod socket;
pub mod share;

View File

@ -5,6 +5,7 @@ use scrcpy_mask::{
adb::{Adb, Device}, adb::{Adb, Device},
client::ScrcpyClient, client::ScrcpyClient,
resource::{ResHelper, ResourceName}, resource::{ResHelper, ResourceName},
share,
socket::connect_socket, socket::connect_socket,
}; };
use std::{fs::read_to_string, sync::Arc}; use std::{fs::read_to_string, sync::Arc};
@ -54,6 +55,17 @@ fn start_scrcpy_server(
address: String, address: String,
app: tauri::AppHandle, app: tauri::AppHandle,
) -> Result<(), String> { ) -> Result<(), String> {
let mut client_info = share::CLIENT_INFO.lock().unwrap();
if let Some(_) = &*client_info {
return Err("client already exists".to_string());
}
*client_info = Some(share::ClientInfo::new(
"unknow".to_string(),
id.clone(),
scid.clone(),
));
let dir = app.path().resource_dir().unwrap().join("resource"); let dir = app.path().resource_dir().unwrap().join("resource");
let version = ScrcpyClient::get_scrcpy_version(); let version = ScrcpyClient::get_scrcpy_version();
@ -108,6 +120,15 @@ fn start_scrcpy_server(
Ok(()) Ok(())
} }
#[tauri::command]
fn get_cur_client_info() -> Result<Option<share::ClientInfo>, String> {
let client_info = share::CLIENT_INFO.lock().unwrap();
match &*client_info {
Some(client) => Ok(Some(client.clone())),
None => Ok(None),
}
}
#[tauri::command] #[tauri::command]
/// get device screen size /// get device screen size
fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32), String> { fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32), String> {
@ -217,6 +238,7 @@ async fn main() {
forward_server_port, forward_server_port,
push_server_file, push_server_file,
start_scrcpy_server, start_scrcpy_server,
get_cur_client_info,
get_device_screen_size, get_device_screen_size,
adb_connect, adb_connect,
load_default_keyconfig load_default_keyconfig

23
src-tauri/src/share.rs Normal file
View File

@ -0,0 +1,23 @@
use lazy_static::lazy_static;
use std::sync::Mutex;
#[derive(Debug, Clone, serde::Serialize)]
pub struct ClientInfo {
pub device_name: String,
pub device_id: String,
pub scid: String,
}
impl ClientInfo {
pub fn new(device_name: String, device_id: String, scid: String) -> Self {
Self {
device_name,
device_id,
scid,
}
}
}
lazy_static! {
pub static ref CLIENT_INFO: Mutex<Option<ClientInfo>> = Mutex::new(None);
}

View File

@ -13,6 +13,7 @@ use tokio::{
use crate::{ use crate::{
control_msg::{self, ControlMsgType}, control_msg::{self, ControlMsgType},
scrcpy_mask_cmd::{self, ScrcpyMaskCmdType}, scrcpy_mask_cmd::{self, ScrcpyMaskCmdType},
share,
}; };
pub async fn connect_socket( pub async fn connect_socket(
@ -26,7 +27,7 @@ pub async fn connect_socket(
.await .await
.context("Socket connect failed")?; .context("Socket connect failed")?;
println!("成功连接scrcpy-server:{:?}", client.local_addr()); println!("connect to scrcpy-server:{:?}", client.local_addr());
let (read_half, write_half) = client.into_split(); let (read_half, write_half) = client.into_split();
@ -71,6 +72,9 @@ async fn read_socket(
end -= 1; end -= 1;
} }
let device_name = std::str::from_utf8(&buf[..end]).unwrap(); let device_name = std::str::from_utf8(&buf[..end]).unwrap();
// update device name for share
share::CLIENT_INFO.lock().unwrap().as_mut().unwrap().device_name = device_name.to_string();
let msg = json!({ let msg = json!({
"type": "MetaData", "type": "MetaData",
"deviceName": device_name, "deviceName": device_name,

View File

@ -17,6 +17,7 @@ import {
startScrcpyServer, startScrcpyServer,
getDeviceScreenSize, getDeviceScreenSize,
adbConnect, adbConnect,
getCurClientInfo,
} from "../invoke"; } from "../invoke";
import { import {
NH4, NH4,
@ -86,6 +87,26 @@ onMounted(async () => {
}); });
onActivated(async () => { onActivated(async () => {
let curClientInfo = await getCurClientInfo();
if (store.controledDevice) {
// update controledDevice if client not exists
if (!curClientInfo) {
await shutdown();
store.controledDevice = null;
message.warning(t("pages.Device.alreadyDisconnected"));
}
} else {
// restore controledDevice if client exists
if (curClientInfo) {
message.warning(t("pages.Device.alreadyControled"));
store.controledDevice = {
scid: curClientInfo.scid,
deviceName: curClientInfo.device_name,
deviceID: curClientInfo.device_id,
};
}
}
await refreshDevices(); await refreshDevices();
}); });
@ -178,6 +199,18 @@ function onMenuClickoutside() {
} }
async function deviceControl() { async function deviceControl() {
let curClientInfo = await getCurClientInfo();
if (curClientInfo) {
message.warning(t("pages.Device.alreadyControled"));
store.controledDevice = {
scid: curClientInfo.scid,
deviceName: curClientInfo.device_name,
deviceID: curClientInfo.device_id,
};
store.hideLoading();
return;
}
if (!port.value) { if (!port.value) {
port.value = 27183; port.value = 27183;
} }

View File

@ -35,7 +35,9 @@
}, },
"controledDevice": "Controlled device", "controledDevice": "Controlled device",
"availableDevice": "Available devices", "availableDevice": "Available devices",
"noControledDevice": "No Controled Device" "noControledDevice": "No Controled Device",
"alreadyControled": "Controlled device already exists",
"alreadyDisconnected": "Controlled device connection has been disconnected"
}, },
"Mask": { "Mask": {
"inputBoxPlaceholder": "Input text and then press enter/esc", "inputBoxPlaceholder": "Input text and then press enter/esc",

View File

@ -35,7 +35,9 @@
}, },
"controledDevice": "受控设备", "controledDevice": "受控设备",
"noControledDevice": "无受控设备", "noControledDevice": "无受控设备",
"availableDevice": "可用设备" "availableDevice": "可用设备",
"alreadyControled": "已存在受控设备",
"alreadyDisconnected": "受控设备连接已断开"
}, },
"Mask": { "Mask": {
"keyconfigException": "按键方案异常,请删除此方案", "keyconfigException": "按键方案异常,请删除此方案",

View File

@ -29,6 +29,14 @@ export async function startScrcpyServer(
return await invoke("start_scrcpy_server", { id, scid, address }); return await invoke("start_scrcpy_server", { id, scid, address });
} }
export async function getCurClientInfo(): Promise<{
device_name: string;
device_id: string;
scid: string;
} | null> {
return await invoke("get_cur_client_info");
}
export async function getDeviceScreenSize( export async function getDeviceScreenSize(
id: string id: string
): Promise<[number, number]> { ): Promise<[number, number]> {