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

View File

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

View File

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