move to tauri windows from custom state machine

main
lambda 3 months ago
parent 2022ebe38c
commit 2e79067db6
Signed by: lambda
GPG Key ID: 07006F27729676AE

@ -88,3 +88,24 @@ pub fn get_device_types(device: &Device) -> Vec<DeviceType> {
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_service_types_for_magene_speedcadence() {
let magene = Device {
id: "hci0/dev_F1_F2_7C_77_14_BE".into(),
address: "F1:F2:7C:77:14:BE".into(),
display_name: Some("10911-17".into()),
rssi: Some(-42),
service_uuids: vec!["00001816-0000-1000-8000-00805f9b34fb".into()],
characteristics: vec![],
manufacturer_data: Some("6b009f2a110300030072".into()),
metadata: None,
};
assert_eq!(get_device_types(&magene), [DeviceType::CadSensor]);
}
}

@ -55,5 +55,50 @@ pub fn repo_from_devices(devices: Vec<Device>) -> Repo {
}
}
fn sort_by_rssi_desc(v: &mut [Device]) {
v.sort_by(|a, b| {
let ra = a.rssi;
let rb = b.rssi;
match (ra, rb) {
(Some(va), Some(vb)) => vb.cmp(&va),
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
});
}
sort_by_rssi_desc(&mut repo.hr_sensors);
sort_by_rssi_desc(&mut repo.cad_sensors);
sort_by_rssi_desc(&mut repo.trainers);
repo
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_service_types_for_magene_speedcadence() {
let magene = Device {
id: "hci0/dev_F1_F2_7C_77_14_BE".into(),
address: "F1:F2:7C:77:14:BE".into(),
display_name: Some("10911-17".into()),
rssi: Some(-42),
service_uuids: vec!["00001816-0000-1000-8000-00805f9b34fb".into()],
characteristics: vec![],
manufacturer_data: Some("6b009f2a110300030072".into()),
metadata: None,
};
let devices = vec![magene];
let x = repo_from_devices(devices);
println!("{:?}", x);
//assert_eq!(repo_from_devices(devices), Repo::new());
assert_eq!(2, 3);
}
}

@ -2,6 +2,8 @@ mod device;
mod ble;
mod device_repo;
use tauri::{AppHandle, Manager};
use crate::{ble::scan_devices, device_repo::{repo_from_devices, try_from_file, Repo}};
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
@ -15,14 +17,28 @@ async fn read_repo() -> Repo {
#[tauri::command]
async fn scan() -> Repo {
println!("kek");
repo_from_devices((scan_devices(5).await).unwrap_or_default())
}
#[tauri::command]
async fn init(app: AppHandle) -> Result<(),()> {
println!("init rideinn app");
let splash = app.get_webview_window("splashscreen").unwrap();
let connect = app.get_webview_window("connect").unwrap();
let repo = repo_from_devices((scan_devices(5).await).unwrap_or_default());
splash.close().unwrap();
connect.show().unwrap();
Ok(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![read_repo, scan])
.invoke_handler(tauri::generate_handler![init, read_repo, scan])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

@ -10,9 +10,17 @@
"withGlobalTauri": true,
"windows": [
{
"title": "rideinn",
"width": 800,
"height": 600
"title": "main",
"visible": false
},
{
"label": "splashscreen",
"url": "splashscreen"
},
{
"label": "connect",
"url": "connect",
"visible": false
}
],
"security": {

@ -16,9 +16,14 @@ const isArray = (maybeArray) =>
const isEmptyArray = (maybeArray) =>
isArray(maybeArray) && maybeArray.length === 0;
const getClosestDevices = () => {
console.warn("getClosestDevices NOT IMPLEMENTED YET");
return [];
const getClosestDevices = (repo) => {
const sortByRssi = (d1, d2) => d1.rssi > d2.rssi;
const cadSensors = repo.cad_sensors.sort(sortByRssi);
const hrSensors = repo.hr_sensors.sort(sortByRssi);
const trainers = repo.trainers.sort(sortByRssi);
return [hrSensors[0], cadSensors[0], trainers[0]];
};
const isRepoEmpty = (repo) =>
@ -27,12 +32,13 @@ const isRepoEmpty = (repo) =>
(!repo.trainers || isEmptyArray(repo.trainers));
window.addEventListener("DOMContentLoaded", async () => {
console.log("hello from main js");
const [savedDevices, scannedDevices] = await Promise.all([
readRepo(),
scan(),
]);
console.log(savedDevices);
console.log(savedDevices, scannedDevices);
if (isRepoEmpty(savedDevices)) {
const closest = getClosestDevices(scannedDevices);

Loading…
Cancel
Save