unify peripheral state struct.

This commit is contained in:
Vixalie 2025-02-28 13:50:53 +08:00
parent c6043b612f
commit 26165f5d5c
3 changed files with 10 additions and 31 deletions

View File

@ -19,7 +19,8 @@ pub struct CentralState {
pub struct PeripheralItem {
pub id: PeripheralId,
pub address: String,
pub represent: String,
pub represent: Option<String>,
pub is_unknown: bool,
pub is_connected: bool,
pub rssi: Option<i16>,
pub battery: Option<i16>,
@ -42,9 +43,8 @@ impl PeripheralItem {
Self {
id: periperial.id(),
address: periperial.address().to_string(),
represent: properties
.and_then(|p| p.local_name.clone())
.unwrap_or(String::from("Unknown")),
represent: properties.and_then(|p| p.local_name.clone()),
is_unknown: properties.and_then(|p| p.local_name.clone()).is_none(),
is_connected: periperial.is_connected().await.unwrap_or(false),
rssi: properties.and_then(|p| p.rssi),
battery: properties.and_then(|p| p.tx_power_level),

View File

@ -19,18 +19,11 @@ type PeripheralItem = {
id: string;
address: string;
represent: string | null;
isUnknown: boolean;
isConnected: boolean;
rssi: number | null;
battery: number | null;
};
type DeviceState = {
id: string | null;
address: string | null;
represent: string | null;
isConnected: boolean | null;
rssi: number | null;
battery: number | null;
};
type BluetoothState = {
ready: boolean | null;
searching: boolean | null;
@ -54,28 +47,14 @@ export const BleState = atomWithRefresh<BluetoothState>(async (get) => {
connected: null,
};
});
export const DeviceState = atomWithRefresh<DeviceState>(async (get) => {
export const DeviceState = atomWithRefresh<PeripheralItem | null>(async (get) => {
try {
const state = await invoke('connected_peripheral_state');
return {
id: state?.id,
address: state?.address,
represent: state?.represent,
isConnected: state?.isConnected,
rssi: state?.rssi,
battery: state?.battery,
};
return state;
} catch (e) {
console.error('[refresh connected]', e);
}
return {
id: null,
address: null,
represent: null,
isConnected: null,
rssi: null,
battery: null,
};
return null;
});
export const FoundPeripherals = atom(
[] as PeripheralItem[],

View File

@ -9,8 +9,8 @@ const DeviceStates: FC = () => {
return (
<>
<IconRssi height={16} level={deviceState.rssi} />
<IconBattery height={16} level={deviceState.battery} />
<IconRssi height={16} level={deviceState?.rssi} />
<IconBattery height={16} level={deviceState?.battery} />
</>
);
};