feat(control): 添加选手 FPS 键鼠操作与 C3 用户 UART 输出
This commit is contained in:
43 files changed
+2389
-77
No files matched your search
@@ -33,6 +33,32 @@ const FALLBACK_INPUT_FPS: f32 = 20.0;
|
||||
|
||||
type Shared<'a> = State<'a, Arc<AppState>>;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn operator_start(
|
||||
state: Shared<'_>,
|
||||
window: tauri::Window,
|
||||
maintenance: State<'_, Arc<crate::maintenance::MaintenanceState>>,
|
||||
startup: State<'_, crate::startup::StartupState>,
|
||||
mode: cam_core::operator::OperatorMode,
|
||||
) -> Result<u32, String> {
|
||||
if maintenance.is_busy() || !startup.finished() {
|
||||
return Err("请先完成启动检查或固件维护,再开启操作".into());
|
||||
}
|
||||
let focused = window
|
||||
.is_focused()
|
||||
.map_err(|error| format!("无法检查窗口焦点:{error}"))?;
|
||||
state.operator_focus(focused);
|
||||
state.operator_start(mode)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn operator_input(
|
||||
state: Shared<'_>,
|
||||
input: cam_core::operator::OperatorInput,
|
||||
) -> Result<(), String> {
|
||||
state.operator_input(input)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn match_action(
|
||||
state: Shared<'_>,
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
|
||||
mod commands;
|
||||
mod demo;
|
||||
mod version;
|
||||
mod firmware_build;
|
||||
mod latency;
|
||||
mod maintenance;
|
||||
mod firmware_build;
|
||||
mod recording;
|
||||
mod server;
|
||||
mod state;
|
||||
mod startup;
|
||||
mod state;
|
||||
mod version;
|
||||
|
||||
use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
@@ -329,6 +329,8 @@ fn run() -> Result<(), Box<dyn Error>> {
|
||||
commands::stream_info,
|
||||
commands::get_snapshot,
|
||||
commands::match_action,
|
||||
commands::operator_start,
|
||||
commands::operator_input,
|
||||
commands::release_match_source,
|
||||
commands::get_config,
|
||||
commands::list_media_files,
|
||||
@@ -372,7 +374,11 @@ fn run() -> Result<(), Box<dyn Error>> {
|
||||
// 解锁框。拦不住的路径仍然存在——`kill -9`、强退、拔电源——所以它同样是"防
|
||||
// 随手误操作",不是安全边界。
|
||||
.on_window_event(|window, event| {
|
||||
if let WindowEvent::Focused(focused) = event {
|
||||
window.state::<Arc<AppState>>().operator_focus(*focused);
|
||||
}
|
||||
if let WindowEvent::CloseRequested { api, .. } = event {
|
||||
window.state::<Arc<AppState>>().stop_operator("窗口正在关闭,操作已停止");
|
||||
if window.state::<Arc<maintenance::MaintenanceState>>().is_busy() {
|
||||
api.prevent_close();
|
||||
let _ = window.emit("maintenance-close-blocked", "固件维护正在进行,请等待完成;编译可先取消。请勿在写入或恢复期间关闭客户端。");
|
||||
@@ -391,15 +397,22 @@ fn run() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
app.run(|app, event| {
|
||||
if let RunEvent::ExitRequested { api, .. } = &event {
|
||||
app.state::<Arc<AppState>>()
|
||||
.stop_operator("正在退出,操作已停止");
|
||||
if app.state::<Arc<maintenance::MaintenanceState>>().is_busy() {
|
||||
api.prevent_exit();
|
||||
let _ = app.emit("maintenance-close-blocked", "固件维护正在进行,请等待完成后退出。");
|
||||
let _ = app.emit(
|
||||
"maintenance-close-blocked",
|
||||
"固件维护正在进行,请等待完成后退出。",
|
||||
);
|
||||
return;
|
||||
}
|
||||
let state = app.state::<Arc<AppState>>();
|
||||
|
||||
if app.state::<startup::StartupState>().finished()
|
||||
&& state.config().role.needs_password() && !state.is_unlocked() {
|
||||
&& state.config().role.needs_password()
|
||||
&& !state.is_unlocked()
|
||||
{
|
||||
api.prevent_exit();
|
||||
state.push(server::WireMsg::ConfirmQuit);
|
||||
return;
|
||||
|
||||
@@ -23,6 +23,7 @@ use anyhow::{Context, Result};
|
||||
use cam_core::armor::{ArmorHealthStore, HealthTracker};
|
||||
use cam_core::config::{Config, Role};
|
||||
use cam_core::match_clock::{MatchAction, MatchClock, MatchView};
|
||||
use cam_core::operator::{OperatorContext, OperatorInput, OperatorMode, OperatorState};
|
||||
use cam_core::password;
|
||||
use cam_core::receiver::{Discovery, LatestFrames, Receiver};
|
||||
use cam_core::slots::SlotAllocator;
|
||||
@@ -59,6 +60,9 @@ pub struct AppState {
|
||||
match_socket: Option<std::net::UdpSocket>,
|
||||
match_error: Mutex<Option<String>>,
|
||||
match_changed: tokio::sync::Notify,
|
||||
operator: Mutex<OperatorState>,
|
||||
operator_socket: Option<std::net::UdpSocket>,
|
||||
operator_focused: AtomicBool,
|
||||
resetting: AtomicBool,
|
||||
pub demo_reset_epoch: AtomicUsize,
|
||||
discovery: Arc<Discovery>,
|
||||
@@ -86,6 +90,107 @@ pub struct AppState {
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
fn operator_context(&self) -> OperatorContext {
|
||||
let config = self.config();
|
||||
let clock = self.match_view();
|
||||
let board = config
|
||||
.armor_id
|
||||
.and_then(|id| self.armor.boards().into_iter().find(|b| b.id == id));
|
||||
OperatorContext {
|
||||
role: config.role,
|
||||
role_chosen: config.role_chosen,
|
||||
board_id: config.armor_id,
|
||||
board_ready: board
|
||||
.as_ref()
|
||||
.is_some_and(|b| b.is_online() && b.team == config.team && b.reports_all_plates()),
|
||||
hp: config
|
||||
.armor_id
|
||||
.and_then(|id| self.armor.health_of_board(id)),
|
||||
phase: clock.phase,
|
||||
connected: clock.connected,
|
||||
has_source: clock.source.is_some(),
|
||||
focused: self.operator_focused.load(Ordering::Acquire),
|
||||
demo: self.demo,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn operator_start(&self, mode: OperatorMode) -> Result<u32, String> {
|
||||
if self.operator_socket.is_none() {
|
||||
return Err("操控 UDP 通道不可用,请重启客户端".into());
|
||||
}
|
||||
let context = self.operator_context();
|
||||
let id = self.operator.lock().expect("operator poisoned").start(
|
||||
mode,
|
||||
&context,
|
||||
self.match_now(),
|
||||
)?;
|
||||
self.push_snapshot();
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn operator_input(&self, input: OperatorInput) -> Result<(), String> {
|
||||
let context = self.operator_context();
|
||||
let result = self.operator.lock().expect("operator poisoned").input(
|
||||
input,
|
||||
&context,
|
||||
self.match_now(),
|
||||
);
|
||||
if !input.active || result.is_err() {
|
||||
self.tick_operator();
|
||||
self.push_snapshot();
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn operator_focus(&self, focused: bool) {
|
||||
self.operator_focused.store(focused, Ordering::Release);
|
||||
if !focused {
|
||||
self.stop_operator("窗口失焦,操作已停止");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop_operator(&self, reason: &str) {
|
||||
self.operator
|
||||
.lock()
|
||||
.expect("operator poisoned")
|
||||
.stop(reason);
|
||||
self.tick_operator();
|
||||
}
|
||||
|
||||
fn tick_operator(&self) {
|
||||
let context = self.operator_context();
|
||||
let packet = self
|
||||
.operator
|
||||
.lock()
|
||||
.expect("operator poisoned")
|
||||
.packet(&context, self.match_now());
|
||||
if self.demo {
|
||||
return;
|
||||
}
|
||||
let Some(packet) = packet else {
|
||||
return;
|
||||
};
|
||||
// 地址只从显式绑定板的上报取得。禁能帧仍发给原板,不受新绑定替换。
|
||||
let destination = self
|
||||
.armor
|
||||
.boards()
|
||||
.into_iter()
|
||||
.find(|b| b.id == packet.board_id)
|
||||
.map(|b| b.addr);
|
||||
let sent = match (&self.operator_socket, destination, packet.encode()) {
|
||||
(Some(socket), Some(destination), Some(bytes)) => {
|
||||
socket.send_to(&bytes, destination).map(|_| ())
|
||||
}
|
||||
_ => Err(std::io::Error::other("装甲板操控地址不可用")),
|
||||
};
|
||||
if let Err(error) = sent {
|
||||
self.operator
|
||||
.lock()
|
||||
.expect("operator poisoned")
|
||||
.stop(&format!("操控发送失败:{error};请重新开启"));
|
||||
}
|
||||
}
|
||||
|
||||
fn match_now(&self) -> u64 {
|
||||
self.started.elapsed().as_millis().min(u64::MAX as u128) as u64
|
||||
}
|
||||
@@ -313,6 +418,9 @@ impl AppState {
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos() as u64;
|
||||
let operator_socket = std::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0))
|
||||
.ok()
|
||||
.filter(|socket| socket.set_nonblocking(true).is_ok());
|
||||
let state = Arc::new(Self {
|
||||
config_path,
|
||||
config: Mutex::new(config),
|
||||
@@ -324,6 +432,9 @@ impl AppState {
|
||||
match_socket,
|
||||
match_error: Mutex::new(match_error),
|
||||
match_changed: tokio::sync::Notify::new(),
|
||||
operator: Mutex::new(OperatorState::new(session)),
|
||||
operator_socket,
|
||||
operator_focused: AtomicBool::new(true),
|
||||
resetting: AtomicBool::new(false),
|
||||
demo_reset_epoch: AtomicUsize::new(0),
|
||||
discovery,
|
||||
@@ -457,6 +568,16 @@ impl AppState {
|
||||
server::serve(listener, token, tx, video, clients).await;
|
||||
});
|
||||
|
||||
let operator_state = Arc::clone(&state);
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut ticker = tokio::time::interval(Duration::from_millis(20));
|
||||
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
operator_state.tick_operator();
|
||||
}
|
||||
});
|
||||
|
||||
let state_for_loop = Arc::clone(&state);
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut ticker = tokio::time::interval(SNAPSHOT_INTERVAL);
|
||||
@@ -507,6 +628,7 @@ impl AppState {
|
||||
/// 每一次可能改变身份、绑定或发现开合的改动之后都要调用它——把这件事收在一个函数
|
||||
/// 里,是为了不让"某个命令忘了重新套用闸门"这种漏洞有时间长出来。
|
||||
pub fn apply_gates(&self) {
|
||||
self.stop_operator("身份、绑定或设置已改变,请重新开启操作");
|
||||
let config = self.config();
|
||||
self.match_clock
|
||||
.lock()
|
||||
@@ -836,6 +958,7 @@ impl AppState {
|
||||
.apply_outcomes(&mut health);
|
||||
|
||||
Snapshot {
|
||||
operator_control: self.operator.lock().expect("operator poisoned").view(),
|
||||
match_clock: self.match_view(),
|
||||
role: config.role,
|
||||
team: config.team,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "RMIC-2026",
|
||||
"version": "0.4.1",
|
||||
"version": "0.5.0",
|
||||
"identifier": "com.simbarobotics.rmic2026",
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
|
||||
Reference in new issue
Block a user