Updating server service

This commit is contained in:
piotrpekala7
2020-06-12 15:44:04 +02:00
parent 5f52f25da4
commit 53b4e064ee
5 changed files with 78 additions and 47 deletions

View File

@ -24,22 +24,26 @@ export class DirectLinkComponent implements OnInit {
const serverIp = this.route.snapshot.paramMap.get('server_ip'); const serverIp = this.route.snapshot.paramMap.get('server_ip');
const serverPort = +this.route.snapshot.paramMap.get('server_port'); const serverPort = +this.route.snapshot.paramMap.get('server_port');
const projectId = this.route.snapshot.paramMap.get('project_id'); const projectId = this.route.snapshot.paramMap.get('project_id');
this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
if (value) {
const servers = await this.serverService.findAll();
const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0];
const servers = await this.serverService.findAll(); if (server) {
const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0]; this.router.navigate(['/server', server.id, 'project', projectId]);
} else {
if (server) { let serverToAdd: Server = new Server();
this.router.navigate(['/server', server.id, 'project', projectId]); serverToAdd.host = serverIp;
} else { serverToAdd.port = serverPort;
let serverToAdd: Server = new Server(); serverToAdd.location = 'bundled';
serverToAdd.host = serverIp; serverToAdd.name = serverIp;
serverToAdd.port = serverPort;
serverToAdd.location = 'bundled'; this.serverService.create(serverToAdd).then((addedServer: Server) => {
serverToAdd.name = serverIp; this.router.navigate(['/server', addedServer.id, 'project', projectId]);
});
this.serverService.create(serverToAdd).then((addedServer: Server) => { }
this.router.navigate(['/server', addedServer.id, 'project', projectId]); }
}); });
}
} }
} }

View File

@ -48,6 +48,8 @@ export class AddServerDialogComponent implements OnInit {
async getDefaultLocation() { async getDefaultLocation() {
console.log('get default location');
const localServers = await this.numberOfLocalServers(); const localServers = await this.numberOfLocalServers();
if(this.electronService.isElectronApp && localServers === 0) { if(this.electronService.isElectronApp && localServers === 0) {
return 'local'; return 'local';
@ -56,7 +58,10 @@ export class AddServerDialogComponent implements OnInit {
} }
async numberOfLocalServers() { async numberOfLocalServers() {
console.log('calling find all');
const servers = await this.serverService.findAll(); const servers = await this.serverService.findAll();
console.log('servers from add server dialog....');
return servers.filter((server) => server.location === 'local').length; return servers.filter((server) => server.location === 'local').length;
} }
@ -83,7 +88,9 @@ export class AddServerDialogComponent implements OnInit {
} }
async ngOnInit() { async ngOnInit() {
console.log('Start.... ');
this.locations = await this.getLocations(); this.locations = await this.getLocations();
console.log('Locations: ', this.locations);
const defaultLocalServerPath = await this.getDefaultLocalServerPath(); const defaultLocalServerPath = await this.getDefaultLocalServerPath();
const defaultUbridgePath = await this.getDefaultUbridgePath(); const defaultUbridgePath = await this.getDefaultUbridgePath();
@ -134,6 +141,7 @@ export class AddServerDialogComponent implements OnInit {
}) })
}); });
console.log('????');
const defaultLocation = await this.getDefaultLocation(); const defaultLocation = await this.getDefaultLocation();
this.serverForm.get('location').setValue(defaultLocation); this.serverForm.get('location').setValue(defaultLocation);
this.serverForm.get('host').setValue(this.getDefaultHost()); this.serverForm.get('host').setValue(this.getDefaultHost());

View File

@ -32,7 +32,11 @@ export class ServerDiscoveryComponent implements OnInit {
) {} ) {}
ngOnInit() { ngOnInit() {
this.discoverFirstAvailableServer(); this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
if (value) {
this.discoverFirstAvailableServer();
}
});
} }
discoverFirstAvailableServer() { discoverFirstAvailableServer() {

View File

@ -40,24 +40,28 @@ export class ServersComponent implements OnInit, OnDestroy {
this.isElectronApp = this.electronService.isElectronApp; this.isElectronApp = this.electronService.isElectronApp;
const runningServersNames = this.serverManagement.getRunningServers(); const runningServersNames = this.serverManagement.getRunningServers();
this.serverService.findAll().then((servers: Server[]) => { this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
servers.forEach((server) => { if (value) {
const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName); this.serverService.findAll().then((servers: Server[]) => {
if(serverIndex >= 0) { servers.forEach((server) => {
server.status = 'running'; const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName);
} if(serverIndex >= 0) {
}); server.status = 'running';
servers.forEach((server) => {
this.serverService.checkServerVersion(server).subscribe(
(serverInfo) => {
if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) {
if (!this.serverDatabase.find(server.name)) this.serverDatabase.addServer(server);
} }
}, });
error => {}
); servers.forEach((server) => {
}); this.serverService.checkServerVersion(server).subscribe(
(serverInfo) => {
if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) {
if (!this.serverDatabase.find(server.name)) this.serverDatabase.addServer(server);
}
},
error => {}
);
});
});
}
}); });
this.dataSource = new ServerDataSource(this.serverDatabase); this.dataSource = new ServerDataSource(this.serverDatabase);

