|
@@ -1,6 +1,8 @@
|
|
|
import { Injectable } from '@angular/core'
|
|
|
-import { SerialPort } from 'serialport'
|
|
|
-import { PartialProfile, ProfilesService } from 'tabby-core'
|
|
|
+import WSABinding from 'serialport-binding-webserialapi'
|
|
|
+import AbstractBinding from '@serialport/binding-abstract'
|
|
|
+import { autoDetect } from '@serialport/bindings-cpp'
|
|
|
+import { HostAppService, PartialProfile, Platform, ProfilesService } from 'tabby-core'
|
|
|
import { SerialPortInfo, SerialProfile } from '../api'
|
|
|
import { SerialTabComponent } from '../components/serialTab.component'
|
|
|
|
|
@@ -8,13 +10,23 @@ import { SerialTabComponent } from '../components/serialTab.component'
|
|
|
export class SerialService {
|
|
|
private constructor (
|
|
|
private profilesService: ProfilesService,
|
|
|
+ private hostApp: HostAppService,
|
|
|
) { }
|
|
|
|
|
|
+ detectBinding (): typeof AbstractBinding {
|
|
|
+ return this.hostApp.platform === Platform.Web ? WSABinding : autoDetect()
|
|
|
+ }
|
|
|
+
|
|
|
async listPorts (): Promise<SerialPortInfo[]> {
|
|
|
- return (await SerialPort.list()).map(x => ({
|
|
|
- name: x.path,
|
|
|
- description: `${x.manufacturer ?? ''} ${x.serialNumber ?? ''}`.trim() || undefined,
|
|
|
- }))
|
|
|
+ try {
|
|
|
+ return (await this.detectBinding().list()).map(x => ({
|
|
|
+ name: x.path,
|
|
|
+ description: `${x.manufacturer ?? ''} ${x.serialNumber ?? ''}`.trim() || undefined,
|
|
|
+ }))
|
|
|
+ } catch (err) {
|
|
|
+ console.error('Failed to list serial ports', err)
|
|
|
+ return []
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
quickConnect (query: string): Promise<SerialTabComponent|null> {
|