This commit is contained in:
Eugene Pankov
2021-07-11 16:13:18 +02:00
parent 29a3b34dd2
commit 74fb173d80
30 changed files with 559 additions and 161 deletions

View File

@@ -4,13 +4,15 @@ import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { LoginService } from '../services/login.service'
import { Config, Version } from '../api'
import { Config, Gateway, Version } from '../api'
export class SocketProxy {
connect$ = new Subject<void>()
data$ = new Subject<Buffer>()
error$ = new Subject<Buffer>()
close$ = new Subject<Buffer>()
url: string
webSocket: WebSocket
initialBuffer: Buffer
options: {
@@ -22,12 +24,18 @@ export class SocketProxy {
this.initialBuffer = Buffer.from('')
}
connect (options) {
async connect (options) {
this.options = options
this.webSocket = new WebSocket(
this.appConnector.loginService.user.custom_connection_gateway ||
`ws://${location.host}/api/1/gateway/tcp`
)
this.url = this.appConnector.loginService.user.custom_connection_gateway
if (!this.url) {
try {
this.url = (await this.appConnector.chooseConnectionGateway()).url
} catch (err) {
this.error$.next(err)
return
}
}
this.webSocket = new WebSocket(this.url)
this.webSocket.onmessage = async event => {
if (typeof(event.data) === 'string') {
this.handleServiceMessage(JSON.parse(event.data))
@@ -35,6 +43,9 @@ export class SocketProxy {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
this.webSocket.onclose = () => {
this.close()
}
}
handleServiceMessage (msg) {
@@ -68,20 +79,23 @@ export class SocketProxy {
}
write (chunk: Buffer): void {
if (!this.webSocket.readyState) {
if (!this.webSocket?.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
}
close (error: Error): void {
close (error?: Error): void {
this.webSocket.close()
if (error) {
this.error$.next(error)
}
this.connect$.complete()
this.data$.complete()
this.error$.complete()
this.close$.next()
this.close$.complete()
}
}
@@ -90,6 +104,7 @@ export class AppConnectorService {
private configUpdate = new Subject<string>()
private config: Config
private version: Version
sockets: SocketProxy[] = []
constructor (
private http: HttpClient,
@@ -135,6 +150,15 @@ export class AppConnectorService {
}
createSocket () {
return new SocketProxy(this)
const socket = new SocketProxy(this)
this.sockets.push(socket)
socket.close$.subscribe(() => {
this.sockets = this.sockets.filter(x => x !== socket)
})
return socket
}
async chooseConnectionGateway (): Promise<Gateway> {
return await this.http.post('/api/1/gateways/choose', {}).toPromise()
}
}

View File

@@ -33,11 +33,17 @@ export class ConfigService {
await this.http.put('/api/1/user', this.user).toPromise()
}
async createNewConfig () {
this.configs.push(await this.http.post('/api/1/configs', {
async createNewConfig (): Promise<Config> {
const config = await this.http.post('/api/1/configs', {
content: '{}',
last_used_with_version: this._activeVersion.version,
}).toPromise())
last_used_with_version: this._activeVersion?.version ?? this.getLatestStableVersion().version,
}).toPromise()
this.configs.push(config)
return config
}
getLatestStableVersion () {
return this.versions[0]
}
async duplicateActiveConfig () {
@@ -60,6 +66,8 @@ export class ConfigService {
this._activeConfig = config
this.activeConfig$.next(config)
this.selectVersion(matchingVersion)
this.loginService.user.active_config = config.id
await this.loginService.updateUser()
}
async selectDefaultConfig () {
@@ -71,7 +79,7 @@ export class ConfigService {
private async init () {
this.configs = await this.http.get('/api/1/configs').toPromise()
this.versions = await this.http.get('/api/1/versions').toPromise()
this.versions.sort((a, b) => semverGT(a, b))
this.versions.sort((a, b) => semverGT(a.version, b.version))
if (!this.configs.length) {
await this.createNewConfig()