View File

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core'; import { Injectable, EventEmitter } from '@angular/core';
import { IndexedDbService } from './indexed-db.service'; import { IndexedDbService } from './indexed-db.service';
import { Server } from '../models/server'; import { Server } from '../models/server';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@ -11,30 +11,35 @@ export class ServerService {
private ready: Promise<any>; private ready: Promise<any>;
private isIncognitoMode: boolean = false; private isIncognitoMode: boolean = false;
private serverIdsInIncognitoMode: string[] = []; private serverIdsInIncognitoMode: string[] = [];
public serviceInitialized: EventEmitter<boolean> = new EventEmitter<boolean>();
constructor( constructor(
private indexedDbService: IndexedDbService, private indexedDbService: IndexedDbService,
private httpServer: HttpServer, private httpServer: HttpServer,
private toasterService: ToasterService private toasterService: ToasterService
) { ) {
this.ready = indexedDbService.get().openDatabase(1, evt => { this.indexedDbService.get().openDatabase(1).then(() => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); this.ready = indexedDbService.get().openDatabase(1, evt => {
}); evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
});
this.ready.then(() => {}).catch(() => { }).catch(() => {
console.log('in incognito mode');
this.isIncognitoMode = true; this.isIncognitoMode = true;
}) }).finally(() => {
this.serviceInitialized.emit(true);
});
} }
public get(id: number): Promise<Server> { public get(id: number): Promise<Server> {
if (this.isIncognitoMode) { if (this.isIncognitoMode) {
let server: Server = JSON.parse(localStorage.getItem(`server-${id}`)); let server: Server = JSON.parse(localStorage.getItem(`server-${id}`));
let promise = new Promise<Server>(resolve => { let promise = new Promise<Server>(resolve => {
return server; resolve(server);
}); });
return promise; return promise;
} }
console.log('pfffff get');
return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise<Server>; return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise<Server>;
} }
@ -45,11 +50,12 @@ export class ServerService {
this.serverIdsInIncognitoMode.push(`server-${server.id}`); this.serverIdsInIncognitoMode.push(`server-${server.id}`);
let promise = new Promise<Server>(resolve => { let promise = new Promise<Server>(resolve => {
return server; resolve(server);
}); });
return promise; return promise;
} }
console.log('pfffff create');
return this.onReady(() => { return this.onReady(() => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
this.indexedDbService this.indexedDbService
@ -70,11 +76,12 @@ export class ServerService {
localStorage.setItem(`server-${server.id}`, JSON.stringify(server)); localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
let promise = new Promise<Server>(resolve => { let promise = new Promise<Server>(resolve => {
return []; resolve(server);
}); });
return promise; return promise;
} }
console.log('pfffff update');
return this.onReady(() => { return this.onReady(() => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
this.indexedDbService this.indexedDbService
@ -96,12 +103,13 @@ export class ServerService {
let server: Server = JSON.parse(localStorage.getItem(n)); let server: Server = JSON.parse(localStorage.getItem(n));
servers.push(server); servers.push(server);
}); });
console.log('pfffff find all iin');
return servers; resolve(servers);
}); });
return promise; return promise;
} }
console.log('pfffff find all');
return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>; return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>;
} }
@ -111,11 +119,12 @@ export class ServerService {
this.serverIdsInIncognitoMode = this.serverIdsInIncognitoMode.filter(n => n !== `server-${server.id}`); this.serverIdsInIncognitoMode = this.serverIdsInIncognitoMode.filter(n => n !== `server-${server.id}`);
let promise = new Promise(resolve => { let promise = new Promise(resolve => {
return server.id; resolve(server.id);
}); });
return promise; return promise;
} }
console.log('pfffff delete');
return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id)); return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id));
} }
@ -154,6 +163,8 @@ export class ServerService {
} }
protected onReady(query) { protected onReady(query) {
console.log('pfffff');
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
this.ready.then( this.ready.then(
() => { () => {