mirror of
https://github.com/Eugeny/tabby.git
synced 2025-10-04 22:14:55 +00:00
move createSession() into session
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
import { Injectable, NgZone } from '@angular/core'
|
||||
import { Injectable } from '@angular/core'
|
||||
import SerialPort from 'serialport'
|
||||
import { LogService, NotificationsService, SelectorService, ProfilesService } from 'tabby-core'
|
||||
import { SerialSession, SerialPortInfo, BAUD_RATES, SerialProfile } from '../api'
|
||||
import { ProfilesService } from 'tabby-core'
|
||||
import { SerialPortInfo, SerialProfile } from '../api'
|
||||
import { SerialTabComponent } from '../components/serialTab.component'
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class SerialService {
|
||||
private constructor (
|
||||
private log: LogService,
|
||||
private zone: NgZone,
|
||||
private notifications: NotificationsService,
|
||||
private profilesService: ProfilesService,
|
||||
private selector: SelectorService,
|
||||
) { }
|
||||
|
||||
async listPorts (): Promise<SerialPortInfo[]> {
|
||||
@@ -21,55 +17,6 @@ export class SerialService {
|
||||
}))
|
||||
}
|
||||
|
||||
createSession (profile: SerialProfile): SerialSession {
|
||||
const session = new SerialSession(profile)
|
||||
session.logger = this.log.create(`serial-${profile.options.port}`)
|
||||
return session
|
||||
}
|
||||
|
||||
async connectSession (session: SerialSession): Promise<SerialPort> {
|
||||
const serial = new SerialPort(session.profile.options.port, {
|
||||
autoOpen: false,
|
||||
baudRate: parseInt(session.profile.options.baudrate as any),
|
||||
dataBits: session.profile.options.databits,
|
||||
stopBits: session.profile.options.stopbits,
|
||||
parity: session.profile.options.parity,
|
||||
rtscts: session.profile.options.rtscts,
|
||||
xon: session.profile.options.xon,
|
||||
xoff: session.profile.options.xoff,
|
||||
xany: session.profile.options.xany,
|
||||
})
|
||||
session.serial = serial
|
||||
let connected = false
|
||||
await new Promise(async (resolve, reject) => {
|
||||
serial.on('open', () => {
|
||||
connected = true
|
||||
this.zone.run(resolve)
|
||||
})
|
||||
serial.on('error', error => {
|
||||
this.zone.run(() => {
|
||||
if (connected) {
|
||||
this.notifications.error(error.toString())
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
serial.on('close', () => {
|
||||
session.emitServiceMessage('Port closed')
|
||||
session.destroy()
|
||||
})
|
||||
|
||||
try {
|
||||
serial.open()
|
||||
} catch (e) {
|
||||
this.notifications.error(e.message)
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
return serial
|
||||
}
|
||||
|
||||
quickConnect (query: string): Promise<SerialTabComponent|null> {
|
||||
let path = query
|
||||
let baudrate = 115200
|
||||
@@ -95,11 +42,4 @@ export class SerialService {
|
||||
window.localStorage.lastSerialConnection = JSON.stringify(profile)
|
||||
return this.profilesService.openNewTabForProfile(profile) as Promise<SerialTabComponent|null>
|
||||
}
|
||||
|
||||
async connectFoundPort (port: SerialPortInfo): Promise<SerialTabComponent|null> {
|
||||
const rate = await this.selector.show('Baud rate', BAUD_RATES.map(x => ({
|
||||
name: x.toString(), result: x,
|
||||
})))
|
||||
return this.quickConnect(`${port.name}@${rate}`)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user