mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-04-21 13:49:03 +00:00
feat: tauri serial polyfill
This commit is contained in:
@@ -2,10 +2,16 @@ import {LineBreakTransformer} from "$lib/serial/line-break-transformer"
|
||||
import {serialLog} from "$lib/serial/connection"
|
||||
import type {Chord} from "$lib/serial/chord"
|
||||
import {parseChordActions, parsePhrase, stringifyChordActions, stringifyPhrase} from "$lib/serial/chord"
|
||||
import {browser} from "$app/environment"
|
||||
|
||||
export const VENDOR_ID = 0x239a
|
||||
|
||||
if (browser && import.meta.env.TAURI_FAMILY !== undefined) {
|
||||
await import("./tauri-serial")
|
||||
}
|
||||
|
||||
export async function getViablePorts(): Promise<SerialPort[]> {
|
||||
console.log(await navigator.serial.getPorts().then(it => it.map(it => it.getInfo())))
|
||||
return navigator.serial.getPorts().then(ports => ports.filter(it => it.getInfo().usbVendorId === VENDOR_ID))
|
||||
}
|
||||
|
||||
|
||||
22
src/lib/serial/tauri-serial.ts
Normal file
22
src/lib/serial/tauri-serial.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {invoke} from "@tauri-apps/api"
|
||||
|
||||
// @ts-expect-error polyfill
|
||||
// noinspection JSConstantReassignment
|
||||
navigator.serial = {
|
||||
getPorts(): Promise<SerialPort[]> {
|
||||
return invoke<any[]>("plugin:serial|get_serial_ports").then(ports =>
|
||||
ports.map<Partial<SerialPort>>(port => ({
|
||||
getInfo() {
|
||||
return {
|
||||
name: port["name"],
|
||||
usbVendorId: port["vendor_id"],
|
||||
usbProductId: port["product_id"],
|
||||
serialNumber: port["serial_number"],
|
||||
manufacturer: port["manufacturer"],
|
||||
product: port["product"],
|
||||
} as SerialPortInfo
|
||||
},
|
||||
})),
|
||||
) as Promise<SerialPort[]>
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user